Nlohmann :: json 3.11.3 - JSON_POINTEC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Nlohmann :: json 3.11.3 - JSON_POINTE

Сообщение Anonymous »

I'm developing a simple program in c++20 that reads a json file using the nlohmann::json 3.11.3 library and Qt 6.8+...
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
}
< /code>
Хорошо, теперь мой пример кода-это использование C ++ 23: < /p>
#include
#include

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();

// 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());

// 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(); // 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(); // 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;
}

< /code>
Здесь сведения о предупреждении я получил ... < /p>

main.cpp:92:53: 'operator ==' Умерен: с 3.11.2; Используйте Operator == (json_pointer, json_pointer) < /p>
< /blockquote>

p>json.hpp:3981:10: в экземпляре спецификации исключений для 'operator () std :: basic_string

json.hpp:3993:31: во время вычета аргумента шаблона для частичной специализации шаблона класса 'is_comparable

Подробнее здесь: https://stackoverflow.com/questions/795 ... ated-since
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»