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