mirror of
https://github.com/nlohmann/json.git
synced 2025-07-28 12:02:00 +03:00
Merge branch 'feature/update' into develop
This commit is contained in:
105
src/json.hpp
105
src/json.hpp
@ -323,6 +323,7 @@ json.exception.type_error.308 | cannot use push_back() with string | The @ref pu
|
||||
json.exception.type_error.309 | cannot use insert() with | The @ref insert() member functions can only be executed for certain JSON types.
|
||||
json.exception.type_error.310 | cannot use swap() with number | The @ref swap() member functions can only be executed for certain JSON types.
|
||||
json.exception.type_error.311 | cannot use emplace_back() with string | The @ref emplace_back() member function can only be executed for certain JSON types.
|
||||
json.exception.type_error.312 | cannot use update() with string | The @ref update() member functions can only be executed for certain JSON types.
|
||||
json.exception.type_error.313 | invalid value to unflatten | The @ref unflatten function converts an object whose keys are JSON Pointers back into an arbitrary nested JSON value. The JSON Pointers must not overlap, because then the resulting value would not be well defined.
|
||||
json.exception.type_error.314 | only objects can be unflattened | The @ref unflatten function only works for an object whose keys are JSON Pointers.
|
||||
json.exception.type_error.315 | values in object must be primitive | The @ref unflatten function only works for an object whose keys are JSON Pointers and whose values are primitive.
|
||||
@ -11896,6 +11897,110 @@ class basic_json
|
||||
m_value.object->insert(first.m_it.object_iterator, last.m_it.object_iterator);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief updates a JSON object from another object, overwriting existing keys
|
||||
|
||||
Inserts all values from JSON object @a j and overwrites existing keys.
|
||||
|
||||
@param[in] j JSON object to read values from
|
||||
|
||||
@throw type_error.312 if called on JSON values other than objects; example:
|
||||
`"cannot use update() with string"`
|
||||
|
||||
@complexity O(N*log(size() + N)), where N is the number of elements to
|
||||
insert.
|
||||
|
||||
@liveexample{The example shows how `update()` is used.,update}
|
||||
|
||||
@sa https://docs.python.org/3.6/library/stdtypes.html#dict.update
|
||||
|
||||
@since version 3.0.0
|
||||
*/
|
||||
void update(const_reference j)
|
||||
{
|
||||
// implicitly convert null value to an empty object
|
||||
if (is_null())
|
||||
{
|
||||
m_type = value_t::object;
|
||||
m_value.object = create<object_t>();
|
||||
assert_invariant();
|
||||
}
|
||||
|
||||
if (JSON_UNLIKELY(not is_object()))
|
||||
{
|
||||
JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
|
||||
}
|
||||
if (JSON_UNLIKELY(not j.is_object()))
|
||||
{
|
||||
JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(j.type_name())));
|
||||
}
|
||||
|
||||
for (auto it = j.begin(); it != j.end(); ++it)
|
||||
{
|
||||
m_value.object->operator[](it.key()) = it.value();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief updates a JSON object from another object, overwriting existing keys
|
||||
|
||||
Inserts all values from from range `[first, last)` and overwrites existing
|
||||
keys.
|
||||
|
||||
@param[in] first begin of the range of elements to insert
|
||||
@param[in] last end of the range of elements to insert
|
||||
|
||||
@throw type_error.312 if called on JSON values other than objects; example:
|
||||
`"cannot use update() with string"`
|
||||
@throw invalid_iterator.202 if iterator @a first or @a last does does not
|
||||
point to an object; example: `"iterators first and last must point to
|
||||
objects"`
|
||||
@throw invalid_iterator.210 if @a first and @a last do not belong to the
|
||||
same JSON value; example: `"iterators do not fit"`
|
||||
|
||||
@complexity O(N*log(size() + N)), where N is the number of elements to
|
||||
insert.
|
||||
|
||||
@liveexample{The example shows how `update()` is used__range.,update}
|
||||
|
||||
@sa https://docs.python.org/3.6/library/stdtypes.html#dict.update
|
||||
|
||||
@since version 3.0.0
|
||||
*/
|
||||
void update(const_iterator first, const_iterator last)
|
||||
{
|
||||
// implicitly convert null value to an empty object
|
||||
if (is_null())
|
||||
{
|
||||
m_type = value_t::object;
|
||||
m_value.object = create<object_t>();
|
||||
assert_invariant();
|
||||
}
|
||||
|
||||
if (JSON_UNLIKELY(not is_object()))
|
||||
{
|
||||
JSON_THROW(type_error::create(312, "cannot use update() with " + std::string(type_name())));
|
||||
}
|
||||
|
||||
// check if range iterators belong to the same JSON object
|
||||
if (JSON_UNLIKELY(first.m_object != last.m_object))
|
||||
{
|
||||
JSON_THROW(invalid_iterator::create(210, "iterators do not fit"));
|
||||
}
|
||||
|
||||
// passed iterators must belong to objects
|
||||
if (JSON_UNLIKELY(not first.m_object->is_object()
|
||||
or not first.m_object->is_object()))
|
||||
{
|
||||
JSON_THROW(invalid_iterator::create(202, "iterators first and last must point to objects"));
|
||||
}
|
||||
|
||||
for (auto it = first; it != last; ++it)
|
||||
{
|
||||
m_value.object->operator[](it.key()) = it.value();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief exchanges the values
|
||||
|
||||
|
Reference in New Issue
Block a user