mirror of
https://github.com/nlohmann/json.git
synced 2025-07-29 23:01:16 +03:00
cleanup and documentation
This commit is contained in:
34
doc/examples/flatten.cpp
Normal file
34
doc/examples/flatten.cpp
Normal file
@ -0,0 +1,34 @@
|
||||
#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';
|
||||
}
|
1
doc/examples/flatten.link
Normal file
1
doc/examples/flatten.link
Normal file
@ -0,0 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/kODXfzcksgstdBRD"><b>online</b></a>
|
16
doc/examples/flatten.output
Normal file
16
doc/examples/flatten.output
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"/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
|
||||
}
|
47
doc/examples/operatorjson_pointer.cpp
Normal file
47
doc/examples/operatorjson_pointer.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#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';
|
||||
}
|
1
doc/examples/operatorjson_pointer.link
Normal file
1
doc/examples/operatorjson_pointer.link
Normal file
@ -0,0 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/6oeNnra3wjPijLSr"><b>online</b></a>
|
8
doc/examples/operatorjson_pointer.output
Normal file
8
doc/examples/operatorjson_pointer.output
Normal file
@ -0,0 +1,8 @@
|
||||
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]
|
23
doc/examples/operatorjson_pointer_const.cpp
Normal file
23
doc/examples/operatorjson_pointer_const.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
#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';
|
||||
}
|
1
doc/examples/operatorjson_pointer_const.link
Normal file
1
doc/examples/operatorjson_pointer_const.link
Normal file
@ -0,0 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/YmjwNAhsoeMXw5Ve"><b>online</b></a>
|
4
doc/examples/operatorjson_pointer_const.output
Normal file
4
doc/examples/operatorjson_pointer_const.output
Normal file
@ -0,0 +1,4 @@
|
||||
1
|
||||
"foo"
|
||||
[1,2]
|
||||
2
|
28
doc/examples/unflatten.cpp
Normal file
28
doc/examples/unflatten.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
#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';
|
||||
}
|
1
doc/examples/unflatten.link
Normal file
1
doc/examples/unflatten.link
Normal file
@ -0,0 +1 @@
|
||||
<a target="_blank" href="http://melpon.org/wandbox/permlink/ITqCZsXmi0I7KGYy"><b>online</b></a>
|
22
doc/examples/unflatten.output
Normal file
22
doc/examples/unflatten.output
Normal file
@ -0,0 +1,22 @@
|
||||
{
|
||||
"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
|
||||
}
|
Reference in New Issue
Block a user