1#include <iostream> 2#include <nlohmann/json.hpp> 3 4using json = nlohmann::json; 5 6int main() 7{ 8 // create JSON values 9 json j_array = {"alpha", "bravo", "charly", "delta", "easy"}; 10 json j_number = 42; 11 json j_object = {{"one", "eins"}, {"two", "zwei"}}; 12 13 // create copies using iterators 14 json j_array_range(j_array.begin() + 1, j_array.end() - 2); 15 json j_number_range(j_number.begin(), j_number.end()); 16 json j_object_range(j_object.begin(), j_object.find("two")); 17 18 // serialize the values 19 std::cout << j_array_range << '\n'; 20 std::cout << j_number_range << '\n'; 21 std::cout << j_object_range << '\n'; 22 23 // example for an exception 24 try 25 { 26 json j_invalid(j_number.begin() + 1, j_number.end()); 27 } 28 catch (json::invalid_iterator& e) 29 { 30 std::cout << e.what() << '\n'; 31 } 32} 33