mirror of
https://github.com/nlohmann/json.git
synced 2025-07-31 10:24:23 +03:00
deploy: d23291ba26
This commit is contained in:
30
examples/diagnostic_positions_exception.cpp
Normal file
30
examples/diagnostic_positions_exception.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#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';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user