mirror of
https://github.com/nlohmann/json.git
synced 2026-10-03 05:00:30 +00:00
Add an error_handler parameter for UTF-8 to the binary readers and writers
Adds error_handler_t::keep (invalid UTF-8 sequences are left unchanged) as a fourth error_handler_t value, and threads an error_handler parameter through the binary writers and readers: - to_cbor/to_ubjson/to_bjdata/to_bson gain a trailing error_handler parameter (default strict, matching their existing type_error.316 behavior); keep writes a string value or object key's bytes as is instead of throwing, and replace/ignore sanitize it exactly like dump() would, including for the BSON length prefix. to_msgpack and to_bon8 are unchanged. - from_cbor/from_msgpack/from_ubjson/from_bjdata/from_bson gain a trailing error_handler parameter (default keep, i.e. the lenient behavior every binary reader had in 3.12.0 and still has after #5741); strict checks every string value and object key and raises parse_error.113 for ill-formed UTF-8, honoring allow_exceptions; replace/ignore sanitize it like dump() would. from_bon8 is unchanged, since UTF-8 lead bytes are structural there. dump()'s own keep support writes ill-formed bytes as is, even with ensure_ascii, while still \u-escaping well-formed characters around them as usual. The UTF-8 validity check (is_valid_utf8) and the replace/ignore sanitizing logic (sanitize_utf8) now live in string_utils.hpp, shared by the serializer and the binary reader/writer; error_handler_t itself moved to its own header (detail/output/error_handler.hpp) so that string_utils.hpp does not need to depend on serializer.hpp. See #5529 and #5741. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -25,10 +25,12 @@ and `ensure_ascii` parameters.
|
||||
result consists of ASCII characters only.
|
||||
|
||||
`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 four possible values (see [`error_handler_t`](error_handler_t.md):
|
||||
`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
|
||||
output unchanged, and invalid bytes are dropped)).
|
||||
with U+FFFD), `ignore` (ignore invalid UTF-8 sequences during serialization; all valid bytes are copied to the
|
||||
output unchanged, and invalid bytes are dropped), and `keep` (write the ill-formed bytes to the output as is,
|
||||
without escaping them, even if `ensure_ascii` is `#!cpp true`; the result is then not valid UTF-8, but equals the
|
||||
input bytes exactly, and well-formed characters around the ill-formed bytes are still escaped as usual)).
|
||||
|
||||
## Return value
|
||||
|
||||
@@ -94,3 +96,4 @@ Binary values are serialized as an object containing two keys:
|
||||
- Indentation character `indent_char`, option `ensure_ascii` and exceptions added in version 3.0.0.
|
||||
- Error handlers added in version 3.4.0.
|
||||
- Serialization of binary values added in version 3.8.0.
|
||||
- Error handler `keep` added in version 3.13.0.
|
||||
|
||||
@@ -4,15 +4,30 @@
|
||||
enum class error_handler_t {
|
||||
strict,
|
||||
replace,
|
||||
ignore
|
||||
ignore,
|
||||
keep
|
||||
};
|
||||
```
|
||||
|
||||
This enumeration is used in the [`dump`](dump.md) function to choose how to treat decoding errors while serializing a
|
||||
`basic_json` value. Three values are differentiated:
|
||||
This enumeration is used to choose how to treat ill-formed UTF-8 in a string value or object key:
|
||||
|
||||
- [`dump`](dump.md) uses it while serializing a `basic_json` value to text.
|
||||
- [`to_cbor`](to_cbor.md), [`to_ubjson`](to_ubjson.md), [`to_bjdata`](to_bjdata.md), and [`to_bson`](to_bson.md) use it
|
||||
while serializing a `basic_json` value to that binary format; none of CBOR, UBJSON, BJData, or BSON requires a
|
||||
decoder to reject ill-formed UTF-8, so by default (`strict`) the library checks on write instead. `to_msgpack` and
|
||||
`to_bon8` do not take this parameter: MessagePack's specification explicitly allows a string to contain ill-formed
|
||||
UTF-8, so `to_msgpack` always passes it through, while BON8 always validates, since UTF-8 lead bytes are structural
|
||||
to that format.
|
||||
- [`from_cbor`](from_cbor.md), [`from_msgpack`](from_msgpack.md), [`from_ubjson`](from_ubjson.md),
|
||||
[`from_bjdata`](from_bjdata.md), and [`from_bson`](from_bson.md) use it while parsing that binary format, to decide
|
||||
whether to check a string value or object key for well-formed UTF-8 at all; by default (`keep`) they do not, as no
|
||||
binary reader did before this parameter was added. `from_bon8` does not take this parameter, for the same reason
|
||||
`to_bon8` does not.
|
||||
|
||||
Four values are differentiated:
|
||||
|
||||
strict
|
||||
: throw a `type_error` exception in case of invalid UTF-8
|
||||
: throw a `type_error`/`parse_error` exception in case of invalid UTF-8
|
||||
|
||||
replace
|
||||
: replace invalid UTF-8 sequences with U+FFFD (� REPLACEMENT CHARACTER)
|
||||
@@ -20,6 +35,12 @@ replace
|
||||
ignore
|
||||
: ignore invalid UTF-8 sequences; all valid bytes are copied to the output unchanged, and invalid bytes are dropped
|
||||
|
||||
keep
|
||||
: keep invalid UTF-8 sequences unchanged; only meaningful for the binary formats mentioned above, since [`dump`]
|
||||
(dump.md) itself must produce text, and `keep` there writes the ill-formed bytes to the output as is, so the
|
||||
result is then not valid UTF-8 (but still equals the input bytes exactly, including around any well-formed
|
||||
characters, which are still escaped as usual)
|
||||
|
||||
## Examples
|
||||
|
||||
??? example
|
||||
@@ -40,3 +61,5 @@ ignore
|
||||
## Version history
|
||||
|
||||
- Added in version 3.4.0.
|
||||
- Added `keep`, and made this enumeration apply to the binary readers and writers in addition to `dump`, in version
|
||||
3.13.0.
|
||||
|
||||
@@ -5,12 +5,14 @@
|
||||
template<typename InputType>
|
||||
static basic_json from_bjdata(InputType&& i,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true);
|
||||
const bool allow_exceptions = true,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
// (2)
|
||||
template<typename IteratorType, typename SentinelType = IteratorType>
|
||||
static basic_json from_bjdata(IteratorType first, SentinelType last,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true);
|
||||
const bool allow_exceptions = true,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
```
|
||||
|
||||
Deserializes a given input to a JSON value using the BJData (Binary JData) serialization format.
|
||||
@@ -58,6 +60,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
`allow_exceptions` (in)
|
||||
: whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default)
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string value or object key that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
BJData does not require a decoder to reject ill-formed UTF-8, so checking is opt-in: the default, `keep`, does not
|
||||
check at all, as every binary reader did before this parameter was added; `strict` checks and throws;
|
||||
`replace`/`ignore` sanitize the string the same way [`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be
|
||||
@@ -73,7 +81,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
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.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
|
||||
successfully
|
||||
successfully, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict`
|
||||
- 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`
|
||||
|
||||
@@ -111,3 +119,4 @@ Linear in the size of the input.
|
||||
- Added in version 3.11.0.
|
||||
- Extended container support (1) to include types with lvalue-only ADL `begin`/`end` (matching `std::begin`/`std::end` semantics) in version 3.13.0.
|
||||
- Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
|
||||
@@ -5,12 +5,14 @@
|
||||
template<typename InputType>
|
||||
static basic_json from_bson(InputType&& i,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true);
|
||||
const bool allow_exceptions = true,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
// (2)
|
||||
template<typename IteratorType, typename SentinelType = IteratorType>
|
||||
static basic_json from_bson(IteratorType first, SentinelType last,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true);
|
||||
const bool allow_exceptions = true,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
```
|
||||
|
||||
Deserializes a given input to a JSON value using the BSON (Binary JSON) serialization format.
|
||||
@@ -58,6 +60,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
`allow_exceptions` (in)
|
||||
: whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default)
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string value or object key that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
BSON does not require a decoder to reject ill-formed UTF-8, so checking is opt-in: the default, `keep`, does not
|
||||
check at all, as every binary reader did before this parameter was added; `strict` checks and throws;
|
||||
`replace`/`ignore` sanitize the string the same way [`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be
|
||||
@@ -75,6 +83,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
invalid string or byte array length)
|
||||
- Throws [`parse_error.114`](../../home/exceptions.md#jsonexceptionparse_error114) if an unsupported BSON record type is
|
||||
encountered
|
||||
- Throws [`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) if a string value or object key is
|
||||
not valid UTF-8 and `error_handler` is `strict`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -111,6 +121,7 @@ Linear in the size of the input.
|
||||
- Added in version 3.4.0.
|
||||
- Extended container support (1) to include types with lvalue-only ADL `begin`/`end` (matching `std::begin`/`std::end` semantics) in version 3.13.0.
|
||||
- Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
|
||||
!!! warning "Deprecation"
|
||||
|
||||
|
||||
@@ -6,14 +6,16 @@ template<typename InputType>
|
||||
static basic_json from_cbor(InputType&& i,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true,
|
||||
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error);
|
||||
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
|
||||
// (2)
|
||||
template<typename IteratorType, typename SentinelType = IteratorType>
|
||||
static basic_json from_cbor(IteratorType first, SentinelType last,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true,
|
||||
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error);
|
||||
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
```
|
||||
|
||||
Deserializes a given input to a JSON value using the CBOR (Concise Binary Object Representation) serialization format.
|
||||
@@ -65,6 +67,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
: how to treat CBOR tags (optional, `error` by default); see [`cbor_tag_handler_t`](cbor_tag_handler_t.md) for more
|
||||
information
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string value or object key that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
CBOR does not require a decoder to reject ill-formed UTF-8, so checking is opt-in: the default, `keep`, does not
|
||||
check at all, as every binary reader did before this parameter was added; `strict` checks and throws;
|
||||
`replace`/`ignore` sanitize the string the same way [`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be
|
||||
@@ -80,8 +88,9 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
the end of the file was not reached when `strict` was set to true
|
||||
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if unsupported features from CBOR were
|
||||
used in the given input or if the input is not valid CBOR
|
||||
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a map key is not a string (keys of other
|
||||
types are not supported, as JSON object keys are always strings) or a string is malformed
|
||||
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a map key is not a string (keys of
|
||||
other types are not supported, as JSON object keys are always strings), or if a string value or object key is not
|
||||
valid UTF-8 and `error_handler` is `strict`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -121,6 +130,7 @@ Linear in the size of the input.
|
||||
- Added `tag_handler` parameter in version 3.9.0.
|
||||
- Extended container support (1) to include types with lvalue-only ADL `begin`/`end` (matching `std::begin`/`std::end` semantics) in version 3.13.0.
|
||||
- Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
|
||||
!!! warning "Deprecation"
|
||||
|
||||
|
||||
@@ -5,12 +5,14 @@
|
||||
template<typename InputType>
|
||||
static basic_json from_msgpack(InputType&& i,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true);
|
||||
const bool allow_exceptions = true,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
// (2)
|
||||
template<typename IteratorType, typename SentinelType = IteratorType>
|
||||
static basic_json from_msgpack(IteratorType first, SentinelType last,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true);
|
||||
const bool allow_exceptions = true,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
```
|
||||
|
||||
Deserializes a given input to a JSON value using the MessagePack serialization format.
|
||||
@@ -58,6 +60,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
`allow_exceptions` (in)
|
||||
: whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default)
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string value or object key that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
MessagePack's specification explicitly allows ill-formed UTF-8, so checking is opt-in: the default, `keep`, does
|
||||
not check at all, as every binary reader did before this parameter was added; `strict` checks and throws;
|
||||
`replace`/`ignore` sanitize the string the same way [`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be
|
||||
@@ -73,8 +81,9 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
the end of the file was not reached when `strict` was set to true
|
||||
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if unsupported features from
|
||||
MessagePack were used in the given input or if the input is not valid MessagePack
|
||||
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a map key is not a string (keys of other
|
||||
types are not supported, as JSON object keys are always strings) or a string is malformed
|
||||
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a map key is not a string (keys of
|
||||
other types are not supported, as JSON object keys are always strings), or if a string value or object key is not
|
||||
valid UTF-8 and `error_handler` is `strict`
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -113,6 +122,7 @@ Linear in the size of the input.
|
||||
- Added `allow_exceptions` parameter in version 3.2.0.
|
||||
- Extended container support (1) to include types with lvalue-only ADL `begin`/`end` (matching `std::begin`/`std::end` semantics) in version 3.13.0.
|
||||
- Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
|
||||
!!! warning "Deprecation"
|
||||
|
||||
|
||||
@@ -5,12 +5,14 @@
|
||||
template<typename InputType>
|
||||
static basic_json from_ubjson(InputType&& i,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true);
|
||||
const bool allow_exceptions = true,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
// (2)
|
||||
template<typename IteratorType, typename SentinelType = IteratorType>
|
||||
static basic_json from_ubjson(IteratorType first, SentinelType last,
|
||||
const bool strict = true,
|
||||
const bool allow_exceptions = true);
|
||||
const bool allow_exceptions = true,
|
||||
const error_handler_t error_handler = error_handler_t::keep);
|
||||
```
|
||||
|
||||
Deserializes a given input to a JSON value using the UBJSON (Universal Binary JSON) serialization format.
|
||||
@@ -58,6 +60,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
`allow_exceptions` (in)
|
||||
: whether to throw exceptions in case of a parse error (optional, `#!cpp true` by default)
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string value or object key that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
UBJSON does not require a decoder to reject ill-formed UTF-8, so checking is opt-in: the default, `keep`, does not
|
||||
check at all, as every binary reader did before this parameter was added; `strict` checks and throws;
|
||||
`replace`/`ignore` sanitize the string the same way [`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
deserialized JSON value; in case of a parse error and `allow_exceptions` set to `#!cpp false`, the return value will be
|
||||
@@ -73,7 +81,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
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.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
|
||||
successfully
|
||||
successfully, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict`
|
||||
- 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`
|
||||
|
||||
@@ -112,6 +120,7 @@ Linear in the size of the input.
|
||||
- Added `allow_exceptions` parameter in version 3.2.0.
|
||||
- Extended container support (1) to include types with lvalue-only ADL `begin`/`end` (matching `std::begin`/`std::end` semantics) in version 3.13.0.
|
||||
- Extended overload (2) to accept heterogeneous iterator+sentinel pairs (C++20 ranges support) in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
|
||||
!!! warning "Deprecation"
|
||||
|
||||
|
||||
@@ -5,15 +5,18 @@
|
||||
static std::vector<std::uint8_t> to_bjdata(const basic_json& j,
|
||||
const bool use_size = false,
|
||||
const bool use_type = false,
|
||||
const bjdata_version_t version = bjdata_version_t::draft2);
|
||||
const bjdata_version_t version = bjdata_version_t::draft2,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
|
||||
// (2)
|
||||
static void to_bjdata(const basic_json& j, detail::output_adapter<std::uint8_t> o,
|
||||
const bool use_size = false, const bool use_type = false,
|
||||
const bjdata_version_t version = bjdata_version_t::draft2);
|
||||
const bjdata_version_t version = bjdata_version_t::draft2,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
static void to_bjdata(const basic_json& j, detail::output_adapter<char> o,
|
||||
const bool use_size = false, const bool use_type = false,
|
||||
const bjdata_version_t version = bjdata_version_t::draft2);
|
||||
const bjdata_version_t version = bjdata_version_t::draft2,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
```
|
||||
|
||||
Serializes a given JSON value `j` to a byte vector using the BJData (Binary JData) serialization format. BJData aims to
|
||||
@@ -43,6 +46,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
: which version of BJData to use (see note on "Binary values" on [BJData](../../features/binary_formats/bjdata.md));
|
||||
optional, `#!cpp bjdata_version_t::draft2` by default.
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string or object key in `j` that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
The default, `strict`, throws; `keep` writes the ill-formed bytes to the output as is, as every version of
|
||||
`to_bjdata` did before this parameter was added; `replace`/`ignore` sanitize it the same way
|
||||
[`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
1. BJData serialization as byte vector
|
||||
@@ -57,7 +66,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [`other_error.502`](../../home/exceptions.md#jsonexceptionother_error502) if `use_type` is true and `use_size`
|
||||
is false.
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||
not valid UTF-8
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default)
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -92,4 +101,5 @@ Linear in the size of the JSON value `j`.
|
||||
|
||||
- Added in version 3.11.0.
|
||||
- BJData version parameter (for draft3 binary encoding) added in version 3.12.0.
|
||||
- Throwing `type_error.316` for a string or object key that is not valid UTF-8 added in version 3.13.0.
|
||||
- Throwing `type_error.316` for a string or object key that is not valid UTF-8 added in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
@@ -2,11 +2,14 @@
|
||||
|
||||
```cpp
|
||||
// (1)
|
||||
static std::vector<std::uint8_t> to_bson(const basic_json& j);
|
||||
static std::vector<std::uint8_t> to_bson(const basic_json& j,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
|
||||
// (2)
|
||||
static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o);
|
||||
static void to_bson(const basic_json& j, detail::output_adapter<char> o);
|
||||
static void to_bson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
static void to_bson(const basic_json& j, detail::output_adapter<char> o,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
```
|
||||
|
||||
BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a
|
||||
@@ -25,6 +28,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
`o` (in)
|
||||
: output adapter to write serialization to
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string or object key in `j` that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
The default, `strict`, throws; `keep` writes the ill-formed bytes to the output as is, as every version of
|
||||
`to_bson` did before this parameter was added; `replace`/`ignore` sanitize it the same way
|
||||
[`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
1. BSON serialization as a byte vector
|
||||
@@ -47,7 +56,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
exceeds 255, the maximum of the BSON binary subtype; example:
|
||||
`"subtype 70000 is too large for the BSON binary subtype (max 255)"`
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key is
|
||||
not valid UTF-8
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default)
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -86,3 +95,4 @@ pass before anything is written.
|
||||
- `out_of_range.415` is now detected before anything is written, like the other exceptions above, since version 3.13.0.
|
||||
- Throwing `type_error.316` for a string value or object key that is not valid UTF-8, detected before anything is
|
||||
written, added in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
|
||||
@@ -2,11 +2,14 @@
|
||||
|
||||
```cpp
|
||||
// (1)
|
||||
static std::vector<std::uint8_t> to_cbor(const basic_json& j);
|
||||
static std::vector<std::uint8_t> to_cbor(const basic_json& j,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
|
||||
// (2)
|
||||
static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o);
|
||||
static void to_cbor(const basic_json& j, detail::output_adapter<char> o);
|
||||
static void to_cbor(const basic_json& j, detail::output_adapter<std::uint8_t> o,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
static void to_cbor(const basic_json& j, detail::output_adapter<char> o,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
```
|
||||
|
||||
Serializes a given JSON value `j` to a byte vector using the CBOR (Concise Binary Object Representation) serialization
|
||||
@@ -26,6 +29,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
`o` (in)
|
||||
: output adapter to write serialization to
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string or object key in `j` that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
The default, `strict`, throws; `keep` writes the ill-formed bytes to the output as is, as every version of
|
||||
`to_cbor` did before this parameter was added; `replace`/`ignore` sanitize it the same way
|
||||
[`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
1. CBOR serialization as a byte vector
|
||||
@@ -38,7 +47,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
## Exceptions
|
||||
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||
not valid UTF-8
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default)
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -74,3 +83,4 @@ Linear in the size of the JSON value `j`.
|
||||
- Added in version 2.0.9.
|
||||
- Compact representation of floating-point numbers added in version 3.8.0.
|
||||
- Throwing `type_error.316` for a string or object key that is not valid UTF-8 added in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
|
||||
@@ -4,13 +4,16 @@
|
||||
// (1)
|
||||
static std::vector<std::uint8_t> to_ubjson(const basic_json& j,
|
||||
const bool use_size = false,
|
||||
const bool use_type = false);
|
||||
const bool use_type = false,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
|
||||
// (2)
|
||||
static void to_ubjson(const basic_json& j, detail::output_adapter<std::uint8_t> o,
|
||||
const bool use_size = false, const bool use_type = false);
|
||||
const bool use_size = false, const bool use_type = false,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
static void to_ubjson(const basic_json& j, detail::output_adapter<char> o,
|
||||
const bool use_size = false, const bool use_type = false);
|
||||
const bool use_size = false, const bool use_type = false,
|
||||
const error_handler_t error_handler = error_handler_t::strict);
|
||||
```
|
||||
|
||||
Serializes a given JSON value `j` to a byte vector using the UBJSON (Universal Binary JSON) serialization format. UBJSON
|
||||
@@ -36,6 +39,12 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
||||
: whether to add type annotations to container types (must be combined with `#!cpp use_size = true`); optional,
|
||||
`#!cpp false` by default.
|
||||
|
||||
`error_handler` (in)
|
||||
: how to treat a string or object key in `j` that is not valid UTF-8; see [`error_handler_t`](error_handler_t.md).
|
||||
The default, `strict`, throws; `keep` writes the ill-formed bytes to the output as is, as every version of
|
||||
`to_ubjson` did before this parameter was added; `replace`/`ignore` sanitize it the same way
|
||||
[`dump`](dump.md) would
|
||||
|
||||
## Return value
|
||||
|
||||
1. UBJSON serialization as a byte vector
|
||||
@@ -50,7 +59,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
- Throws [`other_error.502`](../../home/exceptions.md#jsonexceptionother_error502) if `use_type` is true and `use_size`
|
||||
is false.
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||
not valid UTF-8
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default)
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -85,3 +94,4 @@ Linear in the size of the JSON value `j`.
|
||||
|
||||
- Added in version 3.1.0.
|
||||
- Throwing `type_error.316` for a string or object key that is not valid UTF-8 added in version 3.13.0.
|
||||
- Added `error_handler` parameter in version 3.13.0.
|
||||
|
||||
@@ -20,5 +20,6 @@ int main()
|
||||
<< j_invalid.dump(-1, ' ', false, json::error_handler_t::replace)
|
||||
<< "\nstring with ignored invalid characters: "
|
||||
<< j_invalid.dump(-1, ' ', false, json::error_handler_t::ignore)
|
||||
<< '\n';
|
||||
<< "\nstring with the invalid byte kept as is (" << j_invalid.dump(-1, ' ', false, json::error_handler_t::keep).size()
|
||||
<< " bytes, not valid UTF-8 itself)\n";
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9
|
||||
string with replaced invalid characters: "ä�ü"
|
||||
string with ignored invalid characters: "äü"
|
||||
string with the invalid byte kept as is (7 bytes, not valid UTF-8 itself)
|
||||
|
||||
@@ -216,12 +216,17 @@ The library maps BJData types to JSON value types as follows:
|
||||
|
||||
!!! warning "Ill-formed UTF-8 in string values and object keys"
|
||||
|
||||
BJData strings must use UTF-8 encoding, but this is not enforced on read: `from_bjdata()` accepts a string
|
||||
value or object key whose bytes are not valid UTF-8 and hands them back unchanged. However,
|
||||
BJData strings must use UTF-8 encoding, but checking it on read is opt-in: with the
|
||||
[`error_handler`](../../api/basic_json/from_bjdata.md) parameter left at `keep` (the default), `from_bjdata()`
|
||||
accepts a string value or object key whose bytes are not valid UTF-8 and hands them back unchanged. Passing
|
||||
`error_handler_t::strict` makes `from_bjdata()` check and throw
|
||||
[`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) for ill-formed UTF-8, and
|
||||
`replace`/`ignore` sanitize the string instead of keeping it. However,
|
||||
[`dump()`](../../api/basic_json/dump.md) still requires valid UTF-8 and throws
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) for such a value, unless an error
|
||||
handler is passed that replaces or ignores the ill-formed bytes. `to_bjdata()` is strict as well (see above), so
|
||||
a value read this way cannot be written back to BJData.
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) for a value read with the default
|
||||
`keep` handler, unless an error handler is passed that replaces or ignores the ill-formed bytes. `to_bjdata()`'s
|
||||
own `error_handler` parameter defaults to `strict` (see above), so a value read this way cannot be written back
|
||||
to BJData unless a non-strict handler is passed there too.
|
||||
|
||||
!!! info "Round trips"
|
||||
|
||||
|
||||
@@ -112,14 +112,19 @@ The library maps BSON record types to JSON value types as follows:
|
||||
!!! warning "Ill-formed UTF-8 in string values"
|
||||
|
||||
The BSON specification requires `string` values (type `0x02`) to be valid UTF-8, but this is not required of a
|
||||
decoder. `from_bson()` accepts a `string` value whose bytes are not valid UTF-8 and hands them back unchanged.
|
||||
However, [`dump()`](../../api/basic_json/dump.md) still requires valid UTF-8 and throws
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) for such a value, unless an error
|
||||
handler is passed that replaces or ignores the ill-formed bytes. `to_bson()` is strict as well and throws the
|
||||
same exception for a string value or element (key) name that is not valid UTF-8, so an object with such a key
|
||||
or value cannot be produced in the first place, even though `from_bson()` would accept it from another source.
|
||||
Element (key) names are never validated on read, since they are read byte-by-byte as a C string. `binary`
|
||||
values (type `0x05`) are unaffected, since they are not required to hold text.
|
||||
decoder, so checking is opt-in: with the [`error_handler`](../../api/basic_json/from_bson.md) parameter left at
|
||||
`keep` (the default), `from_bson()` accepts a `string` value whose bytes are not valid UTF-8 and hands them back
|
||||
unchanged. Passing `error_handler_t::strict` makes `from_bson()` check and throw
|
||||
[`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) for ill-formed UTF-8, and
|
||||
`replace`/`ignore` sanitize the string instead of keeping it. However, [`dump()`](../../api/basic_json/dump.md)
|
||||
still requires valid UTF-8 and throws [`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316)
|
||||
for a value read with the default `keep` handler, unless an error handler is passed that replaces or ignores
|
||||
the ill-formed bytes. `to_bson()`'s own `error_handler` parameter defaults to `strict` and throws the same
|
||||
exception for a string value or element (key) name that is not valid UTF-8, so an object with such a key or
|
||||
value cannot be produced in the first place unless a non-strict handler is passed there, even though
|
||||
`from_bson()` would accept it from another source with the default `keep` handler. Element (key) names are
|
||||
never validated on read, since they are read byte-by-byte as a C string. `binary` values (type `0x05`) are
|
||||
unaffected, since they are not required to hold text.
|
||||
|
||||
??? example
|
||||
|
||||
|
||||
@@ -192,13 +192,18 @@ The library maps CBOR types to JSON value types as follows:
|
||||
!!! warning "Ill-formed UTF-8 in text strings"
|
||||
|
||||
[RFC 8949, Section 3.1](https://www.rfc-editor.org/rfc/rfc8949.html#section-3.1) requires CBOR text strings
|
||||
(major type 3) to be valid UTF-8, but leaves it up to the decoder whether to enforce this. This library does
|
||||
not: `from_cbor()` accepts a text string (object keys included) whose bytes are not valid UTF-8 and hands them
|
||||
back unchanged. However, [`dump()`](../../api/basic_json/dump.md) still requires valid UTF-8 and throws
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) for such a value, unless an error
|
||||
handler is passed that replaces or ignores the ill-formed bytes. `to_cbor()` is strict as well and throws the
|
||||
same exception for a string value or object key that is not valid UTF-8, so such a value cannot be written back
|
||||
to CBOR. Byte strings (major type 2) are unaffected, since they are not required to hold text.
|
||||
(major type 3) to be valid UTF-8, but leaves it up to the decoder whether to enforce this, so checking is
|
||||
opt-in: with the [`error_handler`](../../api/basic_json/from_cbor.md) parameter left at `keep` (the default),
|
||||
`from_cbor()` accepts a text string (object keys included) whose bytes are not valid UTF-8 and hands them back
|
||||
unchanged. Passing `error_handler_t::strict` makes `from_cbor()` check and throw
|
||||
[`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) for ill-formed UTF-8, and
|
||||
`replace`/`ignore` sanitize the string instead of keeping it. However, [`dump()`](../../api/basic_json/dump.md)
|
||||
still requires valid UTF-8 and throws [`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316)
|
||||
for a value read with the default `keep` handler, unless an error handler is passed that replaces or ignores
|
||||
the ill-formed bytes. `to_cbor()`'s own [`error_handler`](../../api/basic_json/to_cbor.md) parameter defaults
|
||||
to `strict` and throws the same exception for a string value or object key that is not valid UTF-8, so such a
|
||||
value cannot be written back to CBOR unless a non-strict handler is passed there too. Byte strings (major
|
||||
type 2) are unaffected, since they are not required to hold text.
|
||||
|
||||
!!! warning "Tagged items"
|
||||
|
||||
|
||||
@@ -157,11 +157,17 @@ The library maps MessagePack types to JSON value types as follows:
|
||||
|
||||
The MessagePack specification explicitly allows a `str` value (`fixstr`, `str 8`, `str 16`, `str 32`) to contain
|
||||
a byte sequence that is not valid UTF-8, and expects a deserializer to hand the original bytes back unchanged.
|
||||
This library follows that: `from_msgpack()` reads `str` bytes (object keys included) as-is, without validating
|
||||
them, and `to_msgpack()` writes them back as-is, so such a value round-trips through `from_msgpack(to_msgpack(j))`
|
||||
byte for byte. However, [`dump()`](../../api/basic_json/dump.md) still requires valid UTF-8 and throws
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) for a value read this way, unless an
|
||||
error handler is passed that replaces or ignores the ill-formed bytes.
|
||||
This library follows that by default: with its
|
||||
[`error_handler`](../../api/basic_json/from_msgpack.md) parameter left at `keep` (the default),
|
||||
`from_msgpack()` reads `str` bytes (object keys included) as-is, without validating them, so such a value
|
||||
round-trips through `from_msgpack(to_msgpack(j))` byte for byte. Passing `error_handler_t::strict` makes
|
||||
`from_msgpack()` check anyway and throw
|
||||
[`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) for ill-formed UTF-8, and
|
||||
`replace`/`ignore` sanitize the string instead of keeping it. `to_msgpack()` itself has no `error_handler`
|
||||
parameter and always writes `str` bytes as-is, since the specification permits it. However,
|
||||
[`dump()`](../../api/basic_json/dump.md) still requires valid UTF-8 and throws
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) for a value read this way with the
|
||||
default `keep` handler, unless an error handler is passed that replaces or ignores the ill-formed bytes.
|
||||
|
||||
??? example
|
||||
|
||||
|
||||
@@ -128,12 +128,17 @@ The library maps UBJSON types to JSON value types as follows:
|
||||
|
||||
!!! warning "Ill-formed UTF-8 in string values and object keys"
|
||||
|
||||
UBJSON's required string encoding is UTF-8, but this is not enforced on read: `from_ubjson()` accepts a string
|
||||
value or object key whose bytes are not valid UTF-8 and hands them back unchanged. However,
|
||||
UBJSON's required string encoding is UTF-8, but checking it on read is opt-in: with the
|
||||
[`error_handler`](../../api/basic_json/from_ubjson.md) parameter left at `keep` (the default), `from_ubjson()`
|
||||
accepts a string value or object key whose bytes are not valid UTF-8 and hands them back unchanged. Passing
|
||||
`error_handler_t::strict` makes `from_ubjson()` check and throw
|
||||
[`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) for ill-formed UTF-8, and
|
||||
`replace`/`ignore` sanitize the string instead of keeping it. However,
|
||||
[`dump()`](../../api/basic_json/dump.md) still requires valid UTF-8 and throws
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) for such a value, unless an error
|
||||
handler is passed that replaces or ignores the ill-formed bytes. `to_ubjson()` is strict as well (see above), so
|
||||
a value read this way cannot be written back to UBJSON.
|
||||
[`type_error.316`](../../home/exceptions.md#jsonexceptiontype_error316) for a value read with the default
|
||||
`keep` handler, unless an error handler is passed that replaces or ignores the ill-formed bytes. `to_ubjson()`'s
|
||||
own `error_handler` parameter defaults to `strict` (see above), so a value read this way cannot be written back
|
||||
to UBJSON unless a non-strict handler is passed there too.
|
||||
|
||||
??? example
|
||||
|
||||
|
||||
@@ -340,9 +340,11 @@ An unexpected byte was read in a [binary format](../features/binary_formats/inde
|
||||
### json.exception.parse_error.113
|
||||
|
||||
A string could not be read from a [binary format](../features/binary_formats/index.md): either a value that is not a
|
||||
string was read where one was required (for instance as a map key), or the string's length specification is invalid.
|
||||
The bytes of a string itself are not checked for valid UTF-8 on read; see the ill-formed UTF-8 notes on the
|
||||
individual [binary format](../features/binary_formats/index.md) pages for how such a string is handled afterward.
|
||||
string was read where one was required (for instance as a map key), the string's length specification is invalid, or
|
||||
the string's bytes are not valid UTF-8 and the `error_handler` parameter of the corresponding `from_*` function is
|
||||
set to `strict`. By default (`error_handler_t::keep`), the bytes of a string are not checked for valid UTF-8 on read;
|
||||
see the ill-formed UTF-8 notes on the individual [binary format](../features/binary_formats/index.md) pages for how
|
||||
such a string is handled depending on `error_handler`.
|
||||
|
||||
CBOR and MessagePack allow map keys of any type, but JSON object keys are always strings. Maps with keys of any other
|
||||
type (for instance integers or `null`) are therefore not supported; see the notes on
|
||||
@@ -365,6 +367,9 @@ type (for instance integers or `null`) are therefore not supported; see the note
|
||||
```
|
||||
[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData string: string length must not be negative
|
||||
```
|
||||
```
|
||||
[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing CBOR string: invalid string: ill-formed UTF-8 byte
|
||||
```
|
||||
|
||||
### json.exception.parse_error.114
|
||||
|
||||
|
||||
Reference in New Issue
Block a user