template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
template<class... Args>
reference nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::emplace_back |
( |
Args &&... |
args | ) |
|
|
inline |
add an object to an array
Creates a JSON value from the passed parameters args to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending the value created from args.
- Parameters
-
[in] | args | arguments to forward to a constructor of basic_json |
- Template Parameters
-
Args | compatible types to create a basic_json object |
- Returns
- reference to the inserted element
- Exceptions
-
type_error.311 | when called on a type other than JSON array or null; example: "cannot use emplace_back() with number" |
@complexity Amortized constant.
@liveexample{The example shows how push_back()
can be used to add elements to a JSON array. Note how the null
value was silently converted to a JSON array.,emplace_back}
- Since
- version 2.0.8, returns reference since 3.7.0
Definition at line 23342 of file json.hpp.
23343 {
23344
23346 {
23347 JSON_THROW(type_error::create(311,
"cannot use emplace_back() with " + std::string(
type_name()), *
this));
23348 }
23349
23350
23352 {
23356 }
23357
23358
23359 const auto old_capacity =
m_value.array->capacity();
23360 m_value.array->emplace_back(std::forward<Args>(args)...);
23361 return set_parent(
m_value.array->back(), old_capacity);
23362 }
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
json_value m_value
the value of the current element
constexpr bool is_array() const noexcept
return whether value is an array
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
constexpr bool is_null() const noexcept
return whether value is null
@ array
array (ordered collection of values)