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>
std::pair< iterator, bool > nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::emplace |
( |
Args &&... |
args | ) |
|
|
inline |
add an object to an object if key does not exist
Inserts a new element into a JSON object constructed in-place with the given args if there is no element with the key in the container. If the function is called on a JSON null value, an empty object 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
- a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a bool denoting whether the insertion took place.
- Exceptions
-
type_error.311 | when called on a type other than JSON object or null; example: "cannot use emplace() with number" |
@complexity Logarithmic in the size of the container, O(log(size()
)).
@liveexample{The example shows how emplace()
can be used to add elements to a JSON object. Note how the null
value was silently converted to a JSON object. Further note how no value is added if there was already one value stored with the same key.,emplace}
- Since
- version 2.0.8
Definition at line 23392 of file json.hpp.
23393 {
23394
23396 {
23397 JSON_THROW(type_error::create(311,
"cannot use emplace() with " + std::string(
type_name()), *
this));
23398 }
23399
23400
23402 {
23406 }
23407
23408
23409 auto res =
m_value.object->emplace(std::forward<Args>(args)...);
23410 set_parent(res.first->second);
23411
23413 it.m_it.object_iterator = res.first;
23414
23415 return {it, res.second};
23416 }
iterator begin() noexcept
returns an iterator to the first element
void assert_invariant(bool check_parents=true) const noexcept
checks the class invariants
constexpr bool is_object() const noexcept
return whether value is an object
json_value m_value
the value of the current element
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
@ object
object (unordered set of name/value pairs)