• 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     // define parser callback
37     json::parser_callback_t cb = [](int depth, json::parse_event_t event, json & parsed)
__anon4d12ae3a0102(int depth, json::parse_event_t event, json & parsed) 38     {
39         // skip object elements with key "Thumbnail"
40         if (event == json::parse_event_t::key and parsed == json("Thumbnail"))
41         {
42             return false;
43         }
44         else
45         {
46             return true;
47         }
48     };
49 
50     // fill a stream with JSON text
51     ss.clear();
52     ss << text;
53 
54     // parse (with callback) and serialize JSON
55     json j_filtered = json::parse(ss, cb);
56     std::cout << std::setw(4) << j_filtered << '\n';
57 }