1 #include <iostream>
2 #include <iomanip>
3 #include <sstream>
4 #include <nlohmann/json.hpp>
5
6 using json = nlohmann::json;
7
8 // a simple event consumer that collects string representations of the passed
9 // values; not inheriting from json::json_sax_t is not required, but can
10 // help not to forget a required function
11 class sax_event_consumer : public json::json_sax_t
12 {
13 public:
14 std::vector<std::string> events;
15
null()16 bool null() override
17 {
18 events.push_back("value: null");
19 return true;
20 }
21
boolean(bool val)22 bool boolean(bool val) override
23 {
24 events.push_back("value: " + std::string(val ? "true" : "false"));
25 return true;
26 }
27
number_integer(number_integer_t val)28 bool number_integer(number_integer_t val) override
29 {
30 events.push_back("value: " + std::to_string(val));
31 return true;
32 }
33
number_unsigned(number_unsigned_t val)34 bool number_unsigned(number_unsigned_t val) override
35 {
36 events.push_back("value: " + std::to_string(val));
37 return true;
38 }
39
number_float(number_float_t val,const string_t & s)40 bool number_float(number_float_t val, const string_t& s) override
41 {
42 events.push_back("value: " + s);
43 return true;
44 }
45
string(string_t & val)46 bool string(string_t& val) override
47 {
48 events.push_back("value: " + val);
49 return true;
50 }
51
start_object(std::size_t elements)52 bool start_object(std::size_t elements) override
53 {
54 events.push_back("start: object");
55 return true;
56 }
57
end_object()58 bool end_object() override
59 {
60 events.push_back("end: object");
61 return true;
62 }
63
start_array(std::size_t elements)64 bool start_array(std::size_t elements) override
65 {
66 events.push_back("start: array");
67 return true;
68 }
69
end_array()70 bool end_array() override
71 {
72 events.push_back("end: array");
73 return true;
74 }
75
key(string_t & val)76 bool key(string_t& val) override
77 {
78 events.push_back("key: " + val);
79 return true;
80 }
81
parse_error(std::size_t position,const std::string & last_token,const json::exception & ex)82 bool parse_error(std::size_t position, const std::string& last_token, const json::exception& ex) override
83 {
84 events.push_back("error: " + std::string(ex.what()));
85 return false;
86 }
87 };
88
main()89 int main()
90 {
91 // a JSON text
92 auto text = R"(
93 {
94 "Image": {
95 "Width": 800,
96 "Height": 600,
97 "Title": "View from 15th Floor",
98 "Thumbnail": {
99 "Url": "http://www.example.com/image/481989943",
100 "Height": 125,
101 "Width": 100
102 },
103 "Animated" : false,
104 "IDs": [116, 943, 234, 38793],
105 "Distance": 12.723374634
106 }
107 }
108 )";
109
110 // create a SAX event consumer object
111 sax_event_consumer sec;
112
113 // parse and serialize JSON
114 bool result = json::sax_parse(text, &sec);
115
116 // output the recorded events
117 for (auto& event : sec.events)
118 {
119 std::cout << "(" << event << ") ";
120 }
121
122 // output the result of sax_parse
123 std::cout << "\nresult: " << std::boolalpha << result << std::endl;
124 }
125