]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | # <small>nlohmann::basic_json::</small>basic_json |
2 | ||
3 | ```cpp | |
4 | // (1) | |
5 | basic_json(const value_t v); | |
6 | ||
7 | // (2) | |
8 | basic_json(std::nullptr_t = nullptr) noexcept; | |
9 | ||
10 | // (3) | |
11 | template<typename CompatibleType> | |
12 | basic_json(CompatibleType&& val) noexcept(noexcept( | |
13 | JSONSerializer<U>::to_json(std::declval<basic_json_t&>(), | |
14 | std::forward<CompatibleType>(val)))); | |
15 | ||
16 | // (4) | |
17 | template<typename BasicJsonType> | |
18 | basic_json(const BasicJsonType& val); | |
19 | ||
20 | // (5) | |
21 | basic_json(initializer_list_t init, | |
22 | bool type_deduction = true, | |
23 | value_t manual_type = value_t::array); | |
24 | ||
25 | // (6) | |
26 | basic_json(size_type cnt, const basic_json& val); | |
27 | ||
28 | // (7) | |
29 | basic_json(iterator first, iterator last); | |
30 | basic_json(const_iterator first, const_iterator last); | |
31 | ||
32 | // (8) | |
33 | basic_json(const basic_json& other); | |
34 | ||
35 | // (9) | |
36 | basic_json(basic_json&& other) noexcept; | |
37 | ``` | |
38 | ||
39 | 1. Create an empty JSON value with a given type. The value will be default initialized with an empty value which depends | |
40 | on the type: | |
41 | ||
42 | | Value type | initial value | | |
43 | |------------|----------------| | |
44 | | null | `#!json null` | | |
45 | | boolean | `#!json false` | | |
46 | | string | `#!json ""` | | |
47 | | number | `#!json 0` | | |
48 | | object | `#!json {}` | | |
49 | | array | `#!json []` | | |
50 | | binary | empty array | | |
51 | ||
52 | The postcondition of this constructor can be restored by calling [`clear()`](clear.md). | |
53 | ||
54 | 2. Create a `#!json null` JSON value. It either takes a null pointer as parameter (explicitly creating `#!json null`) | |
55 | or no parameter (implicitly creating `#!json null`). The passed null pointer itself is not read -- it is only used to | |
56 | choose the right constructor. | |
57 | ||
58 | 3. This is a "catch all" constructor for all compatible JSON types; that is, types for which a `to_json()` method | |
59 | exists. The constructor forwards the parameter `val` to that method (to `json_serializer<U>::to_json` method with | |
60 | `U = uncvref_t<CompatibleType>`, to be exact). | |
61 | ||
62 | Template type `CompatibleType` includes, but is not limited to, the following types: | |
63 | ||
64 | - **arrays**: [`array_t`](array_t.md) and all kinds of compatible containers such as `std::vector`, `std::deque`, | |
65 | `std::list`, `std::forward_list`, `std::array`, `std::valarray`, `std::set`, `std::unordered_set`, `std::multiset`, | |
66 | and `std::unordered_multiset` with a `value_type` from which a `basic_json` value can be constructed. | |
67 | - **objects**: [`object_t`](object_t.md) and all kinds of compatible associative containers such as `std::map`, | |
68 | `std::unordered_map`, `std::multimap`, and `std::unordered_multimap` with a `key_type` compatible to `string_t` | |
69 | and a `value_type` from which a `basic_json` value can be constructed. | |
70 | - **strings**: `string_t`, string literals, and all compatible string containers can be used. | |
71 | - **numbers**: [`number_integer_t`](number_integer_t.md), [`number_unsigned_t`](number_unsigned_t.md), | |
72 | [`number_float_t`](number_float_t.md), and all convertible number types such as `int`, `size_t`, `int64_t`, `float` | |
73 | or `double` can be used. | |
74 | - **boolean**: `boolean_t` / `bool` can be used. | |
75 | - **binary**: `binary_t` / `std::vector<uint8_t>` may be used; unfortunately because string literals cannot be | |
76 | distinguished from binary character arrays by the C++ type system, all types compatible with `const char*` will be | |
77 | directed to the string constructor instead. This is both for backwards compatibility, and due to the fact that a | |
78 | binary type is not a standard JSON type. | |
79 | ||
80 | See the examples below. | |
81 | ||
82 | 4. This is a constructor for existing `basic_json` types. It does not hijack copy/move constructors, since the parameter | |
83 | has different template arguments than the current ones. | |
84 | ||
85 | The constructor tries to convert the internal `m_value` of the parameter. | |
86 | ||
87 | 5. Creates a JSON value of type array or object from the passed initializer list `init`. In case `type_deduction` is | |
88 | `#!cpp true` (default), the type of the JSON value to be created is deducted from the initializer list `init` | |
89 | according to the following rules: | |
90 | ||
91 | 1. If the list is empty, an empty JSON object value `{}` is created. | |
92 | 2. If the list consists of pairs whose first element is a string, a JSON object value is created where the first | |
93 | elements of the pairs are treated as keys and the second elements are as values. | |
94 | 3. In all other cases, an array is created. | |
95 | ||
96 | The rules aim to create the best fit between a C++ initializer list and JSON values. The rationale is as follows: | |
97 | ||
98 | 1. The empty initializer list is written as `#!cpp {}` which is exactly an empty JSON object. | |
99 | 2. C++ has no way of describing mapped types other than to list a list of pairs. As JSON requires that keys must be | |
100 | of type string, rule 2 is the weakest constraint one can pose on initializer lists to interpret them as an | |
101 | object. | |
102 | 3. In all other cases, the initializer list could not be interpreted as JSON object type, so interpreting it as JSON | |
103 | array type is safe. | |
104 | ||
105 | With the rules described above, the following JSON values cannot be expressed by an initializer list: | |
106 | ||
107 | - the empty array (`#!json []`): use `array(initializer_list_t)` with an empty initializer list in this case | |
108 | - arrays whose elements satisfy rule 2: use `array(initializer_list_t)` with the same initializer list in this case | |
109 | ||
110 | Function [`array()`](array.md) and [`object()`](object.md) force array and object creation from initializer lists, | |
111 | respectively. | |
112 | ||
113 | 6. Constructs a JSON array value by creating `cnt` copies of a passed value. In case `cnt` is `0`, an empty array is | |
114 | created. | |
115 | ||
116 | 7. Constructs the JSON value with the contents of the range `[first, last)`. The semantics depends on the different | |
117 | types a JSON value can have: | |
118 | ||
119 | - In case of a `#!json null` type, [invalid_iterator.206](../../home/exceptions.md#jsonexceptioninvalid_iterator206) | |
120 | is thrown. | |
121 | - In case of other primitive types (number, boolean, or string), `first` must be `begin()` and `last` must be | |
122 | `end()`. In this case, the value is copied. Otherwise, | |
123 | [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) is thrown. | |
124 | - In case of structured types (array, object), the constructor behaves as similar versions for `std::vector` or | |
125 | `std::map`; that is, a JSON array or object is constructed from the values in the range. | |
126 | ||
127 | 8. Creates a copy of a given JSON value. | |
128 | ||
129 | 9. Move constructor. Constructs a JSON value with the contents of the given value `other` using move semantics. It | |
130 | "steals" the resources from `other` and leaves it as JSON `#!json null` value. | |
131 | ||
132 | ## Template parameters | |
133 | ||
134 | `CompatibleType` | |
135 | : a type such that: | |
136 | ||
137 | - `CompatibleType` is not derived from `std::istream`, | |
138 | - `CompatibleType` is not `basic_json` (to avoid hijacking copy/move constructors), | |
139 | - `CompatibleType` is not a different `basic_json` type (i.e. with different template arguments) | |
140 | - `CompatibleType` is not a `basic_json` nested type (e.g., `json_pointer`, `iterator`, etc.) | |
141 | - `json_serializer<U>` (with `U = uncvref_t<CompatibleType>`) has a `to_json(basic_json_t&, CompatibleType&&)` | |
142 | method | |
143 | ||
144 | `BasicJsonType`: | |
145 | : a type such that: | |
146 | ||
147 | - `BasicJsonType` is a `basic_json` type. | |
148 | - `BasicJsonType` has different template arguments than `basic_json_t`. | |
149 | ||
150 | `U`: | |
151 | : `uncvref_t<CompatibleType>` | |
152 | ||
153 | ## Parameters | |
154 | ||
155 | `v` (in) | |
156 | : the type of the value to create | |
157 | ||
158 | `val` (in) | |
159 | : the value to be forwarded to the respective constructor | |
160 | ||
161 | `init` (in) | |
162 | : initializer list with JSON values | |
163 | ||
164 | `type_deduction` (in) | |
165 | : internal parameter; when set to `#!cpp true`, the type of the JSON value is deducted from the initializer list | |
166 | `init`; when set to `#!cpp false`, the type provided via `manual_type` is forced. This mode is used by the functions | |
167 | `array(initializer_list_t)` and `object(initializer_list_t)`. | |
168 | ||
169 | `manual_type` (in) | |
170 | : internal parameter; when `type_deduction` is set to `#!cpp false`, the created JSON value will use the provided type | |
171 | (only `value_t::array` and `value_t::object` are valid); when `type_deduction` is set to `#!cpp true`, this | |
172 | parameter has no effect | |
173 | ||
174 | `cnt` (in) | |
175 | : the number of JSON copies of `val` to create | |
176 | ||
177 | `first` (in) | |
178 | : begin of the range to copy from (included) | |
179 | ||
180 | `last` (in) | |
181 | : end of the range to copy from (excluded) | |
182 | ||
183 | `other` (in) | |
184 | : the JSON value to copy/move | |
185 | ||
186 | ## Exception safety | |
187 | ||
188 | 1. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. | |
189 | 2. No-throw guarantee: this constructor never throws exceptions. | |
190 | 3. Depends on the called constructor. For types directly supported by the library (i.e., all types for which no | |
191 | `to_json()` function was provided), strong guarantee holds: if an exception is thrown, there are no changes to any | |
192 | JSON value. | |
193 | 4. Depends on the called constructor. For types directly supported by the library (i.e., all types for which no | |
194 | `to_json()` function was provided), strong guarantee holds: if an exception is thrown, there are no changes to any | |
195 | JSON value. | |
196 | 5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. | |
197 | 6. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. | |
198 | 7. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. | |
199 | 8. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. | |
200 | 9. No-throw guarantee: this constructor never throws exceptions. | |
201 | ||
202 | ## Exceptions | |
203 | ||
204 | 1. / | |
205 | 2. The function does not throw exceptions. | |
206 | 3. / | |
207 | 4. / | |
208 | 5. The function can throw the following exceptions: | |
209 | - Throws [`type_error.301`](../../home/exceptions.md#jsonexceptiontype_error301) if `type_deduction` is | |
210 | `#!cpp false`, `manual_type` is `value_t::object`, but `init` contains an element which is not a pair whose first | |
211 | element is a string. In this case, the constructor could not create an object. If `type_deduction` would have been | |
212 | `#!cpp true`, an array would have been created. See `object(initializer_list_t)` for an example. | |
213 | 6. / | |
214 | 7. The function can throw the following exceptions: | |
215 | - Throws [`invalid_iterator.201`](../../home/exceptions.md#jsonexceptioninvalid_iterator201) if iterators `first` | |
216 | and `last` are not compatible (i.e., do not belong to the same JSON value). In this case, the range | |
217 | `[first, last)` is undefined. | |
218 | - Throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) if iterators `first` | |
219 | and `last` belong to a primitive type (number, boolean, or string), but `first` does not point to the first | |
220 | element anymore. In this case, the range `[first, last)` is undefined. See example code below. | |
221 | - Throws [`invalid_iterator.206`](../../home/exceptions.md#jsonexceptioninvalid_iterator206) if iterators `first` | |
222 | and `last` belong to a `#!json null` value. In this case, the range `[first, last)` is undefined. | |
223 | 8. / | |
224 | 9. The function does not throw exceptions. | |
225 | ||
226 | ## Complexity | |
227 | ||
228 | 1. Constant. | |
229 | 2. Constant. | |
230 | 3. Usually linear in the size of the passed `val`, also depending on the implementation of the called `to_json()` | |
231 | method. | |
232 | 4. Usually linear in the size of the passed `val`, also depending on the implementation of the called `to_json()` | |
233 | method. | |
234 | 5. Linear in the size of the initializer list `init`. | |
235 | 6. Linear in `cnt`. | |
236 | 7. Linear in distance between `first` and `last`. | |
237 | 8. Linear in the size of `other`. | |
238 | 9. Constant. | |
239 | ||
240 | ## Notes | |
241 | ||
242 | - Overload 5: | |
243 | ||
244 | !!! note | |
245 | ||
246 | When used without parentheses around an empty initializer list, `basic_json()` is called instead of this | |
247 | function, yielding the JSON `#!json null` value. | |
248 | ||
249 | - Overload 7: | |
250 | ||
251 | !!! info "Preconditions" | |
252 | ||
253 | - Iterators `first` and `last` must be initialized. **This precondition is enforced with an assertion (see | |
254 | warning).** If assertions are switched off, a violation of this precondition yields undefined behavior. | |
255 | - Range `[first, last)` is valid. Usually, this precondition cannot be checked efficiently. Only certain edge | |
256 | cases are detected; see the description of the exceptions above. A violation of this precondition yields | |
257 | undefined behavior. | |
258 | ||
259 | !!! warning | |
260 | ||
261 | A precondition is enforced with a runtime assertion that will result in calling `std::abort` if this | |
262 | precondition is not met. Assertions can be disabled by defining `NDEBUG` at compile time. See | |
263 | <https://en.cppreference.com/w/cpp/error/assert> for more information. | |
264 | ||
265 | - Overload 8: | |
266 | ||
267 | !!! info "Postcondition" | |
268 | ||
269 | `#!cpp *this == other` | |
270 | ||
271 | - Overload 9: | |
272 | ||
273 | !!! info "Postconditions" | |
274 | ||
275 | - `#!cpp `*this` has the same value as `other` before the call. | |
276 | - `other` is a JSON `#!json null` value | |
277 | ||
278 | ## Examples | |
279 | ||
280 | ??? example "Example: (1) create an empty value with a given type" | |
281 | ||
282 | The following code shows the constructor for different `value_t` values. | |
283 | ||
284 | ```cpp | |
285 | --8<-- "examples/basic_json__value_t.cpp" | |
286 | ``` | |
287 | ||
288 | Output: | |
289 | ||
290 | ```json | |
291 | --8<-- "examples/basic_json__value_t.output" | |
292 | ``` | |
293 | ||
294 | ??? example "Example: (2) create a `#!json null` object" | |
295 | ||
296 | The following code shows the constructor with and without a null pointer parameter. | |
297 | ||
298 | ```cpp | |
299 | --8<-- "examples/basic_json__nullptr_t.cpp" | |
300 | ``` | |
301 | ||
302 | Output: | |
303 | ||
304 | ```json | |
305 | --8<-- "examples/basic_json__nullptr_t.output" | |
306 | ``` | |
307 | ||
308 | ??? example "Example: (3) create a JSON value from compatible types" | |
309 | ||
310 | The following code shows the constructor with several compatible types. | |
311 | ||
312 | ```cpp | |
313 | --8<-- "examples/basic_json__CompatibleType.cpp" | |
314 | ``` | |
315 | ||
316 | Output: | |
317 | ||
318 | ```json | |
319 | --8<-- "examples/basic_json__CompatibleType.output" | |
320 | ``` | |
321 | ||
322 | ??? example "Example: (5) create a container (array or object) from an initializer list" | |
323 | ||
324 | The example below shows how JSON values are created from initializer lists. | |
325 | ||
326 | ```cpp | |
327 | --8<-- "examples/basic_json__list_init_t.cpp" | |
328 | ``` | |
329 | ||
330 | Output: | |
331 | ||
332 | ```json | |
333 | --8<-- "examples/basic_json__list_init_t.output" | |
334 | ``` | |
335 | ||
336 | ??? example "Example: (6) construct an array with count copies of given value" | |
337 | ||
338 | The following code shows examples for creating arrays with several copies of a given value. | |
339 | ||
340 | ```cpp | |
341 | --8<-- "examples/basic_json__size_type_basic_json.cpp" | |
342 | ``` | |
343 | ||
344 | Output: | |
345 | ||
346 | ```json | |
347 | --8<-- "examples/basic_json__size_type_basic_json.output" | |
348 | ``` | |
349 | ||
350 | ??? example "Example: (7) construct a JSON container given an iterator range" | |
351 | ||
352 | The example below shows several ways to create JSON values by specifying a subrange with iterators. | |
353 | ||
354 | ```cpp | |
355 | --8<-- "examples/basic_json__InputIt_InputIt.cpp" | |
356 | ``` | |
357 | ||
358 | Output: | |
359 | ||
360 | ```json | |
361 | --8<-- "examples/basic_json__InputIt_InputIt.output" | |
362 | ``` | |
363 | ||
364 | ??? example "Example: (8) copy constructor" | |
365 | ||
366 | The following code shows an example for the copy constructor. | |
367 | ||
368 | ```cpp | |
369 | --8<-- "examples/basic_json__basic_json.cpp" | |
370 | ``` | |
371 | ||
372 | Output: | |
373 | ||
374 | ```json | |
375 | --8<-- "examples/basic_json__basic_json.output" | |
376 | ``` | |
377 | ||
378 | ??? example "Example: (9) move constructor" | |
379 | ||
380 | The code below shows the move constructor explicitly called via `std::move`. | |
381 | ||
382 | ```cpp | |
383 | --8<-- "examples/basic_json__moveconstructor.cpp" | |
384 | ``` | |
385 | ||
386 | Output: | |
387 | ||
388 | ```json | |
389 | --8<-- "examples/basic_json__moveconstructor.output" | |
390 | ``` | |
391 | ||
392 | ## Version history | |
393 | ||
394 | 1. Since version 1.0.0. | |
395 | 2. Since version 1.0.0. | |
396 | 3. Since version 2.1.0. | |
397 | 4. Since version 3.2.0. | |
398 | 5. Since version 1.0.0. | |
399 | 6. Since version 1.0.0. | |
400 | 7. Since version 1.0.0. | |
401 | 8. Since version 1.0.0. | |
402 | 9. Since version 1.0.0. |