forked from nlohmann/json
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue4561 - use diagnostic positions for exceptions
Signed-off-by: Harinath Nampally <harinath922@gmail.com>
- Loading branch information
Showing
9 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#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 | ||
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"]; | ||
} | ||
catch (const json::exception& e) | ||
{ | ||
std::cout << e.what() << '\n'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[json.exception.type_error.302] (bytes 92-95) type must be number, but is string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <iostream> | ||
|
||
#define JSON_DIAGNOSTICS 1 | ||
#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 | ||
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"]; | ||
} | ||
catch (const json::exception& e) | ||
{ | ||
std::cout << e.what() << '\n'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[json.exception.type_error.302] (/address/housenumber) (bytes 92-95) type must be number, but is string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters