forked from axilmar/parserlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.cpp
More file actions
54 lines (40 loc) · 1.44 KB
/
Copy pathjson.cpp
File metadata and controls
54 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include "json.hpp"
using namespace parserlib;
void run_example_json() {
std::string src = R"({
"v1": null,
"v2": false,
"v3": true,
"v4": [false, true, {"x":5, "y":6}, 15, "aaa", null],
"v5": { "a" : 32 },
"v6": 17,
"v7": "abc"
})";
const auto result = json::parse(src);
std::cout << "JSON parser success = " << result->success << std::endl;
if (!result->ast.empty()) {
std::cout << "\nJSON parsed:\n";
result->ast[0]->visit([&](const auto& node, size_t depth) {
std::cout << std::string(depth * 4, ' ') << get_id_name(node.id());
switch (node.id()) {
case json::AST_ID::STRING:
std::cout << ": " << node.begin()->start_position().to_string() << ": " << node.content();
break;
case json::AST_ID::NUMBER:
std::cout << ": " << node.begin()->start_position().to_string() << ": \"" << node.content() << '"';
break;
default:
break;
}
std::cout << std::endl;
});
}
if (!result->errors.empty()) {
std::cout << "\nJSON parse errors: " << std::endl;
for (const auto& error : result->errors) {
std::cout << " ERROR: " << error.start_position().to_string() << ": " << get_id_name(error.id()) << std::endl;
}
}
std::cout << "\n\n";
}