* 👥 update contributor and sponsor list * 🚧 document BJData format * 🚧 document BJData format * 📝 clarified documentation of [json.exception.parse_error.112] * ✏️ adjust titles * 📝 add more examples * 🚨 adjust warnings for index.md files * 📝 add more examples * 🔥 remove example for deprecated code * 📝 add missing enum entry * 📝 overwork table for binary formats * ✅ add test to create table for binary formats * 📝 fix wording in example * 📝 add more examples * Update iterators.md (#3481) * ✨ add check for overloads to linter #3455 * 👥 update contributor list * 📝 add more examples * 📝 fix documentation * 📝 add more examples * 🎨 fix indentation * 🔥 remove example for destructor * 📝 overwork documentation * Updated BJData documentation, #3464 (#3493) * update bjdata.md for #3464 * Minor edit * Fix URL typo * Add info on demoting ND array to a 1-D optimized array when singleton dimension Co-authored-by: Chaoqi Zhang <prncoprs@163.com> Co-authored-by: Qianqian Fang <fangqq@gmail.com>
2.9 KiB
nlohmann::basic_json::push_back
// (1)
void push_back(basic_json&& val);
void push_back(const basic_json& val);
// (2)
void push_back(const typename object_t::value_type& val);
// (3)
void push_back(initializer_list_t init);
-
Appends the given element
valto the end of the JSON array. If the function is called on a JSON null value, an empty array is created before appendingval. -
Inserts the given element
valto the JSON object. If the function is called on a JSON null value, an empty object is created before insertingval. -
This function allows using
push_backwith an initializer list. In case- the current value is an object,
- the initializer list
initcontains only two elements, and - the first element of
initis a string,
initis converted into an object element and added usingpush_back(const typename object_t::value_type&). Otherwise,initis converted to a JSON value and added usingpush_back(basic_json&&).
Parameters
val(in)- the value to add to the JSON array/object
init(in)- an initializer list
Exceptions
All functions can throw the following exception:
- Throws
type_error.308when called on a type other than JSON array or null; example:"cannot use push_back() with number"
Complexity
- Amortized constant.
- Logarithmic in the size of the container, O(log(
size())). - Linear in the size of the initializer list
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 for more information.
Examples
??? example "Example: (1) add element to 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"
```
Output:
```json
--8<-- "examples/push_back.output"
```
??? example "Example: (2) add element to 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"
```
Output:
```json
--8<-- "examples/push_back__object_t__value.output"
```
??? example "Example: (3) add to object from initializer list"
The example shows how initializer lists are treated as objects when possible.
```cpp
--8<-- "examples/push_back__initializer_list.cpp"
```
Output:
```json
--8<-- "examples/push_back__initializer_list.output"
```
Version history
- Since version 1.0.0.
- Since version 1.0.0.
- Since version 2.0.0.