mirror of
https://github.com/nlohmann/json.git
synced 2025-07-29 23:01:16 +03:00
more documentation
This commit is contained in:
18
docs/examples/array.cpp
Normal file
18
docs/examples/array.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
#include <json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON arrays
|
||||
json j_no_init_list = json::array();
|
||||
json j_empty_init_list = json::array({});
|
||||
json j_nonempty_init_list = json::array({1, 2, 3, 4});
|
||||
json j_list_of_pairs = json::array({ {"one", 1}, {"two", 2} });
|
||||
|
||||
// serialize the JSON arrays
|
||||
std::cout << j_no_init_list << '\n';
|
||||
std::cout << j_empty_init_list << '\n';
|
||||
std::cout << j_nonempty_init_list << '\n';
|
||||
std::cout << j_list_of_pairs << '\n';
|
||||
}
|
4
docs/examples/array.output
Normal file
4
docs/examples/array.output
Normal file
@ -0,0 +1,4 @@
|
||||
[]
|
||||
[]
|
||||
[1,2,3,4]
|
||||
[["one",1],["two",2]]
|
20
docs/examples/basic_json__list_init_t.cpp
Normal file
20
docs/examples/basic_json__list_init_t.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
#include <json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON values
|
||||
json j_empty_init_list = json({});
|
||||
json j_object = { {"one", 1}, {"two", 2} };
|
||||
json j_array = {1, 2, 3, 4};
|
||||
json j_nested_object = { {"one", {1}}, {"two", {1, 2}} };
|
||||
json j_nested_array = { {{1}, "one"}, {{1, 2}, "two"} };
|
||||
|
||||
// serialize the JSON value
|
||||
std::cout << j_empty_init_list << '\n';
|
||||
std::cout << j_object << '\n';
|
||||
std::cout << j_array << '\n';
|
||||
std::cout << j_nested_object << '\n';
|
||||
std::cout << j_nested_array << '\n';
|
||||
}
|
5
docs/examples/basic_json__list_init_t.output
Normal file
5
docs/examples/basic_json__list_init_t.output
Normal file
@ -0,0 +1,5 @@
|
||||
{}
|
||||
{"one":1,"two":2}
|
||||
[1,2,3,4]
|
||||
{"one":[1],"two":[1,2]}
|
||||
[[[1],"one"],[[1,2],"two"]]
|
17
docs/examples/basic_json__size_type_basic_json.cpp
Normal file
17
docs/examples/basic_json__size_type_basic_json.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#include <json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create an array by creating copies of a JSON value
|
||||
json value = "Hello";
|
||||
json array_0 = json(0, value);
|
||||
json array_1 = json(1, value);
|
||||
json array_5 = json(5, value);
|
||||
|
||||
// serialize the JSON arrays
|
||||
std::cout << array_0 << '\n';
|
||||
std::cout << array_1 << '\n';
|
||||
std::cout << array_5 << '\n';
|
||||
}
|
3
docs/examples/basic_json__size_type_basic_json.output
Normal file
3
docs/examples/basic_json__size_type_basic_json.output
Normal file
@ -0,0 +1,3 @@
|
||||
[]
|
||||
["Hello"]
|
||||
["Hello","Hello","Hello","Hello","Hello"]
|
33
docs/examples/clear.cpp
Normal file
33
docs/examples/clear.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON values
|
||||
json j_null;
|
||||
json j_boolean = true;
|
||||
json j_number_integer = 17;
|
||||
json j_number_float = 23.42;
|
||||
json j_object = {{"one", 1}, {"two", 2}};
|
||||
json j_array = {1, 2, 4, 8, 16};
|
||||
json j_string = "Hello, world";
|
||||
|
||||
// call clear()
|
||||
j_null.clear();
|
||||
j_boolean.clear();
|
||||
j_number_integer.clear();
|
||||
j_number_float.clear();
|
||||
j_object.clear();
|
||||
j_array.clear();
|
||||
j_string.clear();
|
||||
|
||||
// serialize the cleared values()
|
||||
std::cout << j_null << '\n';
|
||||
std::cout << j_boolean << '\n';
|
||||
std::cout << j_number_integer << '\n';
|
||||
std::cout << j_number_float << '\n';
|
||||
std::cout << j_object << '\n';
|
||||
std::cout << j_array << '\n';
|
||||
std::cout << j_string << '\n';
|
||||
}
|
7
docs/examples/clear.output
Normal file
7
docs/examples/clear.output
Normal file
@ -0,0 +1,7 @@
|
||||
null
|
||||
false
|
||||
0
|
||||
0
|
||||
{}
|
||||
[]
|
||||
""
|
24
docs/examples/max_size.cpp
Normal file
24
docs/examples/max_size.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include <json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON values
|
||||
json j_null;
|
||||
json j_boolean = true;
|
||||
json j_number_integer = 17;
|
||||
json j_number_float = 23.42;
|
||||
json j_object = {{"one", 1}, {"two", 2}};
|
||||
json j_array = {1, 2, 4, 8, 16};
|
||||
json j_string = "Hello, world";
|
||||
|
||||
// call max_size()
|
||||
std::cout << j_null.max_size() << '\n';
|
||||
std::cout << j_boolean.max_size() << '\n';
|
||||
std::cout << j_number_integer.max_size() << '\n';
|
||||
std::cout << j_number_float.max_size() << '\n';
|
||||
std::cout << j_object.max_size() << '\n';
|
||||
std::cout << j_array.max_size() << '\n';
|
||||
std::cout << j_string.max_size() << '\n';
|
||||
}
|
7
docs/examples/max_size.output
Normal file
7
docs/examples/max_size.output
Normal file
@ -0,0 +1,7 @@
|
||||
0
|
||||
1
|
||||
1
|
||||
1
|
||||
256204778801521550
|
||||
1152921504606846975
|
||||
1
|
17
docs/examples/object.cpp
Normal file
17
docs/examples/object.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
#include <json.hpp>
|
||||
|
||||
using namespace nlohmann;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create JSON arrays
|
||||
json j_no_init_list = json::object();
|
||||
json j_empty_init_list = json::object({});
|
||||
json j_list_of_pairs = json::object({ {"one", 1}, {"two", 2} });
|
||||
//json j_invalid_list = json::object({ "one", 1 }); // would throw
|
||||
|
||||
// serialize the JSON arrays
|
||||
std::cout << j_no_init_list << '\n';
|
||||
std::cout << j_empty_init_list << '\n';
|
||||
std::cout << j_list_of_pairs << '\n';
|
||||
}
|
3
docs/examples/object.output
Normal file
3
docs/examples/object.output
Normal file
@ -0,0 +1,3 @@
|
||||
{}
|
||||
{}
|
||||
{"one":1,"two":2}
|
Reference in New Issue
Block a user