1
0
mirror of https://github.com/nlohmann/json.git synced 2025-07-29 23:01:16 +03:00

📝 add more API documentation

This commit is contained in:
Niels Lohmann
2020-08-12 14:29:25 +02:00
parent fe89049aee
commit 874f49e945
22 changed files with 285 additions and 405 deletions

View File

@ -12,13 +12,11 @@ void push_back(const typename object_t::value_type& val);
void push_back(initializer_list_t init);
```
1. Appends the given element `val` to the end of the JSON array. If the
function is called on a JSON null value, an empty array is created before
appending `val`.
1. Appends the given element `val` to the end of the JSON array. If the function is called on a JSON null value, an
empty array is created before appending `val`.
2. Inserts the given element `val` to the JSON object. If the function is
called on a JSON null value, an empty object is created before inserting
`val`.
2. Inserts the given element `val` to the JSON object. If the function is called on a JSON null value, an empty object
is created before inserting `val`.
3. This function allows to use `push_back` with an initializer list. In case
@ -26,9 +24,8 @@ void push_back(initializer_list_t init);
2. the initializer list `init` contains only two elements, and
3. the first element of `init` is a string,
`init` is converted into an object element and added using
`push_back(const typename object_t::value_type&)`. Otherwise, `init`
is converted to a JSON value and added using `push_back(basic_json&&)`.
`init` is converted into an object element and added using `push_back(const typename object_t::value_type&)`.
Otherwise, `init` is converted to a JSON value and added using `push_back(basic_json&&)`.
## Parameters
@ -41,11 +38,11 @@ void push_back(initializer_list_t init);
## Exceptions
1. The function can throw the following exceptions:
- Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than JSON array or
null; example: `"cannot use push_back() with number"`
- Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
JSON array or null; example: `"cannot use push_back() with number"`
2. The function can throw the following exceptions:
- Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than JSON object or
null; example: `"cannot use push_back() with number"`
- Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
JSON object or null; example: `"cannot use push_back() with number"`
## Complexity
@ -55,18 +52,16 @@ void push_back(initializer_list_t init);
## Notes
(3) This function is required to resolve an ambiguous overload error,
because pairs like `{"key", "value"}` can be both interpreted as
`object_t::value_type` or `std::initializer_list<basic_json>`, see
[#235](https://github.com/nlohmann/json/issues/235) for more information.
(3) This function is required to resolve an ambiguous overload error, because pairs like `{"key", "value"}` can be both
interpreted as `object_t::value_type` or `std::initializer_list<basic_json>`, see
[#235](https://github.com/nlohmann/json/issues/235) for more information.
## Examples
??? example
The example shows how `push_back()` and `+=` can be used to
add elements to a JSON array. Note how the `null` value was silently
converted to a JSON array.
The example shows how `push_back()` and `+=` can be used to add elements to a JSON array. Note how the `null` value
was silently converted to a JSON array.
```cpp
--8<-- "examples/push_back.cpp"
@ -80,9 +75,8 @@ because pairs like `{"key", "value"}` can be both interpreted as
??? example
The example shows how `push_back()` and `+=` can be used to
add elements to a JSON object. Note how the `null` value was silently
converted to a JSON object.
The example shows how `push_back()` and `+=` can be used to add elements to a JSON object. Note how the `null` value
was silently converted to a JSON object.
```cpp
--8<-- "examples/push_back__object_t__value.cpp"
@ -96,8 +90,7 @@ because pairs like `{"key", "value"}` can be both interpreted as
??? example
The example shows how initializer lists are treated as
objects when possible.
The example shows how initializer lists are treated as objects when possible.
```cpp
--8<-- "examples/push_back__initializer_list.cpp"