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