sax_parse.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 binary(json::binary_t& val) override
  69. {
  70. events.push_back("binary");
  71. return true;
  72. }
  73. bool parse_error(std::size_t position, const std::string& last_token, const json::exception& ex) override
  74. {
  75. events.push_back("error: " + std::string(ex.what()));
  76. return false;
  77. }
  78. };
  79. int main()
  80. {
  81. // a JSON text
  82. auto text = R"(
  83. {
  84. "Image": {
  85. "Width": 800,
  86. "Height": 600,
  87. "Title": "View from 15th Floor",
  88. "Thumbnail": {
  89. "Url": "http://www.example.com/image/481989943",
  90. "Height": 125,
  91. "Width": 100
  92. },
  93. "Animated" : false,
  94. "IDs": [116, 943, 234, 38793],
  95. "Distance": 12.723374634
  96. }
  97. }
  98. )";
  99. // create a SAX event consumer object
  100. sax_event_consumer sec;
  101. // parse and serialize JSON
  102. bool result = json::sax_parse(text, &sec);
  103. // output the recorded events
  104. for (auto& event : sec.events)
  105. {
  106. std::cout << "(" << event << ") ";
  107. }
  108. // output the result of sax_parse
  109. std::cout << "\nresult: " << std::boolalpha << result << std::endl;
  110. }