2017-07-09 09:51:38 +00:00
|
|
|
#include <iostream>
|
2018-02-01 21:20:26 +00:00
|
|
|
#include <nlohmann/json.hpp>
|
2015-06-22 21:21:49 +00:00
|
|
|
|
2016-01-30 19:23:14 +00:00
|
|
|
using json = nlohmann::json;
|
2015-06-22 21:21:49 +00:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// create JSON values
|
|
|
|
json j_array = {"alpha", "bravo", "charly", "delta", "easy"};
|
|
|
|
json j_number = 42;
|
|
|
|
json j_object = {{"one", "eins"}, {"two", "zwei"}};
|
|
|
|
|
|
|
|
// create copies using iterators
|
|
|
|
json j_array_range(j_array.begin() + 1, j_array.end() - 2);
|
|
|
|
json j_number_range(j_number.begin(), j_number.end());
|
|
|
|
json j_object_range(j_object.begin(), j_object.find("two"));
|
|
|
|
|
|
|
|
// serialize the values
|
|
|
|
std::cout << j_array_range << '\n';
|
|
|
|
std::cout << j_number_range << '\n';
|
|
|
|
std::cout << j_object_range << '\n';
|
2017-03-08 22:12:13 +00:00
|
|
|
|
|
|
|
// example for an exception
|
|
|
|
try
|
|
|
|
{
|
|
|
|
json j_invalid(j_number.begin() + 1, j_number.end());
|
|
|
|
}
|
|
|
|
catch (json::invalid_iterator& e)
|
|
|
|
{
|
|
|
|
std::cout << e.what() << '\n';
|
|
|
|
}
|
2015-06-22 21:21:49 +00:00
|
|
|
}
|