sax_parse.cpp 2.8 KB

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