TerraForge3D  2.3.1
3D Terrain And Landscape Generator

◆ push_back() [2/4]

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>>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::push_back ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType > &  val)
inline

add an object to an array

add an object to an array

Appends the given element val to the end of the JSON value. If the function is called on a JSON null value, an empty array is created before appending val.

Parameters
[in]valthe value to add to the JSON array
Exceptions
type_error.308when called on a type other than JSON array or null; example: "cannot use push_back() with number"

@complexity Amortized constant.

@liveexample{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.,push_back}

Since
version 1.0.0

Definition at line 23185 of file json.hpp.

23186 {
23187 // push_back only works for null objects or arrays
23188 if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array())))
23189 {
23190 JSON_THROW(type_error::create(308, "cannot use push_back() with " + std::string(type_name()), *this));
23191 }
23192
23193 // transform null object into an array
23194 if (is_null())
23195 {
23196 m_type = value_t::array;
23199 }
23200
23201 // add element to array
23202 const auto old_capacity = m_value.array->capacity();
23203 m_value.array->push_back(val);
23204 set_parent(m_value.array->back(), old_capacity);
23205 }
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
Definition: json.hpp:18935
json_value m_value
the value of the current element
Definition: json.hpp:24928
constexpr bool is_array() const noexcept
return whether value is an array
Definition: json.hpp:20432
JSON_HEDLEY_RETURNS_NON_NULL const char * type_name() const noexcept
return the type as string
Definition: json.hpp:24883
constexpr bool is_null() const noexcept
return whether value is null
Definition: json.hpp:20251
@ array
array (ordered collection of values)