diff --git a/doc/examples/binary.cpp b/doc/examples/binary.cpp new file mode 100644 index 000000000..617ce6096 --- /dev/null +++ b/doc/examples/binary.cpp @@ -0,0 +1,16 @@ +#include +#include + +using json = nlohmann::json; + +int main() +{ + // create a binary vector + std::vector vec = {0xCA, 0xFE, 0xBA, 0xBE}; + + // create a binary JSON value with subtype 42 + json j = json::binary(vec, 42); + + // output type and subtype + std::cout << "type: " << j.type_name() << ", subtype: " << j.get_binary().subtype() << std::endl; +} diff --git a/doc/examples/binary.output b/doc/examples/binary.output new file mode 100644 index 000000000..74b05d23f --- /dev/null +++ b/doc/examples/binary.output @@ -0,0 +1 @@ +type: binary, subtype: 42 diff --git a/doc/examples/get_binary.cpp b/doc/examples/get_binary.cpp new file mode 100644 index 000000000..617ce6096 --- /dev/null +++ b/doc/examples/get_binary.cpp @@ -0,0 +1,16 @@ +#include +#include + +using json = nlohmann::json; + +int main() +{ + // create a binary vector + std::vector vec = {0xCA, 0xFE, 0xBA, 0xBE}; + + // create a binary JSON value with subtype 42 + json j = json::binary(vec, 42); + + // output type and subtype + std::cout << "type: " << j.type_name() << ", subtype: " << j.get_binary().subtype() << std::endl; +} diff --git a/doc/examples/get_binary.output b/doc/examples/get_binary.output new file mode 100644 index 000000000..74b05d23f --- /dev/null +++ b/doc/examples/get_binary.output @@ -0,0 +1 @@ +type: binary, subtype: 42 diff --git a/doc/examples/operator_literal_json.cpp b/doc/examples/operator_literal_json.cpp new file mode 100644 index 000000000..2ec008eb4 --- /dev/null +++ b/doc/examples/operator_literal_json.cpp @@ -0,0 +1,12 @@ +#include +#include +#include + +using json = nlohmann::json; + +int main() +{ + json j = R"( {"hello": "world", "answer": 42} )"_json; + + std::cout << std::setw(2) << j << '\n'; +} diff --git a/doc/examples/operator_literal_json.output b/doc/examples/operator_literal_json.output new file mode 100644 index 000000000..6c0a7b34b --- /dev/null +++ b/doc/examples/operator_literal_json.output @@ -0,0 +1,4 @@ +{ + "answer": 42, + "hello": "world" +} diff --git a/doc/examples/operator_literal_json_pointer.cpp b/doc/examples/operator_literal_json_pointer.cpp new file mode 100644 index 000000000..bbdb974af --- /dev/null +++ b/doc/examples/operator_literal_json_pointer.cpp @@ -0,0 +1,13 @@ +#include +#include +#include + +using json = nlohmann::json; + +int main() +{ + json j = R"( {"hello": "world", "answer": 42} )"_json; + auto val = j["/hello"_json_pointer]; + + std::cout << std::setw(2) << val << '\n'; +} diff --git a/doc/examples/operator_literal_json_pointer.output b/doc/examples/operator_literal_json_pointer.output new file mode 100644 index 000000000..b0fcd3479 --- /dev/null +++ b/doc/examples/operator_literal_json_pointer.output @@ -0,0 +1 @@ +"world" diff --git a/doc/examples/std_swap.cpp b/doc/examples/std_swap.cpp new file mode 100644 index 000000000..36ab3ce6b --- /dev/null +++ b/doc/examples/std_swap.cpp @@ -0,0 +1,19 @@ +#include +#include +#include + +using json = nlohmann::json; + +int main() +{ + // create JSON values + json j1 = {{"one", 1}, {"two", 2}}; + json j2 = {1, 2, 4, 8, 16}; + + std::cout << "j1 = " << j1 << " | j2 = " << j2 << '\n'; + + // swap values + std::swap(j1, j2); + + std::cout << "j1 = " << j1 << " | j2 = " << j2 << std::endl; +} diff --git a/doc/examples/std_swap.output b/doc/examples/std_swap.output new file mode 100644 index 000000000..5ae6db780 --- /dev/null +++ b/doc/examples/std_swap.output @@ -0,0 +1,2 @@ +j1 = {"one":1,"two":2} | j2 = [1,2,4,8,16] +j1 = [1,2,4,8,16] | j2 = {"one":1,"two":2} diff --git a/doc/mkdocs/docs/api/basic_json/binary.md b/doc/mkdocs/docs/api/basic_json/binary.md index 059500e23..ce45d8a0f 100644 --- a/doc/mkdocs/docs/api/basic_json/binary.md +++ b/doc/mkdocs/docs/api/basic_json/binary.md @@ -45,6 +45,22 @@ standard value ctor, as both JSON arrays and JSON binary arrays are backed with JSON binary arrays are a non-standard extension it was decided that it would be best to prevent automatic initialization of a binary array type, for backwards compatibility and so it does not happen on accident. +## Examples + +??? example + + The following code shows how to create a binary value. + + ```cpp + --8<-- "examples/binary.cpp" + ``` + + Output: + + ```json + --8<-- "examples/binary.output" + ``` + ## Version history - Added in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/get_binary.md b/doc/mkdocs/docs/api/basic_json/get_binary.md index b4d601819..a910f3aa5 100644 --- a/doc/mkdocs/docs/api/basic_json/get_binary.md +++ b/doc/mkdocs/docs/api/basic_json/get_binary.md @@ -24,6 +24,22 @@ Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) i Constant. +## Examples + +??? example + + The following code shows how to query a binary value. + + ```cpp + --8<-- "examples/get_binary.cpp" + ``` + + Output: + + ```json + --8<-- "examples/get_binary.output" + ``` + ## Version history - Added in version 3.8.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_literal_json.md b/doc/mkdocs/docs/api/basic_json/operator_literal_json.md index b2d3fc5c0..8e0c65df0 100644 --- a/doc/mkdocs/docs/api/basic_json/operator_literal_json.md +++ b/doc/mkdocs/docs/api/basic_json/operator_literal_json.md @@ -27,6 +27,22 @@ The function can throw anything that [`parse(s, s+n)`](parse.md) would throw. Linear. +## Examples + +??? example + + The following code shows how to create JSON values from string literals. + + ```cpp + --8<-- "examples/operator_literal_json.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator_literal_json.output" + ``` + ## Version history - Added in version 1.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/operator_literal_json_pointer.md b/doc/mkdocs/docs/api/basic_json/operator_literal_json_pointer.md index 394138441..38c957e61 100644 --- a/doc/mkdocs/docs/api/basic_json/operator_literal_json_pointer.md +++ b/doc/mkdocs/docs/api/basic_json/operator_literal_json_pointer.md @@ -27,6 +27,22 @@ The function can throw anything that [`json_pointer::json_pointer`](../json_poin Linear. +## Examples + +??? example + + The following code shows how to create JSON Pointers from string literals. + + ```cpp + --8<-- "examples/operator_literal_json_pointer.cpp" + ``` + + Output: + + ```json + --8<-- "examples/operator_literal_json_pointer.output" + ``` + ## Version history - Added in version 2.0.0. diff --git a/doc/mkdocs/docs/api/basic_json/std_swap.md b/doc/mkdocs/docs/api/basic_json/std_swap.md index 5e5c0d4a1..d30f3bccd 100644 --- a/doc/mkdocs/docs/api/basic_json/std_swap.md +++ b/doc/mkdocs/docs/api/basic_json/std_swap.md @@ -8,6 +8,14 @@ namespace std { Exchanges the values of two JSON objects. +## Parameters + +`j1` (in, out) +: value to be replaced by `j2` + +`j2` (in, out) +: value to be replaced by `j1` + ## Possible implementation ```cpp @@ -17,6 +25,22 @@ void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2) } ``` +## Examples + +??? example + + The following code shows how two values are swapped with `std::swap`. + + ```cpp + --8<-- "examples/std_swap.cpp" + ``` + + Output: + + ```json + --8<-- "examples/std_swap.output" + ``` + ## See also - [swap](swap.md)