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