//// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) Copyright (c) 2025 Dmitry Arkhipov (grisumbras@yandex.ru) Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Official repository: https://github.com/boostorg/json //// [#dom_object] = `object` A <> stores an instance of <> as the underlying representation for a JSON object. Instances of the <> type are associative containers holding key and value pairs, where the key is a <> and the mapped type is a <>. These containers are modelled after standard maps with these properties: * The elements are stored contiguously as instances of <>. * Iterators are ordinary pointers, and may become invalidated on insertions and removals. * The order of insertions is preserved, as long as there are no removals. * All inserted values will use the same {ref_memory_resource} as the container itself. An empty object may be constructed without incurring any memory allocations using the <>. A <> can also be explicitly specified: [source] ---- include::../../../test/snippets.cpp[tag=snippet_objects_1,indent=0] ---- Initializer lists consisting of two-element key value pairs can be used to construct objects with initial contents. These constructors may allocate memory and throw: [source] ---- include::../../../test/snippets.cpp[tag=snippet_objects_2,indent=0] ---- Alternatively, elements may be inserted after construction: [source] ---- include::../../../test/snippets.cpp[tag=snippet_objects_3,indent=0] ---- Similar to the `std` counterpart, elements may be accessed directly by their key with bounds checking using <>, or without bounds checking using <> which creates a null element if the key does not already exist: [source] ---- include::../../../test/snippets.cpp[tag=snippet_objects_4,indent=0] ---- Internally, the container computes a hash table over the keys so that the complexity of lookups is in constant time, on average. [WARNING] ==== Unlike traditional node based containers like `std::set`, there is no guarantee of reference stability or iterator stability on insertions and erasures. For example: [source] ---- include::../../../test/snippets.cpp[tag=snippet_objects_5,indent=0] ---- Using `arr` after adding another value to `obj` results in undefined behavior. ==== For the complete listing of all available member functions and nested types, see the reference page for <>. As with `std::pair`, the <> type can be used with structured bindings in {cpp}17. Specializations of `std::tuple_size`, `std::tuple_element`, and overloads of <> are all provided for this purpose. == Formatted Output When an <> is formatted to a {std_ostream}, the result is a valid JSON. That is, the object will be output with curly braces and a comma separated list of key/value pairs, as per the JSON specification.