Merge branch 'develop' into docs/fix-readme-files-github

# Conflicts:
#	docs/mkdocs/docs/api/basic_json/dump.md
This commit is contained in:
Niels Lohmann committed 2026-06-30 09:13:38 +02:00
commit fb4e9026f4
93 files changed
+743 -1209

No files matched your search

@@ -30,7 +30,8 @@ class (either explicitly or via the conversion operators).
## Return value ## Return value
Copy of the JSON value, converted to `ValueType` 1. (none) -- the converted value is written to the output parameter `val`.
2. the JSON value `j` converted to `TargetType`
## Examples ## Examples
+2 -1
View File
@@ -101,6 +101,7 @@ A UTF-8 byte order mark is silently ignored.
## See also ## See also
- [parse](parse.md) - deserialize from a compatible input - [parse](parse.md) - deserialize from a compatible input
- [sax_parse](sax_parse.md) - parse input using the SAX interface
- [operator>>](../operator_gtgt.md) - deserialize from stream - [operator>>](../operator_gtgt.md) - deserialize from stream
## Version history ## Version history
@@ -108,7 +109,7 @@ A UTF-8 byte order mark is silently ignored.
- Added in version 3.0.0. - Added in version 3.0.0.
- Ignoring comments via `ignore_comments` added in version 3.9.0. - Ignoring comments via `ignore_comments` added in version 3.9.0.
- Changed [runtime assertion](../../features/assertions.md) in case of `FILE*` null pointers to exception in version 3.12.0. - Changed [runtime assertion](../../features/assertions.md) in case of `FILE*` null pointers to exception in version 3.12.0.
- Added `ignore_trailing_commas` in version 3.12.1. - Added `ignore_trailing_commas` in version 3.12.x.
!!! warning "Deprecation" !!! warning "Deprecation"
+2
View File
@@ -82,6 +82,8 @@ Strong exception safety: if an exception occurs, the original value stays intact
key of an object which cannot be found. See the example below. key of an object which cannot be found. See the example below.
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if the JSON pointer `ptr` can - Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if the JSON pointer `ptr` can
not be resolved. See the example below. not be resolved. See the example below.
- Throws [`out_of_range.410`](../../home/exceptions.md#jsonexceptionout_of_range410) if an array index in the passed
JSON pointer `ptr` exceeds the range of `size_type` (e.g., on 32-bit platforms).
## Complexity ## Complexity
+1 -1
View File
@@ -9,7 +9,7 @@ The type used to store JSON booleans.
[RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two [RFC 8259](https://tools.ietf.org/html/rfc8259) implicitly describes a boolean as a type which differentiates the two
literals `#!json true` and `#!json false`. literals `#!json true` and `#!json false`.
To store objects in C++, a type is defined by the template parameter `BooleanType` which chooses the type to use. To store boolean values in C++, a type is defined by the template parameter `BooleanType` which chooses the type to use.
## Notes ## Notes
+6 -5
View File
@@ -48,11 +48,7 @@ Strong exception safety: if an exception occurs, the original value stays intact
1. The function does not throw exceptions. 1. The function does not throw exceptions.
2. The function does not throw exceptions. 2. The function does not throw exceptions.
3. The function can throw the following exceptions: 3. The function does not throw exceptions.
- Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index begins with
`0`.
- Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index was not a
number.
## Complexity ## Complexity
@@ -111,6 +107,11 @@ Logarithmic in the size of the JSON object.
--8<-- "examples/contains__json_pointer.output" --8<-- "examples/contains__json_pointer.output"
``` ```
## See also
- [find](find.md) find a value in an object
- [count](count.md) returns the number of occurrences of a key
## Version history ## Version history
1. Added in version 3.11.0. 1. Added in version 3.11.0.
+5
View File
@@ -72,6 +72,11 @@ This method always returns `0` when executed on a JSON type that is not an objec
--8<-- "examples/count__keytype.c++17.output" --8<-- "examples/count__keytype.c++17.output"
``` ```
## See also
- [find](find.md) find a value in an object
- [contains](contains.md) checks whether a key exists
## Version history ## Version history
1. Added in version 3.11.0. 1. Added in version 3.11.0.
+1 -1
View File
@@ -10,7 +10,7 @@ Returns an iterator to the reverse-beginning; that is, the last element.
## Return value ## Return value
reverse iterator to the first element reverse iterator to the last element
## Exception safety ## Exception safety
+1 -1
View File
@@ -25,7 +25,7 @@ Constant.
??? example ??? example
The following code shows an example for `eend()`. The following code shows an example for `crend()`.
```cpp ```cpp
--8<-- "examples/crend.cpp" --8<-- "examples/crend.cpp"
+3
View File
@@ -56,6 +56,9 @@ Currently, only `remove`, `add`, and `replace` operations are generated.
## See also ## See also
- [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) - [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902)
- [patch](patch.md) applies a JSON Patch
- [patch_inplace](patch_inplace.md) applies a JSON Patch in place
- [merge_patch](merge_patch.md) applies a JSON Merge Patch
## Version history ## Version history
+6 -1
View File
@@ -26,7 +26,7 @@ and `ensure_ascii` parameters.
`error_handler` (in) `error_handler` (in)
: how to react on decoding errors; there are three possible values (see [`error_handler_t`](error_handler_t.md): : how to react on decoding errors; there are three possible values (see [`error_handler_t`](error_handler_t.md):
`strict` (throws and exception in case a decoding error occurs; default), `replace` (replace invalid UTF-8 sequences `strict` (throws an exception in case a decoding error occurs; default), `replace` (replace invalid UTF-8 sequences
with U+FFFD), and `ignore` (ignore invalid UTF-8 sequences during serialization; all valid bytes are copied to the with U+FFFD), and `ignore` (ignore invalid UTF-8 sequences during serialization; all valid bytes are copied to the
output unchanged, and invalid bytes are dropped)). output unchanged, and invalid bytes are dropped)).
@@ -71,6 +71,11 @@ Binary values are serialized as an object containing two keys:
--8<-- "examples/dump.output" --8<-- "examples/dump.output"
``` ```
## See also
- [to_string](to_string.md) returns a string representation of a JSON value
- [operator<<](../operator_ltlt.md) serialize to stream
## Version history ## Version history
- Added in version 1.0.0. - Added in version 1.0.0.
@@ -29,6 +29,10 @@ iterators (including the `end()` iterator) and all references to the elements ar
a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and a pair consisting of an iterator to the inserted element, or the already-existing element if no insertion happened, and
a `#!cpp bool` denoting whether the insertion took place. a `#!cpp bool` denoting whether the insertion took place.
## Exception safety
Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
## Exceptions ## Exceptions
Throws [`type_error.311`](../../home/exceptions.md#jsonexceptiontype_error311) when called on a type other than JSON Throws [`type_error.311`](../../home/exceptions.md#jsonexceptiontype_error311) when called on a type other than JSON
@@ -56,6 +60,11 @@ Logarithmic in the size of the container, O(log(`size()`)).
--8<-- "examples/emplace.output" --8<-- "examples/emplace.output"
``` ```
## See also
- [emplace_back](emplace_back.md) add a value to an array
- [insert](insert.md) add values to an array/object
## Version history ## Version history
- Since version 2.0.8. - Since version 2.0.8.
+6 -1
View File
@@ -101,7 +101,7 @@ Strong exception safety: if an exception occurs, the original value stays intact
4. See 3. 4. See 3.
5. The function can throw the following exceptions: 5. The function can throw the following exceptions:
- Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than - Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than
JSON object; example: `"cannot use erase() with null"` JSON array; example: `"cannot use erase() with null"`
- Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) when `idx >= size()`; example: - Throws [`out_of_range.401`](../../home/exceptions.md#jsonexceptionout_of_range401) when `idx >= size()`; example:
`"array index 17 is out of range"` `"array index 17 is out of range"`
@@ -202,6 +202,11 @@ Strong exception safety: if an exception occurs, the original value stays intact
--8<-- "examples/erase__size_type.output" --8<-- "examples/erase__size_type.output"
``` ```
## See also
- [clear](clear.md) clears the contents
- [insert](insert.md) add values to an array/object
## Version history ## Version history
1. Added in version 1.0.0. Added support for binary types in version 3.8.0. 1. Added in version 1.0.0. Added support for binary types in version 3.8.0.
+1
View File
@@ -78,6 +78,7 @@ This method always returns `end()` when executed on a JSON type that is not an o
## See also ## See also
- [count](count.md) returns the number of occurrences of a key
- [contains](contains.md) checks whether a key exists - [contains](contains.md) checks whether a key exists
## Version history ## Version history
@@ -67,6 +67,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs - Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed - Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
successfully successfully
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the size of an optimized container
or n-dimensional array cannot be represented by `std::size_t`
## Complexity ## Complexity
@@ -88,6 +90,14 @@ Linear in the size of the input.
--8<-- "examples/from_bjdata.output" --8<-- "examples/from_bjdata.output"
``` ```
## See also
- [to_bjdata](to_bjdata.md) create a BJData serialization of a JSON value
- [from_cbor](from_cbor.md) create a JSON value from an input in CBOR format
- [from_msgpack](from_msgpack.md) create a JSON value from an input in MessagePack format
- [from_bson](from_bson.md) create a JSON value from an input in BSON format
- [from_ubjson](from_ubjson.md) create a JSON value from an input in UBJSON format
## Version history ## Version history
- Added in version 3.11.0. - Added in version 3.11.0.
+7 -2
View File
@@ -62,8 +62,12 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
## Exceptions ## Exceptions
Throws [`parse_error.114`](../../home/exceptions.md#jsonexceptionparse_error114) if an unsupported BSON record type is - Throws [`parse_error.110`](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely or
encountered. the end of the input was not reached when `strict` was set to true
- Throws [`parse_error.112`](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs (e.g., an
invalid string or byte array length)
- Throws [`parse_error.114`](../../home/exceptions.md#jsonexceptionparse_error114) if an unsupported BSON record type is
encountered
## Complexity ## Complexity
@@ -92,6 +96,7 @@ Linear in the size of the input.
- [from_cbor](from_cbor.md) for the related CBOR format - [from_cbor](from_cbor.md) for the related CBOR format
- [from_msgpack](from_msgpack.md) for the related MessagePack format - [from_msgpack](from_msgpack.md) for the related MessagePack format
- [from_ubjson](from_ubjson.md) for the related UBJSON format - [from_ubjson](from_ubjson.md) for the related UBJSON format
- [from_bjdata](from_bjdata.md) for the related BJData format
## Version history ## Version history
@@ -96,6 +96,14 @@ Linear in the size of the input.
--8<-- "examples/from_cbor.output" --8<-- "examples/from_cbor.output"
``` ```
## See also
- [to_cbor](to_cbor.md) create a CBOR serialization of a JSON value
- [from_msgpack](from_msgpack.md) create a JSON value from an input in MessagePack format
- [from_bson](from_bson.md) create a JSON value from an input in BSON format
- [from_ubjson](from_ubjson.md) create a JSON value from an input in UBJSON format
- [from_bjdata](from_bjdata.md) create a JSON value from an input in BJData format
## Version history ## Version history
- Added in version 2.0.9. - Added in version 2.0.9.
@@ -89,6 +89,14 @@ Linear in the size of the input.
--8<-- "examples/from_msgpack.output" --8<-- "examples/from_msgpack.output"
``` ```
## See also
- [to_msgpack](to_msgpack.md) create a MessagePack serialization of a JSON value
- [from_cbor](from_cbor.md) create a JSON value from an input in CBOR format
- [from_bson](from_bson.md) create a JSON value from an input in BSON format
- [from_ubjson](from_ubjson.md) create a JSON value from an input in UBJSON format
- [from_bjdata](from_bjdata.md) create a JSON value from an input in BJData format
## Version history ## Version history
- Added in version 2.0.9. - Added in version 2.0.9.
@@ -101,7 +109,7 @@ Linear in the size of the input.
- Overload (2) replaces calls to `from_msgpack` with a pointer and a length as first two parameters, which has been - Overload (2) replaces calls to `from_msgpack` with a pointer and a length as first two parameters, which has been
deprecated in version 3.8.0. This overload will be removed in version 4.0.0. Please replace all calls like deprecated in version 3.8.0. This overload will be removed in version 4.0.0. Please replace all calls like
`#!cpp from_msgpack(ptr, len, ...);` with `#!cpp from_msgpack(ptr, ptr+len, ...);`. `#!cpp from_msgpack(ptr, len, ...);` with `#!cpp from_msgpack(ptr, ptr+len, ...);`.
- Overload (2) replaces calls to `from_cbor` with a pair of iterators as their first parameter, which has been - Overload (2) replaces calls to `from_msgpack` with a pair of iterators as their first parameter, which has been
deprecated in version 3.8.0. This overload will be removed in version 4.0.0. Please replace all calls like deprecated in version 3.8.0. This overload will be removed in version 4.0.0. Please replace all calls like
`#!cpp from_msgpack({ptr, ptr+len}, ...);` with `#!cpp from_msgpack(ptr, ptr+len, ...);`. `#!cpp from_msgpack({ptr, ptr+len}, ...);` with `#!cpp from_msgpack(ptr, ptr+len, ...);`.
+11 -1
View File
@@ -65,8 +65,10 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
- Throws [parse_error.110](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely or - Throws [parse_error.110](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely or
the end of the file was not reached when `strict` was set to true the end of the file was not reached when `strict` was set to true
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs - Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed - Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
successfully successfully
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the size of an optimized container
or n-dimensional array cannot be represented by `std::size_t`
## Complexity ## Complexity
@@ -88,6 +90,14 @@ Linear in the size of the input.
--8<-- "examples/from_ubjson.output" --8<-- "examples/from_ubjson.output"
``` ```
## See also
- [to_ubjson](to_ubjson.md) create a UBJSON serialization of a JSON value
- [from_cbor](from_cbor.md) create a JSON value from an input in CBOR format
- [from_msgpack](from_msgpack.md) create a JSON value from an input in MessagePack format
- [from_bson](from_bson.md) create a JSON value from an input in BSON format
- [from_bjdata](from_bjdata.md) create a JSON value from an input in BJData format
## Version history ## Version history
- Added in version 3.1.0. - Added in version 3.1.0.
+12
View File
@@ -88,6 +88,11 @@ constexpr const PointerType get_ptr() const noexcept;
Depends on what `json_serializer<ValueType>` `from_json()` method throws Depends on what `json_serializer<ValueType>` `from_json()` method throws
## Complexity
Depends on the `json_serializer<ValueType>::from_json()` implementation for overloads (1) and (2); constant for
overload (3).
## Notes ## Notes
!!! danger "Undefined behavior for pointers" !!! danger "Undefined behavior for pointers"
@@ -144,6 +149,13 @@ Depends on what `json_serializer<ValueType>` `from_json()` method throws
--8<-- "examples/get__PointerType.output" --8<-- "examples/get__PointerType.output"
``` ```
## See also
- [get_to](get_to.md) convert and write into a passed value
- [get_ptr](get_ptr.md) get a pointer to the stored value
- [get_ref](get_ref.md) get a reference to the stored value
- [operator ValueType](operator_ValueType.md) get a value via implicit conversion
## Version history ## Version history
1. Since version 2.1.0. 1. Since version 2.1.0.
@@ -40,6 +40,11 @@ Constant.
--8<-- "examples/get_binary.output" --8<-- "examples/get_binary.output"
``` ```
## See also
- [get](get.md) get a value (explicit conversion)
- [get_ref](get_ref.md) get a reference to the stored value
## Version history ## Version history
- Added in version 3.8.0. - Added in version 3.8.0.
+10
View File
@@ -34,6 +34,10 @@ the input parameter, allowing chaining calls
Depends on what `json_serializer<ValueType>` `from_json()` method throws Depends on what `json_serializer<ValueType>` `from_json()` method throws
## Complexity
Depends on the `json_serializer<ValueType>::from_json()` implementation.
## Examples ## Examples
??? example ??? example
@@ -53,6 +57,12 @@ Depends on what `json_serializer<ValueType>` `from_json()` method throws
--8<-- "examples/get_to.output" --8<-- "examples/get_to.output"
``` ```
## See also
- [get](get.md) get a value (explicit conversion)
- [get_ref](get_ref.md) get a reference to the stored value
- [get_ptr](get_ptr.md) get a pointer to the stored value
## Version history ## Version history
- Since version 3.3.0. - Since version 3.3.0.
+9 -2
View File
@@ -96,8 +96,8 @@ Strong exception safety: if an exception occurs, the original value stays intact
5. The function can throw the following exceptions: 5. The function can throw the following exceptions:
- Throws [`type_error.309`](../../home/exceptions.md#jsonexceptiontype_error309) if called on JSON values other than - Throws [`type_error.309`](../../home/exceptions.md#jsonexceptiontype_error309) if called on JSON values other than
objects; example: `"cannot use insert() with string"` objects; example: `"cannot use insert() with string"`
- Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an - Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if `first` or `last`
iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"` do not point to an object; example: `"iterators first and last must point to objects"`
- Throws [`invalid_iterator.210`](../../home/exceptions.md#jsonexceptioninvalid_iterator210) if `first` and `last` - Throws [`invalid_iterator.210`](../../home/exceptions.md#jsonexceptioninvalid_iterator210) if `first` and `last`
do not belong to the same JSON value; example: `"iterators do not fit"` do not belong to the same JSON value; example: `"iterators do not fit"`
@@ -181,6 +181,13 @@ Strong exception safety: if an exception occurs, the original value stays intact
--8<-- "examples/insert__range_object.output" --8<-- "examples/insert__range_object.output"
``` ```
## See also
- [emplace](emplace.md) add a value to an object
- [emplace_back](emplace_back.md) add a value to an array
- [push_back](push_back.md) add a value to an array/object
- [update](update.md) merges objects
## Version history ## Version history
1. Added in version 1.0.0. 1. Added in version 1.0.0.
@@ -66,6 +66,7 @@ classDiagram
## See also ## See also
- [`exception`](exception.md) for the base class of all exceptions thrown by the library
- [List of iterator errors](../../home/exceptions.md#iterator-errors) - [List of iterator errors](../../home/exceptions.md#iterator-errors)
- [`parse_error`](parse_error.md) for exceptions indicating a parse error - [`parse_error`](parse_error.md) for exceptions indicating a parse error
- [`type_error`](type_error.md) for exceptions indicating executing a member function with a wrong type - [`type_error`](type_error.md) for exceptions indicating executing a member function with a wrong type
@@ -45,11 +45,13 @@ Constant.
When a value is discarded by a callback function (see [`parser_callback_t`](parser_callback_t.md)) during parsing, When a value is discarded by a callback function (see [`parser_callback_t`](parser_callback_t.md)) during parsing,
then it is removed when it is part of a structured value. For instance, if the second value of an array is discarded, then it is removed when it is part of a structured value. For instance, if the second value of an array is discarded,
instead of `#!json [null, discarded, false]`, the array `#!json [null, false]` is returned. Only if the top-level instead of `#!json [null, discarded, false]`, the array `#!json [null, false]` is returned. If the top-level value
value is discarded, the return value of the `parse` call is discarded. itself is discarded by the callback, the `parse` call returns a `#!json null` value.
This function will always be `#!cpp false` for JSON values after parsing. That is, discarded values can only occur After a successful parse, this function always returns `#!cpp false`: discarded values can only occur during parsing and
during parsing, but will be removed when inside a structured value or replaced by null in other cases. are either removed when inside a structured value or replaced by `#!json null` at the top level. The exception is parsing
with `allow_exceptions` set to `#!cpp false`: a parse error then yields a discarded value for which this function returns
`#!cpp true` (see [`parse`](parse.md)).
## Examples ## Examples
+1 -1
View File
@@ -9,7 +9,7 @@ unsigned) and floating-point values.
## Return value ## Return value
`#!cpp true` if type is number (regardless whether integer, unsigned integer, or floating-type), `#!cpp false` otherwise. `#!cpp true` if type is number (regardless whether integer, unsigned integer, or floating-point), `#!cpp false` otherwise.
## Exception safety ## Exception safety
+5
View File
@@ -84,6 +84,11 @@ When iterating over an array, `key()` will return the index of the element as st
--8<-- "examples/items.output" --8<-- "examples/items.output"
``` ```
## See also
- [begin](begin.md) returns an iterator to the first element
- [end](end.md) returns an iterator to one past the last element
## Version history ## Version history
- Added `iterator_wrapper` in version 3.0.0. - Added `iterator_wrapper` in version 3.0.0.
+1 -1
View File
@@ -13,7 +13,7 @@ JSON object holding version information
| key | description | | key | description |
|-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |-------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `compiler` | Information on the used compiler. It is an object with the following keys: `c++` (the used C++ standard), `family` (the compiler family; possible values are `clang`, `icc`, `gcc`, `ilecpp`, `msvc`, `pgcpp`, `sunpro`, and `unknown`), and `version` (the compiler version). | | `compiler` | Information on the used compiler. It is an object with the following keys: `c++` (the used C++ standard), `family` (the compiler family; possible values are `clang`, `icc`, `gcc`, `ilecpp`, `msvc`, `pgcpp`, `sunpro`, and `unknown`), and `version` (the compiler version). On HP aCC compilers, `compiler` is instead the plain string `hp`. |
| `copyright` | The copyright line for the library as string. | | `copyright` | The copyright line for the library as string. |
| `name` | The name of the library as string. | | `name` | The name of the library as string. |
| `platform` | The used platform as string. Possible values are `win32`, `linux`, `apple`, `unix`, and `unknown`. | | `platform` | The used platform as string. Possible values are `win32`, `linux`, `apple`, `unix`, and `unknown`. |
@@ -32,7 +32,6 @@ With the default values for `NumberIntegerType` (`std::int64_t`), the default va
- The restrictions about leading zeros are not enforced in C++. Instead, leading zeros in integer literals lead to an - The restrictions about leading zeros are not enforced in C++. Instead, leading zeros in integer literals lead to an
interpretation as an octal number. Internally, the value will be stored as a decimal number. For instance, the C++ interpretation as an octal number. Internally, the value will be stored as a decimal number. For instance, the C++
integer literal `010` will be serialized to `8`. During deserialization, leading zeros yield an error. integer literal `010` will be serialized to `8`. During deserialization, leading zeros yield an error.
- Not-a-number (NaN) values will be serialized to `null`.
#### Limits #### Limits
@@ -32,7 +32,6 @@ With the default values for `NumberUnsignedType` (`std::uint64_t`), the default
- The restrictions about leading zeros are not enforced in C++. Instead, leading zeros in integer literals lead to an - The restrictions about leading zeros are not enforced in C++. Instead, leading zeros in integer literals lead to an
interpretation as an octal number. Internally, the value will be stored as a decimal number. For instance, the C++ interpretation as an octal number. Internally, the value will be stored as a decimal number. For instance, the C++
integer literal `010` will be serialized to `8`. During deserialization, leading zeros yield an error. integer literal `010` will be serialized to `8`. During deserialization, leading zeros yield an error.
- Not-a-number (NaN) values will be serialized to `null`.
#### Limits #### Limits
@@ -45,7 +44,7 @@ when used in a constructor. During deserialization, too large or small integer n
as [`number_integer_t`](number_integer_t.md) or [`number_float_t`](number_float_t.md). as [`number_integer_t`](number_integer_t.md) or [`number_float_t`](number_float_t.md).
[RFC 8259](https://tools.ietf.org/html/rfc8259) further states: [RFC 8259](https://tools.ietf.org/html/rfc8259) further states:
> Note that when such software is used, numbers that are integers and are in the range \f$[-2^{53}+1, 2^{53}-1]\f$ are > Note that when such software is used, numbers that are integers and are in the range $[-2^{53}+1, 2^{53}-1]$ are
> interoperable in the sense that implementations will agree exactly on their numeric values. > interoperable in the sense that implementations will agree exactly on their numeric values.
As this range is a subrange (when considered in conjunction with the `number_integer_t` type) of the exactly supported As this range is a subrange (when considered in conjunction with the `number_integer_t` type) of the exactly supported
@@ -50,9 +50,12 @@ invalidates all iterators and all references.
## Exceptions ## Exceptions
All functions can throw the following exception: 1. Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
- Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than JSON array or null; example: `"cannot use push_back() with number"`
JSON array or null; example: `"cannot use operator+=() with number"` 2. Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
JSON object or null; example: `"cannot use push_back() with number"`
3. Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
JSON array or null; example: `"cannot use push_back() with number"`
## Complexity ## Complexity
+11 -1
View File
@@ -5,7 +5,8 @@ basic_json& operator=(basic_json other) noexcept (
std::is_nothrow_move_constructible<value_t>::value && std::is_nothrow_move_constructible<value_t>::value &&
std::is_nothrow_move_assignable<value_t>::value && std::is_nothrow_move_assignable<value_t>::value &&
std::is_nothrow_move_constructible<json_value>::value && std::is_nothrow_move_constructible<json_value>::value &&
std::is_nothrow_move_assignable<json_value>::value std::is_nothrow_move_assignable<json_value>::value &&
std::is_nothrow_move_assignable<json_base_class_t>::value
); );
``` ```
@@ -17,6 +18,10 @@ constructor, destructor, and the `swap()` member function.
`other` (in) `other` (in)
: value to copy from : value to copy from
## Exception safety
Strong guarantee: if an exception is thrown while copying `other`, there are no changes to `#!cpp *this`.
## Complexity ## Complexity
Linear. Linear.
@@ -38,6 +43,11 @@ Linear.
--8<-- "examples/basic_json__copyassignment.output" --8<-- "examples/basic_json__copyassignment.output"
``` ```
## See also
- [basic_json](basic_json.md) create a JSON value
- [swap](swap.md) exchanges the contents of two JSON values
## Version history ## Version history
- Added in version 1.0.0. - Added in version 1.0.0.
@@ -83,6 +83,8 @@ Strong exception safety: if an exception occurs, the original value stays intact
in the passed JSON pointer `ptr` for the const version. in the passed JSON pointer `ptr` for the const version.
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if the JSON pointer `ptr` can - Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if the JSON pointer `ptr` can
not be resolved. not be resolved.
- Throws [`out_of_range.410`](../../home/exceptions.md#jsonexceptionout_of_range410) if an array index in the passed
JSON pointer `ptr` exceeds the range of `size_type` (e.g., on 32-bit platforms).
## Complexity ## Complexity
@@ -95,7 +97,10 @@ Strong exception safety: if an exception occurs, the original value stays intact
!!! danger "Undefined behavior and runtime assertions" !!! danger "Undefined behavior and runtime assertions"
1. If the element with key `idx` does not exist, the behavior is undefined. The following cases apply to the **const** overloads; the non-const overloads instead insert the missing element
(see the notes below).
1. If the element at index `idx` does not exist, the behavior is undefined.
2. If the element with key `key` does not exist, the behavior is undefined and is **guarded by a 2. If the element with key `key` does not exist, the behavior is undefined and is **guarded by a
[runtime assertion](../../features/assertions.md)**! [runtime assertion](../../features/assertions.md)**!
@@ -75,6 +75,10 @@ Linear in the size of the JSON value.
--8<-- "examples/operator__ValueType.output" --8<-- "examples/operator__ValueType.output"
``` ```
## See also
- [get](get.md) get a value (explicit conversion)
## Version history ## Version history
- Since version 1.0.0. - Since version 1.0.0.
@@ -20,7 +20,7 @@ class basic_json {
``` ```
1. Compares two JSON values for equality according to the following rules: 1. Compares two JSON values for equality according to the following rules:
- Two JSON values are equal if (1) neither value is discarded, or (2) they are of the same type and their stored - Two JSON values are equal if (1) neither value is discarded, and (2) they are of the same type and their stored
values are the same according to their respective `operator==`. values are the same according to their respective `operator==`.
- Integer and floating-point numbers are automatically converted before comparison. - Integer and floating-point numbers are automatically converted before comparison.
@@ -79,13 +79,13 @@ Linear.
} }
``` ```
Or you can self-defined operator equal function like this: Or you can define your own equality function like this:
```cpp ```cpp
bool my_equal(const_reference lhs, const_reference rhs) bool my_equal(const_reference lhs, const_reference rhs)
{ {
const auto lhs_type lhs.type(); const auto lhs_type = lhs.type();
const auto rhs_type rhs.type(); const auto rhs_type = rhs.type();
if (lhs_type == rhs_type) if (lhs_type == rhs_type)
{ {
switch(lhs_type) switch(lhs_type)
@@ -162,6 +162,11 @@ Linear.
--8<-- "examples/operator__equal__nullptr_t.output" --8<-- "examples/operator__equal__nullptr_t.output"
``` ```
## See also
- [operator!=](operator_ne.md) compare for inequality
- [operator<=>](operator_spaceship.md) comparison: 3-way (C++20)
## Version history ## Version history
1. Added in version 1.0.0. Added C++20 member functions in version 3.11.0. 1. Added in version 1.0.0. Added C++20 member functions in version 3.11.0.
@@ -35,7 +35,7 @@ bool operator>=(ScalarType lhs, const const_reference rhs) noexcept; // (2)
## Return value ## Return value
whether `lhs` is less than or equal to `rhs` whether `lhs` is greater than or equal to `rhs`
## Exception safety ## Exception safety
@@ -66,6 +66,7 @@ classDiagram
## See also ## See also
- [`exception`](exception.md) for the base class of all exceptions thrown by the library
- [List of other errors](../../home/exceptions.md#further-exceptions) - [List of other errors](../../home/exceptions.md#further-exceptions)
- [`parse_error`](parse_error.md) for exceptions indicating a parse error - [`parse_error`](parse_error.md) for exceptions indicating a parse error
- [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators - [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators
@@ -67,6 +67,7 @@ classDiagram
## See also ## See also
- [`exception`](exception.md) for the base class of all exceptions thrown by the library
- [List of out-of-range errors](../../home/exceptions.md#out-of-range) - [List of out-of-range errors](../../home/exceptions.md#out-of-range)
- [`parse_error`](parse_error.md) for exceptions indicating a parse error - [`parse_error`](parse_error.md) for exceptions indicating a parse error
- [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators - [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators
+6 -5
View File
@@ -81,9 +81,6 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
- Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token, or - Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token, or
empty input like a null `FILE*` or `char*` pointer. empty input like a null `FILE*` or `char*` pointer.
- Throws [`parse_error.102`](../../home/exceptions.md#jsonexceptionparse_error102) if `to_unicode` fails or surrogate
error.
- Throws [`parse_error.103`](../../home/exceptions.md#jsonexceptionparse_error103) if `to_unicode` fails.
## Complexity ## Complexity
@@ -95,6 +92,9 @@ super-linear complexity.
A UTF-8 byte order mark is silently ignored. A UTF-8 byte order mark is silently ignored.
Invalid Unicode escapes and unpaired surrogates in the input are reported as
[`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) with a detailed message.
## Examples ## Examples
??? example "Parsing from a character array" ??? example "Parsing from a character array"
@@ -183,7 +183,7 @@ A UTF-8 byte order mark is silently ignored.
??? example "Effect of `allow_exceptions` parameter" ??? example "Effect of `allow_exceptions` parameter"
The example below demonstrates the effect of the `allow_exceptions` parameter in the ´parse()` function. The example below demonstrates the effect of the `allow_exceptions` parameter in the `parse()` function.
```cpp ```cpp
--8<-- "examples/parse__allow_exceptions.cpp" --8<-- "examples/parse__allow_exceptions.cpp"
@@ -226,6 +226,7 @@ A UTF-8 byte order mark is silently ignored.
## See also ## See also
- [accept](accept.md) - check if the input is valid JSON - [accept](accept.md) - check if the input is valid JSON
- [sax_parse](sax_parse.md) - parse input using the SAX interface
- [operator>>](../operator_gtgt.md) - deserialize from stream - [operator>>](../operator_gtgt.md) - deserialize from stream
## Version history ## Version history
@@ -234,7 +235,7 @@ A UTF-8 byte order mark is silently ignored.
- Overload for contiguous containers (1) added in version 2.0.3. - Overload for contiguous containers (1) added in version 2.0.3.
- Ignoring comments via `ignore_comments` added in version 3.9.0. - Ignoring comments via `ignore_comments` added in version 3.9.0.
- Changed [runtime assertion](../../features/assertions.md) in case of `FILE*` null pointers to exception in version 3.12.0. - Changed [runtime assertion](../../features/assertions.md) in case of `FILE*` null pointers to exception in version 3.12.0.
- Added `ignore_trailing_commas` in version 3.12.1. - Added `ignore_trailing_commas` in version 3.12.x.
!!! warning "Deprecation" !!! warning "Deprecation"
@@ -75,6 +75,7 @@ or the end of file. This also holds true when reading a byte vector for binary f
## See also ## See also
- [`exception`](exception.md) for the base class of all exceptions thrown by the library
- [List of parse errors](../../home/exceptions.md#parse-errors) - [List of parse errors](../../home/exceptions.md#parse-errors)
- [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators - [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators
- [`type_error`](type_error.md) for exceptions indicating executing a member function with a wrong type - [`type_error`](type_error.md) for exceptions indicating executing a member function with a wrong type
@@ -24,6 +24,11 @@ The parser callback distinguishes the following events:
![Example when certain parse events are triggered](../../images/callback_events.png) ![Example when certain parse events are triggered](../../images/callback_events.png)
## See also
- [parser_callback_t](parser_callback_t.md) callback function type for the parser
- [parse](parse.md) deserialize from a compatible input
## Version history ## Version history
- Added in version 1.0.0. - Added in version 1.0.0.
@@ -68,6 +68,11 @@ the latter case, it is either skipped completely or replaced by an empty discard
--8<-- "examples/parse__string__parser_callback_t.output" --8<-- "examples/parse__string__parser_callback_t.output"
``` ```
## See also
- [parse](parse.md) deserialize from a compatible input
- [parse_event_t](parse_event_t.md) enumeration of parser events
## Version history ## Version history
- Added in version 1.0.0. - Added in version 1.0.0.
+1 -1
View File
@@ -32,7 +32,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
could not be resolved successfully in the current JSON value; example: `"key baz not found"`. could not be resolved successfully in the current JSON value; example: `"key baz not found"`.
- Throws [`out_of_range.405`](../../home/exceptions.md#jsonexceptionout_of_range405) if JSON pointer has no parent - Throws [`out_of_range.405`](../../home/exceptions.md#jsonexceptionout_of_range405) if JSON pointer has no parent
("add", "remove", "move") ("add", "remove", "move")
- Throws [`out_of_range.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was - Throws [`other_error.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was
unsuccessful. unsuccessful.
## Complexity ## Complexity
@@ -1,7 +1,7 @@
# <small>nlohmann::basic_json::</small>patch_inplace # <small>nlohmann::basic_json::</small>patch_inplace
```cpp ```cpp
void patch_inplace(const basic_json& json_patch) const; void patch_inplace(const basic_json& json_patch);
``` ```
[JSON Patch](http://jsonpatch.com) defines a JSON document structure for expressing a sequence of operations to apply to [JSON Patch](http://jsonpatch.com) defines a JSON document structure for expressing a sequence of operations to apply to
@@ -28,7 +28,7 @@ No guarantees, value may be corrupted by an unsuccessful patch operation.
could not be resolved successfully in the current JSON value; example: `"key baz not found"`. could not be resolved successfully in the current JSON value; example: `"key baz not found"`.
- Throws [`out_of_range.405`](../../home/exceptions.md#jsonexceptionout_of_range405) if JSON pointer has no parent - Throws [`out_of_range.405`](../../home/exceptions.md#jsonexceptionout_of_range405) if JSON pointer has no parent
("add", "remove", "move") ("add", "remove", "move")
- Throws [`out_of_range.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was - Throws [`other_error.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was
unsuccessful. unsuccessful.
## Complexity ## Complexity
@@ -62,7 +62,7 @@ function throws an exception.
- [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902) - [RFC 6902 (JSON Patch)](https://tools.ietf.org/html/rfc6902)
- [RFC 6901 (JSON Pointer)](https://tools.ietf.org/html/rfc6901) - [RFC 6901 (JSON Pointer)](https://tools.ietf.org/html/rfc6901)
- [patch](patch.md) applies a JSON Merge Patch - [patch](patch.md) applies a JSON Patch
- [merge_patch](merge_patch.md) applies a JSON Merge Patch - [merge_patch](merge_patch.md) applies a JSON Merge Patch
## Version history ## Version history
+6 -3
View File
@@ -46,9 +46,12 @@ invalidates all iterators and all references.
## Exceptions ## Exceptions
All functions can throw the following exception: 1. Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
- Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than JSON array or null; example: `"cannot use push_back() with number"`
JSON array or null; example: `"cannot use push_back() with number"` 2. Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
JSON object or null; example: `"cannot use push_back() with number"`
3. Throws [`type_error.308`](../../home/exceptions.md#jsonexceptiontype_error308) when called on a type other than
JSON array or null; example: `"cannot use push_back() with number"`
## Complexity ## Complexity
+1 -1
View File
@@ -11,7 +11,7 @@ Returns an iterator to the reverse-beginning; that is, the last element.
## Return value ## Return value
reverse iterator to the first element reverse iterator to the last element
## Exception safety ## Exception safety
+1 -1
View File
@@ -26,7 +26,7 @@ Constant.
??? example ??? example
The following code shows an example for `eend()`. The following code shows an example for `rend()`.
```cpp ```cpp
--8<-- "examples/rend.cpp" --8<-- "examples/rend.cpp"
+9 -6
View File
@@ -43,10 +43,11 @@ The SAX event lister must follow the interface of [`json_sax`](../json_sax/index
iterators. iterators.
`IteratorType` `IteratorType`
: Description : a compatible iterator type for overload (2); a pair of character iterators whose `value_type` is an integral type
with a size of 1, 2, or 4 bytes (interpreted respectively as UTF-8, UTF-16, and UTF-32)
`SAX` `SAX`
: Description : a class fulfilling the SAX event listener interface; see [`json_sax`](../json_sax/index.md)
## Parameters ## Parameters
@@ -89,9 +90,6 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
- Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token, or - Throws [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token, or
empty input like a null `FILE*` or `char*` pointer. empty input like a null `FILE*` or `char*` pointer.
- Throws [`parse_error.102`](../../home/exceptions.md#jsonexceptionparse_error102) if `to_unicode` fails or surrogate
error.
- Throws [`parse_error.103`](../../home/exceptions.md#jsonexceptionparse_error103) if `to_unicode` fails.
## Complexity ## Complexity
@@ -119,11 +117,16 @@ A UTF-8 byte order mark is silently ignored.
--8<-- "examples/sax_parse.output" --8<-- "examples/sax_parse.output"
``` ```
## See also
- [parse](parse.md) - deserialize from a compatible input
- [accept](accept.md) - check if the input is valid JSON
## Version history ## Version history
- Added in version 3.2.0. - Added in version 3.2.0.
- Ignoring comments via `ignore_comments` added in version 3.9.0. - Ignoring comments via `ignore_comments` added in version 3.9.0.
- Added `ignore_trailing_commas` in version 3.12.1. - Added `ignore_trailing_commas` in version 3.12.x.
!!! warning "Deprecation" !!! warning "Deprecation"
@@ -16,6 +16,14 @@ Exchanges the values of two JSON objects.
`j2` (in, out) `j2` (in, out)
: value to be replaced by `j1` : value to be replaced by `j1`
## Exception safety
No-throw guarantee: this function never throws exceptions.
## Complexity
Constant.
## Possible implementation ## Possible implementation
```cpp ```cpp
+1 -1
View File
@@ -9,7 +9,7 @@ The type used to store JSON strings.
[RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows: [RFC 8259](https://tools.ietf.org/html/rfc8259) describes JSON strings as follows:
> A string is a sequence of zero or more Unicode characters. > A string is a sequence of zero or more Unicode characters.
To store objects in C++, a type is defined by the template parameter described below. Unicode values are split by the To store strings in C++, a type is defined by the template parameter described below. Unicode values are split by the
JSON class into byte-sized characters during deserialization. JSON class into byte-sized characters during deserialization.
## Template parameters ## Template parameters
+20 -8
View File
@@ -2,10 +2,20 @@
```cpp ```cpp
// (1) // (1)
void swap(reference other) noexcept; void swap(reference other) noexcept (
std::is_nothrow_move_constructible<value_t>::value &&
std::is_nothrow_move_assignable<value_t>::value &&
std::is_nothrow_move_constructible<json_value>::value &&
std::is_nothrow_move_assignable<json_value>::value
);
// (2) // (2)
void swap(reference left, reference right) noexcept; friend void swap(reference left, reference right) noexcept (
std::is_nothrow_move_constructible<value_t>::value &&
std::is_nothrow_move_assignable<value_t>::value &&
std::is_nothrow_move_constructible<json_value>::value &&
std::is_nothrow_move_assignable<json_value>::value
);
// (3) // (3)
void swap(array_t& other); void swap(array_t& other);
@@ -56,15 +66,15 @@ void swap(typename binary_t::container_type& other);
1. No-throw guarantee: this function never throws exceptions. 1. No-throw guarantee: this function never throws exceptions.
2. No-throw guarantee: this function never throws exceptions. 2. No-throw guarantee: this function never throws exceptions.
3. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than 3. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
arrays; example: `"cannot use swap() with boolean"` arrays; example: `"cannot use swap(array_t&) with boolean"`
4. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than 4. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
objects; example: `"cannot use swap() with boolean"` objects; example: `"cannot use swap(object_t&) with boolean"`
5. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than 5. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
strings; example: `"cannot use swap() with boolean"` strings; example: `"cannot use swap(string_t&) with boolean"`
6. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than 6. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
binaries; example: `"cannot use swap() with boolean"` binaries; example: `"cannot use swap(binary_t&) with boolean"`
7. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than 7. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
binaries; example: `"cannot use swap() with boolean"` binaries; example: `"cannot use swap(binary_t::container_type&) with boolean"`
## Complexity ## Complexity
@@ -128,7 +138,7 @@ Constant.
--8<-- "examples/swap__string_t.output" --8<-- "examples/swap__string_t.output"
``` ```
??? example "Example: Swap string (6)" ??? example "Example: Swap binary (6)"
The example below shows how binary values can be swapped with `swap()`. The example below shows how binary values can be swapped with `swap()`.
@@ -145,6 +155,8 @@ Constant.
## See also ## See also
- [std::swap<basic_json\>](std_swap.md) - [std::swap<basic_json\>](std_swap.md)
- [operator=](operator=.md) copy assignment
- [basic_json](basic_json.md) create a JSON value
## Version history ## Version history
@@ -72,6 +72,14 @@ Linear in the size of the JSON value `j`.
--8<-- "examples/to_bjdata.output" --8<-- "examples/to_bjdata.output"
``` ```
## See also
- [from_bjdata](from_bjdata.md) create a JSON value from an input in BJData format
- [to_cbor](to_cbor.md) create a CBOR serialization of a JSON value
- [to_msgpack](to_msgpack.md) create a MessagePack serialization of a JSON value
- [to_bson](to_bson.md) create a BSON serialization of a JSON value
- [to_ubjson](to_ubjson.md) create a UBJSON serialization of a JSON value
## Version history ## Version history
- Added in version 3.11.0. - Added in version 3.11.0.
@@ -34,6 +34,13 @@ The exact mapping and its limitations are described on a [dedicated page](../../
Strong guarantee: if an exception is thrown, there are no changes in the JSON value. Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
## Exceptions
- Throws [`type_error.317`](../../home/exceptions.md#jsonexceptiontype_error317) if the top-level type of the JSON value
is not an object; example: `"to serialize to BSON, top-level type must be object, but is string"`
- Throws [`out_of_range.409`](../../home/exceptions.md#jsonexceptionout_of_range409) if a key in the JSON object contains
a null byte (code point U+0000); example: `"BSON key cannot contain code point U+0000 (at byte 2)"`
## Complexity ## Complexity
Linear in the size of the JSON value `j`. Linear in the size of the JSON value `j`.
@@ -54,6 +61,14 @@ Linear in the size of the JSON value `j`.
--8<-- "examples/to_bson.output" --8<-- "examples/to_bson.output"
``` ```
## See also
- [from_bson](from_bson.md) create a JSON value from an input in BSON format
- [to_cbor](to_cbor.md) create a CBOR serialization of a JSON value
- [to_msgpack](to_msgpack.md) create a MessagePack serialization of a JSON value
- [to_ubjson](to_ubjson.md) create a UBJSON serialization of a JSON value
- [to_bjdata](to_bjdata.md) create a BJData serialization of a JSON value
## Version history ## Version history
- Added in version 3.4.0. - Added in version 3.4.0.
@@ -55,6 +55,14 @@ Linear in the size of the JSON value `j`.
--8<-- "examples/to_cbor.output" --8<-- "examples/to_cbor.output"
``` ```
## See also
- [from_cbor](from_cbor.md) create a JSON value from an input in CBOR format
- [to_msgpack](to_msgpack.md) create a MessagePack serialization of a JSON value
- [to_bson](to_bson.md) create a BSON serialization of a JSON value
- [to_ubjson](to_ubjson.md) create a UBJSON serialization of a JSON value
- [to_bjdata](to_bjdata.md) create a BJData serialization of a JSON value
## Version history ## Version history
- Added in version 2.0.9. - Added in version 2.0.9.
@@ -54,6 +54,14 @@ Linear in the size of the JSON value `j`.
--8<-- "examples/to_msgpack.output" --8<-- "examples/to_msgpack.output"
``` ```
## See also
- [from_msgpack](from_msgpack.md) create a JSON value from an input in MessagePack format
- [to_cbor](to_cbor.md) create a CBOR serialization of a JSON value
- [to_bson](to_bson.md) create a BSON serialization of a JSON value
- [to_ubjson](to_ubjson.md) create a UBJSON serialization of a JSON value
- [to_bjdata](to_bjdata.md) create a BJData serialization of a JSON value
## Version history ## Version history
- Added in version 2.0.9. - Added in version 2.0.9.
@@ -65,6 +65,14 @@ Linear in the size of the JSON value `j`.
--8<-- "examples/to_ubjson.output" --8<-- "examples/to_ubjson.output"
``` ```
## See also
- [from_ubjson](from_ubjson.md) create a JSON value from an input in UBJSON format
- [to_cbor](to_cbor.md) create a CBOR serialization of a JSON value
- [to_msgpack](to_msgpack.md) create a MessagePack serialization of a JSON value
- [to_bson](to_bson.md) create a BSON serialization of a JSON value
- [to_bjdata](to_bjdata.md) create a BJData serialization of a JSON value
## Version history ## Version history
- Added in version 3.1.0. - Added in version 3.1.0.
@@ -67,6 +67,7 @@ classDiagram
## See also ## See also
- [`exception`](exception.md) for the base class of all exceptions thrown by the library
- [List of type errors](../../home/exceptions.md#type-errors) - [List of type errors](../../home/exceptions.md#type-errors)
- [`parse_error`](parse_error.md) for exceptions indicating a parse error - [`parse_error`](parse_error.md) for exceptions indicating a parse error
- [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators - [`invalid_iterator`](invalid_iterator.md) for exceptions indicating errors with iterators
@@ -25,6 +25,10 @@ The function can throw the following exceptions:
- Throws [`type_error.314`](../../home/exceptions.md#jsonexceptiontype_error314) if value is not an object - Throws [`type_error.314`](../../home/exceptions.md#jsonexceptiontype_error314) if value is not an object
- Throws [`type_error.315`](../../home/exceptions.md#jsonexceptiontype_error315) if object values are not primitive - Throws [`type_error.315`](../../home/exceptions.md#jsonexceptiontype_error315) if object values are not primitive
- Throws [`type_error.313`](../../home/exceptions.md#jsonexceptiontype_error313) if a key (JSON pointer) leads to a
conflicting nesting; example: `"invalid value to unflatten"`
- Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index in a key is not a
number; example: `"array index 'one' is not a number"`
## Complexity ## Complexity
+13 -4
View File
@@ -14,6 +14,8 @@ void update(const_iterator first, const_iterator last, bool merge_objects = fals
When `merge_objects` is `#!c false` (default), existing keys are overwritten. When `merge_objects` is `#!c true`, When `merge_objects` is `#!c false` (default), existing keys are overwritten. When `merge_objects` is `#!c true`,
recursively merges objects with common keys. recursively merges objects with common keys.
If the JSON value is `#!json null`, it is implicitly converted to an empty object before the values are inserted.
The function is motivated by Python's [dict.update](https://docs.python.org/3.6/library/stdtypes.html#dict.update) The function is motivated by Python's [dict.update](https://docs.python.org/3.6/library/stdtypes.html#dict.update)
function. function.
@@ -28,8 +30,8 @@ iterators (including the `end()` iterator) and all references to the elements ar
: JSON object to read values from : JSON object to read values from
`merge_objects` (in) `merge_objects` (in)
: when `#!c true`, existing keys are not overwritten, but contents of objects are merged recursively (default: : when `#!c true`, keys that exist in both objects and whose value in the source is itself an object are merged
`#!c false`) recursively; all other values are overwritten as usual (default: `#!c false`)
`first` (in) `first` (in)
: the beginning of the range of elements to insert : the beginning of the range of elements to insert
@@ -37,6 +39,10 @@ iterators (including the `end()` iterator) and all references to the elements ar
`last` (in) `last` (in)
: the end of the range of elements to insert : the end of the range of elements to insert
## Exception safety
Basic guarantee: if an exception is thrown during the operation, the JSON value may be partially modified.
## Exceptions ## Exceptions
1. The function can throw the following exceptions: 1. The function can throw the following exceptions:
@@ -45,8 +51,6 @@ iterators (including the `end()` iterator) and all references to the elements ar
2. The function can throw the following exceptions: 2. The function can throw the following exceptions:
- Throws [`type_error.312`](../../home/exceptions.md#jsonexceptiontype_error312) if called on JSON values other than - Throws [`type_error.312`](../../home/exceptions.md#jsonexceptiontype_error312) if called on JSON values other than
objects; example: `"cannot use update() with string"` objects; example: `"cannot use update() with string"`
- Throws [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202) if called on an
iterator which does not belong to the current JSON value; example: `"iterator does not fit current value"`
- Throws [`invalid_iterator.210`](../../home/exceptions.md#jsonexceptioninvalid_iterator210) if `first` and `last` - Throws [`invalid_iterator.210`](../../home/exceptions.md#jsonexceptioninvalid_iterator210) if `first` and `last`
do not belong to the same JSON value; example: `"iterators do not fit"` do not belong to the same JSON value; example: `"iterators do not fit"`
@@ -141,6 +145,11 @@ iterators (including the `end()` iterator) and all references to the elements ar
} }
``` ```
## See also
- [insert](insert.md) add values to an array/object
- [merge_patch](merge_patch.md) applies a JSON Merge Patch
## Version history ## Version history
- Added in version 3.0.0. - Added in version 3.0.0.
+1 -1
View File
@@ -95,7 +95,7 @@ changes to any JSON value.
- Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if `default_value` does not match - Throws [`type_error.302`](../../home/exceptions.md#jsonexceptiontype_error302) if `default_value` does not match
the type of the value at `ptr` the type of the value at `ptr`
- Throws [`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) if the JSON value is not an object; - Throws [`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) if the JSON value is not an object;
in that case, using `value()` with a key makes no sense. in that case, using `value()` with a JSON pointer makes no sense.
## Complexity ## Complexity
@@ -24,7 +24,7 @@ specific naming scheme in order to override the binary type.
- [(constructor)](byte_container_with_subtype.md) - [(constructor)](byte_container_with_subtype.md)
- **operator==** - comparison: equal - **operator==** - comparison: equal
- **operator!=** - comparison: not equal - **operator!=** - comparison: not equal
- [**set_subtype**](subtype.md) - sets the binary subtype - [**set_subtype**](set_subtype.md) - sets the binary subtype
- [**subtype**](subtype.md) - return the binary subtype - [**subtype**](subtype.md) - return the binary subtype
- [**has_subtype**](has_subtype.md) - return whether the value has a subtype - [**has_subtype**](has_subtype.md) - return whether the value has a subtype
- [**clear_subtype**](clear_subtype.md) - clears the binary subtype - [**clear_subtype**](clear_subtype.md) - clears the binary subtype
@@ -35,3 +35,5 @@ Constant.
``` ```
## Version history ## Version history
- Added in version 3.12.x.
@@ -10,6 +10,10 @@ Returns the parent of this JSON pointer.
Parent of this JSON pointer; in case this JSON pointer is the root, the root itself is returned. Parent of this JSON pointer; in case this JSON pointer is the root, the root itself is returned.
## Exception safety
No-throw guarantee: this function never throws exceptions.
## Complexity ## Complexity
Linear in the length of the JSON pointer. Linear in the length of the JSON pointer.
@@ -30,6 +34,11 @@ Linear in the length of the JSON pointer.
--8<-- "examples/json_pointer__parent_pointer.output" --8<-- "examples/json_pointer__parent_pointer.output"
``` ```
## See also
- [pop_back](pop_back.md) remove the last reference token
- [back](back.md) return the last reference token
## Version history ## Version history
Added in version 3.6.0. Added in version 3.6.0.
@@ -31,3 +31,5 @@ Linear in the number of reference tokens in the `json_pointer`.
``` ```
## Version history ## Version history
- Added in version 3.12.x.
@@ -34,3 +34,5 @@ Linear in the number of reference tokens in the `json_pointer`.
``` ```
## Version history ## Version history
- Added in version 3.12.x.
+1 -1
View File
@@ -9,7 +9,7 @@ The beginning of an array was read.
## Parameters ## Parameters
`elements` (in) `elements` (in)
: number of object elements or `#!cpp -1` if unknown : number of array elements, or `#!cpp std::numeric_limits<std::size_t>::max()` if unknown
## Return value ## Return value
@@ -9,7 +9,7 @@ The beginning of an object was read.
## Parameters ## Parameters
`elements` (in) `elements` (in)
: number of object elements or `#!cpp -1` if unknown : number of object elements, or `#!cpp std::numeric_limits<std::size_t>::max()` if unknown
## Return value ## Return value
@@ -92,4 +92,4 @@ The default value is `0` (disabled — existing behavior is preserved).
## Version history ## Version history
- Added in version 3.12.0. - Added in version 3.12.x.
@@ -122,14 +122,13 @@ The default value is `0`.
void to_json(json& j, const Choice& ch) void to_json(json& j, const Choice& ch)
{ {
auto value = j.get<std::string>(); if (ch == Choice::first)
if (value == "first")
{ {
ch = Choice::first; j = "first";
} }
else if (value == "second") else if (ch == Choice::second)
{ {
ch = Choice::second; j = "second";
} }
} }
@@ -1,10 +1,12 @@
# JSON_HAS_CPP_11, JSON_HAS_CPP_14, JSON_HAS_CPP_17, JSON_HAS_CPP_20 # JSON_HAS_CPP_11, JSON_HAS_CPP_14, JSON_HAS_CPP_17, JSON_HAS_CPP_20, JSON_HAS_CPP_23, JSON_HAS_CPP_26
```cpp ```cpp
#define JSON_HAS_CPP_11 #define JSON_HAS_CPP_11
#define JSON_HAS_CPP_14 #define JSON_HAS_CPP_14
#define JSON_HAS_CPP_17 #define JSON_HAS_CPP_17
#define JSON_HAS_CPP_20 #define JSON_HAS_CPP_20
#define JSON_HAS_CPP_23
#define JSON_HAS_CPP_26
``` ```
The library targets C++11, but also supports some features introduced in later C++ versions (e.g., `std::string_view` The library targets C++11, but also supports some features introduced in later C++ versions (e.g., `std::string_view`
@@ -20,7 +22,9 @@ The default value is detected based on preprocessor macros such as `#!cpp __cplu
## Notes ## Notes
- `#!cpp JSON_HAS_CPP_11` is always defined. - When the C++ standard is detected automatically, `#!cpp JSON_HAS_CPP_11` is always defined. When you override the
detection by defining one of these macros manually, the automatic detection is skipped entirely, so you should define
all applicable macros (including `#!cpp JSON_HAS_CPP_11`) yourself.
- All macros are undefined outside the library. - All macros are undefined outside the library.
## Examples ## Examples
@@ -39,3 +43,5 @@ The default value is detected based on preprocessor macros such as `#!cpp __cplu
## Version history ## Version history
- Added in version 3.10.5. - Added in version 3.10.5.
- Added `JSON_HAS_CPP_23` in version 3.12.0.
- Added `JSON_HAS_CPP_26` in version 3.12.x.
@@ -21,7 +21,7 @@ The default value is detected based on the preprocessor macros `#!cpp __cpp_lib_
## Notes ## Notes
- Note that older compilers or older versions of libstd++ also require the library `stdc++fs` to be linked to for - Note that older compilers or older versions of libstdc++ also require the library `stdc++fs` to be linked to for
filesystem support. filesystem support.
- Both macros are undefined outside the library. - Both macros are undefined outside the library.
@@ -24,6 +24,17 @@ By default, the macro is not defined.
## Examples ## Examples
??? example
The code below switches off the warning about including a different version of the library.
```cpp
#define JSON_SKIP_LIBRARY_VERSION_CHECK 1
#include <nlohmann/json.hpp>
...
```
!!! example !!! example
The following warning will be shown in case a different version of the library was already included: The following warning will be shown in case a different version of the library was already included:
@@ -56,7 +56,7 @@ When the macro is not defined, the library will define it to its default value.
!!! hint "CMake option" !!! hint "CMake option"
Legacy comparison can also be controlled with the CMake option Legacy comparison can also be controlled with the CMake option
[`JSON_LegacyDiscardedValueComparison`](../../integration/cmake.md#json_implicitconversions) [`JSON_LegacyDiscardedValueComparison`](../../integration/cmake.md#json_legacydiscardedvaluecomparison)
(`OFF` by default) which defines `JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON` accordingly. (`OFF` by default) which defines `JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON` accordingly.
## Examples ## Examples
@@ -72,7 +72,7 @@ When the macro is not defined, the library will define it to its default value.
... ...
``` ```
### See also ## See also
- [:simple-cmake: JSON_LegacyDiscardedValueComparison](../../integration/cmake.md#json_legacydiscardedvaluecomparison) - CMake option to control the macro - [:simple-cmake: JSON_LegacyDiscardedValueComparison](../../integration/cmake.md#json_legacydiscardedvaluecomparison) - CMake option to control the macro
@@ -39,7 +39,7 @@ The base type **must** be already serializable/deserializable.
Summary: Summary:
| Need access to private members | Need only de-serialization | Allow missing values when de-serializing | macro | | Need access to private members | Need only serialization | Allow missing values when de-serializing | macro |
|------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|---------------------------------------------------------------| |------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|---------------------------------------------------------------|
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE** | | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE** |
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT** | | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT** |
@@ -160,10 +160,10 @@ void to_json(BasicJsonType& j, const B& b) {
## See also ## See also
- [NLOHMANN_DEFINE_TYPE_INTRUSIVE / NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT / - [NLOHMANN_DEFINE_TYPE_INTRUSIVE / NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT /
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_type_intrusive.md) NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_type_intrusive.md)
for similar macros that can be defined _inside_ a non-derived type. for similar macros that can be defined _inside_ a non-derived type.
- [NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE / NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT / - [NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE / NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT /
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_type_non_intrusive.md) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE](nlohmann_define_type_non_intrusive.md)
for similar macros that can be defined _outside_ a non-derived type. for similar macros that can be defined _outside_ a non-derived type.
- [Arbitrary Type Conversions](../../features/arbitrary_types.md) for an overview. - [Arbitrary Type Conversions](../../features/arbitrary_types.md) for an overview.
@@ -21,7 +21,7 @@ parameter is the name of the class/struct, and all remaining parameters name the
Summary: Summary:
| Need access to private members | Need only de-serialization | Allow missing values when de-serializing | macro | | Need access to private members | Need only serialization | Allow missing values when de-serializing | macro |
|------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|-------------------------------------------------------| |------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|-------------------------------------------------------|
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_INTRUSIVE** | | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_INTRUSIVE** |
| <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT** | | <div style="color: green;">:octicons-check-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT** |
@@ -21,7 +21,7 @@ parameter is the name of the class/struct, and all remaining parameters name the
Summary: Summary:
| Need access to private members | Need only de-serialization | Allow missing values when de-serializing | macro | | Need access to private members | Need only serialization | Allow missing values when de-serializing | macro |
|------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|-------------------------------------------------------| |------------------------------------------------------------------|------------------------------------------------------------------|------------------------------------------------------------------|-------------------------------------------------------|
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE** | | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE** |
| <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT** | | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: red;">:octicons-x-circle-fill-24:</div> | <div style="color: green;">:octicons-check-circle-fill-24:</div> | **NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT** |
@@ -75,4 +75,4 @@ For further information please refer to the corresponding macros without `WITH_N
## Version history ## Version history
1. Added in version 3.11.x. 1. Added in version 3.12.x.
@@ -21,13 +21,13 @@ The default definitions open and close the `nlohmann` namespace. The precise def
```cpp ```cpp
namespace nlohmann namespace nlohmann
{ {
inline namespace json_abi_v3_11_2 inline namespace json_abi_v3_12_0
{ {
``` ```
2. Default definition of `NLOHMANN_JSON_NAMESPACE_END`: 2. Default definition of `NLOHMANN_JSON_NAMESPACE_END`:
```cpp ```cpp
} // namespace json_abi_v3_11_2 } // namespace json_abi_v3_12_0
} // namespace nlohmann } // namespace nlohmann
``` ```
@@ -38,7 +38,10 @@ inline void from_json(const BasicJsonType& j, type& e);
!!! important "Important notes" !!! important "Important notes"
- When using [`get<ENUM_TYPE>()`](../basic_json/get.md), undefined JSON values will throw an exception. - Undefined input throws [`out_of_range.410`](../../home/exceptions.md#jsonexceptionout_of_range410) in both
directions: when serializing an enum value not listed in the conversions, and when deserializing (e.g., via
[`get<ENUM_TYPE>()`](../basic_json/get.md)) a JSON value that matches no conversion; example:
`"enum value out of range for <type>"`.
- If an enum or JSON value is specified in multiple conversions, the first matching conversion from the top of the - If an enum or JSON value is specified in multiple conversions, the first matching conversion from the top of the
list will be returned when converting to or from JSON. See example 2 below. list will be returned when converting to or from JSON. See example 2 below.
@@ -99,4 +102,4 @@ inline void from_json(const BasicJsonType& j, type& e);
## Version history ## Version history
Added in version 3.12.0. Added in version 3.12.x.
+4 -3
View File
@@ -21,8 +21,6 @@ the stream `i`
## Exceptions ## Exceptions
- Throws [`parse_error.101`](../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token. - Throws [`parse_error.101`](../home/exceptions.md#jsonexceptionparse_error101) in case of an unexpected token.
- Throws [`parse_error.102`](../home/exceptions.md#jsonexceptionparse_error102) if `to_unicode` fails or surrogate error.
- Throws [`parse_error.103`](../home/exceptions.md#jsonexceptionparse_error103) if `to_unicode` fails.
## Complexity ## Complexity
@@ -32,6 +30,9 @@ Linear in the length of the input. The parser is a predictive LL(1) parser.
A UTF-8 byte order mark is silently ignored. A UTF-8 byte order mark is silently ignored.
Invalid Unicode escapes and unpaired surrogates in the input are reported as
[`parse_error.101`](../home/exceptions.md#jsonexceptionparse_error101) with a detailed message.
!!! warning "Deprecation" !!! warning "Deprecation"
This function replaces function `#!cpp std::istream& operator<<(basic_json& j, std::istream& i)` which has This function replaces function `#!cpp std::istream& operator<<(basic_json& j, std::istream& i)` which has
@@ -61,4 +62,4 @@ A UTF-8 byte order mark is silently ignored.
## Version history ## Version history
- Added in version 1.0.0. Deprecated in version 3.0.0. - Added in version 1.0.0.
@@ -60,4 +60,4 @@ Linear.
- Added in version 1.0.0. - Added in version 1.0.0.
- Moved to namespace `nlohmann::literals::json_literals` in 3.11.0. - Moved to namespace `nlohmann::literals::json_literals` in 3.11.0.
- Added `char8_t*` overload in 3.12.1. - Added `char8_t*` overload in 3.12.x.
@@ -63,4 +63,4 @@ Linear.
- Added in version 2.0.0. - Added in version 2.0.0.
- Moved to namespace `nlohmann::literals::json_literals` in 3.11.0. - Moved to namespace `nlohmann::literals::json_literals` in 3.11.0.
- Added `char8_t*` overload in 3.12.1. - Added `char8_t*` overload in 3.12.x.
+1 -1
View File
@@ -27,7 +27,7 @@ For more information, see [JSON With Commas and Comments (JWCC)](https://nigelta
} }
``` ```
When calling `parse` without additional argument, a parse error exception is thrown. If `ignore_comments` is set to `#! true`, the comments are ignored during parsing: When calling `parse` without additional argument, a parse error exception is thrown. If `ignore_comments` is set to `#!cpp true`, the comments are ignored during parsing:
```cpp ```cpp
--8<-- "examples/comments.cpp" --8<-- "examples/comments.cpp"
+41 -2
View File
@@ -9,6 +9,14 @@ This macro controls which code is executed for [runtime assertions](assertions.m
See [full documentation of `JSON_ASSERT(x)`](../api/macros/json_assert.md). See [full documentation of `JSON_ASSERT(x)`](../api/macros/json_assert.md).
## `JSON_BRACE_INIT_COPY_SEMANTICS`
When defined to `1`, single-element brace initialization of a `basic_json` value (e.g., `#!cpp json j{value};`) is
treated as a copy/move of the element rather than wrapping it in a single-element array. The default value is `0`, which
preserves the existing behavior.
See [full documentation of `JSON_BRACE_INIT_COPY_SEMANTICS`](../api/macros/json_brace_init_copy_semantics.md).
## `JSON_CATCH_USER(exception)` ## `JSON_CATCH_USER(exception)`
This macro overrides [`#!cpp catch`](https://en.cppreference.com/w/cpp/language/try_catch) calls inside the library. This macro overrides [`#!cpp catch`](https://en.cppreference.com/w/cpp/language/try_catch) calls inside the library.
@@ -44,7 +52,7 @@ The diagnostics positions can also be controlled with the CMake option
See [full documentation of `JSON_DIAGNOSTIC_POSITIONS`](../api/macros/json_diagnostic_positions.md) See [full documentation of `JSON_DIAGNOSTIC_POSITIONS`](../api/macros/json_diagnostic_positions.md)
## `JSON_HAS_CPP_11`, `JSON_HAS_CPP_14`, `JSON_HAS_CPP_17`, `JSON_HAS_CPP_20` ## `JSON_HAS_CPP_11`, `JSON_HAS_CPP_14`, `JSON_HAS_CPP_17`, `JSON_HAS_CPP_20`, `JSON_HAS_CPP_23`, `JSON_HAS_CPP_26`
The library targets C++11, but also supports some features introduced in later C++ versions (e.g., `std::string_view` The library targets C++11, but also supports some features introduced in later C++ versions (e.g., `std::string_view`
support for C++17). For these new features, the library implements some preprocessor checks to determine the C++ support for C++17). For these new features, the library implements some preprocessor checks to determine the C++
@@ -52,7 +60,7 @@ standard. By defining any of these symbols, the internal check is overridden and
unconditionally assumed. This can be helpful for compilers that only implement parts of the standard and would be unconditionally assumed. This can be helpful for compilers that only implement parts of the standard and would be
detected incorrectly. detected incorrectly.
See [full documentation of `JSON_HAS_CPP_11`, `JSON_HAS_CPP_14`, `JSON_HAS_CPP_17`, and `JSON_HAS_CPP_20`](../api/macros/json_has_cpp_11.md). See [full documentation of `JSON_HAS_CPP_11`, `JSON_HAS_CPP_14`, `JSON_HAS_CPP_17`, `JSON_HAS_CPP_20`, `JSON_HAS_CPP_23`, and `JSON_HAS_CPP_26`](../api/macros/json_has_cpp_11.md).
## `JSON_HAS_FILESYSTEM`, `JSON_HAS_EXPERIMENTAL_FILESYSTEM` ## `JSON_HAS_FILESYSTEM`, `JSON_HAS_EXPERIMENTAL_FILESYSTEM`
@@ -115,11 +123,39 @@ When defined to `0`, implicit conversions are switched off. By default, implicit
See [full documentation of `JSON_USE_IMPLICIT_CONVERSIONS`](../api/macros/json_use_implicit_conversions.md). See [full documentation of `JSON_USE_IMPLICIT_CONVERSIONS`](../api/macros/json_use_implicit_conversions.md).
## `JSON_USE_GLOBAL_UDLS`
When defined to `1` (default), the user-defined string literals `operator""_json` and `operator""_json_pointer` are
placed into the global namespace instead of `nlohmann::literals::json_literals`.
See [full documentation of `JSON_USE_GLOBAL_UDLS`](../api/macros/json_use_global_udls.md).
## `JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON`
When defined to `1`, the library restores the legacy behavior in which a discarded value compared equal to itself. This
behavior is deprecated and switched off (`0`) by default.
See [full documentation of `JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON`](../api/macros/json_use_legacy_discarded_value_comparison.md).
## `NLOHMANN_DEFINE_TYPE_*(...)`, `NLOHMANN_DEFINE_DERIVED_TYPE_*(...)` ## `NLOHMANN_DEFINE_TYPE_*(...)`, `NLOHMANN_DEFINE_DERIVED_TYPE_*(...)`
The library defines 12 macros to simplify the serialization/deserialization of types. See the page on The library defines 12 macros to simplify the serialization/deserialization of types. See the page on
[arbitrary type conversion](arbitrary_types.md#simplify-your-life-with-macros) for a detailed discussion. [arbitrary type conversion](arbitrary_types.md#simplify-your-life-with-macros) for a detailed discussion.
## `NLOHMANN_JSON_NAMESPACE`, `NLOHMANN_JSON_NAMESPACE_BEGIN`, `NLOHMANN_JSON_NAMESPACE_END`, `NLOHMANN_JSON_NAMESPACE_NO_VERSION`
These macros relate to the versioned, inline `nlohmann` namespace:
- `NLOHMANN_JSON_NAMESPACE` evaluates to the full name of the `nlohmann` namespace (including the inline ABI namespace).
- `NLOHMANN_JSON_NAMESPACE_BEGIN` / `NLOHMANN_JSON_NAMESPACE_END` open and close the namespace (for example, to add
specializations).
- `NLOHMANN_JSON_NAMESPACE_NO_VERSION`, when defined to `1`, omits the version component from the inline namespace.
See the [`nlohmann` Namespace](namespace.md) page, and the full documentation of
[`NLOHMANN_JSON_NAMESPACE`](../api/macros/nlohmann_json_namespace.md),
[`NLOHMANN_JSON_NAMESPACE_BEGIN` / `NLOHMANN_JSON_NAMESPACE_END`](../api/macros/nlohmann_json_namespace_begin.md), and
[`NLOHMANN_JSON_NAMESPACE_NO_VERSION`](../api/macros/nlohmann_json_namespace_no_version.md).
## `NLOHMANN_JSON_SERIALIZE_ENUM(type, ...)` ## `NLOHMANN_JSON_SERIALIZE_ENUM(type, ...)`
This macro simplifies the serialization/deserialization of enum types. See This macro simplifies the serialization/deserialization of enum types. See
@@ -127,6 +163,9 @@ This macro simplifies the serialization/deserialization of enum types. See
See [full documentation of `NLOHMANN_JSON_SERIALIZE_ENUM`](../api/macros/nlohmann_json_serialize_enum.md). See [full documentation of `NLOHMANN_JSON_SERIALIZE_ENUM`](../api/macros/nlohmann_json_serialize_enum.md).
A strict variant [`NLOHMANN_JSON_SERIALIZE_ENUM_STRICT`](../api/macros/nlohmann_json_serialize_enum_strict.md) throws an
exception on undefined input instead of falling back to the first mapping.
## `NLOHMANN_JSON_VERSION_MAJOR`, `NLOHMANN_JSON_VERSION_MINOR`, `NLOHMANN_JSON_VERSION_PATCH` ## `NLOHMANN_JSON_VERSION_MAJOR`, `NLOHMANN_JSON_VERSION_MINOR`, `NLOHMANN_JSON_VERSION_PATCH`
These macros are defined by the library and contain the version numbers according to These macros are defined by the library and contain the version numbers according to
+1 -1
View File
@@ -10,7 +10,7 @@ The default type `nlohmann::json` uses a `std::map` to store JSON objects, and t
```cpp ```cpp
#include <iostream> #include <iostream>
#include "json.hpp" #include <nlohmann/json.hpp>
using json = nlohmann::json; using json = nlohmann::json;
@@ -70,7 +70,7 @@ The return value indicates whether the parsing should continue, so the function
```cpp ```cpp
#include <iostream> #include <iostream>
#include "json.hpp" #include <nlohmann/json.hpp>
using json = nlohmann::json; using json = nlohmann::json;
+1 -1
View File
@@ -26,7 +26,7 @@ For more information, see [JSON With Commas and Comments (JWCC)](https://nigelta
} }
``` ```
When calling `parse` without additional argument, a parse error exception is thrown. If `ignore_trailing_commas` is set to `#! true`, the trailing commas are ignored during parsing: When calling `parse` without additional argument, a parse error exception is thrown. If `ignore_trailing_commas` is set to `#!cpp true`, the trailing commas are ignored during parsing:
```cpp ```cpp
--8<-- "examples/trailing_commas.cpp" --8<-- "examples/trailing_commas.cpp"
+4 -1
View File
@@ -870,12 +870,15 @@ Key identifiers to be serialized to BSON cannot contain code point U+0000, since
### json.exception.out_of_range.410 ### json.exception.out_of_range.410
Undefined json fields cannot be used with [`NLOHMANN_JSON_SERIALIZE_ENUM_STRICT`](../api/macros/nlohmann_json_serialize_enum_strict.md) This exception is thrown when an undefined value is used with
[`NLOHMANN_JSON_SERIALIZE_ENUM_STRICT`](../api/macros/nlohmann_json_serialize_enum_strict.md), or when an array index in
a JSON pointer exceeds the range of `size_type` (e.g., on 32-bit platforms).
!!! failure "Example message" !!! failure "Example message"
``` ```
enum value out of range enum value out of range
array index 18446744073709551616 exceeds size_type
``` ```
## Further exceptions ## Further exceptions
File diff suppressed because it is too large. Load diff
+6 -1
View File
@@ -167,7 +167,7 @@ Enable the (incorrect) legacy comparison behavior of discarded JSON values by de
### `JSON_MultipleHeaders` ### `JSON_MultipleHeaders`
Use the non-amalgamated version of the library. This option is `OFF` by default. Use the non-amalgamated version of the library. This option is `ON` by default.
### `JSON_SystemInclude` ### `JSON_SystemInclude`
@@ -176,3 +176,8 @@ Treat the library headers like system headers (i.e., adding `SYSTEM` to the [`ta
### `JSON_Valgrind` ### `JSON_Valgrind`
Execute the test suite with [Valgrind](https://valgrind.org). This option is `OFF` by default. Depends on `JSON_BuildTests`. Execute the test suite with [Valgrind](https://valgrind.org). This option is `OFF` by default. Depends on `JSON_BuildTests`.
### `NLOHMANN_JSON_BUILD_MODULES`
Build the experimental [C++ module](../features/modules.md) `nlohmann.json` (requires CMake 3.28 or later and C++20).
This option is `OFF` by default.
@@ -198,10 +198,6 @@ conversions with calls to [`get`](../api/basic_json/get.md), [`get_to`](../api/b
j.get_to(s); j.get_to(s);
``` ```
You can prepare existing code by already defining
[`JSON_USE_IMPLICIT_CONVERSIONS`](../api/macros/json_use_implicit_conversions.md) to `0` and replace any implicit
conversions with calls to [`get`](../api/basic_json/get.md).
## Import namespace `literals` for UDLs ## Import namespace `literals` for UDLs
The user-defined string literals [`operator""_json`](../api/operator_literal_json.md) and The user-defined string literals [`operator""_json`](../api/operator_literal_json.md) and
@@ -13,7 +13,7 @@
![Nuget](../images/package_managers/nuget.svg){: style="height:1em"}&nbsp;[**NuGet**](#nuget) `nlohmann.json` &emsp; ![Nuget](../images/package_managers/nuget.svg){: style="height:1em"}&nbsp;[**NuGet**](#nuget) `nlohmann.json` &emsp;
![Conda](../images/package_managers/conda.svg){: style="height:1em"}&nbsp;[**Conda**](#conda) `nlohmann_json` &emsp; ![Conda](../images/package_managers/conda.svg){: style="height:1em"}&nbsp;[**Conda**](#conda) `nlohmann_json` &emsp;
![MacPorts](../images/package_managers/macports.svg){: style="height:1em"}&nbsp;[**MacPorts**](#macports) `nlohmann-json`<br> ![MacPorts](../images/package_managers/macports.svg){: style="height:1em"}&nbsp;[**MacPorts**](#macports) `nlohmann-json`<br>
![cpm.cmake](../images/package_managers/CPM.png){: style="height:1em"}&nbsp;[**CPM.cmake**](#cpmcmake) `gh:nlohmann/json` ![cpm.cmake](../images/package_managers/CPM.png){: style="height:1em"}&nbsp;[**CPM.cmake**](#cpmcmake) `gh:nlohmann/json` &emsp;
![xmake](../images/package_managers/xmake.svg){: style="height:1em"}&nbsp;[**xmake**](#xmake) `nlohmann_json` ![xmake](../images/package_managers/xmake.svg){: style="height:1em"}&nbsp;[**xmake**](#xmake) `nlohmann_json`
</p> </p>
@@ -105,7 +105,7 @@ The header can be used directly in your code or via CMake.
- :octicons-tag-24: Available versions: current version and select older versions (see - :octicons-tag-24: Available versions: current version and select older versions (see
[WrapDB](https://mesonbuild.com/Wrapdb-projects.html)) [WrapDB](https://mesonbuild.com/Wrapdb-projects.html))
- :octicons-rocket-24: The package is update automatically from file - :octicons-rocket-24: The package is updated automatically from file
[`meson.build`](https://github.com/nlohmann/json/blob/develop/meson.build). [`meson.build`](https://github.com/nlohmann/json/blob/develop/meson.build).
- :octicons-file-24: File issues at the [library issue tracker](https://github.com/nlohmann/json/issues) - :octicons-file-24: File issues at the [library issue tracker](https://github.com/nlohmann/json/issues)
- :octicons-question-24: [Meson website](https://mesonbuild.com/index.html) - :octicons-question-24: [Meson website](https://mesonbuild.com/index.html)
@@ -197,7 +197,7 @@ repository can be referenced within a `MODULE.bazel` by rules such as `archive_o
- [![ConanCenter package](https://repology.org/badge/version-for-repo/conancenter/nlohmann-json.svg)](https://repology.org/project/nlohmann-json/versions) - [![ConanCenter package](https://repology.org/badge/version-for-repo/conancenter/nlohmann-json.svg)](https://repology.org/project/nlohmann-json/versions)
- :octicons-tag-24: Available versions: current version and older versions (see - :octicons-tag-24: Available versions: current version and older versions (see
[Conan Center](https://conan.io/center/recipes/nlohmann_json)) [Conan Center](https://conan.io/center/recipes/nlohmann_json))
- :octicons-rocket-24: The package is update automatically via - :octicons-rocket-24: The package is updated automatically via
[this recipe](https://github.com/conan-io/conan-center-index/tree/master/recipes/nlohmann_json). [this recipe](https://github.com/conan-io/conan-center-index/tree/master/recipes/nlohmann_json).
- :octicons-file-24: File issues at the [Conan Center issue tracker](https://github.com/conan-io/conan-center-index/issues) - :octicons-file-24: File issues at the [Conan Center issue tracker](https://github.com/conan-io/conan-center-index/issues)
- :octicons-question-24: [Conan website](https://conan.io) - :octicons-question-24: [Conan website](https://conan.io)
@@ -603,7 +603,7 @@ conda install -c conda-forge nlohmann_json
--8<-- "integration/conda/example.cpp" --8<-- "integration/conda/example.cpp"
``` ```
2. Create and activate an anvironment "json`: 2. Create and activate an environment `json`:
```shell ```shell
conda create -n json conda create -n json
@@ -667,7 +667,7 @@ to install the [nlohmann-json](https://ports.macports.org/port/nlohmann-json/) p
sudo port install nlohmann-json sudo port install nlohmann-json
``` ```
3. Compile the code and pass the Homebrew prefix to the include path such that the library can be found: 3. Compile the code and pass the MacPorts prefix to the include path such that the library can be found:
```sh ```sh
c++ example.cpp -I/opt/local/include -std=c++11 -o example c++ example.cpp -I/opt/local/include -std=c++11 -o example
@@ -709,17 +709,17 @@ to install the [nlohmann-json](https://ports.macports.org/port/nlohmann-json/) p
- [package's sources (for advanced users)](https://github.com/build2-packaging/nlohmann-json/) - [package's sources (for advanced users)](https://github.com/build2-packaging/nlohmann-json/)
- :octicons-tag-24: Available versions: current version and older versions since `3.7.3` (see [cppget.org](https://cppget.org/nlohmann-json)) - :octicons-tag-24: Available versions: current version and older versions since `3.7.3` (see [cppget.org](https://cppget.org/nlohmann-json))
- :octicons-rocket-24: The package is maintained and published by the `build2` community in [this repository][(https://github.com/build2-packaging/nlohmann-json/](https://github.com/build2-packaging/nlohmann-json/)). - :octicons-rocket-24: The package is maintained and published by the `build2` community in [this repository](https://github.com/build2-packaging/nlohmann-json/).
- :octicons-file-24: File issues at the [package source repository](https://github.com/build2-packaging/nlohmann-json/issues/) - :octicons-file-24: File issues at the [package source repository](https://github.com/build2-packaging/nlohmann-json/issues/)
- :octicons-question-24: [`build2` website](https://build2) - :octicons-question-24: [`build2` website](https://build2.org)
Note: [`build2`](https://build2.org) should not be considered as a standalone package-manager. It is a build-system + package manager + project manager, a set of tools that work hand-in-hand. `build2`-based projects do not rely on existing `CMake` scripts and the build scripts defining the project's targets are specific to `build2`. Note: [`build2`](https://build2.org) should not be considered as a standalone package-manager. It is a build-system + package manager + project manager, a set of tools that work hand-in-hand. `build2`-based projects do not rely on existing `CMake` scripts and the build scripts defining the project's targets are specific to `build2`.
To use this package in an exising [`build2`](https://build2.org) project, the general steps are: To use this package in an existing [`build2`](https://build2.org) project, the general steps are:
1. <details><summary>Make the package available to download from a package repository that provides it.</summary> 1. <details><summary>Make the package available to download from a package repository that provides it.</summary>
Your project's `repositories.manifest` specifies where the package manager will try to acquire packages by default. Make sure one of the repositories specified in this file provides `nlhomann-json` package. Your project's `repositories.manifest` specifies where the package manager will try to acquire packages by default. Make sure one of the repositories specified in this file provides `nlohmann-json` package.
The recommended open-source repository is [`cppget.org`](https://cppget.org/). The recommended open-source repository is [`cppget.org`](https://cppget.org/).
If the project has been created using [`bdep new`](https://build2.org/bdep/doc/bdep-new.xhtml), `cppget.org` is already specified in `repositories.manifest` but commented, just uncomment these lines: If the project has been created using [`bdep new`](https://build2.org/bdep/doc/bdep-new.xhtml), `cppget.org` is already specified in `repositories.manifest` but commented, just uncomment these lines:
@@ -740,13 +740,13 @@ To use this package in an exising [`build2`](https://build2.org) project, the ge
</details> </details>
2. <details><summary>Add this library as dependency of your target that uses it.</summary> 3. <details><summary>Add this library as dependency of your target that uses it.</summary>
In the `buildfile` defining the target that will use this library: In the `buildfile` defining the target that will use this library:
- import the target `lib{json}` from the `nlhomann-json` package, for example: - import the target `lib{json}` from the `nlohmann-json` package, for example:
``` ```
import nljson = nlhomann-json%lib{json} import nljson = nlohmann-json%lib{json}
``` ```
- then add the library's target as requirement for your target using it, for example: - then add the library's target as requirement for your target using it, for example:
@@ -756,7 +756,7 @@ To use this package in an exising [`build2`](https://build2.org) project, the ge
</details> </details>
3. <details><summary>Use the library in your project's code and build it.</summary> 4. <details><summary>Use the library in your project's code and build it.</summary>
At this point, assuming your project is initialized in a build-configuration, any `b` or `bdep update` command that will update/build the project will also acquire the missing dependency automatically, then build it and link it with your target. At this point, assuming your project is initialized in a build-configuration, any `b` or `bdep update` command that will update/build the project will also acquire the missing dependency automatically, then build it and link it with your target.
@@ -818,7 +818,7 @@ To use this package in an exising [`build2`](https://build2.org) project, the ge
# build only, # build only,
b b
# or build and test the executable's output, will only work if the `tescript` is correct # or build and test the executable's output, will only work if the `testscript` is correct
b test b test
``` ```
+3 -3
View File
@@ -88,9 +88,9 @@ class parser
@param[in] strict whether to expect the last token to be EOF @param[in] strict whether to expect the last token to be EOF
@param[in,out] result parsed JSON value @param[in,out] result parsed JSON value
@throw parse_error.101 in case of an unexpected token @throw parse_error.101 in case of an unexpected token (including invalid
@throw parse_error.102 if to_unicode fails or surrogate error unicode escapes and surrogate errors, which are reported with a
@throw parse_error.103 if to_unicode fails detailed message)
*/ */
void parse(const bool strict, BasicJsonType& result) void parse(const bool strict, BasicJsonType& result)
{ {
+3 -3
View File
@@ -13337,9 +13337,9 @@ class parser
@param[in] strict whether to expect the last token to be EOF @param[in] strict whether to expect the last token to be EOF
@param[in,out] result parsed JSON value @param[in,out] result parsed JSON value
@throw parse_error.101 in case of an unexpected token @throw parse_error.101 in case of an unexpected token (including invalid
@throw parse_error.102 if to_unicode fails or surrogate error unicode escapes and surrogate errors, which are reported with a
@throw parse_error.103 if to_unicode fails detailed message)
*/ */
void parse(const bool strict, BasicJsonType& result) void parse(const bool strict, BasicJsonType& result)
{ {