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