2017-07-09 11:51:38 +02:00
|
|
|
#include <iostream>
|
2022-07-31 14:05:58 +02:00
|
|
|
#include <string_view>
|
2018-02-01 22:20:26 +01:00
|
|
|
#include <nlohmann/json.hpp>
|
2015-12-15 08:38:54 +01:00
|
|
|
|
2022-07-31 14:05:58 +02:00
|
|
|
using namespace std::string_view_literals;
|
2016-01-30 20:23:14 +01:00
|
|
|
using json = nlohmann::json;
|
2015-12-15 08:38:54 +01:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// create a JSON object with different entry types
|
|
|
|
json j =
|
|
|
|
{
|
|
|
|
{"integer", 1},
|
|
|
|
{"floating", 42.23},
|
|
|
|
{"string", "hello world"},
|
|
|
|
{"boolean", true},
|
|
|
|
{"object", {{"key1", 1}, {"key2", 2}}},
|
|
|
|
{"array", {1, 2, 3}}
|
|
|
|
};
|
|
|
|
|
|
|
|
// access existing values
|
2022-07-31 14:05:58 +02:00
|
|
|
int v_integer = j.value("integer"sv, 0);
|
|
|
|
double v_floating = j.value("floating"sv, 47.11);
|
2015-12-15 08:38:54 +01:00
|
|
|
|
|
|
|
// access nonexisting values and rely on default value
|
2022-07-31 14:05:58 +02:00
|
|
|
std::string v_string = j.value("nonexisting"sv, "oops");
|
|
|
|
bool v_boolean = j.value("nonexisting"sv, false);
|
2015-12-15 08:38:54 +01:00
|
|
|
|
|
|
|
// output values
|
|
|
|
std::cout << std::boolalpha << v_integer << " " << v_floating
|
|
|
|
<< " " << v_string << " " << v_boolean << "\n";
|
|
|
|
}
|