c++ - nlohmann::json 3.11.3 - json_pointer warning: 'operator==' is deprecated: Since 3.11.2 - Stack Overflow

I'm developing a simple program in c++20 that reads a json file using the nlohmann::json 3.11.3 li

I'm developing a simple program in c++20 that reads a json file using the nlohmann::json 3.11.3 library. My problem is using json_pointers correctly, let me explain better by starting to show you a JSon structure:

{
  "bar": {
    "Qt": "6.8",
    "json": "3.11.3",
    "array_json": ["/bar/Qt", "/bar/json"]
  },
  "foo": null
}

Okay now my example code is this using c++23:

#include <nlohmann/json.hpp>
#include <print>

using json = nlohmann::ordered_json;

int main()
try {
    json data = R"(
{
  "bar": {
    "Qt": "6.8",
    "json": "3.11.3",
    "array_json": ["/bar/Qt", "/bar/json"]
  },
  "foo": null
}
)"_json;

    std::println("nlohmann/json version: {}.{}.{}",
        NLOHMANN_JSON_VERSION_MAJOR, NLOHMANN_JSON_VERSION_MINOR, NLOHMANN_JSON_VERSION_PATCH);

    // get the second element of the "array_json"
    auto s_pointer = data["bar"]["array_json"][1].get<std::string>();

    // expected value of pointer = "/bar/json"
    std::println("{}", s_pointer);

    // create a json_pointer
    json::json_pointer j_pointer(data["bar"]["array_json"][1].get<std::string>());

    // expected value of json pointer = "/bar/json"
    std::println("{}", j_pointer.to_string());

    // get the value of /bar/json using json pointer
    auto value = data[j_pointer].get<std::string>(); // warning here, but its working fine...

    std::println("{}", value); // "3.11.3"

    // my try to surpress the warning
    value = data[j_pointer.to_string()].get<std::string>(); // no warning but dont work properly
    // error : terminate called after throwing an instance of 'nlohmann::json_abi_v3_11_3::detail::type_error'
    // what():  [json.exception.type_error.302] type must be string, but is null

    std::println("{}", value);

    return 0;
}
catch (const std::exception& e) {
    std::print("Exception: {}\n", e.what());
    return 1;
}

Here the details of the warning I got...

main.cpp:92:53: 'operator==' is deprecated: Since 3.11.2; use operator==(json_pointer, json_pointer)

json.hpp:3981:10: in instantiation of exception specification for 'operator()std::basic_string<char, nlohmann::json_pointerstd::basic_string<char> &>' requested here

json.hpp:3993:31: during template argument deduction for class template partial specialization 'is_comparable<Compare, A, B, void_t<decltype(std::declval()(std::declval(), std::declval())), decltype(std::declval()(std::declval(), std::declval()))>>' [with Compare = std::equal_to, A = std::basic_string, B = nlohmann::json_pointerstd::basic_string<char> &] json.hpp:3993:31: in instantiation of template class 'nlohmann::detail::is_comparablestd::equal_to<void, std::basic_string, nlohmann::json_pointerstd::basic_string<char> &>' requested here json.hpp:4011:9: in instantiation of template type alias 'is_usable_as_key_type' requested here

json.hpp:21473:26: in instantiation of template type alias 'is_usable_as_basic_json_key_type' requested here

json.hpp:21474:15: while substituting prior template arguments into non-type template parameter [with KeyType = json::json_pointer &]

main.cpp:92:53: while substituting deduced template arguments into function template 'operator[]' [with KeyType = json::json_pointer &, $1 = (no value)]

json.hpp:14754:13: 'operator==' has been explicitly marked deprecated here my question is how do I eliminate this warning using the updated form to use json_pointer?

My question is how do I eliminate this warning using the updated form to use json_pointer correctly?

EDIT: The warning message was reproduced with Clang 18 and MingGW 13.10 compiler, but not with GCC 14.

This deprecation warning is a confirmed issue with suggested workaround using direct initialization.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744652964a4586011.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>