2020-08-12 19:41:59 +08:00
|
|
|
# basic_json::object_t
|
|
|
|
|
|
|
|
```cpp
|
|
|
|
using object_t = ObjectType<StringType,
|
|
|
|
basic_json,
|
|
|
|
object_comparator_t,
|
|
|
|
AllocatorType<std::pair<const StringType, basic_json>>>;
|
|
|
|
```
|
|
|
|
|
|
|
|
The type used to store JSON objects.
|
|
|
|
|
2021-04-29 02:33:05 +08:00
|
|
|
[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON objects as follows:
|
2020-08-12 20:29:25 +08:00
|
|
|
> An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a
|
|
|
|
> string, number, boolean, null, object, or array.
|
2020-08-12 19:41:59 +08:00
|
|
|
|
2020-08-12 20:29:25 +08:00
|
|
|
To store objects in C++, a type is defined by the template parameters described below.
|
2020-08-12 19:41:59 +08:00
|
|
|
|
|
|
|
## Template parameters
|
|
|
|
|
|
|
|
`ObjectType`
|
|
|
|
: the container to store objects (e.g., `std::map` or `std::unordered_map`)
|
|
|
|
|
|
|
|
`StringType`
|
2020-08-12 20:29:25 +08:00
|
|
|
: the type of the keys or names (e.g., `std::string`). The comparison function `std::less<StringType>` is used to
|
|
|
|
order elements inside the container.
|
2020-08-12 19:41:59 +08:00
|
|
|
|
|
|
|
`AllocatorType`
|
|
|
|
: the allocator to use for objects (e.g., `std::allocator`)
|
|
|
|
|
|
|
|
## Notes
|
|
|
|
|
|
|
|
#### Default type
|
|
|
|
|
2020-08-12 20:29:25 +08:00
|
|
|
With the default values for `ObjectType` (`std::map`), `StringType` (`std::string`), and `AllocatorType`
|
|
|
|
(`std::allocator`), the default value for `object_t` is:
|
2020-08-12 19:41:59 +08:00
|
|
|
|
|
|
|
```cpp
|
2020-08-15 21:18:07 +08:00
|
|
|
// until C++14
|
2020-08-12 19:41:59 +08:00
|
|
|
std::map<
|
|
|
|
std::string, // key_type
|
|
|
|
basic_json, // value_type
|
|
|
|
std::less<std::string>, // key_compare
|
|
|
|
std::allocator<std::pair<const std::string, basic_json>> // allocator_type
|
|
|
|
>
|
2020-08-15 21:18:07 +08:00
|
|
|
|
|
|
|
// since C++14
|
|
|
|
std::map<
|
|
|
|
std::string, // key_type
|
|
|
|
basic_json, // value_type
|
|
|
|
std::less<>, // key_compare
|
|
|
|
std::allocator<std::pair<const std::string, basic_json>> // allocator_type
|
|
|
|
>
|
2020-08-12 19:41:59 +08:00
|
|
|
```
|
|
|
|
|
2020-08-15 21:18:07 +08:00
|
|
|
See [`object_comparator_t`](object_comparator_t.md) for more information.
|
|
|
|
|
2020-08-12 19:41:59 +08:00
|
|
|
#### Behavior
|
|
|
|
|
2020-08-12 20:29:25 +08:00
|
|
|
The choice of `object_t` influences the behavior of the JSON class. With the default type, objects have the following
|
|
|
|
behavior:
|
2020-08-12 19:41:59 +08:00
|
|
|
|
2020-08-12 20:29:25 +08:00
|
|
|
- When all names are unique, objects will be interoperable in the sense that all software implementations receiving that
|
|
|
|
object will agree on the name-value mappings.
|
|
|
|
- When the names within an object are not unique, it is unspecified which one of the values for a given key will be
|
|
|
|
chosen. For instance, `#!json {"key": 2, "key": 1}` could be equal to either `#!json {"key": 1}` or
|
2020-08-12 19:41:59 +08:00
|
|
|
`#!json {"key": 2}`.
|
2020-08-12 20:29:25 +08:00
|
|
|
- Internally, name/value pairs are stored in lexicographical order of the names. Objects will also be serialized (see
|
|
|
|
[`dump`](dump.md)) in this order. For instance, `#!json {"b": 1, "a": 2}` and `#!json {"a": 2, "b": 1}` will be stored
|
2020-08-12 19:41:59 +08:00
|
|
|
and serialized as `#!json {"a": 2, "b": 1}`.
|
2020-08-12 20:29:25 +08:00
|
|
|
- When comparing objects, the order of the name/value pairs is irrelevant. This makes objects interoperable in the sense
|
|
|
|
that they will not be affected by these differences. For instance, `#!json {"b": 1, "a": 2}` and
|
2020-08-12 19:41:59 +08:00
|
|
|
`#!json {"a": 2, "b": 1}` will be treated as equal.
|
|
|
|
|
|
|
|
#### Limits
|
|
|
|
|
2021-04-29 02:33:05 +08:00
|
|
|
[RFC 8259](https://tools.ietf.org/html/rfc8259) specifies:
|
2020-08-12 19:41:59 +08:00
|
|
|
> An implementation may set limits on the maximum depth of nesting.
|
|
|
|
|
2020-08-12 20:29:25 +08:00
|
|
|
In this class, the object's limit of nesting is not explicitly constrained. However, a maximum depth of nesting may be
|
|
|
|
introduced by the compiler or runtime environment. A theoretical limit can be queried by calling the
|
2020-08-12 19:41:59 +08:00
|
|
|
[`max_size`](max_size.md) function of a JSON object.
|
|
|
|
|
|
|
|
#### Storage
|
|
|
|
|
2020-08-12 20:29:25 +08:00
|
|
|
Objects are stored as pointers in a `basic_json` type. That is, for any access to object values, a pointer of type
|
|
|
|
`object_t*` must be dereferenced.
|
2020-08-12 19:41:59 +08:00
|
|
|
|
|
|
|
#### Object key order
|
|
|
|
|
2020-08-12 20:29:25 +08:00
|
|
|
The order name/value pairs are added to the object is *not* preserved by the library. Therefore, iterating an object may
|
|
|
|
return name/value pairs in a different order than they were originally stored. In fact, keys will be traversed in
|
|
|
|
alphabetical order as `std::map` with `std::less` is used by default. Please note this behavior conforms to
|
2021-04-29 02:33:05 +08:00
|
|
|
[RFC 8259](https://tools.ietf.org/html/rfc8259), because any order implements the specified "unordered" nature of JSON objects.
|
2020-08-12 19:41:59 +08:00
|
|
|
|
|
|
|
## Version history
|
|
|
|
|
|
|
|
- Added in version 1.0.0.
|