Commit 40e899a8 authored by Niels's avatar Niels

cleanup and documentation

parent f834965b
#include <json.hpp>
using json = nlohmann::json;
int main()
{
// create JSON value
json j =
{
{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{
"answer", {
{"everything", 42}
}
},
{"list", {1, 0, 2}},
{
"object", {
{"currency", "USD"},
{"value", 42.99},
{"", "empty string"},
{"/", "slash"},
{"~", "tilde"},
{"~1", "tilde1"}
}
}
};
// call flatten()
std::cout << std::setw(4) << j.flatten() << '\n';
}
<a target="_blank" href="http://melpon.org/wandbox/permlink/kODXfzcksgstdBRD"><b>online</b></a>
\ No newline at end of file
{
"/answer/everything": 42,
"/happy": true,
"/list/0": 1,
"/list/1": 0,
"/list/2": 2,
"/name": "Niels",
"/nothing": null,
"/object/": "empty string",
"/object/currency": "USD",
"/object/value": 42.99,
"/object/~0": "tilde",
"/object/~01": "tilde1",
"/object/~1": "slash",
"/pi": 3.141
}
#include <json.hpp>
using json = nlohmann::json;
int main()
{
// create a JSON value
json j =
{
{"number", 1}, {"string", "foo"}, {"array", {1, 2}}
};
// read-only access
// output element with JSON pointer "/number"
std::cout << j["/number"_json_pointer] << '\n';
// output element with JSON pointer "/string"
std::cout << j["/string"_json_pointer] << '\n';
// output element with JSON pointer "/array"
std::cout << j["/array"_json_pointer] << '\n';
// output element with JSON pointer "/array/1"
std::cout << j["/array/1"_json_pointer] << '\n';
// writing access
// change the string
j["/string"_json_pointer] = "bar";
// output the changed string
std::cout << j["string"] << '\n';
// "change" a nonexisting object entry
j["/boolean"_json_pointer] = true;
// output the changed object
std::cout << j << '\n';
// change an array element
j["/array/1"_json_pointer] = 21;
// "change" an array element with nonexisting index
j["/array/4"_json_pointer] = 44;
// output the changed array
std::cout << j["array"] << '\n';
// "change" the arry element past the end
j["/array/-"_json_pointer] = 55;
// output the changed array
std::cout << j["array"] << '\n';
}
<a target="_blank" href="http://melpon.org/wandbox/permlink/6oeNnra3wjPijLSr"><b>online</b></a>
\ No newline at end of file
1
"foo"
[1,2]
2
"bar"
{"array":[1,2],"boolean":true,"number":1,"string":"bar"}
[1,21,null,null,44]
[1,21,null,null,44,55]
#include <json.hpp>
using json = nlohmann::json;
int main()
{
// create a JSON value
const json j =
{
{"number", 1}, {"string", "foo"}, {"array", {1, 2}}
};
// read-only access
// output element with JSON pointer "/number"
std::cout << j["/number"_json_pointer] << '\n';
// output element with JSON pointer "/string"
std::cout << j["/string"_json_pointer] << '\n';
// output element with JSON pointer "/array"
std::cout << j["/array"_json_pointer] << '\n';
// output element with JSON pointer "/array/1"
std::cout << j["/array/1"_json_pointer] << '\n';
}
<a target="_blank" href="http://melpon.org/wandbox/permlink/YmjwNAhsoeMXw5Ve"><b>online</b></a>
\ No newline at end of file
#include <json.hpp>
using json = nlohmann::json;
int main()
{
// create JSON value
json j_flattened =
{
{"/answer/everything", 42},
{"/happy", true},
{"/list/0", 1},
{"/list/1", 0},
{"/list/2", 2},
{"/name", "Niels"},
{"/nothing", nullptr},
{"/object/", "empty string"},
{"/object/currency", "USD"},
{"/object/value", 42.99},
{"/object/~0", "tilde"},
{"/object/~01", "tilde1"},
{"/object/~1", "slash"},
{"/pi", 3.141}
};
// call unflatten()
std::cout << std::setw(4) << j_flattened.unflatten() << '\n';
}
<a target="_blank" href="http://melpon.org/wandbox/permlink/ITqCZsXmi0I7KGYy"><b>online</b></a>
\ No newline at end of file
{
"answer": {
"everything": 42
},
"happy": true,
"list": [
1,
0,
2
],
"name": "Niels",
"nothing": null,
"object": {
"": "empty string",
"/": "slash",
"currency": "USD",
"value": 42.99,
"~": "tilde",
"~1": "tilde1"
},
"pi": 3.141
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment