]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | # <small>nlohmann::basic_json::</small>object |
2 | ||
3 | ```cpp | |
4 | static basic_json object(initializer_list_t init = {}); | |
5 | ``` | |
6 | ||
7 | Creates a JSON object value from a given initializer list. The initializer lists elements must be pairs, and their first | |
8 | elements must be strings. If the initializer list is empty, the empty object `#!json {}` is created. | |
9 | ||
10 | ## Parameters | |
11 | ||
12 | `init` (in) | |
13 | : initializer list with JSON values to create an object from (optional) | |
14 | ||
15 | ## Return value | |
16 | ||
17 | JSON object value | |
18 | ||
19 | ## Exception safety | |
20 | ||
21 | Strong guarantee: if an exception is thrown, there are no changes in the JSON value. | |
22 | ||
23 | ## Exceptions | |
24 | ||
25 | Throws [`type_error.301`](../../home/exceptions.md#jsonexceptiontype_error301) if `init` is not a list of pairs whose | |
26 | first elements are strings. In this case, no object can be created. When such a value is passed to | |
27 | `basic_json(initializer_list_t, bool, value_t)`, an array would have been created from the passed initializer list | |
28 | `init`. See example below. | |
29 | ||
30 | ## Complexity | |
31 | ||
32 | Linear in the size of `init`. | |
33 | ||
34 | ## Notes | |
35 | ||
36 | This function is only added for symmetry reasons. In contrast to the related function `array(initializer_list_t)`, there | |
37 | are no cases which can only be expressed by this function. That is, any initializer list `init` can also be passed to | |
38 | the initializer list constructor `basic_json(initializer_list_t, bool, value_t)`. | |
39 | ||
40 | ## Examples | |
41 | ||
42 | ??? example | |
43 | ||
44 | The following code shows an example for the `object` function. | |
45 | ||
46 | ```cpp | |
47 | --8<-- "examples/object.cpp" | |
48 | ``` | |
49 | ||
50 | Output: | |
51 | ||
52 | ```json | |
53 | --8<-- "examples/object.output" | |
54 | ``` | |
55 | ||
56 | ## See also | |
57 | ||
58 | - [`basic_json(initializer_list_t)`](basic_json.md) - create a JSON value from an initializer list | |
59 | - [`array`](array.md) - create a JSON array value from an initializer list | |
60 | ||
61 | ## Version history | |
62 | ||
63 | - Added in version 1.0.0. |