]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | #include <iostream> |
2 | #include <nlohmann/json.hpp> | |
3 | ||
4 | using json = nlohmann::json; | |
5 | ||
6 | int main() | |
7 | { | |
8 | // an invalid JSON text | |
9 | std::string text = R"( | |
10 | { | |
11 | "key": "value without closing quotes | |
12 | } | |
13 | )"; | |
14 | ||
15 | // parse with exceptions | |
16 | try | |
17 | { | |
18 | json j = json::parse(text); | |
19 | } | |
20 | catch (json::parse_error& e) | |
21 | { | |
22 | std::cout << e.what() << std::endl; | |
23 | } | |
24 | ||
25 | // parse without exceptions | |
26 | json j = json::parse(text, nullptr, false); | |
27 | ||
28 | if (j.is_discarded()) | |
29 | { | |
30 | std::cout << "the input is invalid JSON" << std::endl; | |
31 | } | |
32 | else | |
33 | { | |
34 | std::cout << "the input is valid JSON: " << j << std::endl; | |
35 | } | |
36 | } |