🚸 add examples
This commit is contained in:
parent
9bc244be47
commit
b9a4a4809c
16
doc/examples/binary.cpp
Normal file
16
doc/examples/binary.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// create a binary vector
|
||||||
|
std::vector<std::uint8_t> 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;
|
||||||
|
}
|
||||||
1
doc/examples/binary.output
Normal file
1
doc/examples/binary.output
Normal file
@ -0,0 +1 @@
|
|||||||
|
type: binary, subtype: 42
|
||||||
16
doc/examples/get_binary.cpp
Normal file
16
doc/examples/get_binary.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// create a binary vector
|
||||||
|
std::vector<std::uint8_t> 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;
|
||||||
|
}
|
||||||
1
doc/examples/get_binary.output
Normal file
1
doc/examples/get_binary.output
Normal file
@ -0,0 +1 @@
|
|||||||
|
type: binary, subtype: 42
|
||||||
12
doc/examples/operator_literal_json.cpp
Normal file
12
doc/examples/operator_literal_json.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
json j = R"( {"hello": "world", "answer": 42} )"_json;
|
||||||
|
|
||||||
|
std::cout << std::setw(2) << j << '\n';
|
||||||
|
}
|
||||||
4
doc/examples/operator_literal_json.output
Normal file
4
doc/examples/operator_literal_json.output
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"answer": 42,
|
||||||
|
"hello": "world"
|
||||||
|
}
|
||||||
13
doc/examples/operator_literal_json_pointer.cpp
Normal file
13
doc/examples/operator_literal_json_pointer.cpp
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
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';
|
||||||
|
}
|
||||||
1
doc/examples/operator_literal_json_pointer.output
Normal file
1
doc/examples/operator_literal_json_pointer.output
Normal file
@ -0,0 +1 @@
|
|||||||
|
"world"
|
||||||
19
doc/examples/std_swap.cpp
Normal file
19
doc/examples/std_swap.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
2
doc/examples/std_swap.output
Normal file
2
doc/examples/std_swap.output
Normal file
@ -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}
|
||||||
@ -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
|
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.
|
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
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.8.0.
|
- Added in version 3.8.0.
|
||||||
|
|||||||
@ -24,6 +24,22 @@ Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) i
|
|||||||
|
|
||||||
Constant.
|
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
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.8.0.
|
- Added in version 3.8.0.
|
||||||
|
|||||||
@ -27,6 +27,22 @@ The function can throw anything that [`parse(s, s+n)`](parse.md) would throw.
|
|||||||
|
|
||||||
Linear.
|
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
|
## Version history
|
||||||
|
|
||||||
- Added in version 1.0.0.
|
- Added in version 1.0.0.
|
||||||
|
|||||||
@ -27,6 +27,22 @@ The function can throw anything that [`json_pointer::json_pointer`](../json_poin
|
|||||||
|
|
||||||
Linear.
|
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
|
## Version history
|
||||||
|
|
||||||
- Added in version 2.0.0.
|
- Added in version 2.0.0.
|
||||||
|
|||||||
@ -8,6 +8,14 @@ namespace std {
|
|||||||
|
|
||||||
Exchanges the values of two JSON objects.
|
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
|
## Possible implementation
|
||||||
|
|
||||||
```cpp
|
```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
|
## See also
|
||||||
|
|
||||||
- [swap](swap.md)
|
- [swap](swap.md)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user