You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2 KiB

  1. #include <json.hpp>
  2. using json = nlohmann::json;
  3. int main()
  4. {
  5. // a JSON text
  6. auto text = R"(
  7. {
  8. "Image": {
  9. "Width": 800,
  10. "Height": 600,
  11. "Title": "View from 15th Floor",
  12. "Thumbnail": {
  13. "Url": "http://www.example.com/image/481989943",
  14. "Height": 125,
  15. "Width": 100
  16. },
  17. "Animated" : false,
  18. "IDs": [116, 943, 234, 38793]
  19. }
  20. }
  21. )";
  22. // fill a stream with JSON text
  23. std::stringstream ss;
  24. ss << text;
  25. // create JSON from stream
  26. json j_complete(ss);
  27. std::cout << std::setw(4) << j_complete << "\n\n";
  28. // define parser callback
  29. json::parser_callback_t cb = [](int depth, json::parse_event_t event, json & parsed)
  30. {
  31. // skip object elements with key "Thumbnail"
  32. if (event == json::parse_event_t::key and parsed == json("Thumbnail"))
  33. {
  34. return false;
  35. }
  36. else
  37. {
  38. return true;
  39. }
  40. };
  41. // fill a stream with JSON text
  42. ss.clear();
  43. ss << text;
  44. // create JSON from stream (with callback)
  45. json j_filtered(ss, cb);
  46. std::cout << std::setw(4) << j_filtered << '\n';
  47. }