1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-29 23:01:16 +03:00
Files
json/examples/diagnostic_positions_exception.cpp
2025-01-19 09:46:40 +00:00

31 lines
703 B
C++

#include <iostream>
#define JSON_DIAGNOSTIC_POSITIONS 1
#include <nlohmann/json.hpp>
using json = nlohmann::json;
/* Demonstration of type error exception with diagnostic postions support enabled */
int main()
{
//Invalid json string - housenumber type must be int instead of string
const std::string json_invalid_string = R"(
{
"address": {
"street": "Fake Street",
"housenumber": "1"
}
}
)";
json j = json::parse(json_invalid_string);
try
{
int housenumber = j["address"]["housenumber"];
std::cout << housenumber;
}
catch (const json::exception& e)
{
std::cout << e.what() << '\n';
}
}