diff --git a/docs/mkdocs/docs/api/adl_serializer/index.md b/docs/mkdocs/docs/api/adl_serializer/index.md index e83b66dec..e994a30cf 100644 --- a/docs/mkdocs/docs/api/adl_serializer/index.md +++ b/docs/mkdocs/docs/api/adl_serializer/index.md @@ -30,6 +30,14 @@ struct adl_serializer { - [**from_json**](from_json.md) - convert a JSON value to any value type - [**to_json**](to_json.md) - convert any value type to a JSON value +## See also + +- [Arbitrary type conversions](../../features/arbitrary_types.md) - how to make your types convertible +- [**get**](../basic_json/get.md) / [**get_to**](../basic_json/get_to.md) - convert a JSON value to a C++ type +- [**basic_json**](../basic_json/basic_json.md) - construct a JSON value from a C++ value +- [**NLOHMANN_DEFINE_TYPE_INTRUSIVE**](../macros/nlohmann_define_type_intrusive.md) - generate the conversion functions for a type +- [Specializing enum conversion](../../features/enum_conversion.md) + ## Version history - Added in version 2.1.0. diff --git a/docs/mkdocs/docs/api/basic_json/accept.md b/docs/mkdocs/docs/api/basic_json/accept.md index 41c6969a1..345d0d507 100644 --- a/docs/mkdocs/docs/api/basic_json/accept.md +++ b/docs/mkdocs/docs/api/basic_json/accept.md @@ -23,6 +23,9 @@ Checks whether the input is valid JSON. respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to the iterator type with `operator!=`. + Ill-formed UTF-16 and UTF-32 sequences are rejected: a lone surrogate, a high surrogate that is not followed by its + low surrogate, and a code point above `U+10FFFF` all make this function return `false`. + Unlike the [`parse()`](parse.md) function, this function neither throws an exception in case of invalid JSON input (i.e., a parse error) nor creates diagnostic information. @@ -35,9 +38,12 @@ Unlike the [`parse()`](parse.md) function, this function neither throws an excep - a `#!c FILE` pointer (throws if null) - a C-style array of characters - a pointer to a null-terminated string of single byte characters (throws if null) - - a `std::string` + - a `std::string` or a `std::string_view` (C++17) - a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators (as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`) + - wide characters (`wchar_t`, `char16_t`, `char32_t`), which are converted to UTF-8: a wide string object + (`std::wstring`, `std::u16string`, `std::u32string`) or an array (e.g. a wide string literal); a pointer to a + null-terminated wide string (e.g. `std::u16string::c_str()`) is not supported `IteratorType` : a compatible iterator type, for instance. @@ -125,6 +131,21 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by --8<-- "examples/accept__iterator_pair.output" ``` +??? example "Example: (3) reading from UTF-16 strings" + + The encoding is deduced from the character type of the input. String literals like `u"..."`, `U"..."`, and + `L"..."` work as well. + + ```cpp + --8<-- "examples/accept__wide_string.cpp" + ``` + + Output: + + ```json + --8<-- "examples/accept__wide_string.output" + ``` + ## See also - [parse](parse.md) - deserialize from a compatible input @@ -151,6 +172,9 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by ([`JSON_DISABLE_STREAM_BULK_SCAN`](../macros/json_disable_stream_bulk_scan.md)). - Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0 ([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md)). +- Ill-formed UTF-16 and UTF-32 input (a lone surrogate, a high surrogate that is not followed by its low surrogate, or + a code point above `U+10FFFF`) is rejected in version 3.13.0; before that, some such sequences were accepted or + ended the input silently. !!! warning "Deprecation" diff --git a/docs/mkdocs/docs/api/basic_json/at.md b/docs/mkdocs/docs/api/basic_json/at.md index 22df4b5bb..194b123ce 100644 --- a/docs/mkdocs/docs/api/basic_json/at.md +++ b/docs/mkdocs/docs/api/basic_json/at.md @@ -239,9 +239,8 @@ Strong exception safety: if an exception occurs, the original value stays intact 1. Added in version 1.0.0. 2. Added in version 1.0.0. -3. Added in version 3.11.0. Fixed in version 3.13.0 to consistently accept `std::string_view`-convertible keys, as - already supported by [`operator[]`](operator[].md), [`value`](value.md), [`find`](find.md), and other lookup - functions. +3. Added in version 3.11.0. Support for keys convertible to `std::string_view` added in version 3.13.0, consistently + across the lookup functions. 4. Added in version 2.0.0. Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) for a one-character array index that is not a digit (e.g., `/x`) in version 3.13.0, as it already did for longer ones. diff --git a/docs/mkdocs/docs/api/basic_json/back.md b/docs/mkdocs/docs/api/basic_json/back.md index 685c1c84b..32066d8c7 100644 --- a/docs/mkdocs/docs/api/basic_json/back.md +++ b/docs/mkdocs/docs/api/basic_json/back.md @@ -63,4 +63,4 @@ Constant. ## Version history - Added in version 1.0.0. -- Adjusted code to return reference to binary values in version 3.8.0. +- Binary values are supported since version 3.8.0. diff --git a/docs/mkdocs/docs/api/basic_json/basic_json.md b/docs/mkdocs/docs/api/basic_json/basic_json.md index e553509ea..d3df598ad 100644 --- a/docs/mkdocs/docs/api/basic_json/basic_json.md +++ b/docs/mkdocs/docs/api/basic_json/basic_json.md @@ -482,15 +482,15 @@ basic_json(basic_json&& other) noexcept; ## Version history -1. Since version 1.0.0. -2. Since version 1.0.0. -3. Since version 2.1.0. -4. Since version 3.2.0. Explicit for different string types if `JSON_USE_IMPLICIT_CONVERSIONS` is `0` since +1. Added in version 1.0.0. +2. Added in version 1.0.0. +3. Added in version 2.1.0. +4. Added in version 3.1.2. Explicit for different string types if `JSON_USE_IMPLICIT_CONVERSIONS` is `0` since version 3.13.0. -5. Since version 1.0.0. -6. Since version 1.0.0. -7. Since version 1.0.0. Fixed in version 3.13.0 to also check the iterator range for binary values; before, a range +5. Added in version 1.0.0. +6. Added in version 1.0.0. +7. Added in version 1.0.0. Fixed in version 3.13.0 to also check the iterator range for binary values; before, a range that did not cover the whole value (such as `(end(), end())`) was accepted and the whole binary value was copied, unlike the other primitive types. -8. Since version 1.0.0. -9. Since version 1.0.0. +8. Added in version 1.0.0. +9. Added in version 1.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/contains.md b/docs/mkdocs/docs/api/basic_json/contains.md index 64a3e49a1..c6e2e7d77 100644 --- a/docs/mkdocs/docs/api/basic_json/contains.md +++ b/docs/mkdocs/docs/api/basic_json/contains.md @@ -141,9 +141,8 @@ Logarithmic in the size of the JSON object. ## Version history 1. Added in version 3.11.0. -2. Added in version 3.6.0. Extended template `KeyType` to support comparable types in version 3.11.0. Fixed in - version 3.13.0 to consistently accept `std::string_view`-convertible keys, as already supported by - [`operator[]`](operator[].md), [`at`](at.md), [`value`](value.md), and other lookup functions. +2. Added in version 3.6.0. Extended template `KeyType` to support comparable types in version 3.11.0. Support for keys + convertible to `std::string_view` added in version 3.13.0, consistently across the lookup functions. 3. Added in version 3.7.0. 4. Deleted overloads for integral key types added in version 3.13.0 to reject such calls at compile time instead of causing undefined behavior at runtime. diff --git a/docs/mkdocs/docs/api/basic_json/count.md b/docs/mkdocs/docs/api/basic_json/count.md index 52dc0ff14..580b79884 100644 --- a/docs/mkdocs/docs/api/basic_json/count.md +++ b/docs/mkdocs/docs/api/basic_json/count.md @@ -92,8 +92,7 @@ Logarithmic in the size of the JSON object. ## Version history 1. Added in version 3.11.0. -2. Added in version 1.0.0. Changed parameter `key` type to `KeyType&&` in version 3.11.0. Fixed in version 3.13.0 to - consistently accept `std::string_view`-convertible keys, as already supported by [`operator[]`](operator[].md), - [`at`](at.md), [`value`](value.md), and other lookup functions. +2. Added in version 1.0.0. Changed to support comparable key types in version 3.11.0. Support for keys convertible to + `std::string_view` added in version 3.13.0, consistently across the lookup functions. 3. Deleted overload for integral key types added in version 3.13.0 to reject such calls at compile time instead of causing undefined behavior at runtime. diff --git a/docs/mkdocs/docs/api/basic_json/emplace.md b/docs/mkdocs/docs/api/basic_json/emplace.md index 09953d347..b1fd856ae 100644 --- a/docs/mkdocs/docs/api/basic_json/emplace.md +++ b/docs/mkdocs/docs/api/basic_json/emplace.md @@ -69,6 +69,6 @@ Logarithmic in the size of the container, O(log(`size()`)). ## Version history -- Since version 2.0.8. +- Added in version 2.0.8. - Fixed in version 3.13.0: for [`ordered_json`](../ordered_json.md), the value could previously only be passed as an rvalue; it can now also be passed as an lvalue or a `#!cpp const` lvalue, matching the behavior of `json`. diff --git a/docs/mkdocs/docs/api/basic_json/emplace_back.md b/docs/mkdocs/docs/api/basic_json/emplace_back.md index 782b61f36..38a6d5c3d 100644 --- a/docs/mkdocs/docs/api/basic_json/emplace_back.md +++ b/docs/mkdocs/docs/api/basic_json/emplace_back.md @@ -67,5 +67,5 @@ Amortized constant. ## Version history -- Since version 2.0.8. +- Added in version 2.0.8. - Returns reference since 3.7.0. diff --git a/docs/mkdocs/docs/api/basic_json/erase.md b/docs/mkdocs/docs/api/basic_json/erase.md index 47531fed8..1782b6162 100644 --- a/docs/mkdocs/docs/api/basic_json/erase.md +++ b/docs/mkdocs/docs/api/basic_json/erase.md @@ -213,7 +213,6 @@ Strong exception safety: if an exception occurs, the original value stays intact 1. Added in version 1.0.0. Added support for binary types in version 3.8.0. 2. Added in version 1.0.0. Added support for binary types in version 3.8.0. 3. Added in version 1.0.0. -4. Added in version 3.11.0. Fixed in version 3.13.0 to consistently accept `std::string_view`-convertible keys, as - already supported by [`operator[]`](operator[].md), [`at`](at.md), [`value`](value.md), and other lookup - functions. +4. Added in version 3.11.0. Support for keys convertible to `std::string_view` added in version 3.13.0, consistently + across the lookup functions. 5. Added in version 1.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/exception.md b/docs/mkdocs/docs/api/basic_json/exception.md index b592d62ee..88cf51b1f 100644 --- a/docs/mkdocs/docs/api/basic_json/exception.md +++ b/docs/mkdocs/docs/api/basic_json/exception.md @@ -84,4 +84,4 @@ constructor. ## Version history -- Since version 3.0.0. +- Added in version 3.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/find.md b/docs/mkdocs/docs/api/basic_json/find.md index 92ab380c1..8883dc398 100644 --- a/docs/mkdocs/docs/api/basic_json/find.md +++ b/docs/mkdocs/docs/api/basic_json/find.md @@ -99,8 +99,7 @@ Logarithmic in the size of the JSON object. ## Version history 1. Added in version 3.11.0. -2. Added in version 1.0.0. Changed to support comparable types in version 3.11.0. Fixed in version 3.13.0 to - consistently accept `std::string_view`-convertible keys, as already supported by [`operator[]`](operator[].md), - [`at`](at.md), [`value`](value.md), and other lookup functions. +2. Added in version 1.0.0. Changed to support comparable types in version 3.11.0. Support for keys convertible to + `std::string_view` added in version 3.13.0, consistently across the lookup functions. 3. Deleted overloads for integral key types added in version 3.13.0 to reject such calls at compile time instead of causing undefined behavior at runtime. diff --git a/docs/mkdocs/docs/api/basic_json/front.md b/docs/mkdocs/docs/api/basic_json/front.md index dbe92f589..08c871b9a 100644 --- a/docs/mkdocs/docs/api/basic_json/front.md +++ b/docs/mkdocs/docs/api/basic_json/front.md @@ -57,4 +57,4 @@ Constant. ## Version history - Added in version 1.0.0. -- Adjusted code to return reference to binary values in version 3.8.0. +- Binary values are supported since version 3.8.0. diff --git a/docs/mkdocs/docs/api/basic_json/get.md b/docs/mkdocs/docs/api/basic_json/get.md index fef1be766..fcb5c595a 100644 --- a/docs/mkdocs/docs/api/basic_json/get.md +++ b/docs/mkdocs/docs/api/basic_json/get.md @@ -188,6 +188,6 @@ overload (3). ## Version history -1. Since version 2.1.0. -2. Since version 2.1.0. Extended to work with other specializations of `basic_json` in version 3.2.0. -3. Since version 1.0.0. +1. Added in version 2.1.0. +2. Added in version 2.1.0. Extended to work with other specializations of `basic_json` in version 3.1.2. +3. Added in version 1.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/get_to.md b/docs/mkdocs/docs/api/basic_json/get_to.md index 29a78f064..233d151b0 100644 --- a/docs/mkdocs/docs/api/basic_json/get_to.md +++ b/docs/mkdocs/docs/api/basic_json/get_to.md @@ -76,5 +76,5 @@ Depends on the `json_serializer::from_json()` implementation. ## Version history -- Since version 3.3.0. +- Added in version 3.3.0. - Added a `static_assert` with a clear message for `const` arguments in version 3.13.0. diff --git a/docs/mkdocs/docs/api/basic_json/index.md b/docs/mkdocs/docs/api/basic_json/index.md index 8d945de9b..acb67401f 100644 --- a/docs/mkdocs/docs/api/basic_json/index.md +++ b/docs/mkdocs/docs/api/basic_json/index.md @@ -44,6 +44,22 @@ are collected in [Template Parameter Requirements](../../features/types/template - [**json**](../json.md) - default specialization - [**ordered_json**](../ordered_json.md) - a specialization that maintains the insertion order of object keys +## Documents and views + +Besides `basic_json`, the library offers a second way to hold a parsed JSON text: a +[`basic_json_document`](../basic_json_document/index.md), which owns or borrows the text and holds a flat index of the +values in it, and a [`basic_json_view`](../basic_json_view/index.md), a read-only handle to one value of such a document. +Both are defined in the header ``; see +[Zero-copy JSON views](../../features/json_view.md) for the concept and the trade-offs. + +- [**json_document**](../json_document.md) / [**ordered_json_document**](../ordered_json_document.md) - read-only + documents of the `basic_json` specialization they belong to +- [**json_editable_document**](../json_editable_document.md) / + [**ordered_json_editable_document**](../ordered_json_editable_document.md) - documents that can be modified in place +- [**json_view**](../json_view.md) / [**ordered_json_view**](../ordered_json_view.md) - views of a document +- [**json_editable_view**](../json_editable_view.md) / + [**ordered_json_editable_view**](../ordered_json_editable_view.md) - views of an editable document + ## Iterator invalidation All operations that add values to an **array** ([`push_back`](push_back.md) , [`operator+=`](operator+=.md), @@ -343,6 +359,9 @@ Access to the JSON value ## See also - [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259) +- [basic_json_document](../basic_json_document/index.md) / [basic_json_view](../basic_json_view/index.md) - parse a JSON + text into a flat index and read it through a view +- [Zero-copy JSON views](../../features/json_view.md) - when to use a document and a view instead of a `basic_json` ## Version history diff --git a/docs/mkdocs/docs/api/basic_json/invalid_iterator.md b/docs/mkdocs/docs/api/basic_json/invalid_iterator.md index 6c19a3e7e..d757825d8 100644 --- a/docs/mkdocs/docs/api/basic_json/invalid_iterator.md +++ b/docs/mkdocs/docs/api/basic_json/invalid_iterator.md @@ -75,4 +75,4 @@ classDiagram ## Version history -- Since version 3.0.0. +- Added in version 3.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/items.md b/docs/mkdocs/docs/api/basic_json/items.md index 06e725ddd..025002e07 100644 --- a/docs/mkdocs/docs/api/basic_json/items.md +++ b/docs/mkdocs/docs/api/basic_json/items.md @@ -104,8 +104,8 @@ When iterating over an array, `key()` will return the index of the element as st ## Version history -- Added `iterator_wrapper` in version 3.0.0. -- Added `items` and deprecated `iterator_wrapper` in version 3.1.0. +- Added in version 3.1.0 as the replacement of the static function `iterator_wrapper`, which was available since + version 1.0.0 and is deprecated since version 3.1.0. - Added structured binding support in version 3.5.0. !!! warning "Deprecation" diff --git a/docs/mkdocs/docs/api/basic_json/json_serializer.md b/docs/mkdocs/docs/api/basic_json/json_serializer.md index 20e9cfd44..011f7d113 100644 --- a/docs/mkdocs/docs/api/basic_json/json_serializer.md +++ b/docs/mkdocs/docs/api/basic_json/json_serializer.md @@ -51,4 +51,4 @@ deserializes. See [Template Parameter Requirements](../../features/types/templat ## Version history -- Since version 2.0.0. +- Added in version 2.1.0. diff --git a/docs/mkdocs/docs/api/basic_json/merge_patch.md b/docs/mkdocs/docs/api/basic_json/merge_patch.md index 00e1bc8e5..1fafb1ece 100644 --- a/docs/mkdocs/docs/api/basic_json/merge_patch.md +++ b/docs/mkdocs/docs/api/basic_json/merge_patch.md @@ -71,6 +71,6 @@ returned by `#!cpp (*this)[key]`); it is read as it was when `merge_patch()` was ## Version history -- Added in version 3.0.0. +- Added in version 3.1.0. - Fixed use of freed or relocated memory when `apply_patch` is `#!cpp *this` or refers to a value contained in `#!cpp *this`, in version 3.13.0. diff --git a/docs/mkdocs/docs/api/basic_json/operator+=.md b/docs/mkdocs/docs/api/basic_json/operator+=.md index a3dc61b64..3aee3f22d 100644 --- a/docs/mkdocs/docs/api/basic_json/operator+=.md +++ b/docs/mkdocs/docs/api/basic_json/operator+=.md @@ -128,6 +128,6 @@ interpreted as `object_t::value_type` or `std::initializer_list`, se ## Version history -1. Since version 1.0.0. -2. Since version 1.0.0. -3. Since version 2.0.0. +1. Added in version 1.0.0. +2. Added in version 1.0.0. +3. Added in version 2.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/operator[].md b/docs/mkdocs/docs/api/basic_json/operator[].md index d8df9b73a..4356969a9 100644 --- a/docs/mkdocs/docs/api/basic_json/operator[].md +++ b/docs/mkdocs/docs/api/basic_json/operator[].md @@ -284,10 +284,10 @@ Strong exception safety: if an exception occurs, the original value stays intact 1. Added in version 1.0.0. Fixed in version 3.13.0 to throw `#!cpp std::length_error` instead of emptying the array and accessing it out of bounds when `idx` equals the maximum value of `size_type`. A missing index in the const version is guarded by a runtime assertion since version 3.13.0. -2. Added in version 1.0.0. Added overloads for `T* key` in version 1.1.0. Removed overloads for `T* key` (replaced by 3) - in version 3.11.0. -3. Added in version 3.11.0. Fixed in version 3.13.0 to consistently accept `std::string_view`-convertible keys, as - already supported by [`at`](at.md), [`value`](value.md), [`find`](find.md), and other lookup functions. +2. Added in version 1.0.0. Added overloads for `T* key` in version 1.1.0. The `T* key` overloads forward to + overload 3 since version 3.11.0. +3. Added in version 3.11.0. Support for keys convertible to `std::string_view` added in version 3.13.0, consistently + across the lookup functions. 4. Added in version 2.0.0. A missing array index in the const version is guarded by a runtime assertion since version 3.13.0. Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) for a one-character array index that is not diff --git a/docs/mkdocs/docs/api/basic_json/operator_ValueType.md b/docs/mkdocs/docs/api/basic_json/operator_ValueType.md index 48abfb3b5..dca5ebfd1 100644 --- a/docs/mkdocs/docs/api/basic_json/operator_ValueType.md +++ b/docs/mkdocs/docs/api/basic_json/operator_ValueType.md @@ -89,6 +89,6 @@ Linear in the size of the JSON value. ## Version history -- Since version 1.0.0. +- Added in version 1.0.0. - Macros `JSON_EXPLICIT`/[`JSON_USE_IMPLICIT_CONVERSIONS`](../macros/json_use_implicit_conversions.md) added in version 3.9.0. diff --git a/docs/mkdocs/docs/api/basic_json/other_error.md b/docs/mkdocs/docs/api/basic_json/other_error.md index 7019e75c5..6149cc3fb 100644 --- a/docs/mkdocs/docs/api/basic_json/other_error.md +++ b/docs/mkdocs/docs/api/basic_json/other_error.md @@ -75,4 +75,4 @@ classDiagram ## Version history -- Since version 3.0.0. +- Added in version 3.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/out_of_range.md b/docs/mkdocs/docs/api/basic_json/out_of_range.md index e1e54ae07..be579b35d 100644 --- a/docs/mkdocs/docs/api/basic_json/out_of_range.md +++ b/docs/mkdocs/docs/api/basic_json/out_of_range.md @@ -76,4 +76,4 @@ classDiagram ## Version history -- Since version 3.0.0. +- Added in version 3.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/parse.md b/docs/mkdocs/docs/api/basic_json/parse.md index f2e7eda69..a069f2086 100644 --- a/docs/mkdocs/docs/api/basic_json/parse.md +++ b/docs/mkdocs/docs/api/basic_json/parse.md @@ -25,6 +25,10 @@ static basic_json parse(IteratorType first, SentinelType last, respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to the iterator type with `operator!=`. + Ill-formed UTF-16 and UTF-32 sequences are rejected: a lone surrogate, a high surrogate that is not followed by its + low surrogate, and a code point above `U+10FFFF` all yield a + [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101). + ## Template parameters `InputType` @@ -34,9 +38,12 @@ static basic_json parse(IteratorType first, SentinelType last, - a `FILE` pointer (throws if null) - a C-style array of characters - a pointer to a null-terminated string of single byte characters (throws if null) - - a `std::string` + - a `std::string` or a `std::string_view` (C++17) - a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators (as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`) + - wide characters (`wchar_t`, `char16_t`, `char32_t`), which are converted to UTF-8: a wide string object + (`std::wstring`, `std::u16string`, `std::u32string`) or an array (e.g. a wide string literal); a pointer to a + null-terminated wide string (e.g. `std::u16string::c_str()`) is not supported `IteratorType` : a compatible iterator type, for instance. @@ -112,6 +119,13 @@ By default, a `'\0'` (NUL) byte anywhere in the input is treated as end of input outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-bytes-in-the-input) for details and the [`JSON_STRICT_NUL_HANDLING`](../macros/json_strict_nul_handling.md) macro to opt into rejecting it instead. +!!! tip "Parsing into a document instead of a value" + + [`basic_json_document::parse`](../basic_json_document/parse.md) accepts the same inputs and the same options, but + keeps the result as a flat index of the parsed values instead of a tree of `basic_json` values. It is faster and uses + less memory when only parts of the document are read, and it also accepts a pointer to a null-terminated wide string, + which `parse()` does not. See [Zero-copy JSON views](../../features/json_view.md). + !!! tip "Performance" Contiguous inputs (strings, string views, vectors, arrays, pointer ranges) are the fastest to parse. A @@ -250,6 +264,22 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by --8<-- "examples/trailing_commas.output" ``` +??? example "Example: parse from UTF-16, UTF-32, and wide strings" + + The encoding is deduced from the character type of the input; `std::u16string` and `std::u32string` are interpreted + as UTF-16 and UTF-32, and `std::wstring` uses the platform's `wchar_t` width. String literals like `u"..."`, + `U"..."`, and `L"..."` work as well, as do iterators over wide characters. + + ```cpp + --8<-- "examples/parse__wide_string.cpp" + ``` + + Output: + + ```json + --8<-- "examples/parse__wide_string.output" + ``` + ## See also - [accept](accept.md) - check if the input is valid JSON @@ -280,6 +310,9 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by time). - Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0 ([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md) restores `std::fgetc`). +- Ill-formed UTF-16 and UTF-32 input (a lone surrogate, a high surrogate that is not followed by its low surrogate, or + a code point above `U+10FFFF`) is rejected with `parse_error.101` in version 3.13.0; before that, some such + sequences were accepted or ended the input silently. !!! warning "Deprecation" diff --git a/docs/mkdocs/docs/api/basic_json/parse_error.md b/docs/mkdocs/docs/api/basic_json/parse_error.md index c3f49ef13..0daae8de2 100644 --- a/docs/mkdocs/docs/api/basic_json/parse_error.md +++ b/docs/mkdocs/docs/api/basic_json/parse_error.md @@ -84,4 +84,4 @@ or the end of file. This also holds true when reading a byte vector for binary f ## Version history -- Since version 3.0.0. +- Added in version 3.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/push_back.md b/docs/mkdocs/docs/api/basic_json/push_back.md index 386a589a3..f6ed6a426 100644 --- a/docs/mkdocs/docs/api/basic_json/push_back.md +++ b/docs/mkdocs/docs/api/basic_json/push_back.md @@ -125,6 +125,6 @@ throws. ## Version history -1. Since version 1.0.0. -2. Since version 1.0.0. -3. Since version 2.0.0. +1. Added in version 1.0.0. +2. Added in version 1.0.0. +3. Added in version 2.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/sax_parse.md b/docs/mkdocs/docs/api/basic_json/sax_parse.md index 9f70945a1..60e41e1c7 100644 --- a/docs/mkdocs/docs/api/basic_json/sax_parse.md +++ b/docs/mkdocs/docs/api/basic_json/sax_parse.md @@ -31,6 +31,10 @@ Read from input and generate SAX events respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to the iterator type with `operator!=`. + Ill-formed UTF-16 and UTF-32 sequences are rejected: a lone surrogate, a high surrogate that is not followed by its + low surrogate, and a code point above `U+10FFFF` all yield a + [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101). + The SAX event lister must follow the interface of [`json_sax`](../json_sax/index.md). ## Template parameters @@ -42,8 +46,12 @@ The SAX event lister must follow the interface of [`json_sax`](../json_sax/index - a `FILE` pointer - a C-style array of characters - a pointer to a null-terminated string of single byte characters + - a `std::string` or a `std::string_view` (C++17) - a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators (as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`) + - wide characters (`wchar_t`, `char16_t`, `char32_t`), which are converted to UTF-8: a wide string object + (`std::wstring`, `std::u16string`, `std::u32string`) or an array (e.g. a wide string literal); a pointer to a + null-terminated wide string (e.g. `std::u16string::c_str()`) is not supported `IteratorType` : a compatible iterator type for overload (2); a pair of character iterators whose `value_type` is an integral type @@ -163,6 +171,9 @@ use the same file with stdio functions, but it must not wait for another thread ([`JSON_DISABLE_STREAM_BULK_SCAN`](../macros/json_disable_stream_bulk_scan.md)). - Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0 ([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md)). +- Ill-formed UTF-16 and UTF-32 input (a lone surrogate, a high surrogate that is not followed by its low surrogate, or + a code point above `U+10FFFF`) is rejected with `parse_error.101` in version 3.13.0; before that, some such + sequences were accepted or ended the input silently. !!! warning "Deprecation" diff --git a/docs/mkdocs/docs/api/basic_json/swap.md b/docs/mkdocs/docs/api/basic_json/swap.md index 4704852f3..4bc36b125 100644 --- a/docs/mkdocs/docs/api/basic_json/swap.md +++ b/docs/mkdocs/docs/api/basic_json/swap.md @@ -182,10 +182,10 @@ Constant. ## Version history -1. Since version 1.0.0. Exchanges the `json_base_class_t` subobject along with the value since version 3.13.0. -2. Since version 1.0.0. Exchanges the `json_base_class_t` subobject along with the value since version 3.13.0. -3. Since version 1.0.0. -4. Since version 1.0.0. -5. Since version 1.0.0. -6. Since version 3.8.0. -7. Since version 3.8.0. \ No newline at end of file +1. Added in version 1.0.0. Exchanges the `json_base_class_t` subobject along with the value since version 3.13.0. +2. Added in version 1.0.0. Exchanges the `json_base_class_t` subobject along with the value since version 3.13.0. +3. Added in version 1.0.0. +4. Added in version 1.0.0. +5. Added in version 1.0.0. +6. Added in version 3.8.0. +7. Added in version 3.8.0. \ No newline at end of file diff --git a/docs/mkdocs/docs/api/basic_json/to_bjdata.md b/docs/mkdocs/docs/api/basic_json/to_bjdata.md index aa0240a73..a85d8ff8a 100644 --- a/docs/mkdocs/docs/api/basic_json/to_bjdata.md +++ b/docs/mkdocs/docs/api/basic_json/to_bjdata.md @@ -124,5 +124,3 @@ Linear in the size of the JSON value `j`. [`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) throws `type_error.316`. - Throws `type_error.321` for a discarded value since version 3.13.0; previously, a discarded value nested in an array or object was silently skipped, producing invalid BJData. -- Writes unsigned integers wider than 64 bits as high-precision numbers since version 3.13.0; previously, they were - silently truncated to 64 bits. \ No newline at end of file diff --git a/docs/mkdocs/docs/api/basic_json/type_error.md b/docs/mkdocs/docs/api/basic_json/type_error.md index b13fdd278..dcc30b29a 100644 --- a/docs/mkdocs/docs/api/basic_json/type_error.md +++ b/docs/mkdocs/docs/api/basic_json/type_error.md @@ -76,4 +76,4 @@ classDiagram ## Version history -- Since version 3.0.0. +- Added in version 3.0.0. diff --git a/docs/mkdocs/docs/api/basic_json/value.md b/docs/mkdocs/docs/api/basic_json/value.md index 12293fd1e..aee09a537 100644 --- a/docs/mkdocs/docs/api/basic_json/value.md +++ b/docs/mkdocs/docs/api/basic_json/value.md @@ -231,11 +231,8 @@ changes to any JSON value. 1. Added in version 1.0.0. Changed parameter `default_value` type from `const ValueType&` to `ValueType&&` in version 3.11.0. Deleted overload for integral key types added in version 3.13.0 to reject such calls at compile time instead of causing undefined behavior at runtime. -2. Added in version 3.11.0. Made `ValueType` the first template parameter in version 3.11.2. Fixed in version 3.13.0 - to consistently accept `std::string_view`-convertible keys, as already supported by - [`operator[]`](operator[].md), [`at`](at.md), [`find`](find.md), and other lookup functions. -3. Added in version 2.0.2. Extended to work with arrays in version 3.13.0, including fixing an issue where resolving - `ptr` through an array unexpectedly threw `out_of_range` instead of returning the resolved element (or - `default_value`, as documented). Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) +2. Added in version 3.11.0. Made `ValueType` the first template parameter in version 3.11.2. Support for keys + convertible to `std::string_view` added in version 3.13.0, consistently across the lookup functions. +3. Added in version 2.0.2. Extended to work with arrays in version 3.13.0. Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of returning `default_value` for a one-character array index that is not a digit (e.g., `/x`) in version 3.13.0, as it already did for longer ones. diff --git a/docs/mkdocs/docs/api/basic_json_document/index.md b/docs/mkdocs/docs/api/basic_json_document/index.md index d1321959a..ea5f449db 100644 --- a/docs/mkdocs/docs/api/basic_json_document/index.md +++ b/docs/mkdocs/docs/api/basic_json_document/index.md @@ -9,8 +9,8 @@ class basic_json_document; A parsed JSON text, held as a flat index of its values ([16 bytes per value](../../home/architecture.md#node-index-of-json-views)) instead of a tree of `BasicJsonType` values. -Strings and numbers stay in the source text; only strings that contain escapes are decoded, into one buffer owned by -the document. [`basic_json_view`](../basic_json_view/index.md) is a read-only handle to one value of a +Strings and floating-point numbers stay in the source text; integers are converted while parsing, and strings that +contain escapes are decoded into one buffer owned by the document. [`basic_json_view`](../basic_json_view/index.md) is a read-only handle to one value of a `basic_json_document`; [`materialize()`](../basic_json_view/materialize.md) turns a subtree into the `BasicJsonType` value that [`BasicJsonType::parse()`](../basic_json/parse.md) would have produced for it. @@ -115,6 +115,13 @@ points apply to every edit: [`source_offset()`](../basic_json_view/source_offset.md) of a value introduced by an edit is `#!cpp static_cast(-1)`, the same value it reports for a decoded string. +## See also + +- [Zero-copy JSON views](../../features/json_view.md) - guide to documents and views +- [**basic_json_view**](../basic_json_view/index.md) - the non-owning handle to a value in a document +- [**json_document**](../json_document.md) and [**json_editable_document**](../json_editable_document.md) - the default specializations +- [**materialize**](../basic_json_view/materialize.md) - build a `basic_json` value from a view + ## Version history - Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/basic_json_document/parse.md b/docs/mkdocs/docs/api/basic_json_document/parse.md index 7bc00a9f1..3f39f3485 100644 --- a/docs/mkdocs/docs/api/basic_json_document/parse.md +++ b/docs/mkdocs/docs/api/basic_json_document/parse.md @@ -21,17 +21,19 @@ static basic_json_document parse(IteratorType first, IteratorType last, Both overloads accept the same JSON text as [`BasicJsonType::parse()`](../basic_json/parse.md), with the same `ignore_comments`/`ignore_trailing_commas` options, but build a [`basic_json_document`](index.md) (a flat index into -the input) instead of a tree of `BasicJsonType` values. They take the same inputs as `BasicJsonType::parse()`. Byte -inputs that stay alive can be borrowed; all others (streams, `#!cpp FILE*`, wide characters, non-contiguous -iterators) are read into a copy that the document owns -- see the Notes. Use [`parse_copy()`](parse_copy.md) to get an -owned copy of a borrowable input as well. +the input) instead of a tree of `BasicJsonType` values. They accept every input `BasicJsonType::parse()` takes, and in +addition a pointer to a null-terminated wide string (which `BasicJsonType::parse()` rejects). Contiguous inputs of +single-byte characters that stay alive can be borrowed; all others (streams, `#!cpp FILE*`, wide characters, which +must be converted to UTF-8, and non-contiguous iterators) are read into a copy that the document owns -- see the +Notes. Use [`parse_copy()`](parse_copy.md) to get an owned copy of a borrowable input +as well. ## Template parameters `InputType` : A compatible input, for instance: - - a `#!cpp std::string`, `#!cpp std::string_view`, or a C-style array of single-byte characters + - a `#!cpp std::string`, a `#!cpp std::string_view` (C++17), or a C-style array of single-byte characters - a pointer to a null-terminated string of single-byte characters (`#!cpp char`, `#!cpp signed char`, `#!cpp unsigned char`, `#!cpp std::uint8_t`) - a container for which `#!cpp obj.data()` and `#!cpp obj.size()` give contiguous single-byte access, e.g. @@ -47,6 +49,10 @@ owned copy of a borrowable input as well. `#!cpp std::string::iterator`; the iterators of single-byte characters are borrowed or read like the byte inputs above, those of wide characters are converted to UTF-8 +Ill-formed UTF-16 and UTF-32 sequences are rejected: a lone surrogate, a high surrogate that is not followed by its low +surrogate, and a code point above `U+10FFFF` all yield a +[`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101). + ## Parameters `input` (in) @@ -155,3 +161,5 @@ buffer of a given length, pass a pair of pointers: `#!cpp parse(ptr, ptr + len)` ## Version history - Added in version 3.13.0. +- Ill-formed UTF-16 and UTF-32 input (a lone surrogate, a high surrogate that is not followed by its low surrogate, or + a code point above `U+10FFFF`) is rejected with `parse_error.101`. diff --git a/docs/mkdocs/docs/api/basic_json_view/index.md b/docs/mkdocs/docs/api/basic_json_view/index.md index 4d6d7dd05..5f0d90694 100644 --- a/docs/mkdocs/docs/api/basic_json_view/index.md +++ b/docs/mkdocs/docs/api/basic_json_view/index.md @@ -143,6 +143,13 @@ still refers to it -- including ones taken before the change -- reads the new va - [**source_offset**](source_offset.md) - byte offset of this value in the document's source text +## See also + +- [Zero-copy JSON views](../../features/json_view.md) - guide to documents and views +- [**basic_json_document**](../basic_json_document/index.md) - the document that owns the parsed data +- [**json_view**](../json_view.md) and [**json_editable_view**](../json_editable_view.md) - the default specializations +- [**materialize**](materialize.md) - build a `basic_json` value from a view + ## Version history - Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/byte_container_with_subtype/byte_container_with_subtype.md b/docs/mkdocs/docs/api/byte_container_with_subtype/byte_container_with_subtype.md index 23d5c9beb..62b0e24fc 100644 --- a/docs/mkdocs/docs/api/byte_container_with_subtype/byte_container_with_subtype.md +++ b/docs/mkdocs/docs/api/byte_container_with_subtype/byte_container_with_subtype.md @@ -51,4 +51,4 @@ byte_container_with_subtype(container_type&& container, subtype_type subtype); ## Version history -Since version 3.8.0. +Added in version 3.8.0. diff --git a/docs/mkdocs/docs/api/byte_container_with_subtype/clear_subtype.md b/docs/mkdocs/docs/api/byte_container_with_subtype/clear_subtype.md index 7fd8fe9a4..ec2e61760 100644 --- a/docs/mkdocs/docs/api/byte_container_with_subtype/clear_subtype.md +++ b/docs/mkdocs/docs/api/byte_container_with_subtype/clear_subtype.md @@ -39,4 +39,4 @@ Constant. ## Version history -Since version 3.8.0. +Added in version 3.8.0. diff --git a/docs/mkdocs/docs/api/byte_container_with_subtype/has_subtype.md b/docs/mkdocs/docs/api/byte_container_with_subtype/has_subtype.md index 98596dab0..e22429f9c 100644 --- a/docs/mkdocs/docs/api/byte_container_with_subtype/has_subtype.md +++ b/docs/mkdocs/docs/api/byte_container_with_subtype/has_subtype.md @@ -42,4 +42,4 @@ Constant. ## Version history -Since version 3.8.0. +Added in version 3.8.0. diff --git a/docs/mkdocs/docs/api/byte_container_with_subtype/index.md b/docs/mkdocs/docs/api/byte_container_with_subtype/index.md index cb5a3781d..b184d8496 100644 --- a/docs/mkdocs/docs/api/byte_container_with_subtype/index.md +++ b/docs/mkdocs/docs/api/byte_container_with_subtype/index.md @@ -29,6 +29,14 @@ specific naming scheme in order to override the binary type. - [**has_subtype**](has_subtype.md) - return whether the value has a subtype - [**clear_subtype**](clear_subtype.md) - clears the binary subtype +## See also + +- [Binary values](../../features/binary_values.md) - how binary values and subtypes are handled +- [**binary**](../basic_json/binary.md) - create a binary array value +- [**get_binary**](../basic_json/get_binary.md) - get a reference to the stored binary value +- [**binary_t**](../basic_json/binary_t.md) - type for binary arrays +- [Binary formats](../../features/binary_formats/index.md) - formats that can store binary values + ## Version history - Added in version 3.8.0. diff --git a/docs/mkdocs/docs/api/byte_container_with_subtype/set_subtype.md b/docs/mkdocs/docs/api/byte_container_with_subtype/set_subtype.md index 6cd319e1d..281de5aef 100644 --- a/docs/mkdocs/docs/api/byte_container_with_subtype/set_subtype.md +++ b/docs/mkdocs/docs/api/byte_container_with_subtype/set_subtype.md @@ -44,4 +44,4 @@ Constant. ## Version history -Since version 3.8.0. +Added in version 3.8.0. diff --git a/docs/mkdocs/docs/api/json.md b/docs/mkdocs/docs/api/json.md index 55504ef9a..b41b691ac 100644 --- a/docs/mkdocs/docs/api/json.md +++ b/docs/mkdocs/docs/api/json.md @@ -30,4 +30,4 @@ types. ## Version history -Since version 1.0.0. +Added in version 1.0.0. diff --git a/docs/mkdocs/docs/api/json_document.md b/docs/mkdocs/docs/api/json_document.md index ea365badc..cfa22e80b 100644 --- a/docs/mkdocs/docs/api/json_document.md +++ b/docs/mkdocs/docs/api/json_document.md @@ -32,4 +32,4 @@ specialization. ## Version history -Since version 3.13.0. +Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/json_editable_document.md b/docs/mkdocs/docs/api/json_editable_document.md index ab3fc3868..ec5226b78 100644 --- a/docs/mkdocs/docs/api/json_editable_document.md +++ b/docs/mkdocs/docs/api/json_editable_document.md @@ -40,4 +40,4 @@ parsing, without ever rewriting the source text -- see [Edits](basic_json_docume ## Version history -Since version 3.13.0. +Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/json_editable_view.md b/docs/mkdocs/docs/api/json_editable_view.md index f5fa59e57..ddee4e372 100644 --- a/docs/mkdocs/docs/api/json_editable_view.md +++ b/docs/mkdocs/docs/api/json_editable_view.md @@ -38,4 +38,4 @@ still referring to it seeing the change, see [Edits](basic_json_document/index.m ## Version history -Since version 3.13.0. +Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/json_pointer/operator_string_t.md b/docs/mkdocs/docs/api/json_pointer/operator_string_t.md index 9fd8bce94..44dde733d 100644 --- a/docs/mkdocs/docs/api/json_pointer/operator_string_t.md +++ b/docs/mkdocs/docs/api/json_pointer/operator_string_t.md @@ -51,5 +51,5 @@ operator string_t() const ## Version history -- Since version 2.0.0. +- Added in version 2.0.0. - Changed type to `string_t` and deprecated in version 3.11.0. diff --git a/docs/mkdocs/docs/api/json_pointer/to_string.md b/docs/mkdocs/docs/api/json_pointer/to_string.md index 2f700e1db..b994ff80e 100644 --- a/docs/mkdocs/docs/api/json_pointer/to_string.md +++ b/docs/mkdocs/docs/api/json_pointer/to_string.md @@ -49,5 +49,5 @@ ptr == json_pointer(ptr.to_string()); ## Version history -- Since version 2.0.0. +- Added in version 2.0.0. - Changed return type to `string_t` in version 3.11.0. diff --git a/docs/mkdocs/docs/api/json_sax/index.md b/docs/mkdocs/docs/api/json_sax/index.md index 42983d0c5..084503d92 100644 --- a/docs/mkdocs/docs/api/json_sax/index.md +++ b/docs/mkdocs/docs/api/json_sax/index.md @@ -60,6 +60,13 @@ leading `-` sign. - [**start_object**](start_object.md) (_virtual_) - the beginning of an object was read - [**string**](string.md) (_virtual_) - a string value was read +## See also + +- [SAX interface](../../features/parsing/sax_interface.md) - guide to event-based parsing +- [**sax_parse**](../basic_json/sax_parse.md) - generate SAX events from an input +- [**parse**](../basic_json/parse.md) - DOM parsing, with optional [parser callbacks](../../features/parsing/parser_callbacks.md) +- [Parse exceptions](../../features/parsing/parse_exceptions.md) + ## Version history - Added in version 3.2.0. diff --git a/docs/mkdocs/docs/api/json_view.md b/docs/mkdocs/docs/api/json_view.md index 819dfe501..89229ed6f 100644 --- a/docs/mkdocs/docs/api/json_view.md +++ b/docs/mkdocs/docs/api/json_view.md @@ -31,4 +31,4 @@ This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a [`j ## Version history -Since version 3.13.0. +Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/macros/index.md b/docs/mkdocs/docs/api/macros/index.md index 1230cd052..ae11a6084 100644 --- a/docs/mkdocs/docs/api/macros/index.md +++ b/docs/mkdocs/docs/api/macros/index.md @@ -137,4 +137,13 @@ header. See also the [macro overview page](../../features/macros.md). - [**NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT_WITH_NAMES**](nlohmann_define_type_with_names.md) - serialize/deserialize a derived class; uses default values; uses custom names - [**NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES**](nlohmann_define_type_with_names.md) - serialize a derived - class; uses custom names \ No newline at end of file + class; uses custom names + +## See also + +- [Macros](../../features/macros.md) - guide to the supported macros +- [Runtime assertions](../../features/assertions.md) +- [Arbitrary type conversions](../../features/arbitrary_types.md) - using the `NLOHMANN_DEFINE_TYPE_*` macros +- [Enum conversion](../../features/enum_conversion.md) - using `NLOHMANN_JSON_SERIALIZE_ENUM` +- [The `nlohmann` namespace](../../features/namespace.md) +- [CMake integration](../../integration/cmake.md) - setting macros from the build system diff --git a/docs/mkdocs/docs/api/ordered_json.md b/docs/mkdocs/docs/api/ordered_json.md index feea642cd..30f295fed 100644 --- a/docs/mkdocs/docs/api/ordered_json.md +++ b/docs/mkdocs/docs/api/ordered_json.md @@ -42,4 +42,4 @@ parsing an object of `n` keys costs O(n²) rather than O(n log n). See ## Version history -Since version 3.9.0. +Added in version 3.9.0. diff --git a/docs/mkdocs/docs/api/ordered_json_document.md b/docs/mkdocs/docs/api/ordered_json_document.md index dd6760ead..b737c2e4d 100644 --- a/docs/mkdocs/docs/api/ordered_json_document.md +++ b/docs/mkdocs/docs/api/ordered_json_document.md @@ -35,4 +35,4 @@ of object keys, instead of sorting them like [`json_document`](json_document.md) ## Version history -Since version 3.13.0. +Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/ordered_json_editable_document.md b/docs/mkdocs/docs/api/ordered_json_editable_document.md index 3127eea70..cf86307e5 100644 --- a/docs/mkdocs/docs/api/ordered_json_editable_document.md +++ b/docs/mkdocs/docs/api/ordered_json_editable_document.md @@ -44,4 +44,4 @@ members [`set`](basic_json_document/set.md) added -- instead of sorting them lik ## Version history -Since version 3.13.0. +Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/ordered_json_editable_view.md b/docs/mkdocs/docs/api/ordered_json_editable_view.md index 78351121e..1c9953d57 100644 --- a/docs/mkdocs/docs/api/ordered_json_editable_view.md +++ b/docs/mkdocs/docs/api/ordered_json_editable_view.md @@ -37,4 +37,4 @@ This type is a [`basic_json_view`](basic_json_view/index.md) of a value of an ## Version history -Since version 3.13.0. +Added in version 3.13.0. diff --git a/docs/mkdocs/docs/api/ordered_json_view.md b/docs/mkdocs/docs/api/ordered_json_view.md index 9800b0752..9df72040f 100644 --- a/docs/mkdocs/docs/api/ordered_json_view.md +++ b/docs/mkdocs/docs/api/ordered_json_view.md @@ -32,4 +32,4 @@ This type is a [`basic_json_view`](basic_json_view/index.md) of a value of an ## Version history -Since version 3.13.0. +Added in version 3.13.0. diff --git a/docs/mkdocs/docs/community/roadmap.md b/docs/mkdocs/docs/community/roadmap.md index 1b1f287a9..d294bd5f7 100644 --- a/docs/mkdocs/docs/community/roadmap.md +++ b/docs/mkdocs/docs/community/roadmap.md @@ -58,7 +58,7 @@ particular, a 3.x release does not: - remove or rename a documented macro, CMake option, CMake target, or header, or change what a documented macro does. Exceptions to these rules, for instance when fixing a bug requires changing the exception a function throws, are -documented in the [release notes](../home/releases.md). +documented in the [release notes](../home/releases/index.md). The following are **not** part of the public API and may change in any release, including patch releases: diff --git a/docs/mkdocs/docs/css/custom.css b/docs/mkdocs/docs/css/custom.css index 1fec0e57c..133609ec9 100644 --- a/docs/mkdocs/docs/css/custom.css +++ b/docs/mkdocs/docs/css/custom.css @@ -18,3 +18,32 @@ white-space: nowrap; cursor: help; } + +/* byte layout tables of the binary formats */ +.md-typeset .byte-layout td code { + white-space: pre-wrap; +} + +.md-typeset .bl { + display: inline-block; + padding: 0 .25em; + border-radius: .2em; + font-family: var(--md-code-font-family); + font-size: .85em; + white-space: nowrap; +} + +.md-typeset .bl-t { + background-color: rgba(66, 133, 244, .25); + box-shadow: inset 0 -.12em 0 rgba(66, 133, 244, .8); +} + +.md-typeset .bl-l { + background-color: rgba(251, 140, 0, .25); + box-shadow: inset 0 -.12em 0 rgba(251, 140, 0, .8); +} + +.md-typeset .bl-d { + background-color: rgba(67, 160, 71, .25); + box-shadow: inset 0 -.12em 0 rgba(67, 160, 71, .8); +} diff --git a/docs/mkdocs/docs/examples/accept__wide_string.cpp b/docs/mkdocs/docs/examples/accept__wide_string.cpp new file mode 100644 index 000000000..63dce67db --- /dev/null +++ b/docs/mkdocs/docs/examples/accept__wide_string.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +using json = nlohmann::json; + +int main() +{ + // a valid JSON text, given as UTF-16 + const std::u16string valid_text = u"{\"name\":\"\u00e9\"}"; + + // an invalid JSON text, given as UTF-16 + const std::u16string invalid_text = u"{\"name\":\"\u00e9\",}"; + + std::cout << std::boolalpha + << json::accept(valid_text) << ' ' + << json::accept(invalid_text) << '\n'; +} diff --git a/docs/mkdocs/docs/examples/accept__wide_string.output b/docs/mkdocs/docs/examples/accept__wide_string.output new file mode 100644 index 000000000..836a5934c --- /dev/null +++ b/docs/mkdocs/docs/examples/accept__wide_string.output @@ -0,0 +1 @@ +true false diff --git a/docs/mkdocs/docs/examples/parse__wide_string.cpp b/docs/mkdocs/docs/examples/parse__wide_string.cpp new file mode 100644 index 000000000..93a599947 --- /dev/null +++ b/docs/mkdocs/docs/examples/parse__wide_string.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +using json = nlohmann::json; + +int main() +{ + // the same JSON text, given in different encodings + const std::string utf8 = "{\"name\":\"\u00e9\"}"; + const std::u16string utf16 = u"{\"name\":\"\u00e9\"}"; + const std::u32string utf32 = U"{\"name\":\"\u00e9\"}"; + const std::wstring wide = L"{\"name\":\"\u00e9\"}"; + + // the encoding is deduced from the character type of the input, and all + // of them yield the same value + std::cout << json::parse(utf8) << '\n' + << json::parse(utf16) << '\n' + << json::parse(utf32) << '\n' + << json::parse(wide) << '\n'; + + // a wide string literal works as well: u"..." is UTF-16, U"..." is UTF-32 + std::cout << json::parse(u"[1, 2, 3]") << '\n'; + + // iterators over wide characters work as well + std::cout << json::parse(utf16.begin(), utf16.end()) << '\n'; +} diff --git a/docs/mkdocs/docs/examples/parse__wide_string.output b/docs/mkdocs/docs/examples/parse__wide_string.output new file mode 100644 index 000000000..cc75f0381 --- /dev/null +++ b/docs/mkdocs/docs/examples/parse__wide_string.output @@ -0,0 +1,6 @@ +{"name":"é"} +{"name":"é"} +{"name":"é"} +{"name":"é"} +[1,2,3] +{"name":"é"} diff --git a/docs/mkdocs/docs/features/binary_formats/bjdata.md b/docs/mkdocs/docs/features/binary_formats/bjdata.md index 12ef19d59..4591652eb 100644 --- a/docs/mkdocs/docs/features/binary_formats/bjdata.md +++ b/docs/mkdocs/docs/features/binary_formats/bjdata.md @@ -190,6 +190,41 @@ The library uses the following mapping from JSON values types to BJData types ac --8<-- "examples/to_bjdata.output" ``` + + + + +### Byte layout + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_bjdata` encodes it to 51 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{7Bobject start ({)
"id": 300,69 02 69 64 49 2C 01key: length 2 as int8 (i), then UTF-8 bytes; value: int16 (I), little-endian
"name": "json",69 04 6E 61 6D 65 53 69 04 6A 73 6F 6Ekey: length 4; value: string (S), length 4 as int8, UTF-8 bytes
"pi": 3.14,69 02 70 69 44 1F 85 EB 51 B8 1E 09 40key: length 2; value: float64 (D), little-endian
"tags": [69 04 74 61 67 73 5Bkey: length 4; value: array start ([)
"a",53 69 01 61string, length 1
"b"53 69 01 62string, length 1
]5Darray end (])
}7Dobject end (})
+ ## Deserialization The library maps BJData types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/bon8.md b/docs/mkdocs/docs/features/binary_formats/bon8.md index d2d20da19..555942def 100644 --- a/docs/mkdocs/docs/features/binary_formats/bon8.md +++ b/docs/mkdocs/docs/features/binary_formats/bon8.md @@ -105,6 +105,41 @@ serialized as 0x81 0x65 0xFF, but `[1,2,3,4,"e"]` as 0x85 0x91 0x92 0x93 0x94 0x --8<-- "examples/to_bon8.output" ``` + + + + +### Byte layout + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_bon8` encodes it to 35 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{8Aobject with 4 members
"id": 300,69 64 C4 04key: UTF-8 bytes; the string ends at the next byte, which cannot continue a UTF-8 character; value: 2-byte positive integer (lead byte, then a byte below 0x80)
"name": "json",6E 61 6D 65 FF 6A 73 6F 6E FFkey: UTF-8 bytes; 0xFF terminates the string as another string follows; value: UTF-8 bytes; terminated as a string key follows
"pi": 3.14,70 69 8F 40 09 1E B8 51 EB 85 1Fkey: UTF-8 bytes; value: binary64 (IEEE 754, big-endian); its marker also ends "pi"
"tags": [74 61 67 73 82key: UTF-8 bytes; value: array with 2 elements; its marker also ends "tags"
"a",61 FFUTF-8 bytes; terminated as a string follows
"b"62 FFUTF-8 bytes; terminated as nothing follows
]no end marker: the element count was given up front
}no end marker: the member count was given up front
+ ## Deserialization The library maps BON8 types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/bson.md b/docs/mkdocs/docs/features/binary_formats/bson.md index f205cba17..7a9f78d9a 100644 --- a/docs/mkdocs/docs/features/binary_formats/bson.md +++ b/docs/mkdocs/docs/features/binary_formats/bson.md @@ -61,6 +61,41 @@ The library uses the following mapping from JSON values types to BSON types: ``` + + + + +### Byte layout + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_bson` encodes it to 69 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{45 00 00 00document size: 69 bytes (int32, little-endian)
"id": 300,10 69 64 00 2C 01 00 00int32 element; key as NUL-terminated string; value (little-endian)
"name": "json",02 6E 61 6D 65 00 05 00 00 00 6A 73 6F 6E 00string element; key; length incl. NUL (int32); NUL-terminated UTF-8 bytes
"pi": 3.14,01 70 69 00 1F 85 EB 51 B8 1E 09 40double element; key; value (little-endian)
"tags": [04 74 61 67 73 00 17 00 00 00array element; key; embedded document of 23 bytes (keys are the indices)
"a",02 30 00 02 00 00 00 61 00string element; key "0"; length 2; a + NUL
"b"02 31 00 02 00 00 00 62 00string element; key "1"; length 2; b + NUL
]00end of the array document
}00end of the document
+ ## Deserialization The library maps BSON record types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/cbor.md b/docs/mkdocs/docs/features/binary_formats/cbor.md index a0a0ed8d0..20f9230fc 100644 --- a/docs/mkdocs/docs/features/binary_formats/cbor.md +++ b/docs/mkdocs/docs/features/binary_formats/cbor.md @@ -110,6 +110,41 @@ see "binary" cells in the table above. --8<-- "examples/to_cbor.output" ``` + + + + +### Byte layout + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_cbor` encodes it to 39 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{A4map with 4 key/value pairs
"id": 300,62 69 64 19 01 2Ckey: text string of length 2, then its UTF-8 bytes; value: unsigned integer, 2 bytes follow (big-endian)
"name": "json",64 6E 61 6D 65 64 6A 73 6F 6Ekey: text string of length 4; value: text string of length 4
"pi": 3.14,62 70 69 FB 40 09 1E B8 51 EB 85 1Fkey: text string of length 2; value: double-precision float (IEEE 754, big-endian)
"tags": [64 74 61 67 73 82key: text string of length 4; value: array with 2 elements
"a",61 61text string of length 1
"b"61 62text string of length 1
]no end marker: the element count was given up front
}no end marker: the member count was given up front
+ ## Deserialization The library maps CBOR types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/index.md b/docs/mkdocs/docs/features/binary_formats/index.md index 2c45995a6..8024b99cf 100644 --- a/docs/mkdocs/docs/features/binary_formats/index.md +++ b/docs/mkdocs/docs/features/binary_formats/index.md @@ -45,12 +45,19 @@ See [binary values](../binary_values.md) for more information. | BJData | 53.2 % | 91.1 % | 78.1 % | 96.6 % | | BJData (size) | 58.6 % | 92.1 % | 86.7 % | 97.4 % | | BJData (size+type) | 58.6 % | 92.1 % | 86.5 % | 97.4 % | -| BON8 | 50.5 % | 83.8 % | 63.5 % | 87.5 % | +| BON8 | **50.5 %** | **83.8 %** | **63.5 %** | **87.5 %** | | BSON | 85.8 % | 95.2 % | 95.8 % | 106.7 % | -| CBOR | 50.5 % | 86.3 % | 68.4 % | 88.0 % | -| MessagePack | 50.5 % | 86.0 % | 68.5 % | 87.9 % | +| CBOR | **50.5 %** | 86.3 % | 68.4 % | 88.0 % | +| MessagePack | **50.5 %** | 86.0 % | 68.5 % | 87.9 % | | UBJSON | 53.2 % | 91.3 % | 78.2 % | 96.6 % | | UBJSON (size) | 58.6 % | 92.3 % | 86.8 % | 97.4 % | | UBJSON (size+type) | 55.9 % | 92.3 % | 85.0 % | 95.0 % | -Sizes compared to minified JSON value. +Sizes compared to minified JSON value; the smallest size per file is printed in bold. + +## See also + +- [Binary values](../binary_values.md) - binary values and subtypes +- [**to_cbor**](../../api/basic_json/to_cbor.md) / [**from_cbor**](../../api/basic_json/from_cbor.md), [**to_msgpack**](../../api/basic_json/to_msgpack.md) / [**from_msgpack**](../../api/basic_json/from_msgpack.md), [**to_bson**](../../api/basic_json/to_bson.md) / [**from_bson**](../../api/basic_json/from_bson.md), [**to_ubjson**](../../api/basic_json/to_ubjson.md) / [**from_ubjson**](../../api/basic_json/from_ubjson.md), [**to_bjdata**](../../api/basic_json/to_bjdata.md) / [**from_bjdata**](../../api/basic_json/from_bjdata.md), [**to_bon8**](../../api/basic_json/to_bon8.md) / [**from_bon8**](../../api/basic_json/from_bon8.md) - the conversion functions +- [Parsing untrusted input](../parsing/untrusted_input.md) +- [SAX interface](../parsing/sax_interface.md) - decode binary formats without building a value diff --git a/docs/mkdocs/docs/features/binary_formats/messagepack.md b/docs/mkdocs/docs/features/binary_formats/messagepack.md index 24eeab139..3bdd1a1be 100644 --- a/docs/mkdocs/docs/features/binary_formats/messagepack.md +++ b/docs/mkdocs/docs/features/binary_formats/messagepack.md @@ -91,6 +91,41 @@ specification: --8<-- "examples/to_msgpack.output" ``` + + + + +### Byte layout + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_msgpack` encodes it to 39 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{84map with 4 key/value pairs
"id": 300,A2 69 64 CD 01 2Ckey: fixstr of length 2, then its UTF-8 bytes; value: uint 16 (big-endian)
"name": "json",A4 6E 61 6D 65 A4 6A 73 6F 6Ekey: fixstr of length 4; value: fixstr of length 4
"pi": 3.14,A2 70 69 CB 40 09 1E B8 51 EB 85 1Fkey: fixstr of length 2; value: float 64 (IEEE 754, big-endian)
"tags": [A4 74 61 67 73 92key: fixstr of length 4; value: fixarray with 2 elements
"a",A1 61fixstr of length 1
"b"A1 62fixstr of length 1
]no end marker: the element count was given up front
}no end marker: the member count was given up front
+ ## Deserialization The library maps MessagePack types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/binary_formats/ubjson.md b/docs/mkdocs/docs/features/binary_formats/ubjson.md index f7a14d855..6b7973627 100644 --- a/docs/mkdocs/docs/features/binary_formats/ubjson.md +++ b/docs/mkdocs/docs/features/binary_formats/ubjson.md @@ -103,6 +103,41 @@ The library uses the following mapping from JSON values types to UBJSON types ac --8<-- "examples/to_ubjson.output" ``` + + + + +### Byte layout + +Consider the following JSON value (shown formatted for readability): + +```json +{ + "id": 300, + "name": "json", + "pi": 3.14, + "tags": [ + "a", + "b" + ] +} +``` + +`to_ubjson` encodes it to 51 bytes (shown in hexadecimal; the encoding contains no whitespace). The colors mark the role of each byte: type/marker length/count/size data. + +
+ + + + + + + + + + +
JSONBytesMeaning
{7Bobject start ({)
"id": 300,69 02 69 64 49 01 2Ckey: length 2 as int8 (i), then UTF-8 bytes; value: int16 (I), big-endian
"name": "json",69 04 6E 61 6D 65 53 69 04 6A 73 6F 6Ekey: length 4; value: string (S), length 4 as int8, UTF-8 bytes
"pi": 3.14,69 02 70 69 44 40 09 1E B8 51 EB 85 1Fkey: length 2; value: float64 (D), big-endian
"tags": [69 04 74 61 67 73 5Bkey: length 4; value: array start ([)
"a",53 69 01 61string, length 1
"b"53 69 01 62string, length 1
]5Darray end (])
}7Dobject end (})
+ ## Deserialization The library maps UBJSON types to JSON value types as follows: diff --git a/docs/mkdocs/docs/features/conversions.md b/docs/mkdocs/docs/features/conversions.md index 5ba42b7cf..a2d848da5 100644 --- a/docs/mkdocs/docs/features/conversions.md +++ b/docs/mkdocs/docs/features/conversions.md @@ -38,6 +38,18 @@ which avoids repeating it: --8<-- "examples/get_to.output" ``` +!!! info "Getting values out of a document or a view" + + A [view](../api/basic_json_view/index.md) offers the same `get()` conversion. For the built-in types (booleans, + numbers, strings, `#!cpp std::vector`, `#!cpp std::map`, ...) it reads the value straight from the document's + index, without building a `basic_json` value first; user-defined types with a `from_json()` are converted from a + `basic_json` value, see the [list](../api/basic_json_view/get.md). Two conversions never copy at all: + [`get_string()`](../api/basic_json_view/get_string.md) returns a string as a view into the source text, and + [`number_token()`](../api/basic_json_view/number_token.md) returns a number exactly as it was written. Every other + type is obtained from a subtree that is built on demand with + [`materialize()`](../api/basic_json_view/materialize.md). See + [Zero-copy JSON views](json_view.md#getting-values-out-without-copying). + The library already knows how to convert to and from the scalar types and the STL containers (such as `#!cpp std::vector`, `#!cpp std::map`, `#!cpp std::array`, `#!cpp std::optional`, and many more). Converting a JSON object back to a `#!cpp std::map` or a JSON array back to a `#!cpp std::vector` therefore works without any extra code: @@ -234,3 +246,4 @@ The conversions above are built in for standard types. To make the same syntax w - [`get_ref`](../api/basic_json/get_ref.md) / [`get_ptr`](../api/basic_json/get_ptr.md) - access the stored value without copying - [Arbitrary types conversions](arbitrary_types.md) - support your own types - [`JSON_USE_IMPLICIT_CONVERSIONS`](../api/macros/json_use_implicit_conversions.md) - toggle implicit conversions +- [Zero-copy JSON views](json_view.md) - get values out of a document or a view diff --git a/docs/mkdocs/docs/features/creating_values.md b/docs/mkdocs/docs/features/creating_values.md index fc35b3401..42a927b78 100644 --- a/docs/mkdocs/docs/features/creating_values.md +++ b/docs/mkdocs/docs/features/creating_values.md @@ -1,7 +1,9 @@ # Creating JSON values There are several ways to create a JSON value in memory. This page gives an overview; to read a value from JSON text -instead, see [parsing](parsing/index.md). +instead, see [parsing](parsing/index.md). A JSON text can also be parsed into a +[document](../api/basic_json_document/index.md) and read through a [view](../api/basic_json_view/index.md) instead of +into a value; see [Zero-copy JSON views](json_view.md). ## From C++ values @@ -101,3 +103,4 @@ string `#!json "42"`. - [`operator""_json`](../api/operator_literal_json.md) - the `_json` literal - [Converting values](conversions.md) - which C++ types can be used - [Parsing](parsing/index.md) - create a value from JSON text +- [Zero-copy JSON views](json_view.md) - create a document and a view from JSON text instead of a value diff --git a/docs/mkdocs/docs/features/element_access/index.md b/docs/mkdocs/docs/features/element_access/index.md index 262c057b8..17d0a56db 100644 --- a/docs/mkdocs/docs/features/element_access/index.md +++ b/docs/mkdocs/docs/features/element_access/index.md @@ -21,3 +21,12 @@ flowchart TD A --> F{"just testing first?"} F -->|"yes"| G["contains() / find()"] ``` + +## See also + +- [**operator[]**](../../api/basic_json/operator%5B%5D.md) - unchecked access +- [**at**](../../api/basic_json/at.md) - checked access +- [**value**](../../api/basic_json/value.md) - access with default value +- [**contains**](../../api/basic_json/contains.md) and [**find**](../../api/basic_json/find.md) - test for existence +- [**front**](../../api/basic_json/front.md) and [**back**](../../api/basic_json/back.md) - first and last element +- [**items**](../../api/basic_json/items.md) - iterate over keys and values diff --git a/docs/mkdocs/docs/features/index.md b/docs/mkdocs/docs/features/index.md index 4ffe7e585..88443c796 100644 --- a/docs/mkdocs/docs/features/index.md +++ b/docs/mkdocs/docs/features/index.md @@ -59,3 +59,11 @@ C++ types, and finally serialize it again. This section gives conceptual overviews. For the precise signature, parameters, and return value of a function, see the [API Documentation](../api/basic_json/index.md). + +## See also + +- [API documentation](../api/basic_json/index.md) - reference of all classes and functions +- [FAQ](../home/faq.md) - answers to common questions +- [Exceptions](../home/exceptions.md) - all exceptions and their error codes +- [Design goals](../home/design_goals.md) +- [Integration](../integration/index.md) - how to add the library to a project diff --git a/docs/mkdocs/docs/features/macros.md b/docs/mkdocs/docs/features/macros.md index 620cbe0da..773ea5ac3 100644 --- a/docs/mkdocs/docs/features/macros.md +++ b/docs/mkdocs/docs/features/macros.md @@ -287,6 +287,23 @@ external dependency and is not defined by default. See [full documentation of `JSON_USE_SIMDUTF`](../api/macros/json_use_simdutf.md). +## `JSON_VIEW_NO_SIMD` + +When defined, the parser of [`basic_json_document`](../api/basic_json_document/index.md) +(``) uses only portable C++ to scan strings, instead of vector instructions (SSE2 on x86-64, +NEON on AArch64). The same input is accepted or rejected either way; only the speed differs. It is not defined by +default. + +See [full documentation of `JSON_VIEW_NO_SIMD`](../api/macros/json_view_no_simd.md). + +## `JSON_VIEW_USE_SSSE3` + +When defined on x86-64, the parser of [`basic_json_document`](../api/basic_json_document/index.md) +(``) validates non-ASCII text in strings with SSSE3 without asking the CPU first. It is not +defined by default. + +See [full documentation of `JSON_VIEW_USE_SSSE3`](../api/macros/json_view_use_ssse3.md). + ## `NLOHMANN_DEFINE_TYPE_*(...)`, `NLOHMANN_DEFINE_DERIVED_TYPE_*(...)` The library defines 12 macros to simplify the serialization/deserialization of types. See the page on diff --git a/docs/mkdocs/docs/features/modifying_values.md b/docs/mkdocs/docs/features/modifying_values.md index 10814ac2e..1d6f2e27d 100644 --- a/docs/mkdocs/docs/features/modifying_values.md +++ b/docs/mkdocs/docs/features/modifying_values.md @@ -85,6 +85,18 @@ json a = {1, 2, 3, 4}; a.erase(1); // [1,3,4] (erase by index) ``` +## Editing a document + +The operations above work on a `basic_json` value. A JSON text that was parsed into a +[document](json_view.md#editing-a-document) can also be changed in place, without building a `basic_json` tree for the +parts that are not touched: [`set`](../api/basic_json_document/set.md) replaces a value, adds or replaces an object +member, or replaces an array element (also through a JSON pointer), +[`push_back`](../api/basic_json_document/push_back.md) appends to an array, +[`insert`](../api/basic_json_document/insert.md) inserts into an array (unlike `basic_json::insert`, it has no object +overload; members are added with `set`), and [`erase`](../api/basic_json_document/erase.md) removes a member or an +element. These functions are available on the +editable document types, such as [`json_editable_document`](../api/json_editable_document.md). + ## See also - [`push_back`](../api/basic_json/push_back.md) / [`emplace_back`](../api/basic_json/emplace_back.md) - append to an array @@ -93,3 +105,4 @@ a.erase(1); // [1,3,4] (erase by index) - [`merge_patch`](../api/basic_json/merge_patch.md) - apply an RFC 7396 merge patch - [`erase`](../api/basic_json/erase.md) / [`clear`](../api/basic_json/clear.md) - remove elements - [JSON Patch and Diff](json_patch.md) and [JSON Merge Patch](merge_patch.md) - structured modifications +- [Editing a document](json_view.md#editing-a-document) - change a parsed document in place diff --git a/docs/mkdocs/docs/features/parsing/index.md b/docs/mkdocs/docs/features/parsing/index.md index 68ac8480e..2aeda41a1 100644 --- a/docs/mkdocs/docs/features/parsing/index.md +++ b/docs/mkdocs/docs/features/parsing/index.md @@ -18,6 +18,7 @@ flowchart LR The [`parse`](../../api/basic_json/parse.md) function reads a JSON value from an input. The input can be - a string (`#!cpp std::string`, C string, or string literal), +- a wide string (`#!cpp std::u16string`, `#!cpp std::u32string`, or `#!cpp std::wstring`, or a wide string literal), - a `#!cpp std::istream` (e.g., an `#!cpp std::ifstream` reading from a file), - a `#!cpp FILE*` pointer, - a pair of iterators over a contiguous range (e.g., a `#!cpp std::vector`), or @@ -32,12 +33,18 @@ std::ifstream f("example.json"); json data = json::parse(f); ``` -The input must be encoded in UTF-8; other encodings are not supported. A single input may contain only one JSON value. +The input must be encoded in UTF-8; other encodings are not supported. The exception is wide-character input, which +is read as UTF-16 or UTF-32 -- depending on the character type -- and converted to UTF-8; see the +[`parse`](../../api/basic_json/parse.md) page for an example. A single input may contain only one JSON value. Inputs consisting of multiple values separated by newlines are handled by the [JSON Lines](json_lines.md) format. By default, the library rejects comments and trailing commas. Both can be enabled with parameters of the `parse` function — see [comments](../comments.md) and [trailing commas](../trailing_commas.md). +[`basic_json_document::parse`](../../api/basic_json_document/parse.md) accepts the same inputs with the same options, +but keeps the result in a document rather than in a `basic_json` value; see +[Documents and views](#documents-and-views) below. + ## Strictness and trailing data [`parse`](../../api/basic_json/parse.md) reads a single JSON value and requires the whole input to be consumed: any @@ -98,6 +105,26 @@ The library offers two parsing models: You can influence a DOM parse without switching to the SAX interface by passing a [parser callback](parser_callbacks.md), which is called during parsing and can, for example, discard parts of the input. +## Documents and views + +A third model sits between DOM and SAX: [`basic_json_document::parse`](../../api/basic_json_document/parse.md) parses +the input into a document that keeps the text and a flat index of the values in it, and +[`basic_json_view`](../../api/basic_json_view/index.md) gives read-only access to the values through that index. +The whole input is validated during parsing, but only integers and strings that contain escape sequences are converted +right away (the strings into a buffer owned by the document). Floating-point numbers and all other strings stay in the +source text and are only converted when they are read, which is faster and uses less memory than building a tree of `basic_json` values when only parts +of the document are needed. A document can also be +modified in place, and it can be saved and loaded again without parsing it. + +```cpp +#include + +json_document doc = json_document::parse(R"({"happy": true, "pi": 3.141})"); +std::cout << doc.root()["happy"].get() << '\n'; +``` + +See [Zero-copy JSON views](../json_view.md) for the concept and the trade-offs. + ## Exceptions When the input is not valid JSON, the `parse` function throws an exception by default. If exceptions are undesired or @@ -108,6 +135,7 @@ options. To get as much as possible out of malformed input, a SAX parser can [re ## See also - [`parse`](../../api/basic_json/parse.md) - deserialize from a compatible input +- [`basic_json_document::parse`](../../api/basic_json_document/parse.md) - deserialize into a document instead of a value - [`accept`](../../api/basic_json/accept.md) - check if the input is valid JSON - [`sax_parse`](../../api/basic_json/sax_parse.md) - generate SAX events - [JSON Lines](json_lines.md) - parse newline-delimited JSON @@ -116,3 +144,4 @@ options. To get as much as possible out of malformed input, a SAX parser can [re - [parsing and exceptions](parse_exceptions.md) - control error handling - [error recovery](error_recovery.md) - get as much as possible out of malformed input - [parsing untrusted input](untrusted_input.md) - what to consider when parsing input from untrusted sources +- [Zero-copy JSON views](../json_view.md) - parse into a document and read it through a view diff --git a/docs/mkdocs/docs/features/parsing/untrusted_input.md b/docs/mkdocs/docs/features/parsing/untrusted_input.md index 9c5fa4c9e..551f9770e 100644 --- a/docs/mkdocs/docs/features/parsing/untrusted_input.md +++ b/docs/mkdocs/docs/features/parsing/untrusted_input.md @@ -81,6 +81,9 @@ Invalid UTF-8 is rejected while parsing, not just while serializing: - In JSON text, an ill-formed UTF-8 byte in a string is a [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) ("invalid string: ill-formed UTF-8 byte"). +- In UTF-16 or UTF-32 input, an ill-formed sequence is a + [`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101) as well: a lone surrogate, a high surrogate + that is not followed by its low surrogate, and a code point above `U+10FFFF` are all rejected. - In a binary format, a string that is not valid UTF-8 is a [`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113). diff --git a/docs/mkdocs/docs/features/types/index.md b/docs/mkdocs/docs/features/types/index.md index 67e97df6e..03e8c0bae 100644 --- a/docs/mkdocs/docs/features/types/index.md +++ b/docs/mkdocs/docs/features/types/index.md @@ -286,3 +286,12 @@ This implementation does exactly follow this approach, as it uses double precisi ### Storage Integer number values, unsigned integer number values, and floating-point number values are stored directly inside a `basic_json` type. + +## See also + +- [Number handling](number_handling.md) - how numbers are stored and converted +- [Template parameter requirements](template_parameters.md) +- [**basic_json**](../../api/basic_json/index.md) - the class template and its member types +- [**type**](../../api/basic_json/type.md) and [**value_t**](../../api/basic_json/value_t.md) - query the type of a value +- [**object_t**](../../api/basic_json/object_t.md), [**array_t**](../../api/basic_json/array_t.md), [**string_t**](../../api/basic_json/string_t.md), [**number_integer_t**](../../api/basic_json/number_integer_t.md), [**number_float_t**](../../api/basic_json/number_float_t.md) - the underlying types +- [Object order](../object_order.md) diff --git a/docs/mkdocs/docs/home/debugging.md b/docs/mkdocs/docs/home/debugging.md index 36aa5f39f..362afaa6b 100644 --- a/docs/mkdocs/docs/home/debugging.md +++ b/docs/mkdocs/docs/home/debugging.md @@ -1,7 +1,7 @@ # Debugging -This page collects the library's built-in debugger integrations and other debugging-related features. They are -not linked from a single place elsewhere in the docs, so are collected here. +This page collects the library's built-in debugger integrations and other debugging-related features. They are not +covered by any single page elsewhere in the docs, so they are collected here. ## Visual Studio (natvis) diff --git a/docs/mkdocs/docs/home/exceptions.md b/docs/mkdocs/docs/home/exceptions.md index 7658fb3cb..00d4058dc 100644 --- a/docs/mkdocs/docs/home/exceptions.md +++ b/docs/mkdocs/docs/home/exceptions.md @@ -99,7 +99,8 @@ As this global context comes at the price of storing one additional pointer per Now the exception message contains a JSON Pointer `/address/housenumber` that indicates which value has the wrong type. -See [documentation of `JSON_DIAGNOSTICS`](../api/macros/json_diagnostics.md) for more information. +See [documentation of `JSON_DIAGNOSTICS`](../api/macros/json_diagnostics.md) for more information, and +[Debugging](debugging.md) for the debugger integrations of the library. ## Parse errors diff --git a/docs/mkdocs/docs/home/faq.md b/docs/mkdocs/docs/home/faq.md index 2381f4f7e..dc0f4158a 100644 --- a/docs/mkdocs/docs/home/faq.md +++ b/docs/mkdocs/docs/home/faq.md @@ -81,8 +81,9 @@ rejected by default, and the library never adds them when serializing. The library supports **Unicode input** as follows: -- Only **UTF-8** encoded input is supported, which is the default encoding for JSON, according to [RFC 8259](https://tools.ietf.org/html/rfc8259.html#section-8.1). -- `std::u16string` and `std::u32string` can be parsed, assuming UTF-16 and UTF-32 encoding, respectively. These encodings are not supported when reading from files or other input containers. +- **UTF-8** encoded input is supported, which is the default encoding for JSON, according to [RFC 8259](https://tools.ietf.org/html/rfc8259.html#section-8.1). +- `std::u16string` and `std::u32string` can be parsed, assuming UTF-16 and UTF-32 encoding, respectively; `std::wstring` works as well and is interpreted as UTF-16 or UTF-32, depending on the width of the platform's `wchar_t`. These encodings cannot be read from a `FILE*` or a `std::istream`, which carry bytes only. +- Ill-formed UTF-16 or UTF-32 input is rejected: a lone surrogate, a high surrogate that is not followed by its low surrogate, and a code point above `U+10FFFF` all yield a parse error. The same holds for iterators over 2- or 4-byte characters. - Other encodings such as Latin-1 or ISO 8859-1 are **not** supported and will yield parse or serialization errors. - The library will not replace [Unicode noncharacters](http://www.unicode.org/faq/private_use.html#nonchar1). - Invalid surrogates (e.g., incomplete pairs such as `\uDEAD`) will yield parse errors. @@ -146,7 +147,7 @@ json::parse(std::string("\"") + '\0' + "\""); // throws parse_error.101: control Why are wide strings (e.g., `std::wstring`) dumped as arrays of numbers? -As described [above](#parse-errors-reading-non-ascii-characters), the library assumes UTF-8 as encoding. To store a wide string, you need to change the encoding. +As described [above](#parse-errors-reading-non-ascii-characters), the library stores its strings as UTF-8. To store a wide string as a string, convert it to a UTF-8 encoded `std::string` first, as in the example below. !!! example diff --git a/docs/mkdocs/docs/home/releases.md b/docs/mkdocs/docs/home/releases.md deleted file mode 100644 index c0865270e..000000000 --- a/docs/mkdocs/docs/home/releases.md +++ /dev/null @@ -1,453 +0,0 @@ -# Releases - -This page summarizes the notable changes of every release and links to the relevant documentation. -The **complete release notes** — including all changes, the download files, and their checksums — are -published on the [GitHub releases page](https://github.com/nlohmann/json/releases). - -!!! info "Unreleased changes" - - This documentation is built from the `develop` branch and may describe changes that are not part of a release - yet. Their version numbers are followed by an unreleased badge. - -## Unreleased - -- Converting an rvalue container to JSON (`json j = std::move(vec);`, `json j = std::map{...};`) - now **moves** the elements instead of copying them (e.g., `std::vector` with 100,000 strings: 4.7 ms to - 2.3 ms). Two consequences: the source container is left in a valid but unspecified (moved-from) state, and if an - exception is thrown during the conversion, the source may be partially moved-from, so the strong exception guarantee - no longer holds for the source of an rvalue conversion (the JSON value itself is unaffected). Lvalues and `const` - objects are still copied. See [`basic_json`](../api/basic_json/basic_json.md). - -## v3.12.0 (2025-04-11) - -Fixes bugs found in 3.11.3 and adds several features. All changes are backward-compatible. - -- Adds diagnostic byte positions via [`JSON_DIAGNOSTIC_POSITIONS`](../api/macros/json_diagnostic_positions.md), - exposed through the new [`start_pos`](../api/basic_json/start_pos.md) and - [`end_pos`](../api/basic_json/end_pos.md) member functions. -- Makes the [conversion macros](../features/arbitrary_types.md#simplify-your-life-with-macros) - templated (so they also work with [`ordered_json`](../api/ordered_json.md)) and adds - [`NLOHMANN_DEFINE_DERIVED_TYPE`](../api/macros/nlohmann_define_derived_type.md) for derived classes. -- Adds `std::optional` support (C++17) and lets [`patch`](../api/basic_json/patch.md), - [`diff`](../api/basic_json/diff.md), and [`flatten`](../api/basic_json/flatten.md) work with - arbitrary string types. -- Extends the [binary formats](../features/binary_formats/index.md): - [BJData](../features/binary_formats/bjdata.md) draft 3 and unsigned 64-bit integers for - [BSON](../features/binary_formats/bson.md). -- Adds multidimensional C-array conversion and UTF-8 encoded `std::filesystem::path` conversions, and - raises the minimum [CMake](../integration/cmake.md) version to 3.5 and supports CMake 4.0. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.12.0). - -## v3.11.3 (2023-11-28) - -Adds features and fixes bugs found in 3.11.2. All changes are backward-compatible. - -- Adds a [custom base class](../api/basic_json/json_base_class_t.md) as a node customization point. -- Adds serialization-only [conversion macros](../features/macros.md) - (`NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE` and `NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE`) - and a clearer parse error for empty input. -- Adds [Bazel](../integration/package_managers.md#bazel) and - [Swift Package Manager](../integration/package_managers.md#swift-package-manager) build support. -- Fixes custom allocators, a memory leak in [`adl_serializer`](../api/adl_serializer/to_json.md)'s - `to_json`, initializer-list construction when `size_type` is not `int`, and many compiler warnings. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.11.3). - -## v3.11.2 (2022-08-12) - -Fixes bugs found in 3.11.1 and restructures the namespace. All changes are backward-compatible. - -- Fixes the [`value`](../api/basic_json/value.md) function (broken for strings, size types, and - `nullptr` in 3.11.0) and makes `json_fwd.hpp` self-contained. -- Restores using [`json_pointer`](../api/json_pointer/index.md) as a key in associative containers and - comparing it with strings. -- Restructures the inline [namespace](../features/namespace.md) and allows disabling the version - component, and avoids heap allocations in the [BJData](../features/binary_formats/bjdata.md) parser. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.11.2). - -## v3.11.1 (2022-08-01) - -Fixes a regression from 3.11.0. All changes are backward-compatible. - -- Restores the global [user-defined string literals](../api/macros/json_use_global_udls.md) - [`operator""_json`](../api/operator_literal_json.md) and - [`operator""_json_pointer`](../api/operator_literal_json_pointer.md), which 3.11.0 had moved into a - namespace by default. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.11.1). - -## v3.11.0 (2022-08-01) - -One of the largest releases ever. All changes are backward-compatible. - -- Allows `std::string_view` as object keys in [`at`](../api/basic_json/at.md), - [`operator[]`](../api/basic_json/operator%5B%5D.md), [`value`](../api/basic_json/value.md), - [`erase`](../api/basic_json/erase.md), [`find`](../api/basic_json/find.md), - [`contains`](../api/basic_json/contains.md), and [`count`](../api/basic_json/count.md). -- Adds the [BJData](../features/binary_formats/bjdata.md) binary format (the fifth supported format). -- Improves C++20 support, including [`operator<=>`](../api/basic_json/operator_spaceship.md) and - ``-compatible iterators. -- Adds a versioned, ABI-tagged inline [namespace](../features/namespace.md) - ([`NLOHMANN_JSON_NAMESPACE`](../api/macros/nlohmann_json_namespace.md)) and the option to move the - UDLs out of the global namespace ([`JSON_USE_GLOBAL_UDLS`](../api/macros/json_use_global_udls.md)). -- Adds [`patch_inplace`](../api/basic_json/patch_inplace.md), default values for the - [conversion macros](../features/arbitrary_types.md#simplify-your-life-with-macros), and an option to - disable enum serialization ([`JSON_DISABLE_ENUM_SERIALIZATION`](../api/macros/json_disable_enum_serialization.md)). - -This release introduced a UDL regression that was fixed in -[3.11.1](https://github.com/nlohmann/json/releases/tag/v3.11.1). -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.11.0). - -## v3.10.5 (2022-01-03) - -Bug-fix release. All changes are backward-compatible. - -- Guards the `std::filesystem` conversions behind compiler-support checks - ([`JSON_HAS_FILESYSTEM`](../api/macros/json_has_filesystem.md)), which can be set to `0` to disable - them altogether. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.5). - -## v3.10.4 (2021-10-16) - -Fixes regressions introduced in 3.10.0. All changes are backward-compatible. - -- Fixes the `std::filesystem::path` conversion (which could trigger a stack overflow and broke - compilation on Windows). -- Fixes compilation for types with an explicit defaulted constructor and for code relying on the - return values of `std::find` and `std::remove`. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.4). - -## v3.10.3 (2021-10-08) - -Fixes more regressions from 3.10.0. All changes are backward-compatible. - -- Fixes [extended-diagnostics](../api/macros/json_diagnostics.md) assertions triggered by - [`update`](../api/basic_json/update.md) and by inserting into arrays. -- Supports custom allocators when writing binary formats into a `std::vector`, and allows conversion - from types that only provide `begin()`/`end()`. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.3). - -## v3.10.2 (2021-08-26) - -Re-release of 3.10.1, whose Git tag pointed at the wrong commit due to a bug in the release script. -All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.2). - -## v3.10.1 (2021-08-24) - -Fixes a regression from 3.10.0. All changes are backward-compatible. - -- Fixes an [extended-diagnostics](../api/macros/json_diagnostics.md) assertion triggered when used - with [`ordered_json`](../api/ordered_json.md), and hardens the GDB pretty-printer. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.1). - -## v3.10.0 (2021-08-17) - -Feature release. All changes are backward-compatible. - -- Adds [extended diagnostic messages](../api/macros/json_diagnostics.md) - ([`JSON_DIAGNOSTICS`](../api/macros/json_diagnostics.md)) that prepend a JSON pointer to exception - messages to pinpoint the offending value. -- Adds a GDB pretty-printer and a [`cbor_tag_handler_t`](../api/basic_json/cbor_tag_handler_t.md) - `store` option to keep CBOR tags as binary subtypes. -- Supports containers with non-default-constructible types and parsing from `std::byte`. -- Adds [`JSON_NO_IO`](../api/macros/json_no_io.md) to exclude the I/O headers and the - [`JSON_HAS_CPP_*`](../api/macros/json_has_cpp_11.md) macros to override the detected C++ standard. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.0). - -## v3.9.1 (2020-08-06) - -Fixes two regressions from 3.9.0. All changes are backward-compatible. - -- Accepts consecutive [comments](../features/comments.md) and completes the - [`ordered_json`](../api/ordered_json.md) interface (e.g. `ordered_json::parse`). - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.9.1). - -## v3.9.0 (2020-07-27) - -Feature release adding four long-requested features. All changes are backward-compatible. - -- Optional [comment](../features/comments.md) parsing in [`parse`](../api/basic_json/parse.md) via the - `ignore_comments` parameter. -- [`ordered_json`](../api/ordered_json.md) to preserve the [insertion order](../features/object_order.md) - of object keys. -- An option to switch off [implicit conversions](../api/macros/json_use_implicit_conversions.md). -- The [`NLOHMANN_DEFINE_TYPE_*`](../features/arbitrary_types.md#simplify-your-life-with-macros) - convenience macros, plus high-precision-number support for - [UBJSON](../features/binary_formats/ubjson.md) and CBOR tag handling. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.9.0). - -## v3.8.0 (2020-06-14) - -Feature release. All changes are backward-compatible. - -- Introduces a [binary value](../features/binary_values.md) type that is read from and written to - [CBOR](../features/binary_formats/cbor.md), [BSON](../features/binary_formats/bson.md), and - [MessagePack](../features/binary_formats/messagepack.md), and can be shared between formats. -- Generalizes the input adapters to read from any `LegacyInputIterator` container (3–10 % faster - parsing). -- Fixes [`contains`](../api/basic_json/contains.md) for JSON pointers and makes the binary - [`from_cbor`](../api/basic_json/from_cbor.md)/[`from_msgpack`](../api/basic_json/from_msgpack.md)/etc. - functions respect `allow_exceptions`. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.8.0). - -## v3.7.3 (2019-11-17) - -Fixes a regression from 3.7.2 that could yield quadratic complexity in destructor calls. All changes -are backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v3.7.3). - -## v3.7.2 (2019-11-10) - -Fixes a stack overflow for deeply nested input by making the destructor iterative; parsing is now -bounded only by available memory. All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.7.2). - -## v3.7.1 (2019-11-06) - -Bug-fix release. All changes are backward-compatible. - -- Fixes a segmentation fault when serializing the `std::int64_t` minimum value and fixes - [`contains`](../api/basic_json/contains.md) for JSON pointers. -- Allows [`items`](../api/basic_json/items.md) with a custom string type and makes `json_pointer::back` - `const`. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.7.1). - -## v3.7.0 (2019-07-28) - -Convenience features and house-keeping. All changes are backward-compatible. - -- Adds a [`contains`](../api/basic_json/contains.md) overload that checks a JSON pointer without - throwing, a generic `to_string`, and a return value for - [`emplace_back`](../api/basic_json/emplace_back.md). - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.7.0). - -## v3.6.1 (2019-03-20) - -Fixes a regression (GCC 7/8 compilation) and a `` build error introduced in 3.6.0. All -changes are backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v3.6.1). - -## v3.6.0 (2019-03-20) - -Feature release. All changes are backward-compatible. - -- Reworks the [JSON pointer](../features/json_pointer.md) interface (`operator/`, `push_back`, - `parent_pointer`, …). -- Adds a [`contains`](../api/basic_json/contains.md) function to test for an object key and greatly - improves the performance of integer serialization. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.6.0). - -## v3.5.0 (2018-12-22) - -Feature release. All changes are backward-compatible. - -- Adds structured-binding support via the [`items`](../api/basic_json/items.md) function and reading - from `FILE*` in the [`parse`](../api/basic_json/parse.md) function. -- Fixes the `eofbit` handling on input streams and a bug in the BSON SAX parser. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.5.0). - -## v3.4.0 (2018-10-30) - -Feature release. All changes are backward-compatible. - -- Adds [BSON](../features/binary_formats/bson.md) read/write support. -- Adds configurable Unicode error handlers to [`dump`](../api/basic_json/dump.md) (throw, replace with - U+FFFD, or ignore) and the - [`NLOHMANN_JSON_SERIALIZE_ENUM`](../api/macros/nlohmann_json_serialize_enum.md) macro for - [enum conversion](../features/enum_conversion.md). -- Improves parse-error messages with line/column positions and context. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.4.0). - -## v3.3.0 (2018-10-05) - -Feature release. All changes are backward-compatible. - -- Adds GCC 4.8 support, the [`get_to`](../api/basic_json/get_to.md) function, and an overhauled and - documented [CMake](../integration/cmake.md) integration. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.3.0). - -## v3.2.0 (2018-08-20) - -Feature release. All changes are backward-compatible. - -- Adds a [SAX interface](../features/parsing/sax_interface.md) and a non-recursive parser. -- Adds parsing from wide-string types (`std::wstring`, `std::u16string`, `std::u32string`) and - `std::string_view` (C++17), and round-tripping of `std::map`/`std::unordered_map` with non-string - keys. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.2.0). - -## v3.1.2 (2018-03-14) - -Bug-fix release. All changes are backward-compatible. - -- Fixes a memory leak in the parser callback and adds user-defined string-type support to the parser - and serializer. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.1.2). - -## v3.1.1 (2018-02-13) - -Bug-fix release. All changes are backward-compatible. - -- Fixes parsing of indefinite-length CBOR strings, a user-defined conversion to vector types, and - overflow detection for UBJSON containers. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.1.1). - -## v3.1.0 (2018-02-01) - -Feature release. All changes are backward-compatible. - -- Adds [UBJSON](../features/binary_formats/ubjson.md) read/write support and - [JSON Merge Patch](../features/merge_patch.md) via [`merge_patch`](../api/basic_json/merge_patch.md). -- Switches to the Grisu2 algorithm for short, round-trippable floating-point output, and splits the - header into [multiple files](../integration/index.md) with a forward-declaration header. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.1.0). - -## v3.0.1 (2017-12-29) - -Fixes small issues in the [JSON Pointer](../features/json_pointer.md) and -[JSON Patch](../features/json_patch.md) implementations (invalid "copy" targets and non-integer array -indices). All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.0.1). - -## v3.0.0 (2017-12-17) - -First 3.x release — a major release with breaking changes (see the -[migration guide](../integration/migration_guide.md)). - -- Introduces user-defined [exceptions](exceptions.md) (`json::exception` and subtypes, each with an - identifier). -- Adds a non-throwing [`accept`](../api/basic_json/accept.md) function and an `allow_exceptions` flag - for [`parse`](../api/basic_json/parse.md), and an [`update`](../api/basic_json/update.md) function to - merge objects. -- Adds streaming for CBOR and MessagePack and allows storing NaN/infinity. -- Non-UTF-8 strings now throw on serialization, and the iterator category changed to bidirectional. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.0.0). - -## v2.1.1 (2017-02-25) - -Bug-fix release. All changes are backward-compatible. - -- Makes number parsing and serialization locale-independent with correct floating-point - round-tripping; released files are now GPG-signed. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.1.1). - -## v2.1.0 (2017-01-28) - -Feature release. All changes are backward-compatible. - -- Adds conversions from and to [arbitrary user-defined types](../features/arbitrary_types.md) via - `to_json`/`from_json`, the [`meta`](../api/basic_json/meta.md) function, and the option to switch off - exceptions ([`JSON_NOEXCEPTION`](../api/macros/json_noexception.md)). - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.1.0). - -## v2.0.10 (2017-01-02) - -Fixes several security-relevant bugs in the CBOR and MessagePack parsers found by OSS-Fuzz. All -changes are backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.10). - -## v2.0.9 (2016-12-16) - -Adds the [CBOR](../features/binary_formats/cbor.md) and -[MessagePack](../features/binary_formats/messagepack.md) binary formats. All changes are -backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.9). - -## v2.0.8 (2016-12-02) - -Adds the [`emplace`](../api/basic_json/emplace.md) and -[`emplace_back`](../api/basic_json/emplace_back.md) functions and improves parsing and serialization -performance. All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.8). - -## v2.0.7 (2016-11-02) - -Fixes several parser bugs found through the "Parsing JSON is a Minefield" study (short files, encoding -detection, surrogate pairs). All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.7). - -## v2.0.6 (2016-10-15) - -Fixes [`operator[]`](../api/basic_json/operator%5B%5D.md) for [JSON pointers](../features/json_pointer.md) -so that it creates missing values like the other overloads. All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.6). - -## v2.0.5 (2016-09-14) - -Fixes a remaining stream end-of-file detection bug in the parser. All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.5). - -## v2.0.4 (2016-09-11) - -Fixes stream end-of-file detection in the parser. All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.4). - -## v2.0.3 (2016-08-31) - -Generalizes the parser to accept any contiguous sequence of one-byte elements and deprecates the -input-stream constructor in favor of the [`parse`](../api/basic_json/parse.md) function. All changes -are backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.3). - -## v2.0.2 (2016-07-31) - -Overhauls the parser (now rejecting unescaped control characters), tightens the class invariants, and -cleans up the code. All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.2). - -## v2.0.1 (2016-06-28) - -Fixes a performance regression in the [`dump`](../api/basic_json/dump.md) function by adjusting the -stream locale once per serialization. All changes are backward-compatible. -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.1). - -## v2.0.0 (2016-06-24) - -Feature release with a minor (potentially non-backward-compatible) API change from added `noexcept` -and `constexpr` specifiers. - -- Adds [JSON Pointer](../features/json_pointer.md) support in [`at`](../api/basic_json/at.md) and - [`operator[]`](../api/basic_json/operator%5B%5D.md), plus [`flatten`](../api/basic_json/flatten.md) - and [`unflatten`](../api/basic_json/unflatten.md). -- Adds [JSON Patch](../features/json_patch.md) via [`diff`](../api/basic_json/diff.md) and - [`patch`](../api/basic_json/patch.md), unsigned 64-bit integer support, and locale-independent - serialization. - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.0). - -## v1.1.0 (2016-01-24) - -Bug-fix and feature release. All changes are backward-compatible. - -- Improves floating-point round-tripping, adds a `get_ref` accessor for stored values, and introduces - runtime [assertions](../features/assertions.md). - -[Full release notes](https://github.com/nlohmann/json/releases/tag/v1.1.0). - -## v1.0.0 (2015-12-28) - -First official release. [Full release notes](https://github.com/nlohmann/json/releases/tag/v1.0.0). - -## See also - -- [Migration Guide](../integration/migration_guide.md) — how to future-proof your code for the next - major version and replace deprecated functions. diff --git a/docs/mkdocs/docs/home/releases/index.md b/docs/mkdocs/docs/home/releases/index.md new file mode 100644 index 000000000..e0dbca2d5 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/index.md @@ -0,0 +1,155 @@ +# Releases + +This section summarizes the notable changes of every release and links to the relevant documentation. +The **complete release notes** — including all changes, the download files, and their checksums — are +published on the [GitHub releases page](https://github.com/nlohmann/json/releases). + +!!! info "Unreleased changes" + + This documentation is built from the `develop` branch and may describe changes that are not part of a release + yet. Their version numbers are followed by an unreleased badge. + +Every release has its own page with a summary of its notable changes and an overview of the API that was added or +changed in it. The API overview is generated from the "Version history" section of the +[API pages](../../api/basic_json/index.md); it is not a change log, but helps to see whether a feature is available in +the version you use. + +## Unreleased + +| Release | Date | Summary | +|---------|------|---------| +| [Unreleased](unreleased.md) | | Changes on the `develop` branch that are not part of a release yet. | + +## Version 3.12.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.12.0](v3.12.0.md) | 2025-04-11 | Fixes bugs found in 3.11.3 and adds several features. No public API was removed or renamed. | + +## Version 3.11.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.11.3](v3.11.3.md) | 2023-11-28 | Adds features and fixes bugs found in 3.11.2. All changes are backward-compatible. | +| [v3.11.2](v3.11.2.md) | 2022-08-12 | Fixes bugs found in 3.11.1 and restructures the namespace. All changes are backward-compatible. | +| [v3.11.1](v3.11.1.md) | 2022-08-01 | Fixes a regression from 3.11.0. All changes are backward-compatible. | +| [v3.11.0](v3.11.0.md) | 2022-08-01 | One of the largest releases ever. All changes are backward-compatible. | + +## Version 3.10.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.10.5](v3.10.5.md) | 2022-01-03 | Bug-fix release. All changes are backward-compatible. | +| [v3.10.4](v3.10.4.md) | 2021-10-16 | Fixes regressions introduced in 3.10.0. All changes are backward-compatible. | +| [v3.10.3](v3.10.3.md) | 2021-10-08 | Fixes more regressions from 3.10.0. All changes are backward-compatible. | +| [v3.10.2](v3.10.2.md) | 2021-08-26 | Re-release of 3.10.1, whose Git tag pointed at the wrong commit due to a bug in the release script. All changes are backward-compatible. | +| [v3.10.1](v3.10.1.md) | 2021-08-24 | Fixes a regression from 3.10.0. All changes are backward-compatible. | +| [v3.10.0](v3.10.0.md) | 2021-08-17 | Feature release. All changes are backward-compatible. | + +## Version 3.9.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.9.1](v3.9.1.md) | 2020-08-06 | Fixes two regressions from 3.9.0. All changes are backward-compatible. | +| [v3.9.0](v3.9.0.md) | 2020-07-27 | Feature release adding four long-requested features. All changes are backward-compatible. | + +## Version 3.8.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.8.0](v3.8.0.md) | 2020-06-14 | Feature release. All changes are backward-compatible. | + +## Version 3.7.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.7.3](v3.7.3.md) | 2019-11-17 | Fixes a regression from 3.7.2 that could yield quadratic complexity in destructor calls. All changes are backward-compatible. | +| [v3.7.2](v3.7.2.md) | 2019-11-10 | Fixes a stack overflow for deeply nested input by making the destructor iterative; parsing of JSON text is now bounded only by available memory (the binary-format parsers still recurse). All changes are backward-compatible. | +| [v3.7.1](v3.7.1.md) | 2019-11-06 | Bug-fix release. All changes are backward-compatible. | +| [v3.7.0](v3.7.0.md) | 2019-07-28 | Convenience features and house-keeping. All changes are backward-compatible. | + +## Version 3.6.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.6.1](v3.6.1.md) | 2019-03-20 | Fixes a regression (GCC 7/8 compilation) and a `` build error introduced in 3.6.0. All changes are backward-compatible. | +| [v3.6.0](v3.6.0.md) | 2019-03-20 | Feature release. All changes are backward-compatible. | + +## Version 3.5.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.5.0](v3.5.0.md) | 2018-12-22 | Feature release. All changes are backward-compatible. | + +## Version 3.4.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.4.0](v3.4.0.md) | 2018-10-30 | Feature release. All changes are backward-compatible. | + +## Version 3.3.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.3.0](v3.3.0.md) | 2018-10-05 | Feature release. All changes are backward-compatible. | + +## Version 3.2.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.2.0](v3.2.0.md) | 2018-08-20 | Feature release. All changes are backward-compatible. | + +## Version 3.1.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.1.2](v3.1.2.md) | 2018-03-14 | Bug-fix release. All changes are backward-compatible. | +| [v3.1.1](v3.1.1.md) | 2018-02-13 | Bug-fix release. All changes are backward-compatible. | +| [v3.1.0](v3.1.0.md) | 2018-02-01 | Feature release. All changes are backward-compatible. | + +## Version 3.0.x + +| Release | Date | Summary | +|---------|------|---------| +| [v3.0.1](v3.0.1.md) | 2017-12-29 | Fixes small issues in the [JSON Pointer](../../features/json_pointer.md) and [JSON Patch](../../features/json_patch.md) implementations (invalid "copy" targets and non-integer array indices). All changes are backward-compatible. | +| [v3.0.0](v3.0.0.md) | 2017-12-17 | First 3.x release — a major release with breaking changes (see the [migration guide](../../integration/migration_guide.md)). | + +## Version 2.1.x + +| Release | Date | Summary | +|---------|------|---------| +| [v2.1.1](v2.1.1.md) | 2017-02-25 | Bug-fix release. All changes are backward-compatible. | +| [v2.1.0](v2.1.0.md) | 2017-01-28 | Feature release. All changes are backward-compatible. | + +## Version 2.0.x + +| Release | Date | Summary | +|---------|------|---------| +| [v2.0.10](v2.0.10.md) | 2017-01-02 | Fixes several security-relevant bugs in the CBOR and MessagePack parsers found by OSS-Fuzz. All changes are backward-compatible. | +| [v2.0.9](v2.0.9.md) | 2016-12-16 | Adds the [CBOR](../../features/binary_formats/cbor.md) and [MessagePack](../../features/binary_formats/messagepack.md) binary formats. All changes are backward-compatible. | +| [v2.0.8](v2.0.8.md) | 2016-12-02 | Adds the [`emplace`](../../api/basic_json/emplace.md) and [`emplace_back`](../../api/basic_json/emplace_back.md) functions and improves parsing and serialization performance. All changes are backward-compatible. | +| [v2.0.7](v2.0.7.md) | 2016-11-02 | Fixes several parser bugs found through the "Parsing JSON is a Minefield" study (short files, encoding detection, surrogate pairs). All changes are backward-compatible. | +| [v2.0.6](v2.0.6.md) | 2016-10-15 | Fixes [`operator[]`](../../api/basic_json/operator%5B%5D.md) for [JSON pointers](../../features/json_pointer.md) so that it creates missing values like the other overloads. All changes are backward-compatible. | +| [v2.0.5](v2.0.5.md) | 2016-09-14 | Fixes a remaining stream end-of-file detection bug in the parser. All changes are backward-compatible. | +| [v2.0.4](v2.0.4.md) | 2016-09-11 | Fixes stream end-of-file detection in the parser. All changes are backward-compatible. | +| [v2.0.3](v2.0.3.md) | 2016-08-31 | Generalizes the parser to accept any contiguous sequence of one-byte elements and deprecates the input-stream constructor in favor of the [`parse`](../../api/basic_json/parse.md) function. All changes are backward-compatible. | +| [v2.0.2](v2.0.2.md) | 2016-07-31 | Overhauls the parser (now rejecting unescaped control characters), tightens the class invariants, and cleans up the code. All changes are backward-compatible. | +| [v2.0.1](v2.0.1.md) | 2016-06-28 | Fixes a performance regression in the [`dump`](../../api/basic_json/dump.md) function by adjusting the stream locale once per serialization. All changes are backward-compatible. | +| [v2.0.0](v2.0.0.md) | 2016-06-24 | Feature release with a minor (potentially non-backward-compatible) API change from added `noexcept` and `constexpr` specifiers. | + +## Version 1.1.x + +| Release | Date | Summary | +|---------|------|---------| +| [v1.1.0](v1.1.0.md) | 2016-01-24 | Bug-fix and feature release. All changes are backward-compatible. | + +## Version 1.0.x + +| Release | Date | Summary | +|---------|------|---------| +| [v1.0.0](v1.0.0.md) | 2015-12-28 | First official release. | + +## See also + +- [Migration Guide](../../integration/migration_guide.md) — how to future-proof your code for the next + major version and replace deprecated functions. diff --git a/docs/mkdocs/docs/home/releases/unreleased.md b/docs/mkdocs/docs/home/releases/unreleased.md new file mode 100644 index 000000000..b27da9748 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/unreleased.md @@ -0,0 +1,14 @@ +# Unreleased + +These changes are part of the `develop` branch, but not of a release yet. + +- Converting an rvalue container to JSON (`json j = std::move(vec);`, `json j = std::map{...};`) + now **moves** the elements instead of copying them (e.g., `std::vector` with 100,000 strings: 4.7 ms to + 2.3 ms). Two consequences: the source container is left in a valid but unspecified (moved-from) state, and if an + exception is thrown during the conversion, the source may be partially moved-from, so the strong exception guarantee + no longer holds for the source of an rvalue conversion (the JSON value itself is unaffected). Lvalues and `const` + objects are still copied. See [`basic_json`](../../api/basic_json/basic_json.md). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v1.0.0.md b/docs/mkdocs/docs/home/releases/v1.0.0.md new file mode 100644 index 000000000..dde38ca57 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v1.0.0.md @@ -0,0 +1,9 @@ +# v1.0.0 + +Released on 2015-12-28. + +First official release. [Full release notes](https://github.com/nlohmann/json/releases/tag/v1.0.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v1.1.0.md b/docs/mkdocs/docs/home/releases/v1.1.0.md new file mode 100644 index 000000000..1bdff4b59 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v1.1.0.md @@ -0,0 +1,14 @@ +# v1.1.0 + +Released on 2016-01-24. + +Bug-fix and feature release. All changes are backward-compatible. + +- Improves floating-point round-tripping, adds a `get_ref` accessor for stored values, and introduces + runtime [assertions](../../features/assertions.md). + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v1.1.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.0.md b/docs/mkdocs/docs/home/releases/v2.0.0.md new file mode 100644 index 000000000..540593592 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.0.md @@ -0,0 +1,19 @@ +# v2.0.0 + +Released on 2016-06-24. + +Feature release with a minor (potentially non-backward-compatible) API change from added `noexcept` +and `constexpr` specifiers. + +- Adds [JSON Pointer](../../features/json_pointer.md) support in [`at`](../../api/basic_json/at.md) and + [`operator[]`](../../api/basic_json/operator%5B%5D.md), plus [`flatten`](../../api/basic_json/flatten.md) + and [`unflatten`](../../api/basic_json/unflatten.md). +- Adds [JSON Patch](../../features/json_patch.md) via [`diff`](../../api/basic_json/diff.md) and + [`patch`](../../api/basic_json/patch.md), unsigned 64-bit integer support, and locale-independent + serialization. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.1.md b/docs/mkdocs/docs/home/releases/v2.0.1.md new file mode 100644 index 000000000..8d2820f14 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.1.md @@ -0,0 +1,11 @@ +# v2.0.1 + +Released on 2016-06-28. + +Fixes a performance regression in the [`dump`](../../api/basic_json/dump.md) function by adjusting the +stream locale once per serialization. All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.1). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.10.md b/docs/mkdocs/docs/home/releases/v2.0.10.md new file mode 100644 index 000000000..56e390f44 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.10.md @@ -0,0 +1,10 @@ +# v2.0.10 + +Released on 2017-01-02. + +Fixes several security-relevant bugs in the CBOR and MessagePack parsers found by OSS-Fuzz. All +changes are backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.10). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.2.md b/docs/mkdocs/docs/home/releases/v2.0.2.md new file mode 100644 index 000000000..4824db4a9 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.2.md @@ -0,0 +1,11 @@ +# v2.0.2 + +Released on 2016-07-31. + +Overhauls the parser (now rejecting unescaped control characters), tightens the class invariants, and +cleans up the code. All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.2). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.3.md b/docs/mkdocs/docs/home/releases/v2.0.3.md new file mode 100644 index 000000000..66182cb1d --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.3.md @@ -0,0 +1,11 @@ +# v2.0.3 + +Released on 2016-08-31. + +Generalizes the parser to accept any contiguous sequence of one-byte elements and deprecates the +input-stream constructor in favor of the [`parse`](../../api/basic_json/parse.md) function. All changes +are backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.3). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.4.md b/docs/mkdocs/docs/home/releases/v2.0.4.md new file mode 100644 index 000000000..b7cebb942 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.4.md @@ -0,0 +1,10 @@ +# v2.0.4 + +Released on 2016-09-11. + +Fixes stream end-of-file detection in the parser. All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.4). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.5.md b/docs/mkdocs/docs/home/releases/v2.0.5.md new file mode 100644 index 000000000..2f383efc9 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.5.md @@ -0,0 +1,10 @@ +# v2.0.5 + +Released on 2016-09-14. + +Fixes a remaining stream end-of-file detection bug in the parser. All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.5). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.6.md b/docs/mkdocs/docs/home/releases/v2.0.6.md new file mode 100644 index 000000000..cfee027fb --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.6.md @@ -0,0 +1,11 @@ +# v2.0.6 + +Released on 2016-10-15. + +Fixes [`operator[]`](../../api/basic_json/operator%5B%5D.md) for [JSON pointers](../../features/json_pointer.md) +so that it creates missing values like the other overloads. All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.6). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.7.md b/docs/mkdocs/docs/home/releases/v2.0.7.md new file mode 100644 index 000000000..3d8069ae6 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.7.md @@ -0,0 +1,11 @@ +# v2.0.7 + +Released on 2016-11-02. + +Fixes several parser bugs found through the "Parsing JSON is a Minefield" study (short files, encoding +detection, surrogate pairs). All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.7). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.8.md b/docs/mkdocs/docs/home/releases/v2.0.8.md new file mode 100644 index 000000000..177e335a5 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.8.md @@ -0,0 +1,12 @@ +# v2.0.8 + +Released on 2016-12-02. + +Adds the [`emplace`](../../api/basic_json/emplace.md) and +[`emplace_back`](../../api/basic_json/emplace_back.md) functions and improves parsing and serialization +performance. All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.8). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.0.9.md b/docs/mkdocs/docs/home/releases/v2.0.9.md new file mode 100644 index 000000000..2587c790e --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.0.9.md @@ -0,0 +1,11 @@ +# v2.0.9 + +Released on 2016-12-16. + +Adds the [CBOR](../../features/binary_formats/cbor.md) and +[MessagePack](../../features/binary_formats/messagepack.md) binary formats. All changes are +backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v2.0.9). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.1.0.md b/docs/mkdocs/docs/home/releases/v2.1.0.md new file mode 100644 index 000000000..3e42cbd32 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.1.0.md @@ -0,0 +1,15 @@ +# v2.1.0 + +Released on 2017-01-28. + +Feature release. All changes are backward-compatible. + +- Adds conversions from and to [arbitrary user-defined types](../../features/arbitrary_types.md) via + `to_json`/`from_json`, the [`meta`](../../api/basic_json/meta.md) function, and the option to switch off + exceptions ([`JSON_NOEXCEPTION`](../../api/macros/json_noexception.md)). + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.1.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v2.1.1.md b/docs/mkdocs/docs/home/releases/v2.1.1.md new file mode 100644 index 000000000..7167dcd16 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v2.1.1.md @@ -0,0 +1,14 @@ +# v2.1.1 + +Released on 2017-02-25. + +Bug-fix release. All changes are backward-compatible. + +- Makes number parsing and serialization locale-independent with correct floating-point + round-tripping; commits and released files are GPG-signed. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v2.1.1). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.0.0.md b/docs/mkdocs/docs/home/releases/v3.0.0.md new file mode 100644 index 000000000..cea928ee1 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.0.0.md @@ -0,0 +1,20 @@ +# v3.0.0 + +Released on 2017-12-17. + +First 3.x release — a major release with breaking changes (see the +[migration guide](../../integration/migration_guide.md)). + +- Introduces user-defined [exceptions](../exceptions.md) (`json::exception` and subtypes, each with an + identifier). +- Adds a non-throwing [`accept`](../../api/basic_json/accept.md) function and an `allow_exceptions` flag + for [`parse`](../../api/basic_json/parse.md), and an [`update`](../../api/basic_json/update.md) function to + merge objects. +- Adds streaming for CBOR and MessagePack and allows storing NaN/infinity. +- Non-UTF-8 strings now throw on serialization, and the iterator category changed to bidirectional. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.0.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.0.1.md b/docs/mkdocs/docs/home/releases/v3.0.1.md new file mode 100644 index 000000000..95c6141cf --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.0.1.md @@ -0,0 +1,12 @@ +# v3.0.1 + +Released on 2017-12-29. + +Fixes small issues in the [JSON Pointer](../../features/json_pointer.md) and +[JSON Patch](../../features/json_patch.md) implementations (invalid "copy" targets and non-integer array +indices). All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.0.1). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.1.0.md b/docs/mkdocs/docs/home/releases/v3.1.0.md new file mode 100644 index 000000000..31428a66f --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.1.0.md @@ -0,0 +1,16 @@ +# v3.1.0 + +Released on 2018-02-01. + +Feature release. All changes are backward-compatible. + +- Adds [UBJSON](../../features/binary_formats/ubjson.md) read/write support and + [JSON Merge Patch](../../features/merge_patch.md) via [`merge_patch`](../../api/basic_json/merge_patch.md). +- Switches to the Grisu2 algorithm for short, round-trippable floating-point output, and splits the + header into [multiple files](../../integration/index.md) with a forward-declaration header. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.1.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.1.1.md b/docs/mkdocs/docs/home/releases/v3.1.1.md new file mode 100644 index 000000000..b7c6aca1f --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.1.1.md @@ -0,0 +1,14 @@ +# v3.1.1 + +Released on 2018-02-13. + +Bug-fix release. All changes are backward-compatible. + +- Fixes parsing of indefinite-length CBOR strings, a user-defined conversion to vector types, and + overflow detection for UBJSON containers. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.1.1). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.1.2.md b/docs/mkdocs/docs/home/releases/v3.1.2.md new file mode 100644 index 000000000..30917e031 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.1.2.md @@ -0,0 +1,14 @@ +# v3.1.2 + +Released on 2018-03-14. + +Bug-fix release. All changes are backward-compatible. + +- Fixes a memory leak in the parser callback and adds user-defined string-type support to the parser + and serializer. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.1.2). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.10.0.md b/docs/mkdocs/docs/home/releases/v3.10.0.md new file mode 100644 index 000000000..520e8ab39 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.10.0.md @@ -0,0 +1,20 @@ +# v3.10.0 + +Released on 2021-08-17. + +Feature release. All changes are backward-compatible. + +- Adds [extended diagnostic messages](../../api/macros/json_diagnostics.md) + ([`JSON_DIAGNOSTICS`](../../api/macros/json_diagnostics.md)) that prepend a JSON pointer to exception + messages to pinpoint the offending value. +- Adds a GDB pretty-printer and a [`cbor_tag_handler_t`](../../api/basic_json/cbor_tag_handler_t.md) + `store` option to keep CBOR tags as binary subtypes. +- Supports containers with non-default-constructible types and parsing from `std::byte`. +- Adds [`JSON_NO_IO`](../../api/macros/json_no_io.md) to exclude the I/O headers and the + [`JSON_HAS_CPP_*`](../../api/macros/json_has_cpp_11.md) macros to override the detected C++ standard. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.10.1.md b/docs/mkdocs/docs/home/releases/v3.10.1.md new file mode 100644 index 000000000..e6881beee --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.10.1.md @@ -0,0 +1,14 @@ +# v3.10.1 + +Released on 2021-08-24. + +Fixes a regression from 3.10.0. All changes are backward-compatible. + +- Fixes an [extended-diagnostics](../../api/macros/json_diagnostics.md) assertion triggered when used + with [`ordered_json`](../../api/ordered_json.md), and hardens the GDB pretty-printer. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.1). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.10.2.md b/docs/mkdocs/docs/home/releases/v3.10.2.md new file mode 100644 index 000000000..c442ac668 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.10.2.md @@ -0,0 +1,11 @@ +# v3.10.2 + +Released on 2021-08-26. + +Re-release of 3.10.1, whose Git tag pointed at the wrong commit due to a bug in the release script. +All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.2). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.10.3.md b/docs/mkdocs/docs/home/releases/v3.10.3.md new file mode 100644 index 000000000..0c84ccfee --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.10.3.md @@ -0,0 +1,16 @@ +# v3.10.3 + +Released on 2021-10-08. + +Fixes more regressions from 3.10.0. All changes are backward-compatible. + +- Fixes [extended-diagnostics](../../api/macros/json_diagnostics.md) assertions triggered by + [`update`](../../api/basic_json/update.md) and by inserting into arrays. +- Supports custom allocators when writing binary formats into a `std::vector`, and allows conversion + from types that only provide `begin()`/`end()`. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.3). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.10.4.md b/docs/mkdocs/docs/home/releases/v3.10.4.md new file mode 100644 index 000000000..dcb81e38a --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.10.4.md @@ -0,0 +1,16 @@ +# v3.10.4 + +Released on 2021-10-16. + +Fixes regressions introduced in 3.10.0. All changes are backward-compatible. + +- Fixes the `std::filesystem::path` conversion (which could trigger a stack overflow and broke + compilation on Windows). +- Fixes compilation for types with an explicit defaulted constructor and for code relying on the + return values of `std::find` and `std::remove`. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.4). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.10.5.md b/docs/mkdocs/docs/home/releases/v3.10.5.md new file mode 100644 index 000000000..e1c82ba41 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.10.5.md @@ -0,0 +1,15 @@ +# v3.10.5 + +Released on 2022-01-03. + +Bug-fix release. All changes are backward-compatible. + +- Guards the `std::filesystem` conversions behind compiler-support checks + ([`JSON_HAS_FILESYSTEM`](../../api/macros/json_has_filesystem.md)), which can be set to `0` to disable + them altogether. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.10.5). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.11.0.md b/docs/mkdocs/docs/home/releases/v3.11.0.md new file mode 100644 index 000000000..c25a069a2 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.11.0.md @@ -0,0 +1,27 @@ +# v3.11.0 + +Released on 2022-08-01. + +One of the largest releases ever. All changes are backward-compatible. + +- Allows `std::string_view` as object keys in [`at`](../../api/basic_json/at.md), + [`operator[]`](../../api/basic_json/operator%5B%5D.md), [`value`](../../api/basic_json/value.md), + [`erase`](../../api/basic_json/erase.md), [`find`](../../api/basic_json/find.md), + [`contains`](../../api/basic_json/contains.md), and [`count`](../../api/basic_json/count.md). +- Adds the [BJData](../../features/binary_formats/bjdata.md) binary format (the fifth supported format). +- Improves C++20 support, including [`operator<=>`](../../api/basic_json/operator_spaceship.md) and + ``-compatible iterators. +- Adds a versioned, ABI-tagged inline [namespace](../../features/namespace.md) + ([`NLOHMANN_JSON_NAMESPACE`](../../api/macros/nlohmann_json_namespace.md)) and the option to move the + UDLs out of the global namespace ([`JSON_USE_GLOBAL_UDLS`](../../api/macros/json_use_global_udls.md)). +- Adds [`patch_inplace`](../../api/basic_json/patch_inplace.md), default values for the + [conversion macros](../../features/arbitrary_types.md#simplify-your-life-with-macros), and an option to + disable enum serialization ([`JSON_DISABLE_ENUM_SERIALIZATION`](../../api/macros/json_disable_enum_serialization.md)). + +This release introduced a UDL regression that was fixed in +[3.11.1](https://github.com/nlohmann/json/releases/tag/v3.11.1). +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.11.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.11.1.md b/docs/mkdocs/docs/home/releases/v3.11.1.md new file mode 100644 index 000000000..666a8951b --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.11.1.md @@ -0,0 +1,16 @@ +# v3.11.1 + +Released on 2022-08-01. + +Fixes a regression from 3.11.0. All changes are backward-compatible. + +- Restores the global [user-defined string literals](../../api/macros/json_use_global_udls.md) + [`operator""_json`](../../api/operator_literal_json.md) and + [`operator""_json_pointer`](../../api/operator_literal_json_pointer.md), which 3.11.0 had moved into a + namespace by default. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.11.1). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.11.2.md b/docs/mkdocs/docs/home/releases/v3.11.2.md new file mode 100644 index 000000000..a5f9e7ce3 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.11.2.md @@ -0,0 +1,18 @@ +# v3.11.2 + +Released on 2022-08-12. + +Fixes bugs found in 3.11.1 and restructures the namespace. All changes are backward-compatible. + +- Fixes the [`value`](../../api/basic_json/value.md) function (broken for strings, size types, and + `nullptr` in 3.11.0) and makes `json_fwd.hpp` self-contained. +- Restores using [`json_pointer`](../../api/json_pointer/index.md) as a key in associative containers and + comparing it with strings. +- Restructures the inline [namespace](../../features/namespace.md) and allows disabling the version + component, and avoids heap allocations in the [BJData](../../features/binary_formats/bjdata.md) parser. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.11.2). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.11.3.md b/docs/mkdocs/docs/home/releases/v3.11.3.md new file mode 100644 index 000000000..238a774c7 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.11.3.md @@ -0,0 +1,20 @@ +# v3.11.3 + +Released on 2023-11-28. + +Adds features and fixes bugs found in 3.11.2. All changes are backward-compatible. + +- Adds a [custom base class](../../api/basic_json/json_base_class_t.md) as a node customization point. +- Adds serialization-only [conversion macros](../../features/macros.md) + (`NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE` and `NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE`) + and a clearer parse error for empty input. +- Adds [Bazel](../../integration/package_managers.md#bazel) and + [Swift Package Manager](../../integration/package_managers.md#swift-package-manager) build support. +- Fixes custom allocators, a memory leak in [`adl_serializer`](../../api/adl_serializer/to_json.md)'s + `to_json`, initializer-list construction when `size_type` is not `int`, and many compiler warnings. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.11.3). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.12.0.md b/docs/mkdocs/docs/home/releases/v3.12.0.md new file mode 100644 index 000000000..07be55494 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.12.0.md @@ -0,0 +1,26 @@ +# v3.12.0 + +Released on 2025-04-11. + +Fixes bugs found in 3.11.3 and adds several features. No public API was removed or renamed. + +- Adds diagnostic byte positions via [`JSON_DIAGNOSTIC_POSITIONS`](../../api/macros/json_diagnostic_positions.md), + exposed through the new [`start_pos`](../../api/basic_json/start_pos.md) and + [`end_pos`](../../api/basic_json/end_pos.md) member functions. +- Makes the [conversion macros](../../features/arbitrary_types.md#simplify-your-life-with-macros) + templated (so they also work with [`ordered_json`](../../api/ordered_json.md)) and adds + [`NLOHMANN_DEFINE_DERIVED_TYPE`](../../api/macros/nlohmann_define_derived_type.md) for derived classes. +- Adds `std::optional` support (C++17) and lets [`patch`](../../api/basic_json/patch.md), + [`diff`](../../api/basic_json/diff.md), and [`flatten`](../../api/basic_json/flatten.md) work with + arbitrary string types. +- Extends the [binary formats](../../features/binary_formats/index.md): + [BJData](../../features/binary_formats/bjdata.md) draft 3 and unsigned 64-bit integers for + [BSON](../../features/binary_formats/bson.md). +- Adds multidimensional C-array conversion and UTF-8 encoded `std::filesystem::path` conversions, and + raises the minimum [CMake](../../integration/cmake.md) version to 3.5 and supports CMake 4.0. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.12.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.2.0.md b/docs/mkdocs/docs/home/releases/v3.2.0.md new file mode 100644 index 000000000..6e6690254 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.2.0.md @@ -0,0 +1,16 @@ +# v3.2.0 + +Released on 2018-08-20. + +Feature release. All changes are backward-compatible. + +- Adds a [SAX interface](../../features/parsing/sax_interface.md) and a non-recursive parser. +- Adds parsing from wide-string types (`std::wstring`, `std::u16string`, `std::u32string`), conversion to + `std::string_view` (C++17), and round-tripping of `std::map`/`std::unordered_map` with non-string + keys. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.2.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.3.0.md b/docs/mkdocs/docs/home/releases/v3.3.0.md new file mode 100644 index 000000000..e4ee07c84 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.3.0.md @@ -0,0 +1,14 @@ +# v3.3.0 + +Released on 2018-10-05. + +Feature release. All changes are backward-compatible. + +- Adds GCC 4.8 support, the [`get_to`](../../api/basic_json/get_to.md) function, and an overhauled and + documented [CMake](../../integration/cmake.md) integration. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.3.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.4.0.md b/docs/mkdocs/docs/home/releases/v3.4.0.md new file mode 100644 index 000000000..49a4cf595 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.4.0.md @@ -0,0 +1,18 @@ +# v3.4.0 + +Released on 2018-10-30. + +Feature release. All changes are backward-compatible. + +- Adds [BSON](../../features/binary_formats/bson.md) read/write support. +- Adds configurable Unicode error handlers to [`dump`](../../api/basic_json/dump.md) (throw, replace with + U+FFFD, or ignore) and the + [`NLOHMANN_JSON_SERIALIZE_ENUM`](../../api/macros/nlohmann_json_serialize_enum.md) macro for + [enum conversion](../../features/enum_conversion.md). +- Improves parse-error messages with line/column positions and context. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.4.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.5.0.md b/docs/mkdocs/docs/home/releases/v3.5.0.md new file mode 100644 index 000000000..40a14e159 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.5.0.md @@ -0,0 +1,15 @@ +# v3.5.0 + +Released on 2018-12-22. + +Feature release. All changes are backward-compatible. + +- Adds structured-binding support via the [`items`](../../api/basic_json/items.md) function and reading + from `FILE*` in the [`parse`](../../api/basic_json/parse.md) function. +- Fixes the `eofbit` handling on input streams and a bug in the BSON SAX parser. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.5.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.6.0.md b/docs/mkdocs/docs/home/releases/v3.6.0.md new file mode 100644 index 000000000..48aa6e9b9 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.6.0.md @@ -0,0 +1,16 @@ +# v3.6.0 + +Released on 2019-03-20. + +Feature release. All changes are backward-compatible. + +- Reworks the [JSON pointer](../../features/json_pointer.md) interface (`operator/`, `push_back`, + `parent_pointer`, …). +- Adds a [`contains`](../../api/basic_json/contains.md) function to test for an object key and greatly + improves the performance of integer serialization. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.6.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.6.1.md b/docs/mkdocs/docs/home/releases/v3.6.1.md new file mode 100644 index 000000000..c1e8daf47 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.6.1.md @@ -0,0 +1,10 @@ +# v3.6.1 + +Released on 2019-03-20. + +Fixes a regression (GCC 7/8 compilation) and a `` build error introduced in 3.6.0. All +changes are backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v3.6.1). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.7.0.md b/docs/mkdocs/docs/home/releases/v3.7.0.md new file mode 100644 index 000000000..c9e7b15b6 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.7.0.md @@ -0,0 +1,15 @@ +# v3.7.0 + +Released on 2019-07-28. + +Convenience features and house-keeping. All changes are backward-compatible. + +- Adds a [`contains`](../../api/basic_json/contains.md) overload that checks a JSON pointer without + throwing, a generic `to_string`, and a return value for + [`emplace_back`](../../api/basic_json/emplace_back.md). + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.7.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.7.1.md b/docs/mkdocs/docs/home/releases/v3.7.1.md new file mode 100644 index 000000000..98a85fb68 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.7.1.md @@ -0,0 +1,16 @@ +# v3.7.1 + +Released on 2019-11-06. + +Bug-fix release. All changes are backward-compatible. + +- Fixes a segmentation fault when serializing the `std::int64_t` minimum value and fixes + [`contains`](../../api/basic_json/contains.md) for JSON pointers. +- Allows [`items`](../../api/basic_json/items.md) with a custom string type and makes `json_pointer::back` + `const`. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.7.1). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.7.2.md b/docs/mkdocs/docs/home/releases/v3.7.2.md new file mode 100644 index 000000000..aea237be8 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.7.2.md @@ -0,0 +1,11 @@ +# v3.7.2 + +Released on 2019-11-10. + +Fixes a stack overflow for deeply nested input by making the destructor iterative; parsing of JSON +text is now bounded only by available memory (the binary-format parsers still recurse). All changes are backward-compatible. +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.7.2). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.7.3.md b/docs/mkdocs/docs/home/releases/v3.7.3.md new file mode 100644 index 000000000..f0a106f39 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.7.3.md @@ -0,0 +1,10 @@ +# v3.7.3 + +Released on 2019-11-17. + +Fixes a regression from 3.7.2 that could yield quadratic complexity in destructor calls. All changes +are backward-compatible. [Full release notes](https://github.com/nlohmann/json/releases/tag/v3.7.3). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.8.0.md b/docs/mkdocs/docs/home/releases/v3.8.0.md new file mode 100644 index 000000000..babd55fac --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.8.0.md @@ -0,0 +1,20 @@ +# v3.8.0 + +Released on 2020-06-14. + +Feature release. All changes are backward-compatible. + +- Introduces a [binary value](../../features/binary_values.md) type that is read from and written to + [CBOR](../../features/binary_formats/cbor.md), [BSON](../../features/binary_formats/bson.md), and + [MessagePack](../../features/binary_formats/messagepack.md), and can be shared between formats. +- Generalizes the input adapters to read from any `LegacyInputIterator` container (3–10 % faster + parsing). +- Fixes [`contains`](../../api/basic_json/contains.md) for JSON pointers and makes the binary + [`from_cbor`](../../api/basic_json/from_cbor.md)/[`from_msgpack`](../../api/basic_json/from_msgpack.md)/etc. + functions respect `allow_exceptions`. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.8.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.9.0.md b/docs/mkdocs/docs/home/releases/v3.9.0.md new file mode 100644 index 000000000..c04668c62 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.9.0.md @@ -0,0 +1,20 @@ +# v3.9.0 + +Released on 2020-07-27. + +Feature release adding four long-requested features. All changes are backward-compatible. + +- Optional [comment](../../features/comments.md) parsing in [`parse`](../../api/basic_json/parse.md) via the + `ignore_comments` parameter. +- [`ordered_json`](../../api/ordered_json.md) to preserve the [insertion order](../../features/object_order.md) + of object keys. +- An option to switch off [implicit conversions](../../api/macros/json_use_implicit_conversions.md). +- The [`NLOHMANN_DEFINE_TYPE_*`](../../features/arbitrary_types.md#simplify-your-life-with-macros) + convenience macros, plus high-precision-number support for + [UBJSON](../../features/binary_formats/ubjson.md) and CBOR tag handling. + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.9.0). + +## API changes + + diff --git a/docs/mkdocs/docs/home/releases/v3.9.1.md b/docs/mkdocs/docs/home/releases/v3.9.1.md new file mode 100644 index 000000000..a06f819d7 --- /dev/null +++ b/docs/mkdocs/docs/home/releases/v3.9.1.md @@ -0,0 +1,14 @@ +# v3.9.1 + +Released on 2020-08-06. + +Fixes two regressions from 3.9.0. All changes are backward-compatible. + +- Accepts consecutive [comments](../../features/comments.md) and completes the + [`ordered_json`](../../api/ordered_json.md) interface (e.g. `ordered_json::parse`). + +[Full release notes](https://github.com/nlohmann/json/releases/tag/v3.9.1). + +## API changes + + diff --git a/docs/mkdocs/docs/images/range-begin-end.svg b/docs/mkdocs/docs/images/range-begin-end.svg index 8e2b2fb6c..99c9f8662 100644 --- a/docs/mkdocs/docs/images/range-begin-end.svg +++ b/docs/mkdocs/docs/images/range-begin-end.svg @@ -16,6 +16,18 @@ version="1.1" inkscape:version="0.48.4 r9939" sodipodi:docname="range-begin-end.svg"> + + ` holds the user-defined string literals on their own, and +`` adds zero-copy views and editable documents. See +[header-only integration](integration/index.md) for details. It is heavily unit-tested with 100% code coverage, checked with Valgrind and the Clang Sanitizers for memory leaks, and continuously fuzz-tested by Google OSS-Fuzz. @@ -58,7 +60,8 @@ Conda, and more), and pkg-config. --- - Creating, parsing, accessing, and serializing JSON values, JSON Pointer/Patch, binary formats, and more. + Creating, parsing, accessing, and serializing JSON values, JSON Pointer/Patch, binary formats, zero-copy views, + and more. [:octicons-arrow-right-24: Features](features/index.md) @@ -74,7 +77,8 @@ Conda, and more), and pkg-config. --- - The complete reference for `basic_json` and its member functions, types, and related classes. + The complete reference for `basic_json` and its member functions, types, and related classes, as well as for + `json_document` and `json_view`. [:octicons-arrow-right-24: API documentation](api/basic_json/index.md) @@ -92,7 +96,7 @@ Conda, and more), and pkg-config. What changed in each release, with links to the relevant documentation. - [:octicons-arrow-right-24: Releases](home/releases.md) + [:octicons-arrow-right-24: Releases](home/releases/index.md) - :octicons-people-24:{ .lg .middle } __Community__ @@ -108,7 +112,7 @@ Conda, and more), and pkg-config. This documentation is built from the `develop` branch and may describe changes that are not part of a release yet. Their version numbers are followed by an unreleased badge; see - [Releases](home/releases.md) for what shipped in each version. + [Releases](home/releases/index.md) for what shipped in each version. The library is licensed under the [MIT License](home/license.md). The source code, issue tracker, and discussions are on [GitHub](https://github.com/nlohmann/json). diff --git a/docs/mkdocs/docs/integration/index.md b/docs/mkdocs/docs/integration/index.md index f022ec6ed..f6f303142 100644 --- a/docs/mkdocs/docs/integration/index.md +++ b/docs/mkdocs/docs/integration/index.md @@ -54,3 +54,12 @@ For the read-only, non-owning [`basic_json_document`](../api/basic_json_document types, additionally include [`single_include/nlohmann/json_view.hpp`](https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json_view.hpp); see [Zero-copy JSON views](../features/json_view.md). + +## See also + +- [CMake](cmake.md) - integrate the library into a CMake project +- [Package managers](package_managers.md) +- [pkg-config](pkg-config.md) +- [Compile times](compile_times.md) - reduce the compile time overhead +- [Migration guide](migration_guide.md) - upgrade from an earlier version +- [Releases](../home/releases/index.md) diff --git a/docs/mkdocs/hooks/api_changes.py b/docs/mkdocs/hooks/api_changes.py new file mode 100644 index 000000000..94073f534 --- /dev/null +++ b/docs/mkdocs/hooks/api_changes.py @@ -0,0 +1,163 @@ +"""Generate the "API changes" section of the release pages from the version histories of the API pages.""" + +# Every page below docs/api ends in a "Version history" section ("Added in version 3.8.0.", "... since version 3.13.0."). +# This hook collects these entries and replaces the `` marker of each page in home/releases with +# the pages that were added in that release and the changes made to existing pages. The page of a release is named +# after its version (v3.12.0.md); unreleased.md collects all versions newer than the latest release. The result is an +# overview of what changed in the API, not a change log. + +import logging +import os +import posixpath +import re + +log = logging.getLogger("mkdocs.hooks.api_changes") + +_DIR = "home/releases" +_PAGE_VERSION = re.compile(r"v(\d+)\.(\d+)\.(\d+)\.md") +_MARKER = "" +_HISTORY = re.compile(r"^## Version history\n(.*?)(?=^## |\Z)", re.DOTALL | re.MULTILINE) +_TITLE = re.compile(r"^# (.+)$", re.MULTILINE) +_ITEM = re.compile(r"^(?:[-*]|\d+\.)\s+") +_SENTENCE = re.compile(r"(?<=\.)\s+(?=[A-Z`\[])") +_VERSION = re.compile(r"\b(?:version|since|in)\s+(\d+)\.(\d+)\.(\d+)\b", re.IGNORECASE) +_FUTURE = re.compile(r"\bwill\b|\bplanned\b", re.IGNORECASE) # statements about versions that do not exist yet +_NEXT_MAJOR = (4, 0, 0) # versions from here on are announcements, not changes +_ADDED = re.compile(r"^(?:Added|Introduced)\b.*\bversion \d|^Since version \d+\.\d+\.\d+\.?$", re.IGNORECASE) +_LINK = re.compile(r"(\]\()([^)\s]+)(\))") + + +def on_page_markdown(markdown, *, page, config, files): + uri = page.file.src_uri + if posixpath.dirname(uri) != _DIR or _MARKER not in markdown: + return markdown + docs_dir = config["docs_dir"] + entries = _collect(docs_dir) + released = sorted(tuple(map(int, m.groups())) for m in + (_PAGE_VERSION.fullmatch(name) for name in os.listdir(os.path.join(docs_dir, _DIR))) if m) + match = _PAGE_VERSION.fullmatch(posixpath.basename(uri)) + if match: + versions = [tuple(map(int, match.groups()))] + else: # unreleased.md + versions = sorted((v for v in entries if not released or v > released[-1]), reverse=True) + return markdown.replace(_MARKER, _render(uri, [(v, entries[v]) for v in versions if v in entries])) + + +def _title(text, path): + match = _TITLE.search(text) + title = match.group(1) if match else os.path.splitext(os.path.basename(path))[0] + title = re.sub(r"", "", title) + title = title.replace("`", "").replace("\\", "").strip() + return title[len("nlohmann::"):] if title.startswith("nlohmann::") else title + + +def _collect(docs_dir): + """Return {version: {"added": [(title, uri)], "changed": [(title, uri, text)]}}.""" + result = {} + api = os.path.join(docs_dir, "api") + for root, _, names in sorted(os.walk(api)): + for name in sorted(names): + if not name.endswith(".md"): + continue + path = os.path.join(root, name) + uri = posixpath.relpath(path.replace(os.sep, "/"), docs_dir.replace(os.sep, "/")) + with open(path, encoding="utf-8") as source: + text = source.read() + history = _HISTORY.search(text) + if not history: + continue + title = _title(text, path) + found = _sentences(history.group(1)) + # a page is added in the earliest version named by an "Added in" sentence that opens an entry (an entry of + # a numbered list describes one overload; later unnumbered entries extend an existing page) + starts = [v for i, n, first, v, sentence in found if first and _ADDED.match(sentence) and (i == 0 or n)] + added = min(starts) if starts else None + page_added = False + for index, number, first, version, sentence in found: + if version >= _NEXT_MAJOR: + continue + bucket = result.setdefault(version, {"added": [], "changed": []}) + opens = first and _ADDED.match(sentence) and (index == 0 or number) + if opens and version == added: + if not page_added: + bucket["added"].append((title, uri)) + page_added = True + continue + if number and re.fullmatch(r"Added in version [\d.]+\.?", sentence): + sentence = f"Overload {number} added." + elif number: + sentence = f"Overload {number}: {sentence}" + if (title, uri, sentence) not in bucket["changed"]: + bucket["changed"].append((title, uri, sentence)) + return result + + +def _sentences(section): + """Yield (entry index, overload number, opens entry, version, sentence) for every sentence naming a version.""" + found = [] + for index, (number, entry) in enumerate(_entries(section)): + for position, sentence in enumerate(_SENTENCE.split(entry)): + version = _VERSION.search(sentence) + if not version or _FUTURE.search(sentence): + continue + found.append((index, number, position == 0, tuple(int(part) for part in version.groups()), sentence.strip())) + return found + + +def _entries(section): + entries = [] + for line in section.split("\n"): + item = _ITEM.match(line) + if item: + number = re.match(r"\d+", line) + entries.append([number.group(0) if number else None, _ITEM.sub("", line).strip()]) + elif line.strip() and entries and line[0].isspace(): + entries[-1][1] += " " + line.strip() # wrapped list item + elif line.strip(): + break # text after the list, such as an admonition + return [tuple(entry) for entry in entries] + + +def _relink(text, uri, page): + """Rewrite links relative to the API page `uri` so they work from the generated page.""" + def fix(match): + target = match.group(2) + if re.match(r"[a-z]+:|#|/", target): + return match.group(0) + resolved = posixpath.normpath(posixpath.join(posixpath.dirname(uri), target)) + return match.group(1) + posixpath.relpath(resolved, posixpath.dirname(page)) + match.group(3) + return _LINK.sub(fix, text) + + +def _link(title, uri, page): + return f"[`{title}`]({posixpath.relpath(uri, posixpath.dirname(page))})" + + +def _added(pages, page): + """Group the added pages by class: `basic_json`: [`get`](..), ...; pages without a class come first.""" + groups, single = {}, [] + for title, uri in pages: + directory = posixpath.dirname(uri) + if directory == "api": + single.append(_link(title, uri, page)) + elif posixpath.basename(uri) == "index.md": + groups.setdefault(directory, []).insert(0, f"[(overview)]({posixpath.relpath(uri, posixpath.dirname(page))})") + else: + groups.setdefault(directory, []).append(_link(title.split("::")[-1], uri, page)) + lines = [f"- {item}" for item in single] + for directory, items in groups.items(): + lines.append(f"- `{posixpath.basename(directory)}`: " + ", ".join(items)) + return lines + + +def _render(page, entries): + lines = [] + for version, bucket in entries: + if len(entries) > 1: + lines += [f"### Version {'.'.join(map(str, version))}", ""] + if bucket["added"]: + lines += [f"**Added ({len(bucket['added'])} pages):**", ""] + _added(bucket["added"], page) + [""] + if bucket["changed"]: + lines += ["**Changed:**", ""] + lines += [f"- {_link(t, u, page)}: {_relink(x, u, page)}" for t, u, x in bucket["changed"]] + [""] + return "\n".join(lines) if lines else "No documented API changes." diff --git a/docs/mkdocs/mkdocs.yml b/docs/mkdocs/mkdocs.yml index 27c46aa5a..fb08005eb 100644 --- a/docs/mkdocs/mkdocs.yml +++ b/docs/mkdocs/mkdocs.yml @@ -51,7 +51,53 @@ nav: - home/license.md - "FAQ": home/faq.md - home/exceptions.md - - home/releases.md + - Releases: + - 'Overview': home/releases/index.md + - 'Unreleased': home/releases/unreleased.md + - 'v3.12.0': home/releases/v3.12.0.md + - 'v3.11.3': home/releases/v3.11.3.md + - 'v3.11.2': home/releases/v3.11.2.md + - 'v3.11.1': home/releases/v3.11.1.md + - 'v3.11.0': home/releases/v3.11.0.md + - 'v3.10.5': home/releases/v3.10.5.md + - 'v3.10.4': home/releases/v3.10.4.md + - 'v3.10.3': home/releases/v3.10.3.md + - 'v3.10.2': home/releases/v3.10.2.md + - 'v3.10.1': home/releases/v3.10.1.md + - 'v3.10.0': home/releases/v3.10.0.md + - 'v3.9.1': home/releases/v3.9.1.md + - 'v3.9.0': home/releases/v3.9.0.md + - 'v3.8.0': home/releases/v3.8.0.md + - 'v3.7.3': home/releases/v3.7.3.md + - 'v3.7.2': home/releases/v3.7.2.md + - 'v3.7.1': home/releases/v3.7.1.md + - 'v3.7.0': home/releases/v3.7.0.md + - 'v3.6.1': home/releases/v3.6.1.md + - 'v3.6.0': home/releases/v3.6.0.md + - 'v3.5.0': home/releases/v3.5.0.md + - 'v3.4.0': home/releases/v3.4.0.md + - 'v3.3.0': home/releases/v3.3.0.md + - 'v3.2.0': home/releases/v3.2.0.md + - 'v3.1.2': home/releases/v3.1.2.md + - 'v3.1.1': home/releases/v3.1.1.md + - 'v3.1.0': home/releases/v3.1.0.md + - 'v3.0.1': home/releases/v3.0.1.md + - 'v3.0.0': home/releases/v3.0.0.md + - 'v2.1.1': home/releases/v2.1.1.md + - 'v2.1.0': home/releases/v2.1.0.md + - 'v2.0.10': home/releases/v2.0.10.md + - 'v2.0.9': home/releases/v2.0.9.md + - 'v2.0.8': home/releases/v2.0.8.md + - 'v2.0.7': home/releases/v2.0.7.md + - 'v2.0.6': home/releases/v2.0.6.md + - 'v2.0.5': home/releases/v2.0.5.md + - 'v2.0.4': home/releases/v2.0.4.md + - 'v2.0.3': home/releases/v2.0.3.md + - 'v2.0.2': home/releases/v2.0.2.md + - 'v2.0.1': home/releases/v2.0.1.md + - 'v2.0.0': home/releases/v2.0.0.md + - 'v1.1.0': home/releases/v1.1.0.md + - 'v1.0.0': home/releases/v1.0.0.md - home/design_goals.md - home/architecture.md - home/customers.md @@ -485,6 +531,7 @@ validation: unrecognized_links: warn hooks: + - hooks/api_changes.py - hooks/copy_markdown_source.py - hooks/unreleased_versions.py diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 5cb123c59..51f26706c 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -8,7 +8,7 @@ #pragma once -#include // transform +#include // min, transform #include // array #include // forward_list #include // inserter, front_inserter, end @@ -69,12 +69,10 @@ void from_json(const BasicJsonType& j, std::optional& opt) } #endif // JSON_HAS_CPP_17 -// overloads for basic_json template parameters -template < typename BasicJsonType, typename ArithmeticType, - enable_if_t < std::is_arithmetic::value&& - !std::is_same::value, - int > = 0 > -void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) +// read a number value of any of the three number types into val; +// returns false (leaving val untouched) if j is not a number +template +bool get_number_value(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast(j)) { @@ -85,17 +83,17 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) const auto* ptr = j.template get_ptr(); JSON_ASSERT(ptr != nullptr); val = static_cast(*ptr); - break; + return true; } case value_t::number_integer: { val = static_cast(*j.template get_ptr()); - break; + return true; } case value_t::number_float: { val = static_cast(*j.template get_ptr()); - break; + return true; } case value_t::null: @@ -106,7 +104,20 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) case value_t::binary: case value_t::discarded: default: - throw_type_must_be("number", j); + return false; + } +} + +// overloads for basic_json template parameters +template < typename BasicJsonType, typename ArithmeticType, + enable_if_t < std::is_arithmetic::value&& + !std::is_same::value, + int > = 0 > +void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) +{ + if (JSON_HEDLEY_UNLIKELY(!get_number_value(j, val))) + { + throw_type_must_be("number", j); } } @@ -245,29 +256,44 @@ inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType: arr = *j.template get_ptr(); } +// fill a std::array from the JSON array j: the size is checked once and the +// elements are read in order, so a short array converts the elements it has and +// then throws the out_of_range error at() would throw for the first missing one +template +void from_json_std_array(const BasicJsonType& j, std::array& arr) +{ + const auto& inner_array = *j.template get_ptr(); + const std::size_t size = (std::min)(N, inner_array.size()); + for (std::size_t i = 0; i < size; ++i) + { + arr[i] = inner_array[i].template get(); + } + if (JSON_HEDLEY_UNLIKELY(size < N)) + { + static_cast(j.at(size)); + } +} + template auto from_json_array_impl(const BasicJsonType& j, std::array& arr, priority_tag<2> /*unused*/) -> decltype(j.template get(), void()) { - for (std::size_t i = 0; i < N; ++i) - { - arr[i] = j.at(i).template get(); - } + from_json_std_array(j, arr); } -// reserve() is called through this pair (modeled on from_json_object_reserve) -// so from_json_array_impl below has a single body for both ConstructibleArrayType -// that support reserve() and those that don't. -template -auto from_json_array_reserve(ConstructibleArrayType& arr, typename ConstructibleArrayType::size_type size, priority_tag<1> /*unused*/) --> decltype(arr.reserve(size), void()) +// reserve() is called through this pair (used for arrays and objects alike) +// so from_json_array_impl and the object from_json have a single body for both +// containers that support reserve() and those that don't. +template +auto reserve_if_possible(ConstructibleContainerType& c, typename ConstructibleContainerType::size_type size, priority_tag<1> /*unused*/) +-> decltype(c.reserve(size), void()) { - arr.reserve(size); + c.reserve(size); } -template -void from_json_array_reserve(ConstructibleArrayType& /*arr*/, std::size_t /*size*/, priority_tag<0> /*unused*/) +template +void reserve_if_possible(ConstructibleContainerType& /*c*/, std::size_t /*size*/, priority_tag<0> /*unused*/) {} // append through push_back() where available; containers without it (such as @@ -297,7 +323,7 @@ auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, p { ConstructibleArrayType ret; const auto* inner_array = j.template get_ptr(); - from_json_array_reserve(ret, inner_array->size(), priority_tag<1> {}); + reserve_if_possible(ret, inner_array->size(), priority_tag<1> {}); for (const auto& elem : *inner_array) { // get() returns *this, this won't call a from_json @@ -336,6 +362,37 @@ std::array from_json_inplace_array_impl(const BasicJsonType& return { { j.at(Idx).template get()... } }; } +// std::is_trivial is deprecated in C++26 (see binary_writer::to_char_type); +// a struct rather than an alias, as astyle indents an #ifdef block of aliases +template +struct is_trivial_element +#ifdef JSON_HAS_CPP_26 + : conjunction, std::is_trivially_default_constructible> +#else + : std::is_trivial +#endif +{}; + +// the element type may not be default constructible: construct every element +// in place (a pack expansion that is unrolled N times) +template +std::array from_json_make_array(const BasicJsonType& j, identity_tag> tag, + std::false_type /*is_trivial_element*/) +{ + return from_json_inplace_array_impl(j, tag, make_index_sequence {}); +} + +// a trivial element type cannot tell default construction plus assignment from +// construction in place, so use a loop instead of unrolling N conversions +template +std::array from_json_make_array(const BasicJsonType& j, identity_tag> /*unused*/, + std::true_type /*is_trivial_element*/) +{ + std::array arr; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init): every element is assigned or an exception is thrown + from_json_std_array(j, arr); + return arr; +} + template < typename BasicJsonType, typename T, std::size_t N > auto from_json(const BasicJsonType& j, identity_tag> tag) -> decltype(from_json_inplace_array_impl(j, tag, make_index_sequence {})) @@ -345,7 +402,7 @@ auto from_json(const BasicJsonType& j, identity_tag> tag) throw_type_must_be("array", j); } - return from_json_inplace_array_impl(j, tag, make_index_sequence {}); + return from_json_make_array(j, tag, is_trivial_element {}); } template @@ -378,17 +435,6 @@ inline void from_json(const BasicJsonType& j, CompatibleArrayType& bin) } } -template -auto from_json_object_reserve(ConstructibleObjectType& obj, typename ConstructibleObjectType::size_type size, priority_tag<1> /*unused*/) --> decltype(obj.reserve(size), void()) -{ - obj.reserve(size); -} - -template -inline void from_json_object_reserve(ConstructibleObjectType& /*obj*/, std::size_t /*size*/, priority_tag<0> /*unused*/) -{} - // the keys of the source are sorted for the usual object types, so hinting the // end makes every insertion constant-time (for containers that order their keys // the same way); containers without emplace_hint() (such as ordered_map) fall @@ -417,7 +463,7 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) ConstructibleObjectType ret; const auto* inner_object = j.template get_ptr(); - from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {}); + reserve_if_possible(ret, inner_object->size(), priority_tag<1> {}); for (const auto& p : *inner_object) { from_json_object_emplace(ret, p.first, p.second.template get(), priority_tag<1> {}); @@ -437,42 +483,17 @@ template < typename BasicJsonType, typename ArithmeticType, int > = 0 > inline void from_json(const BasicJsonType& j, ArithmeticType& val) { - switch (static_cast(j)) + if (get_number_value(j, val)) { - case value_t::number_unsigned: - { - // never null: a basic_json whose number_unsigned_t is number_integer_t - // stores no number_unsigned values (#2573) - const auto* ptr = j.template get_ptr(); - JSON_ASSERT(ptr != nullptr); - val = static_cast(*ptr); - break; - } - case value_t::number_integer: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::number_float: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::boolean: - { - val = static_cast(*j.template get_ptr()); - break; - } - - case value_t::null: - case value_t::object: - case value_t::array: - case value_t::string: - case value_t::binary: - case value_t::discarded: - default: - throw_type_must_be("number", j); + return; } + + // unlike get_arithmetic_value(), a boolean is accepted here + if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) + { + throw_type_must_be("number", j); + } + val = static_cast(*j.template get_ptr()); } template diff --git a/include/nlohmann/detail/exceptions.hpp b/include/nlohmann/detail/exceptions.hpp index 7d7f4e038..cdef3019f 100644 --- a/include/nlohmann/detail/exceptions.hpp +++ b/include/nlohmann/detail/exceptions.hpp @@ -66,6 +66,7 @@ enum class exception_id : int invalid_string_or_size = 113, ///< a binary input contains an invalid string or size specification bson_unsupported_type = 114, ///< a BSON record type is not supported invalid_high_precision_number = 115, ///< a UBJSON/BJData high-precision number cannot be parsed + invalid_document_image = 116, ///< json_document::load() rejects an image // invalid_iterator iterators_incompatible = 201, ///< the iterators of a range belong to different values iterator_from_other_value = 202, ///< an iterator does not belong to the value it is used with @@ -100,6 +101,8 @@ enum class exception_id : int invalid_utf8 = 316, ///< dump() finds a string that is not valid UTF-8 type_not_serializable = 317, ///< a value cannot be serialized to the requested binary format enum_key_duplicate = 318, ///< an enum-keyed map has two keys that serialize to the same string + document_binary_value = 319, ///< a binary value is stored in a json_document + document_image_unsupported = 320, ///< a json_document image cannot be saved or loaded (discarded document, big-endian target) discarded_value_used = 321, ///< a discarded value is used // out_of_range array_index_out_of_range = 401, ///< an array index is out of range @@ -117,6 +120,7 @@ enum class exception_id : int patch_remove_parent_not_container = 413, ///< the parent of a JSON Patch 'remove' target is not a container patch_move_into_child = 414, ///< a JSON Patch 'move' moves a value into one of its children subtype_out_of_range = 415, ///< a binary subtype does not fit into one byte + document_too_large = 416, ///< a json_document input, its edit storage, or an image reaches the 32-bit (4 GiB) limit // other_error internal_error = 500, ///< unreachable code was reached patch_test_failed = 501, ///< a JSON Patch 'test' operation failed @@ -146,6 +150,13 @@ class exception : public std::exception return concat("[json.exception.", ename, '.', std::to_string(id_), "] "); } + /// build the message "[json.exception..] " + template + static std::string what_impl(const std::string& ename, int id_, const std::string& what_arg, BasicJsonContext context) + { + return concat(name(ename, id_), diagnostics(context), what_arg); + } + static std::string diagnostics(std::nullptr_t /*leaf_element*/) { return ""; @@ -158,48 +169,33 @@ class exception : public std::exception std::vector tokens; for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent) { - switch (current->m_parent->type()) + const auto* parent = current->m_parent; + if (parent->is_array()) { - case value_t::array: + for (std::size_t i = 0; i < parent->m_data.m_value.array->size(); ++i) { - for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i) + if (&parent->m_data.m_value.array->operator[](i) == current) { - if (¤t->m_parent->m_data.m_value.array->operator[](i) == current) - { - tokens.emplace_back(std::to_string(i)); - break; - } + tokens.emplace_back(std::to_string(i)); + break; } - break; } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_data.m_value.object) - { - if (&element.second == current) - { - // data() is null-terminated, so a key containing - // a null byte is cut short here rather than - // truncating the whole message at what() - tokens.emplace_back(element.first.data()); - break; - } - } - break; - } - - case value_t::null: // LCOV_EXCL_LINE - case value_t::string: // LCOV_EXCL_LINE - case value_t::boolean: // LCOV_EXCL_LINE - case value_t::number_integer: // LCOV_EXCL_LINE - case value_t::number_unsigned: // LCOV_EXCL_LINE - case value_t::number_float: // LCOV_EXCL_LINE - case value_t::binary: // LCOV_EXCL_LINE - case value_t::discarded: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - break; // LCOV_EXCL_LINE } + else if (parent->is_object()) + { + for (const auto& element : *parent->m_data.m_value.object) + { + if (&element.second == current) + { + // data() is null-terminated, so a key containing + // a null byte is cut short here rather than + // truncating the whole message at what() + tokens.emplace_back(element.first.data()); + break; + } + } + } + // all other parent types (primitives) contribute no token } if (tokens.empty()) @@ -315,7 +311,7 @@ class invalid_iterator : public exception template::value, int> = 0> static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context) { - const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg); + const std::string w = exception::what_impl("invalid_iterator", id_, what_arg, context); return {id_, w.c_str()}; } @@ -339,7 +335,7 @@ class type_error : public exception template::value, int> = 0> static type_error create(int id_, const std::string& what_arg, BasicJsonContext context) { - const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg); + const std::string w = exception::what_impl("type_error", id_, what_arg, context); return {id_, w.c_str()}; } @@ -362,7 +358,7 @@ class out_of_range : public exception template::value, int> = 0> static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context) { - const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg); + const std::string w = exception::what_impl("out_of_range", id_, what_arg, context); return {id_, w.c_str()}; } @@ -385,7 +381,7 @@ class other_error : public exception template::value, int> = 0> static other_error create(int id_, const std::string& what_arg, BasicJsonContext context) { - const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg); + const std::string w = exception::what_impl("other_error", id_, what_arg, context); return {id_, w.c_str()}; } diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index 2ba96e8ec..e2b0ae774 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -331,6 +331,26 @@ class binary_reader return is_object ? sax->end_object() : sax->end_array(); } + /*! + @brief close the innermost open container and report whether that + completed the value being read + + The value begun by the driver is complete once its container is not + inside another one. + + @param[out] value_complete whether no container is left open + @return whether the SAX parser accepted the end event + */ + bool leave_container_and_check(bool& value_complete) + { + if (JSON_HEDLEY_UNLIKELY(!leave_container())) + { + return false; + } + value_complete = container_stack.empty(); + return true; + } + ////////// // BSON // ////////// @@ -1268,6 +1288,34 @@ class binary_reader return last_byte_error(exception_id::invalid_string_or_size, concat("indefinite-length ", type_name, " is not allowed inside indefinite-length ", type_name, "; last byte: 0x", get_token_string()), context); } + /*! + @brief report an object key that is not a string + + @param[in] found description of what was found instead of a string key + @return false + */ + bool unsupported_key_type(const char* found) + { + return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + } + + /*! + @brief read the length of a CBOR string or byte array and then its bytes + + @tparam NumberType the type of the length field + @tparam ContainerType the destination container (string_t or binary_t) + @param[in] context further context information (for diagnostics) + @param[out] result container the bytes are appended to + + @return whether reading completed + */ + template + bool get_cbor_sized_bytes(const char* context, ContainerType& result) + { + NumberType len{}; + return get_number(len) && get_bytes(len, context, result); + } + /*! @brief reads a definite-length CBOR string @@ -1318,26 +1366,22 @@ class binary_reader case 0x78: // UTF-8 string (one-byte uint8_t for n follows) { - std::uint8_t len{}; - return get_number(len) && get_string(len, result); + return get_cbor_sized_bytes("string", result); } case 0x79: // UTF-8 string (two-byte uint16_t for n follow) { - std::uint16_t len{}; - return get_number(len) && get_string(len, result); + return get_cbor_sized_bytes("string", result); } case 0x7A: // UTF-8 string (four-byte uint32_t for n follow) { - std::uint32_t len{}; - return get_number(len) && get_string(len, result); + return get_cbor_sized_bytes("string", result); } case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow) { - std::uint64_t len{}; - return get_number(len) && get_string(len, result); + return get_cbor_sized_bytes("string", result); } default: @@ -1476,7 +1520,7 @@ class binary_reader // a key that begins an item is skipped with its value when recovering // (see resync); a break stop code or a reserved byte begins none skip_requested = is_cbor_item_head(static_cast(current)); - return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + return unsupported_key_type(found); } /*! @@ -1529,30 +1573,22 @@ class binary_reader case 0x58: // Binary data (one-byte uint8_t for n follows) { - std::uint8_t len{}; - return get_number(len) && - get_binary(len, result); + return get_cbor_sized_bytes("binary", result); } case 0x59: // Binary data (two-byte uint16_t for n follow) { - std::uint16_t len{}; - return get_number(len) && - get_binary(len, result); + return get_cbor_sized_bytes("binary", result); } case 0x5A: // Binary data (four-byte uint32_t for n follow) { - std::uint32_t len{}; - return get_number(len) && - get_binary(len, result); + return get_cbor_sized_bytes("binary", result); } case 0x5B: // Binary data (eight-byte uint64_t for n follow) { - std::uint64_t len{}; - return get_number(len) && - get_binary(len, result); + return get_cbor_sized_bytes("binary", result); } default: @@ -1767,12 +1803,12 @@ class binary_reader if (at_end) { - if (JSON_HEDLEY_UNLIKELY(!leave_container())) + bool value_complete = false; + if (JSON_HEDLEY_UNLIKELY(!leave_container_and_check(value_complete))) { return false; } - // the value begun here is complete once its container is - if (container_stack.empty()) + if (value_complete) { return true; } @@ -2373,20 +2409,17 @@ class binary_reader case 0xD9: // str 8 { - std::uint8_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 0xDA: // str 16 { - std::uint16_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 0xDB: // str 32 { - std::uint32_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } default: @@ -2486,7 +2519,7 @@ class binary_reader // the key is skipped with its value when recovering (see resync) skip_requested = true; - return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + return unsupported_key_type(found); } /*! @@ -2631,12 +2664,12 @@ class binary_reader if (container_stack.back().remaining == 0) { - if (JSON_HEDLEY_UNLIKELY(!leave_container())) + bool value_complete = false; + if (JSON_HEDLEY_UNLIKELY(!leave_container_and_check(value_complete))) { return false; } - // the value begun here is complete once its container is - if (container_stack.empty()) + if (value_complete) { return true; } @@ -2981,8 +3014,7 @@ class binary_reader { case 'U': { - std::uint8_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 'i': @@ -3015,8 +3047,7 @@ class binary_reader { break; } - std::uint16_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 'm': @@ -3025,8 +3056,7 @@ class binary_reader { break; } - std::uint32_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 'M': @@ -3035,8 +3065,7 @@ class binary_reader { break; } - std::uint64_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } default: @@ -3942,12 +3971,12 @@ class binary_reader if (at_end) { - if (JSON_HEDLEY_UNLIKELY(!leave_container())) + bool value_complete = false; + if (JSON_HEDLEY_UNLIKELY(!leave_container_and_check(value_complete))) { return false; } - // the value begun here is complete once its container is - if (container_stack.empty()) + if (value_complete) { return true; } @@ -4833,6 +4862,22 @@ class binary_reader return true; } + /*! + @brief read a length of type @a NumberType, then a string of that many bytes + + @tparam NumberType the type of the length field + @param[out] result string the bytes are appended to + @param[in] context further context information (for diagnostics) + + @return whether reading and validating the string succeeded + */ + template + bool get_string_with_length(string_t& result, const char* context) + { + NumberType len{}; + return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + } + /*! @brief create a byte array by reading bytes from the input @@ -5334,37 +5379,7 @@ class binary_reader const std::string& context) const { std::string error_msg = "syntax error while parsing "; - - switch (input_format) - { - case input_format_t::cbor: - error_msg += "CBOR"; - break; - - case input_format_t::msgpack: - error_msg += "MessagePack"; - break; - - case input_format_t::ubjson: - error_msg += "UBJSON"; - break; - - case input_format_t::bson: - error_msg += "BSON"; - break; - - case input_format_t::bjdata: - error_msg += "BJData"; - break; - - case input_format_t::bon8: - error_msg += "BON8"; - break; - - case input_format_t::json: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } + error_msg += input_format_name(input_format); return concat(error_msg, ' ', context, ": ", detail); } diff --git a/include/nlohmann/detail/input/input_adapters.hpp b/include/nlohmann/detail/input/input_adapters.hpp index 63266b6f4..b1b92256c 100644 --- a/include/nlohmann/detail/input/input_adapters.hpp +++ b/include/nlohmann/detail/input/input_adapters.hpp @@ -67,6 +67,36 @@ namespace detail /// the supported input formats enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata, bon8 }; +/// @return the name of a binary input format, as used in exception messages +inline const char* input_format_name(const input_format_t format) noexcept +{ + switch (format) + { + case input_format_t::cbor: + return "CBOR"; + case input_format_t::msgpack: + return "MessagePack"; + case input_format_t::ubjson: + return "UBJSON"; + case input_format_t::bson: + return "BSON"; + case input_format_t::bjdata: + return "BJData"; + case input_format_t::bon8: + return "BON8"; + case input_format_t::json: // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE + return ""; // LCOV_EXCL_LINE + } +} + +/// @return the message of the parse_error thrown or reported for an empty input +inline const char* empty_input_message() noexcept +{ + return "attempting to parse an empty input; check that your input string or stream contains the expected JSON"; +} + //////////////////// // input adapters // //////////////////// @@ -1273,7 +1303,7 @@ inline file_input_adapter input_adapter(std::FILE* file) { if (file == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } return file_input_adapter(file); } @@ -1282,7 +1312,7 @@ inline input_stream_adapter input_adapter(std::istream& stream) { if (stream.rdbuf() == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } return input_stream_adapter(stream); } @@ -1307,7 +1337,7 @@ contiguous_bytes_input_adapter input_adapter(CharT b) { if (b == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } auto length = std::strlen(reinterpret_cast(b)); const auto* ptr = reinterpret_cast(b); diff --git a/include/nlohmann/detail/input/json_sax.hpp b/include/nlohmann/detail/input/json_sax.hpp index 23df9925c..febf37f9c 100644 --- a/include/nlohmann/detail/input/json_sax.hpp +++ b/include/nlohmann/detail/input/json_sax.hpp @@ -323,6 +323,52 @@ struct diagnostic_positions }; #endif +/*! +@brief error state shared by the DOM-building SAX parsers + +Holds whether a parse error occurred and whether errors are thrown, and +implements the parse_error() and is_errored() members of the SAX interface for +json_sax_dom_parser and json_sax_dom_callback_parser. +*/ +class json_sax_dom_error_state +{ + public: + explicit json_sax_dom_error_state(const bool allow_exceptions_) noexcept + : allow_exceptions(allow_exceptions_) + {} + + // make class move-only + json_sax_dom_error_state(const json_sax_dom_error_state&) = delete; + json_sax_dom_error_state(json_sax_dom_error_state&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) + json_sax_dom_error_state& operator=(const json_sax_dom_error_state&) = delete; + json_sax_dom_error_state& operator=(json_sax_dom_error_state&&) = delete; // the const member prevents assignment + ~json_sax_dom_error_state() = default; + + template + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, + const Exception& ex) + { + errored = true; + static_cast(ex); + if (allow_exceptions) + { + JSON_THROW(ex); + } + return false; + } + + bool is_errored() const + { + return errored; + } + + private: + /// whether a syntax error occurred + bool errored = false; + /// whether to throw exceptions in case of errors + const bool allow_exceptions = true; +}; + /*! @brief a hash table that finds the position of a key in an object whose elements are kept in a vector (ordered_map) @@ -434,7 +480,7 @@ read up to it. not use the value after a parse error) */ template -class json_sax_dom_parser +class json_sax_dom_parser : public json_sax_dom_error_state { public: using number_integer_t = typename BasicJsonType::number_integer_t; @@ -450,7 +496,7 @@ class json_sax_dom_parser @param[in] allow_exceptions_ whether parse errors yield exceptions */ explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr) - : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_) + : json_sax_dom_error_state(allow_exceptions_), root(r), m_lexer_ref(lexer_) {} // make class move-only @@ -601,24 +647,6 @@ class json_sax_dom_parser return true; } - template - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const Exception& ex) - { - errored = true; - static_cast(ex); - if (allow_exceptions) - { - JSON_THROW(ex); - } - return false; - } - - constexpr bool is_errored() const - { - return errored; - } - private: using object_t = typename BasicJsonType::object_t; using array_t = typename BasicJsonType::array_t; @@ -916,7 +944,7 @@ class json_sax_dom_parser /// directly if the value was the one too many. void place_container(const frame& f, frame* enclosing, BasicJsonType&& value, const bool enclosing_is_innermost) { - // a container without a slot of its own is collected for an enclosing one + // only a container with a slot may have no enclosing container JSON_ASSERT(f.slot != nullptr || enclosing != nullptr); if (f.slot != nullptr) { @@ -1334,10 +1362,6 @@ class json_sax_dom_parser /// the array that stores the elements of the innermost array as they /// arrive (frames.back().array), or nullptr array_t* stored_array = nullptr; - /// whether a syntax error occurred - bool errored = false; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; /// the lexer reference to obtain the current position lexer_t* m_lexer_ref = nullptr; }; @@ -1362,7 +1386,7 @@ constructor contains the parsed value. @tparam BasicJsonType the JSON type */ template -class json_sax_dom_binary_parser +class json_sax_dom_binary_parser : public json_sax_dom_error_state { public: using number_integer_t = typename BasicJsonType::number_integer_t; @@ -1378,7 +1402,7 @@ class json_sax_dom_binary_parser @param[in] allow_exceptions_ whether parse errors yield exceptions */ explicit json_sax_dom_binary_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr) - : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_) + : json_sax_dom_error_state(allow_exceptions_), root(r), m_lexer_ref(lexer_) {} // make class move-only @@ -1507,24 +1531,6 @@ class json_sax_dom_binary_parser return true; } - template - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const Exception& ex) - { - errored = true; - static_cast(ex); - if (allow_exceptions) - { - JSON_THROW(ex); - } - return false; - } - - constexpr bool is_errored() const - { - return errored; - } - private: /*! @@ -1578,16 +1584,12 @@ class json_sax_dom_binary_parser std::vector ref_stack {}; /// helper to hold the reference for the next object element BasicJsonType* object_element = nullptr; - /// whether a syntax error occurred - bool errored = false; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; /// the lexer reference to obtain the current position lexer_t* m_lexer_ref = nullptr; }; template -class json_sax_dom_callback_parser +class json_sax_dom_callback_parser : public json_sax_dom_error_state { public: using number_integer_t = typename BasicJsonType::number_integer_t; @@ -1603,7 +1605,7 @@ class json_sax_dom_callback_parser parser_callback_t cb, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr) - : root(r), callback(std::move(cb)), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_) + : json_sax_dom_error_state(allow_exceptions_), root(r), callback(std::move(cb)), m_lexer_ref(lexer_) { keep_stack.push_back(true); } @@ -1867,24 +1869,6 @@ class json_sax_dom_callback_parser return true; } - template - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const Exception& ex) - { - errored = true; - static_cast(ex); - if (allow_exceptions) - { - JSON_THROW(ex); - } - return false; - } - - constexpr bool is_errored() const - { - return errored; - } - private: /*! @@ -2151,12 +2135,8 @@ class json_sax_dom_callback_parser /// value is later rejected by the callback, instead of erasing the /// member entirely std::vector> duplicate_key_stash {}; - /// whether a syntax error occurred - bool errored = false; /// callback function const parser_callback_t callback = nullptr; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; /// a discarded value for the callback BasicJsonType discarded = BasicJsonType::value_t::discarded; /// the value of the key event for the callback (see key_value()) diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp index e2fe6ed33..6fba8f6cb 100644 --- a/include/nlohmann/detail/input/lexer.hpp +++ b/include/nlohmann/detail/input/lexer.hpp @@ -683,225 +683,74 @@ class lexer : public lexer_base // invalid control characters case 0x00: - { - error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x01: - { - error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x02: - { - error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x03: - { - error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x04: - { - error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x05: - { - error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x06: - { - error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x07: - { - error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x08: - { - error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x09: - { - error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0A: - { - error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0B: - { - error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0C: - { - error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0D: - { - error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0E: - { - error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0F: - { - error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x10: - { - error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x11: - { - error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x12: - { - error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x13: - { - error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x14: - { - error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x15: - { - error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x16: - { - error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x17: - { - error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x18: - { - error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x19: - { - error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1A: - { - error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1B: - { - error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1C: - { - error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1D: - { - error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1E: - { - error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1F: { - error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F"; + static const std::array control_character_messages = {{ + "invalid string: control character U+0000 (NUL) must be escaped to \\u0000", + "invalid string: control character U+0001 (SOH) must be escaped to \\u0001", + "invalid string: control character U+0002 (STX) must be escaped to \\u0002", + "invalid string: control character U+0003 (ETX) must be escaped to \\u0003", + "invalid string: control character U+0004 (EOT) must be escaped to \\u0004", + "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005", + "invalid string: control character U+0006 (ACK) must be escaped to \\u0006", + "invalid string: control character U+0007 (BEL) must be escaped to \\u0007", + "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b", + "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t", + "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n", + "invalid string: control character U+000B (VT) must be escaped to \\u000B", + "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f", + "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r", + "invalid string: control character U+000E (SO) must be escaped to \\u000E", + "invalid string: control character U+000F (SI) must be escaped to \\u000F", + "invalid string: control character U+0010 (DLE) must be escaped to \\u0010", + "invalid string: control character U+0011 (DC1) must be escaped to \\u0011", + "invalid string: control character U+0012 (DC2) must be escaped to \\u0012", + "invalid string: control character U+0013 (DC3) must be escaped to \\u0013", + "invalid string: control character U+0014 (DC4) must be escaped to \\u0014", + "invalid string: control character U+0015 (NAK) must be escaped to \\u0015", + "invalid string: control character U+0016 (SYN) must be escaped to \\u0016", + "invalid string: control character U+0017 (ETB) must be escaped to \\u0017", + "invalid string: control character U+0018 (CAN) must be escaped to \\u0018", + "invalid string: control character U+0019 (EM) must be escaped to \\u0019", + "invalid string: control character U+001A (SUB) must be escaped to \\u001A", + "invalid string: control character U+001B (ESC) must be escaped to \\u001B", + "invalid string: control character U+001C (FS) must be escaped to \\u001C", + "invalid string: control character U+001D (GS) must be escaped to \\u001D", + "invalid string: control character U+001E (RS) must be escaped to \\u001E", + "invalid string: control character U+001F (US) must be escaped to \\u001F" + } + }; + error_message = control_character_messages[static_cast(current)]; error_cause = error_kind::string; return token_type::parse_error; } diff --git a/include/nlohmann/detail/input/parser.hpp b/include/nlohmann/detail/input/parser.hpp index 340b21a84..73f84533d 100644 --- a/include/nlohmann/detail/input/parser.hpp +++ b/include/nlohmann/detail/input/parser.hpp @@ -236,6 +236,39 @@ class parser return m_lexer.get_string(); } + /*! + @brief apply the end-of-input handling after a value was parsed + + In strict mode, the next token must be the end of the input; otherwise + parse_error.101 is reported through @a sax. In non-strict mode, the + lookahead is released so the caller can keep reading the input right after + the parsed value. + + @param[in,out] sax the SAX parser to report an error to + @param[in] strict whether to expect the last token to be EOF + @return true, or what @a sax returned for the error + */ + template + bool finish_input(SAX& sax, const bool strict) + { + if (strict) + { + // strict mode: next byte must be EOF + if (get_token() != token_type::end_of_input) + { + return syntax_error(sax, exception_message(token_type::end_of_input, "value")); + } + } + else + { + // the caller keeps using the input: position it right after + // the value by leaving the character that terminated it + m_lexer.release_lookahead(); + } + + return true; + } + /*! @brief run a DOM SAX parser to completion and position the lexer @@ -255,20 +288,7 @@ class parser { sax_parse_internal(&sdp); - if (strict) - { - // in strict mode, input must be completely read - if (get_token() != token_type::end_of_input) - { - syntax_error(sdp, exception_message(token_type::end_of_input, "value")); - } - } - else - { - // the caller keeps using the input: position it right after - // the value by leaving the character that terminated it - m_lexer.release_lookahead(); - } + static_cast(finish_input(sdp, strict)); return !sdp.is_errored(); } @@ -493,7 +513,7 @@ class parser { // there is nothing to recover static_cast(report_error(sax, parse_error::create(exception_id::syntax_error, m_lexer.get_position(), - "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr), allow_recovery)); + empty_input_message(), nullptr), allow_recovery)); return false; } diff --git a/include/nlohmann/detail/input/string_scan.hpp b/include/nlohmann/detail/input/string_scan.hpp index abe39e475..d01a2f1a4 100644 --- a/include/nlohmann/detail/input/string_scan.hpp +++ b/include/nlohmann/detail/input/string_scan.hpp @@ -96,7 +96,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t find_string_special(const unsigned char* d // printable ASCII except '"' and '\\' - is emitted unchanged. Note this differs // from is_string_special() only in that 0x7F is also a stop (it is escaped as // \u007f under ensure_ascii). -inline bool is_ascii_copyable(unsigned char c) noexcept +constexpr bool is_ascii_copyable(unsigned char c) noexcept { return c >= 0x20u && c < 0x7Fu && c != '"' && c != '\\'; } diff --git a/include/nlohmann/detail/iterators/iter_impl.hpp b/include/nlohmann/detail/iterators/iter_impl.hpp index 120c1b893..84a850356 100644 --- a/include/nlohmann/detail/iterators/iter_impl.hpp +++ b/include/nlohmann/detail/iterators/iter_impl.hpp @@ -108,33 +108,18 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci { JSON_ASSERT(m_object != nullptr); - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - { - m_it.object_iterator = typename object_t::iterator(); - break; - } - - case value_t::array: - { - m_it.array_iterator = typename array_t::iterator(); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator = primitive_iterator_t(); - break; - } + m_it.object_iterator = typename object_t::iterator(); + } + else if (m_object->is_array()) + { + m_it.array_iterator = typename array_t::iterator(); + } + else + { + // primitive (is_primitive(), including discarded) + m_it.primitive_iterator = primitive_iterator_t(); } } @@ -249,33 +234,28 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci { JSON_ASSERT(m_object != nullptr); - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - { - m_it.object_iterator = m_object->m_data.m_value.object->end(); - break; - } + m_it.object_iterator = m_object->m_data.m_value.object->end(); + } + else if (m_object->is_array()) + { + m_it.array_iterator = m_object->m_data.m_value.array->end(); + } + else + { + // primitive (is_primitive(), including discarded) + m_it.primitive_iterator.set_end(); + } + } - case value_t::array: - { - m_it.array_iterator = m_object->m_data.m_value.array->end(); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator.set_end(); - break; - } + /// @throw invalid_iterator.212 if @a other points to another container + template + JSON_HEDLEY_ALWAYS_INLINE void check_same_container(const IterImpl& other) const + { + if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) + { + throw_literal(exception_id::iterators_compare_different_values, "cannot compare iterators of different containers", m_object); } } @@ -424,10 +404,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci bool operator==(const IterImpl& other) const { // if objects are not the same, the comparison is undefined - if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) - { - throw_literal(exception_id::iterators_compare_different_values, "cannot compare iterators of different containers", m_object); - } + check_same_container(other); // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493 if (m_object == nullptr) @@ -466,10 +443,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci bool operator<(const iter_impl& other) const { // if objects are not the same, the comparison is undefined - if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) - { - throw_literal(exception_id::iterators_compare_different_values, "cannot compare iterators of different containers", m_object); - } + check_same_container(other); // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493 if (m_object == nullptr) @@ -478,25 +452,18 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci return false; } - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - throw_literal(exception_id::iterator_order_on_object, "cannot compare order of object iterators", m_object); - - case value_t::array: - return (m_it.array_iterator < other.m_it.array_iterator); - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return (m_it.primitive_iterator < other.m_it.primitive_iterator); + throw_literal(exception_id::iterator_order_on_object, "cannot compare order of object iterators", m_object); } + + if (m_object->is_array()) + { + return (m_it.array_iterator < other.m_it.array_iterator); + } + + // primitive (is_primitive(), including discarded) + return (m_it.primitive_iterator < other.m_it.primitive_iterator); } /*! @@ -534,30 +501,19 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci { JSON_ASSERT(m_object != nullptr); - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - throw_literal(exception_id::iterator_arithmetic_on_object, "cannot use offsets with object iterators", m_object); + throw_literal(exception_id::iterator_arithmetic_on_object, "cannot use offsets with object iterators", m_object); + } - case value_t::array: - { - std::advance(m_it.array_iterator, i); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator += i; - break; - } + if (m_object->is_array()) + { + std::advance(m_it.array_iterator, i); + } + else + { + // primitive (is_primitive(), including discarded) + m_it.primitive_iterator += i; } return *this; @@ -613,25 +569,18 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci { JSON_ASSERT(m_object != nullptr); - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - throw_literal(exception_id::iterator_arithmetic_on_object, "cannot use offsets with object iterators", m_object); - - case value_t::array: - return m_it.array_iterator - other.m_it.array_iterator; - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return m_it.primitive_iterator - other.m_it.primitive_iterator; + throw_literal(exception_id::iterator_arithmetic_on_object, "cannot use offsets with object iterators", m_object); } + + if (m_object->is_array()) + { + return m_it.array_iterator - other.m_it.array_iterator; + } + + // primitive (is_primitive(), including discarded) + return m_it.primitive_iterator - other.m_it.primitive_iterator; } /*! diff --git a/include/nlohmann/detail/iterators/iteration_proxy.hpp b/include/nlohmann/detail/iterators/iteration_proxy.hpp index c66a5c0fd..23a9e3b0d 100644 --- a/include/nlohmann/detail/iterators/iteration_proxy.hpp +++ b/include/nlohmann/detail/iterators/iteration_proxy.hpp @@ -109,35 +109,25 @@ template class iteration_proxy_value { JSON_ASSERT(anchor.m_object != nullptr); - switch (anchor.m_object->type()) + if (anchor.m_object->is_array()) { // use integer array index as key - case value_t::array: + if (array_index != array_index_last) { - if (array_index != array_index_last) - { - int_to_string( array_index_str, array_index ); - array_index_last = array_index; - } - return array_index_str; + int_to_string( array_index_str, array_index ); + array_index_last = array_index; } - - // use key from the object - case value_t::object: - return anchor.key(); - - // use an empty key for all primitive types - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return empty_str; + return array_index_str; } + + if (anchor.m_object->is_object()) + { + // use key from the object + return anchor.key(); + } + + // use an empty key for all primitive types + return empty_str; } /// return value of the iterator diff --git a/include/nlohmann/detail/json_pointer.hpp b/include/nlohmann/detail/json_pointer.hpp index 50fe7f53b..4560714ab 100644 --- a/include/nlohmann/detail/json_pointer.hpp +++ b/include/nlohmann/detail/json_pointer.hpp @@ -38,6 +38,102 @@ NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { struct json_pointer_access; + +/*! +@brief result of parse_array_index() + +json_pointer::array_index() maps each value to the corresponding +parse_error/out_of_range exception; json_pointer::contains() and +get_checked_or_null(), which must not throw for an out-of-range or +unrepresentable index, and the json_view pointer resolution switch on it +directly instead. +*/ +enum class array_index_status +{ + ok, ///< @a s is a valid, representable array index + leading_zero, ///< @a s begins with '0' but has more than one character + not_a_number, ///< @a s is neither empty nor "-" and does not begin with a digit + unresolved, ///< @a s could not be converted to an integer + exceeds_size_type ///< @a s converts to an integer that exceeds SizeType +}; + +/*! +@brief convert a JSON Pointer reference token into an array index (RFC 6901, Sect. 4) + +@param[in] s reference token to be converted into an array index; its data() + must be NUL-terminated +@param[out] idx the integer representation of @a s if array_index_status::ok + is returned; left unchanged otherwise + +@return whether @a s is a valid array index, and if not, why + +@note this function never throws; json_pointer::array_index(), the callers that + must not throw, and the json_view pointer resolution build on it instead + of each re-implementing the RFC 6901 digit rules and the range check +@note force-inlined: as a free function template it is no longer inlined into + the pointer resolution, which makes JsonPointerAt up to 17% slower (#5891) +*/ +template +JSON_HEDLEY_ALWAYS_INLINE array_index_status parse_array_index(const StringType& s, SizeType& idx) noexcept +{ + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) + { + return array_index_status::leading_zero; + } + + // error condition (cf. RFC 6901, Sect. 4); this also covers single- + // character tokens, so "/x" and "/xy" fail alike; "-" and the empty + // token are left to the conversion below and are reported as unresolved + if (JSON_HEDLEY_UNLIKELY(!s.empty() && !(s.size() == 1 && s[0] == '-') && !(s[0] >= '0' && s[0] <= '9'))) + { + return array_index_status::not_a_number; + } + + // fast path: up to 18 decimal digits cannot overflow unsigned long long or + // SizeType (9 digits for a 32-bit SizeType), so strtoull and errno are not needed + constexpr std::size_t max_fast_digits = sizeof(SizeType) >= 8 ? 18 : 9; + if (!s.empty() && s.size() <= max_fast_digits) + { + SizeType acc = 0; + for (std::size_t i = 0; i < s.size(); ++i) + { + const char c = s[i]; + if (JSON_HEDLEY_UNLIKELY(c < '0' || c > '9')) + { + break; // not a plain number: let strtoull decide + } + acc = static_cast((acc * 10) + static_cast(c - '0')); + if (JSON_HEDLEY_UNLIKELY(i == s.size() - 1)) + { + idx = acc; + return array_index_status::ok; + } + } + } + + const char* p = s.data(); + char* p_end = nullptr; // NOLINT(misc-const-correctness) + errno = 0; // strtoull doesn't reset errno + const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int) + if (p == p_end // invalid input or empty string + || errno == ERANGE // out of range + || JSON_HEDLEY_UNLIKELY(static_cast(p_end - p) != s.size())) // incomplete read + { + return array_index_status::unresolved; + } + + // the index does not fit into SizeType; on 64-bit platforms this is + // only SIZE_MAX itself (see #2203 and #5395) + if (res >= static_cast((std::numeric_limits::max)())) // NOLINT(runtime/int) + { + return array_index_status::exceeds_size_type; + } + + idx = static_cast(res); + return array_index_status::ok; +} + } // namespace detail /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document @@ -302,6 +398,28 @@ class json_pointer JSON_THROW(detail::out_of_range::create(detail::exception_id::patch_on_root, "JSON pointer has no parent", nullptr)); } + /// whether the '~' at @a pos of @a s is not followed by '0' or '1' + JSON_HEDLEY_ALWAYS_INLINE static bool is_invalid_escape(const string_t& s, std::size_t pos) + { + return pos == s.size() - 1 || (s[pos + 1] != '0' && s[pos + 1] != '1'); + } + + /// @throw parse_error.107 always + JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE static void throw_missing_slash(const string_t& pointer) + { + static_cast(pointer); // unused when JSON_NOEXCEPTION is defined + JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_missing_slash, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", pointer, "'"), nullptr)); + } + + /// @throw parse_error.107 if a nonempty @a pointer does not begin with '/' + JSON_HEDLEY_ALWAYS_INLINE static void check_begins_with_slash(const string_t& pointer) + { + if (JSON_HEDLEY_UNLIKELY(pointer[0] != '/')) + { + throw_missing_slash(pointer); + } + } + /// @throw out_of_range.404 always template JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE static void throw_unresolved(const string_t& reference_token, BasicJsonContext context) @@ -311,21 +429,7 @@ class json_pointer JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_unresolved, detail::concat("unresolved reference token '", reference_token, "'"), context)); } - /*! - @brief result of @ref parse_array_index - - @ref array_index maps each value to the corresponding parse_error/out_of_range - exception; @ref contains and @ref get_checked_or_null, which must not throw for - an out-of-range or unrepresentable index, switch on it directly instead. - */ - enum class array_index_status - { - ok, ///< @a s is a valid, representable array index - leading_zero, ///< @a s begins with '0' but has more than one character - not_a_number, ///< @a s is neither empty nor "-" and does not begin with a digit - unresolved, ///< @a s could not be converted to an integer - exceeds_size_type ///< @a s converts to an integer that exceeds size_type - }; + using array_index_status = detail::array_index_status; /// whether a reference token is "-" (a plain comparison with a string literal is not always inlined) static bool is_dash(const string_t& s) noexcept @@ -333,78 +437,11 @@ class json_pointer return s.size() == 1 && s[0] == '-'; } - /*! - @param[in] s reference token to be converted into an array index - @param[out] idx the integer representation of @a s if @ref array_index_status::ok - is returned; left unchanged otherwise - - @return whether @a s is a valid array index, and if not, why - - @note this function never throws; @ref array_index and the callers that must not - throw (@ref contains, @ref get_checked_or_null) build on it instead of each - re-implementing the RFC 6901 digit rules and the @a size_type range check - */ + /// detail::parse_array_index() for the size_type of @a BasicJsonType template static array_index_status parse_array_index(const string_t& s, typename BasicJsonType::size_type& idx) noexcept { - using size_type = typename BasicJsonType::size_type; - - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) - { - return array_index_status::leading_zero; - } - - // error condition (cf. RFC 6901, Sect. 4); this also covers single- - // character tokens, so "/x" and "/xy" fail alike; "-" and the empty - // token are left to the conversion below and are reported as unresolved - if (JSON_HEDLEY_UNLIKELY(!s.empty() && !is_dash(s) && !(s[0] >= '0' && s[0] <= '9'))) - { - return array_index_status::not_a_number; - } - - // fast path: up to 18 decimal digits cannot overflow unsigned long long or - // size_type (9 digits for a 32-bit size_type), so strtoull and errno are not needed - constexpr std::size_t max_fast_digits = sizeof(size_type) >= 8 ? 18 : 9; - if (!s.empty() && s.size() <= max_fast_digits) - { - size_type acc = 0; - for (std::size_t i = 0; i < s.size(); ++i) - { - const char c = s[i]; - if (JSON_HEDLEY_UNLIKELY(c < '0' || c > '9')) - { - break; // not a plain number: let strtoull decide - } - acc = static_cast((acc * 10) + static_cast(c - '0')); - if (JSON_HEDLEY_UNLIKELY(i == s.size() - 1)) - { - idx = acc; - return array_index_status::ok; - } - } - } - - const char* p = s.data(); - char* p_end = nullptr; // NOLINT(misc-const-correctness) - errno = 0; // strtoull doesn't reset errno - const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int) - if (p == p_end // invalid input or empty string - || errno == ERANGE // out of range - || JSON_HEDLEY_UNLIKELY(static_cast(p_end - p) != s.size())) // incomplete read - { - return array_index_status::unresolved; - } - - // the index does not fit into size_type; on 64-bit platforms this is - // only SIZE_MAX itself (see #2203 and #5395) - if (res >= static_cast((std::numeric_limits::max)())) // NOLINT(runtime/int) - { - return array_index_status::exceeds_size_type; - } - - idx = static_cast(res); - return array_index_status::ok; + return detail::parse_array_index(s, idx); } /*! @@ -454,10 +491,7 @@ class json_pointer return reference_tokens.empty(); } - if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) - { - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_missing_slash, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr)); - } + check_begins_with_slash(reference_string); // the whole string is validated, whether or not it matches const std::size_t size = reference_string.size(); @@ -465,7 +499,7 @@ class json_pointer pos != string_t::npos; pos = reference_string.find_first_of('~', pos + 1)) { - if (JSON_HEDLEY_UNLIKELY(pos == size - 1 || (reference_string[pos + 1] != '0' && reference_string[pos + 1] != '1'))) + if (JSON_HEDLEY_UNLIKELY(is_invalid_escape(reference_string, pos))) { JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_invalid_escape, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); } @@ -743,10 +777,7 @@ class json_pointer } const char* const data = key.data(); - if (JSON_HEDLEY_UNLIKELY(data[0] != '/')) - { - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_missing_slash, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", key, "'"), nullptr)); - } + check_begins_with_slash(key); // The beginning of the key that it has in common with the previous // one, which was checked already, is skipped: the scan starts with @@ -779,7 +810,7 @@ class json_pointer else if (data[i] == '~') { // ~ must be followed by 0 or 1 - if (JSON_HEDLEY_UNLIKELY(i + 1 == size || (data[i + 1] != '0' && data[i + 1] != '1'))) + if (JSON_HEDLEY_UNLIKELY(is_invalid_escape(key, i))) { JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_invalid_escape, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); } @@ -878,58 +909,46 @@ class json_pointer ++end; } - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + token.clear(); + token.append(begin, static_cast(end - begin)); + const auto it = ptr->m_data.m_value.object->find(token); + if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) { - token.clear(); - token.append(begin, static_cast(end - begin)); - const auto it = ptr->m_data.m_value.object->find(token); - if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) - { - return nullptr; - } - ptr = &it->second; - break; - } - - case detail::value_t::array: - { - // an index of up to 18 digits without a leading zero fits into size_type - // (9 digits for a 32-bit size_type); everything else is left to json_pointer - constexpr std::size_t max_digits = sizeof(typename BasicJsonType::size_type) >= 8 ? 18 : 9; - const auto digits = static_cast(end - begin); - if (JSON_HEDLEY_UNLIKELY(digits == 0 || digits > max_digits || (digits > 1 && *begin == '0'))) - { - return nullptr; - } - typename BasicJsonType::size_type idx = 0; - for (const char* c = begin; c != end; ++c) - { - if (JSON_HEDLEY_UNLIKELY(*c < '0' || *c > '9')) - { - return nullptr; - } - idx = static_cast((idx * 10) + static_cast(*c - '0')); - } - if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) - { - return nullptr; - } - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: return nullptr; + } + ptr = &it->second; + } + else if (ptr->is_array()) + { + // an index of up to 18 digits without a leading zero fits into size_type + // (9 digits for a 32-bit size_type); everything else is left to json_pointer + constexpr std::size_t max_digits = sizeof(typename BasicJsonType::size_type) >= 8 ? 18 : 9; + const auto digits = static_cast(end - begin); + if (JSON_HEDLEY_UNLIKELY(digits == 0 || digits > max_digits || (digits > 1 && *begin == '0'))) + { + return nullptr; + } + typename BasicJsonType::size_type idx = 0; + for (const char* c = begin; c != end; ++c) + { + if (JSON_HEDLEY_UNLIKELY(*c < '0' || *c > '9')) + { + return nullptr; + } + idx = static_cast((idx * 10) + static_cast(*c - '0')); + } + if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) + { + return nullptr; + } + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // primitive value with a reference token left + return nullptr; } pos = static_cast(end - path.data()); steps.emplace_back(pos, ptr); @@ -979,40 +998,28 @@ class json_pointer : detail::value_t::object; } - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + // use unchecked object access + ptr = &object_member(*ptr, reference_token); + } + else if (ptr->is_array()) + { + if (reference_token == "-") { - // use unchecked object access - ptr = &object_member(*ptr, reference_token); - break; + // explicitly treat "-" as index beyond the end + ptr = &ptr->operator[](ptr->m_data.m_value.array->size()); } - - case detail::value_t::array: + else { - if (reference_token == "-") - { - // explicitly treat "-" as index beyond the end - ptr = &ptr->operator[](ptr->m_data.m_value.array->size()); - } - else - { - // convert array index to number; unchecked access - ptr = &ptr->operator[](array_index(reference_token)); - } - break; + // convert array index to number; unchecked access + ptr = &ptr->operator[](array_index(reference_token)); } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - throw_unresolved(reference_token, ptr); + } + else + { + // primitive value with a reference token left + throw_unresolved(reference_token, ptr); } } @@ -1030,51 +1037,39 @@ class json_pointer { for (const auto& reference_token : reference_tokens) { - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + const auto it = ptr->m_data.m_value.object->find(reference_token); + if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) { - const auto it = ptr->m_data.m_value.object->find(reference_token); - if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) - { - // at() throws out_of_range.403 with the usual message - ptr->at(reference_token); - } - ptr = &it->second; - break; + // at() throws out_of_range.403 with the usual message + ptr->at(reference_token); + } + ptr = &it->second; + } + else if (ptr->is_array()) + { + if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) + { + // "-" always fails the range check + JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_past_the_end_index, detail::concat( + "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), + ") is out of range"), ptr)); } - case detail::value_t::array: + const auto idx = array_index(reference_token); + // Bounds check before access to avoid exception with JSON_NOEXCEPTION + if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) { - if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) - { - // "-" always fails the range check - JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_past_the_end_index, detail::concat( - "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), - ") is out of range"), ptr)); - } - - const auto idx = array_index(reference_token); - // Bounds check before access to avoid exception with JSON_NOEXCEPTION - if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) - { - JSON_THROW(detail::out_of_range::create(detail::exception_id::array_index_out_of_range, detail::concat( - "array index ", std::to_string(idx), " is out of range"), ptr)); - } - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; + JSON_THROW(detail::out_of_range::create(detail::exception_id::array_index_out_of_range, detail::concat( + "array index ", std::to_string(idx), " is out of range"), ptr)); } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - throw_unresolved(reference_token, ptr); + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // primitive value with a reference token left + throw_unresolved(reference_token, ptr); } } @@ -1103,44 +1098,32 @@ class json_pointer { for (const auto& reference_token : reference_tokens) { - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + // use unchecked object access; the const operator[] + // asserts that the key exists + const auto it = ptr->m_data.m_value.object->find(reference_token); + JSON_ASSERT(it != ptr->m_data.m_value.object->end()); + ptr = &it->second; + } + else if (ptr->is_array()) + { + if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) { - // use unchecked object access; the const operator[] - // asserts that the key exists - const auto it = ptr->m_data.m_value.object->find(reference_token); - JSON_ASSERT(it != ptr->m_data.m_value.object->end()); - ptr = &it->second; - break; + // "-" cannot be used for const access + JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_past_the_end_index, detail::concat("array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), ") is out of range"), ptr)); } - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) - { - // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_past_the_end_index, detail::concat("array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), ") is out of range"), ptr)); - } - - // use unchecked array access; the const operator[] - // asserts that the index exists - const auto idx = array_index(reference_token); - JSON_ASSERT(idx < ptr->m_data.m_value.array->size()); - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - throw_unresolved(reference_token, ptr); + // use unchecked array access; the const operator[] + // asserts that the index exists + const auto idx = array_index(reference_token); + JSON_ASSERT(idx < ptr->m_data.m_value.array->size()); + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // primitive value with a reference token left + throw_unresolved(reference_token, ptr); } } @@ -1164,66 +1147,54 @@ class json_pointer { for (const auto& reference_token : reference_tokens) { - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + const auto it = ptr->m_data.m_value.object->find(reference_token); + if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) { - const auto it = ptr->m_data.m_value.object->find(reference_token); - if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) - { - return nullptr; - } - ptr = &it->second; - break; - } - - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) - { - // "-" always fails the range check - return nullptr; - } - - // a malformed index throws parse_error.106/109; an - // index that is syntactically valid but cannot be - // represented (out_of_range.404/410) is treated like an - // out-of-range index below - typename BasicJsonType::size_type idx{}; - switch (parse_array_index(reference_token, idx)) - { - // the branches differ in their messages, not after JSON_THROW's expansion - // NOLINTNEXTLINE(bugprone-branch-clone) - case array_index_status::leading_zero: - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_index_leading_zero, 0, detail::concat("array index '", reference_token, "' must not begin with '0'"), nullptr)); - case array_index_status::not_a_number: - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_index_not_a_number, 0, detail::concat("array index '", reference_token, "' is not a number"), nullptr)); - case array_index_status::unresolved: - case array_index_status::exceeds_size_type: - return nullptr; - case array_index_status::ok: - default: - break; - } - - if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) - { - return nullptr; - } - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: return nullptr; + } + ptr = &it->second; + } + else if (ptr->is_array()) + { + if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) + { + // "-" always fails the range check + return nullptr; + } + + // a malformed index throws parse_error.106/109; an + // index that is syntactically valid but cannot be + // represented (out_of_range.404/410) is treated like an + // out-of-range index below + typename BasicJsonType::size_type idx{}; + switch (parse_array_index(reference_token, idx)) + { + // the branches differ in their messages, not after JSON_THROW's expansion + // NOLINTNEXTLINE(bugprone-branch-clone) + case array_index_status::leading_zero: + JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_index_leading_zero, 0, detail::concat("array index '", reference_token, "' must not begin with '0'"), nullptr)); + case array_index_status::not_a_number: + JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_index_not_a_number, 0, detail::concat("array index '", reference_token, "' is not a number"), nullptr)); + case array_index_status::unresolved: + case array_index_status::exceeds_size_type: + return nullptr; + case array_index_status::ok: + default: + break; + } + + if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) + { + return nullptr; + } + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // primitive value with a reference token left + return nullptr; } } @@ -1239,63 +1210,48 @@ class json_pointer { for (const auto& reference_token : reference_tokens) { - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + const auto it = ptr->m_data.m_value.object->find(reference_token); + if (it == ptr->m_data.m_value.object->end()) { - const auto it = ptr->m_data.m_value.object->find(reference_token); - if (it == ptr->m_data.m_value.object->end()) - { - // we did not find the key in the object - return false; - } - - ptr = &it->second; - break; - } - - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) - { - // "-" always fails the range check - return false; - } - - // any parse failure (malformed index, or one that is syntactically - // valid but not representable as size_type) means the reference - // token cannot denote an existing array element -- contains() must - // not throw (see #5395), so it is treated as "not found" - typename BasicJsonType::size_type idx{}; - if (JSON_HEDLEY_UNLIKELY(parse_array_index(reference_token, idx) != array_index_status::ok)) - { - return false; - } - - if (idx >= ptr->m_data.m_value.array->size()) - { - // index out of range - return false; - } - - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - { - // we do not expect primitive values if there is still a - // reference token to process + // we did not find the key in the object return false; } + + ptr = &it->second; + } + else if (ptr->is_array()) + { + if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) + { + // "-" always fails the range check + return false; + } + + // any parse failure (malformed index, or one that is syntactically + // valid but not representable as size_type) means the reference + // token cannot denote an existing array element -- contains() must + // not throw (see #5395), so it is treated as "not found" + typename BasicJsonType::size_type idx{}; + if (JSON_HEDLEY_UNLIKELY(parse_array_index(reference_token, idx) != array_index_status::ok)) + { + return false; + } + + if (idx >= ptr->m_data.m_value.array->size()) + { + // index out of range + return false; + } + + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // we do not expect primitive values if there is still a + // reference token to process + return false; } } @@ -1323,10 +1279,7 @@ class json_pointer } // check if a nonempty reference string begins with slash - if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) - { - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_missing_slash, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr)); - } + check_begins_with_slash(reference_string); // every slash starts a reference token const std::size_t size = reference_string.size(); @@ -1367,9 +1320,7 @@ class json_pointer JSON_ASSERT(reference_token[pos] == '~'); // ~ must be followed by 0 or 1 - if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || - (reference_token[pos + 1] != '0' && - reference_token[pos + 1] != '1'))) + if (JSON_HEDLEY_UNLIKELY(is_invalid_escape(reference_token, pos))) { JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_invalid_escape, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); } @@ -1526,50 +1477,34 @@ class json_pointer auto hint = object.end(); const auto enter = [&stack, &path, &object, &hint](const BasicJsonType & v) { - switch (v.type()) + if (v.is_array()) { - case detail::value_t::array: + if (v.m_data.m_value.array->empty()) { - if (v.m_data.m_value.array->empty()) - { - // flatten empty array as null - add_flattened(object, hint, path, nullptr, detail::priority_tag<2> {}); - } - else - { - stack.emplace_back(&v, object_const_iterator(), path.size()); - } - return; + // flatten empty array as null + add_flattened(object, hint, path, nullptr, detail::priority_tag<2> {}); } - - case detail::value_t::object: + else { - if (v.m_data.m_value.object->empty()) - { - // flatten empty object as null - add_flattened(object, hint, path, nullptr, detail::priority_tag<2> {}); - } - else - { - stack.emplace_back(&v, v.m_data.m_value.object->begin(), path.size()); - } - return; + stack.emplace_back(&v, object_const_iterator(), path.size()); } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: + } + else if (v.is_object()) + { + if (v.m_data.m_value.object->empty()) { - // add a primitive value with its reference string - add_flattened(object, hint, path, v, detail::priority_tag<2> {}); - return; + // flatten empty object as null + add_flattened(object, hint, path, nullptr, detail::priority_tag<2> {}); } + else + { + stack.emplace_back(&v, v.m_data.m_value.object->begin(), path.size()); + } + } + else + { + // add a primitive value with its reference string + add_flattened(object, hint, path, v, detail::priority_tag<2> {}); } }; diff --git a/include/nlohmann/detail/output/binary_writer.hpp b/include/nlohmann/detail/output/binary_writer.hpp index f75c84a48..7ff828074 100644 --- a/include/nlohmann/detail/output/binary_writer.hpp +++ b/include/nlohmann/detail/output/binary_writer.hpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -311,9 +312,7 @@ class binary_writer write_cbor_head(0x40, N); // step 2: write each element - oa.write_characters( - reinterpret_cast(j.m_data.m_value.binary->data()), - N); + write_bytes(j.m_data.m_value.binary->data(), N); break; } @@ -326,7 +325,7 @@ class binary_writer case value_t::discarded: default: - throw_on_discarded(j, "CBOR"); + throw_on_discarded(j, input_format_t::cbor); } } @@ -526,9 +525,7 @@ class binary_writer } // step 2: write the byte string - oa.write_characters( - reinterpret_cast(j.m_data.m_value.binary->data()), - N); + write_bytes(j.m_data.m_value.binary->data(), N); break; } @@ -542,7 +539,7 @@ class binary_writer case value_t::discarded: default: - throw_on_discarded(j, "MessagePack"); + throw_on_discarded(j, input_format_t::msgpack); } } @@ -626,7 +623,7 @@ class binary_writer case value_t::array: { bool prefix_required = true; - const bool write_closer = write_ubjson_start_array(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); + const bool write_closer = write_ubjson_start_container(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); { const auto& array = *j.m_data.m_value.array; @@ -676,9 +673,7 @@ class binary_writer if (use_type) { - oa.write_characters( - reinterpret_cast(j.m_data.m_value.binary->data()), - j.m_data.m_value.binary->size()); + write_bytes(j.m_data.m_value.binary->data(), j.m_data.m_value.binary->size()); } else { @@ -704,7 +699,7 @@ class binary_writer } bool prefix_required = true; - const bool write_closer = write_ubjson_start_object(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); + const bool write_closer = write_ubjson_start_container(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); write_ubjson_members(j, use_count, use_type, prefix_required, use_bjdata, bjdata_version, depth); @@ -718,7 +713,7 @@ class binary_writer case value_t::discarded: default: - throw_on_discarded(j, use_bjdata ? "BJData" : "UBJSON"); + throw_on_discarded(j, use_bjdata ? input_format_t::bjdata : input_format_t::ubjson); } } @@ -744,11 +739,11 @@ class binary_writer @brief throws because @a j is discarded and cannot be serialized @throw type_error.321 always */ - JSON_HEDLEY_NO_RETURN static void throw_on_discarded(const BasicJsonType& j, const char* format_name) + JSON_HEDLEY_NO_RETURN static void throw_on_discarded(const BasicJsonType& j, const input_format_t format) { static_cast(j); // unused when JSON_NOEXCEPTION is defined - static_cast(format_name); - JSON_THROW(type_error::create(exception_id::discarded_value_used, concat("cannot serialize discarded value to ", format_name), &j)); + static_cast(format); + JSON_THROW(type_error::create(exception_id::discarded_value_used, concat("cannot serialize discarded value to ", input_format_name(format)), &j)); } /// how the range of integer type @a NumberType relates to that of @a TargetType @@ -868,11 +863,11 @@ class binary_writer } /*! - @brief throws because the integer @a j is too large for @a format_name + @brief throws because the integer @a j is too large for @a format @throw out_of_range.407 always */ JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE - static void throw_integer_out_of_range(const BasicJsonType& j, const char* format_name, const char* range) + static void throw_integer_out_of_range(const BasicJsonType& j, const input_format_t format, const char* range) { // neither std::to_string(), which has no overload for integer types // wider than 64 bits, nor dump(), which would also instantiate the @@ -883,9 +878,9 @@ class binary_writer ? integer_to_string(j.m_data.m_value.number_unsigned, std::integral_constant()) : integer_to_string(j.m_data.m_value.number_integer, std::integral_constant()); static_cast(number); // unused when JSON_NOEXCEPTION is defined - static_cast(format_name); + static_cast(format); static_cast(range); - JSON_THROW(out_of_range::create(exception_id::integer_too_large, concat("integer number ", number, " cannot be represented by ", format_name, " as it does not fit ", range), &j)); + JSON_THROW(out_of_range::create(exception_id::integer_too_large, concat("integer number ", number, " cannot be represented by ", input_format_name(format), " as it does not fit ", range), &j)); } // The exceptions below are thrown from functions of their own, out of line: @@ -928,53 +923,56 @@ class binary_writer JSON_THROW(other_error::create(exception_id::size_marker_required, "use_type requires use_size = true", &j)); } + /// @brief write @a size bytes starting at @a data to the output adapter + JSON_HEDLEY_ALWAYS_INLINE + void write_bytes(const void* data, const std::size_t size) + { + oa.write_characters(static_cast(data), size); + } + + /// @brief write the MessagePack prefix of an array or object with @a N + /// elements: the fix marker @a fix_marker | n, or @a marker_16 or + /// @a marker_32 followed by the length + JSON_HEDLEY_ALWAYS_INLINE + void write_msgpack_container_prefix(const std::size_t N, const BasicJsonType& j, + const std::uint8_t fix_marker, const std::uint8_t marker_16, const std::uint8_t marker_32) + { + const auto n = to_msgpack_length(N, j); + if (n <= 15) + { + // fixarray, fixmap + oa.write_character(to_char_type(static_cast(fix_marker | n))); + } + else if (n <= (std::numeric_limits::max)()) + { + // array 16, map 16 + oa.write_character(to_char_type(marker_16)); + write_number(static_cast(n)); + } + else + { + // array 32, map 32 + oa.write_character(to_char_type(marker_32)); + write_number(static_cast(n)); + } + } + + JSON_HEDLEY_ALWAYS_INLINE void write_msgpack_array_prefix(const std::size_t N, const BasicJsonType& j) { - const auto n = to_msgpack_length(N, j); - if (n <= 15) - { - // fixarray - oa.write_character(to_char_type(static_cast(0x90 | n))); - } - else if (n <= (std::numeric_limits::max)()) - { - // array 16 - oa.write_character(to_char_type(0xDC)); - write_number(static_cast(n)); - } - else - { - // array 32 - oa.write_character(to_char_type(0xDD)); - write_number(static_cast(n)); - } + write_msgpack_container_prefix(N, j, 0x90, 0xDC, 0xDD); } + JSON_HEDLEY_ALWAYS_INLINE void write_msgpack_object_prefix(const std::size_t N, const BasicJsonType& j) { - const auto n = to_msgpack_length(N, j); - if (n <= 15) - { - // fixmap - oa.write_character(to_char_type(static_cast(0x80 | (n & 0xF)))); - } - else if (n <= (std::numeric_limits::max)()) - { - // map 16 - oa.write_character(to_char_type(0xDE)); - write_number(static_cast(n)); - } - else - { - // map 32 - oa.write_character(to_char_type(0xDF)); - write_number(static_cast(n)); - } + write_msgpack_container_prefix(N, j, 0x80, 0xDE, 0xDF); } - /// @brief a CBOR, MessagePack, or BON8 array or object whose elements - /// @ref write_cbor_iterative, @ref write_msgpack_iterative, or - /// @ref write_bon8_iterative is still writing + /// @brief a CBOR, MessagePack, BON8, or (as the base of @ref ubjson_frame) + /// UBJSON array or object whose elements @ref write_container_iterative, + /// @ref write_bon8_iterative, or @ref write_ubjson_iterative is still + /// writing struct binary_container_frame { explicit binary_container_frame(const BasicJsonType* value_) noexcept @@ -1010,19 +1008,82 @@ class binary_writer typename BasicJsonType::array_t::const_iterator array_it{}; }; + /// @brief the CBOR specifics of @ref write_container_iterative + struct cbor_walk + { + static void array_head(binary_writer& w, const BasicJsonType& j) + { + w.write_cbor_head(0x80, j.m_data.m_value.array->size()); + } + + static void object_head(binary_writer& w, const BasicJsonType& j) + { + w.write_cbor_head(0xA0, j.m_data.m_value.object->size()); + } + + static void scalar(binary_writer& w, const BasicJsonType& j) + { + w.write_cbor(j); + } + + static void string(binary_writer& w, const string_t& key, const BasicJsonType& context) + { + w.write_cbor_string(key, context); + } + + template + static void key(binary_writer& w, const KeyType& k, const BasicJsonType& object) + { + w.write_cbor_key(k, object); + } + }; + + /// @brief the MessagePack specifics of @ref write_container_iterative + struct msgpack_walk + { + static void array_head(binary_writer& w, const BasicJsonType& j) + { + w.write_msgpack_array_prefix(j.m_data.m_value.array->size(), j); + } + + static void object_head(binary_writer& w, const BasicJsonType& j) + { + w.write_msgpack_object_prefix(j.m_data.m_value.object->size(), j); + } + + static void scalar(binary_writer& w, const BasicJsonType& j) + { + w.write_msgpack(j); + } + + static void string(binary_writer& w, const string_t& key, const BasicJsonType& context) + { + w.write_msgpack_string(key, context); + } + + template + static void key(binary_writer& w, const KeyType& k, const BasicJsonType& object) + { + w.write_msgpack_key(k, object); + } + }; + /*! - @brief write @a j with @ref write_cbor, or write its header and push a - frame for @ref write_cbor_iterative to continue with its elements + @brief write @a j with @a Walk::scalar, or write its header and push a + frame for @ref write_container_iterative to continue with its + elements A scalar, and an empty array or object, are written out in full: there is - nothing below them for @ref write_cbor_iterative to come back to, so + nothing below them for @ref write_container_iterative to come back to, so nothing is pushed for them. */ - void write_cbor_value_or_push(const BasicJsonType& j, std::vector& stack) + template + JSON_HEDLEY_ALWAYS_INLINE + void write_container_value_or_push(const BasicJsonType& j, std::vector& stack) { if (j.is_array()) { - write_cbor_head(0x80, j.m_data.m_value.array->size()); + Walk::array_head(*this, j); if (!j.m_data.m_value.array->empty()) { stack.emplace_back(&j); @@ -1032,7 +1093,7 @@ class binary_writer if (j.is_object()) { - write_cbor_head(0xA0, j.m_data.m_value.object->size()); + Walk::object_head(*this, j); if (!j.m_data.m_value.object->empty()) { stack.emplace_back(&j); @@ -1040,22 +1101,26 @@ class binary_writer return; } - write_cbor(j); + Walk::scalar(*this, j); } /*! @brief write out @a root and everything below it without the call stack - Emits the same bytes as @ref write_cbor, keeping the containers it has + Emits the same bytes as the recursive writer, keeping the containers it has entered on an explicit stack instead of descending into them. Only reached for values nested deeper than @ref recursion_depth_limit, which is why it - is not written for speed. + is not written for speed. Shared by CBOR and MessagePack, which differ only + in the @a Walk policy (@ref cbor_walk, @ref msgpack_walk); BON8 has its own + walker, as it tracks an open string run and closes a container with a + marker. */ - void write_cbor_iterative(const BasicJsonType& root) + template + void write_container_iterative(const BasicJsonType& root) { - // only a container with elements is ever pushed; see write_cbor_value_or_push + // only a container with elements is ever pushed; see write_container_value_or_push std::vector stack; - write_cbor_value_or_push(root, stack); + write_container_value_or_push(root, stack); while (!stack.empty()) { @@ -1073,7 +1138,7 @@ class binary_writer // read the child before pushing: entering it can move every frame const BasicJsonType* child = &(*current.array_it); ++stack.back().array_it; - write_cbor_value_or_push(*child, stack); + write_container_value_or_push(*child, stack); } else { @@ -1085,110 +1150,53 @@ class binary_writer } // the key is written directly (not via a temporary basic_json), - // with the object as diagnostics context, as in write_cbor - write_cbor_key(current.object_it->first, *current.value); + // with the object as diagnostics context, as in the recursive writer + Walk::key(*this, current.object_it->first, *current.value); const BasicJsonType* child = &(current.object_it->second); ++stack.back().object_it; - write_cbor_value_or_push(*child, stack); + write_container_value_or_push(*child, stack); } } } - /*! - @brief write @a j with @ref write_msgpack, or write its header and push a - frame for @ref write_msgpack_iterative to continue with its elements - - @sa @ref write_cbor_value_or_push - */ - void write_msgpack_value_or_push(const BasicJsonType& j, std::vector& stack) + void write_cbor_iterative(const BasicJsonType& root) { - if (j.is_array()) - { - write_msgpack_array_prefix(j.m_data.m_value.array->size(), j); - if (!j.m_data.m_value.array->empty()) - { - stack.emplace_back(&j); - } - return; - } - - if (j.is_object()) - { - write_msgpack_object_prefix(j.m_data.m_value.object->size(), j); - if (!j.m_data.m_value.object->empty()) - { - stack.emplace_back(&j); - } - return; - } - - write_msgpack(j); + write_container_iterative(root); } - /*! - @brief write out @a root and everything below it without the call stack - - @sa @ref write_cbor_iterative - */ void write_msgpack_iterative(const BasicJsonType& root) { - std::vector stack; - write_msgpack_value_or_push(root, stack); - - while (!stack.empty()) - { - const binary_container_frame current = stack.back(); - - if (current.value->is_array()) - { - const auto& array = *current.value->m_data.m_value.array; - if (current.array_it == array.cend()) - { - stack.pop_back(); - continue; - } - - const BasicJsonType* child = &(*current.array_it); - ++stack.back().array_it; - write_msgpack_value_or_push(*child, stack); - } - else - { - const auto& object = *current.value->m_data.m_value.object; - if (current.object_it == object.cend()) - { - stack.pop_back(); - continue; - } - - // as in write_cbor_iterative, the key is written directly with - // the object as diagnostics context - write_msgpack_key(current.object_it->first, *current.value); - const BasicJsonType* child = &(current.object_it->second); - ++stack.back().object_it; - write_msgpack_value_or_push(*child, stack); - } - } + write_container_iterative(root); } - /// @return true when a closing ']' still has to be written after the elements - bool write_ubjson_start_array(const BasicJsonType& j, const bool use_count, const bool use_type, - const bool add_prefix, const bool use_bjdata, bool& prefix_required) + /*! + @brief write the start of an array (@a IsArray) or object: its opening + marker, the optimized type, and the count + + @return true when a closing ']' or '}' still has to be written after the + elements + */ + template + bool write_ubjson_start_container(const BasicJsonType& j, const bool use_count, const bool use_type, + const bool add_prefix, const bool use_bjdata, bool& prefix_required) { prefix_required = true; if (add_prefix) { - oa.write_character(to_char_type('[')); + oa.write_character(to_char_type(IsArray ? '[' : '{')); } - if (use_type && !j.m_data.m_value.array->empty()) + const std::size_t size = IsArray ? j.m_data.m_value.array->size() : j.m_data.m_value.object->size(); + + if (use_type && size != 0) { if (!use_count) { throw_size_marker_required(j); } const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata); - const bool same_prefix = std::all_of(j.begin() + 1, j.end(), + // the first element of an array has been compared with itself + const bool same_prefix = std::all_of(IsArray ? j.begin() + 1 : j.begin(), j.end(), [this, first_prefix, use_bjdata](const BasicJsonType & v) { return ubjson_prefix(v, use_bjdata) == first_prefix; @@ -1200,8 +1208,9 @@ class binary_writer // those, at one byte per element, so the result can be read back. // Objects are not affected: every element is preceded by its key. const bool valueless_type = (first_prefix == 'Z' || first_prefix == 'T' || first_prefix == 'F'); - const bool excessive_valueless = valueless_type - && j.m_data.m_value.array->size() > detail::max_valueless_container_size; + const bool excessive_valueless = IsArray + && valueless_type + && size > detail::max_valueless_container_size; if (same_prefix && !excessive_valueless && !(use_bjdata && is_bjdata_excluded_type_marker(first_prefix))) @@ -1214,46 +1223,7 @@ class binary_writer if (use_count) { - write_ubjson_count(j.m_data.m_value.array->size(), use_bjdata); - } - - return !use_count; - } - - /// @return true when a closing '}' still has to be written after the elements - bool write_ubjson_start_object(const BasicJsonType& j, const bool use_count, const bool use_type, - const bool add_prefix, const bool use_bjdata, bool& prefix_required) - { - prefix_required = true; - if (add_prefix) - { - oa.write_character(to_char_type('{')); - } - - if (use_type && !j.m_data.m_value.object->empty()) - { - if (!use_count) - { - throw_size_marker_required(j); - } - const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata); - const bool same_prefix = std::all_of(j.begin(), j.end(), - [this, first_prefix, use_bjdata](const BasicJsonType & v) - { - return ubjson_prefix(v, use_bjdata) == first_prefix; - }); - - if (same_prefix && !(use_bjdata && is_bjdata_excluded_type_marker(first_prefix))) - { - prefix_required = false; - oa.write_character(to_char_type('$')); - oa.write_character(first_prefix); - } - } - - if (use_count) - { - write_ubjson_count(j.m_data.m_value.object->size(), use_bjdata); + write_ubjson_count(size, use_bjdata); } return !use_count; @@ -1281,21 +1251,12 @@ class binary_writer /// @brief an object or array @ref write_ubjson_iterative is still writing /// the elements of - struct ubjson_frame + struct ubjson_frame : binary_container_frame { ubjson_frame(const BasicJsonType* value_, const bool prefix_required_) noexcept - : value(value_) + : binary_container_frame(value_) , prefix_required(prefix_required_) - { - if (value->is_object()) - { - object_it = value->m_data.m_value.object->cbegin(); - } - else - { - array_it = value->m_data.m_value.array->cbegin(); - } - } + {} // declared for GCC's -Weffc++, which asks for them in a class with // pointer members and a non-trivial destructor; the exception @@ -1307,13 +1268,9 @@ class binary_writer ubjson_frame& operator=(ubjson_frame&&) = default; ~ubjson_frame() = default; - /// the array or object being written - const BasicJsonType* value; /// whether value's elements each carry their own type marker; an /// optimized ($type) container writes it once for all of them instead bool prefix_required; - typename BasicJsonType::object_t::const_iterator object_it{}; - typename BasicJsonType::array_t::const_iterator array_it{}; }; /*! @@ -1324,7 +1281,7 @@ class binary_writer elements of an optimized container, and everything below the top level, never repeat it) - @sa @ref write_cbor_value_or_push + @sa @ref write_container_value_or_push */ void write_ubjson_value_or_push(const BasicJsonType& j, const bool add_prefix, const bool use_count, const bool use_type, const bool use_bjdata, const bjdata_version_t bjdata_version, @@ -1347,11 +1304,11 @@ class binary_writer bool prefix_required = true; if (is_array) { - write_ubjson_start_array(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); + write_ubjson_start_container(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); } else { - write_ubjson_start_object(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); + write_ubjson_start_container(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); } const bool empty = is_array ? j.m_data.m_value.array->empty() : j.m_data.m_value.object->empty(); @@ -1361,7 +1318,7 @@ class binary_writer return; } - // write_ubjson_start_array/_object return !use_count, i.e. whether a + // write_ubjson_start_container returns !use_count, i.e. whether a // closer still has to be written; use_count is constant for the whole // document, so that is recomputed here instead of being carried along if (!use_count) @@ -1550,14 +1507,15 @@ class binary_writer { oa.write_character(to_char_type(element_type)); - if (error_handler == error_handler_t::keep || error_handler == error_handler_t::strict || is_valid_utf8(name)) + if (JSON_HEDLEY_LIKELY(error_handler == error_handler_t::keep || error_handler == error_handler_t::strict)) { - oa.write_characters(reinterpret_cast(name.data()), name.size()); + write_bytes(name.data(), name.size()); } else { - const string_t sanitized = sanitize_utf8(name, error_handler); - oa.write_characters(reinterpret_cast(sanitized.data()), sanitized.size()); + string_t storage; + const string_t& written = sanitize_utf8_for_bson_write(name, storage); + write_bytes(written.data(), written.size()); } // the terminating null byte is written explicitly rather than taken @@ -1633,16 +1591,11 @@ class binary_writer { write_bson_entry_header(name, 0x02); - const bool sanitize = error_handler != error_handler_t::keep - && error_handler != error_handler_t::strict - && !is_valid_utf8(value); - const string_t sanitized = sanitize ? sanitize_utf8(value, error_handler) : string_t{}; - const string_t& written = sanitize ? sanitized : value; + string_t storage; + const string_t& written = sanitize_utf8_for_bson_write(value, storage); write_number(to_bson_length(written.size() + 1ul), true); - oa.write_characters( - reinterpret_cast(written.data()), - written.size()); + write_bytes(written.data(), written.size()); // the terminating null byte is written explicitly rather than taken // from the buffer, so that string_t::data() need not be null-terminated oa.write_character(to_char_type(0x00)); @@ -1748,7 +1701,7 @@ class binary_writer write_number(value.has_subtype() ? static_cast(value.subtype()) : static_cast(0x00)); - oa.write_characters(reinterpret_cast(value.data()), value.size()); + write_bytes(value.data(), value.size()); } /*! @@ -1778,14 +1731,14 @@ class binary_writer case value_t::number_integer: if (JSON_HEDLEY_UNLIKELY(!integer_fits(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "BSON", "int64"); + throw_integer_out_of_range(j, input_format_t::bson, "int64"); } return calc_bson_integer_size(static_cast(j.m_data.m_value.number_integer)); case value_t::number_unsigned: if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_unsigned))) { - throw_integer_out_of_range(j, "BSON", "uint64"); + throw_integer_out_of_range(j, input_format_t::bson, "uint64"); } return calc_bson_unsigned_size(conditional_static_cast(j.m_data.m_value.number_unsigned)); @@ -1796,7 +1749,7 @@ class binary_writer return 0ul; case value_t::discarded: - throw_on_discarded(j, "BSON"); + throw_on_discarded(j, input_format_t::bson); // LCOV_EXCL_START case value_t::object: @@ -1960,7 +1913,7 @@ class binary_writer // the terminating null byte is written explicitly rather than // taken from the buffer, so that string_t::data() need not be // null-terminated - oa.write_characters(reinterpret_cast(value.data()), value.size()); + write_bytes(value.data(), value.size()); oa.write_character(to_char_type(0x00)); return true; } @@ -1977,7 +1930,7 @@ class binary_writer p += sizeof(std::int32_t); *p++ = value.has_subtype() ? to_char_type(static_cast(value.subtype())) : to_char_type(0x00); oa.commit(static_cast(p - out)); - oa.write_characters(reinterpret_cast(value.data()), value.size()); + write_bytes(value.data(), value.size()); return true; } @@ -2025,7 +1978,7 @@ class binary_writer return write_bson_null(name); case value_t::discarded: - throw_on_discarded(j, "BSON"); + throw_on_discarded(j, input_format_t::bson); // LCOV_EXCL_START case value_t::object: @@ -2806,7 +2759,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_unsigned))) { - throw_integer_out_of_range(j, "CBOR", "uint64"); + throw_integer_out_of_range(j, input_format_t::cbor, "uint64"); } write_cbor_head(0x00, conditional_static_cast(j.m_data.m_value.number_unsigned)); return; @@ -2816,7 +2769,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "CBOR", "[-2^64, 2^64-1]"); + throw_integer_out_of_range(j, input_format_t::cbor, "[-2^64, 2^64-1]"); } // CBOR does not differentiate between positive signed // integers and unsigned integers @@ -2827,7 +2780,7 @@ class binary_writer // a negative integer n is encoded as -1 - n if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(-1 - j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "CBOR", "[-2^64, 2^64-1]"); + throw_integer_out_of_range(j, input_format_t::cbor, "[-2^64, 2^64-1]"); } write_cbor_head(0x20, static_cast(-1 - j.m_data.m_value.number_integer)); } @@ -2903,7 +2856,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_unsigned))) { - throw_integer_out_of_range(j, "MessagePack", "uint64"); + throw_integer_out_of_range(j, input_format_t::msgpack, "uint64"); } write_msgpack_unsigned(static_cast(j.m_data.m_value.number_unsigned)); return; @@ -2913,7 +2866,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "MessagePack", "[-2^63, 2^64-1]"); + throw_integer_out_of_range(j, input_format_t::msgpack, "[-2^63, 2^64-1]"); } // MessagePack does not differentiate between positive // signed integers and unsigned integers. @@ -2923,7 +2876,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_min(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "MessagePack", "[-2^63, 2^64-1]"); + throw_integer_out_of_range(j, input_format_t::msgpack, "[-2^63, 2^64-1]"); } if (j.m_data.m_value.number_integer >= -32) { @@ -3057,40 +3010,37 @@ class binary_writer } /*! - @brief write an object key as a CBOR text string + @brief write an object key as a CBOR or MessagePack string (@a Walk) A key convertible to string_t is written directly. Other key types (only an explicit conversion, or only a to_json overload) go through a temporary basic_json, as in version 3.12.0; the temporary is then the diagnostics context for strict UTF-8 checks. */ - template::value, int> = 0> + void write_object_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& context) + { + Walk::string(*this, key, context); + } + + template>::value, int> = 0> + void write_object_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& /*context*/) + { + Walk::scalar(*this, BasicJsonType(key)); + } + + /// @brief write an object key as a CBOR text string void write_cbor_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& context) { - write_cbor_string(key, context); + write_object_key(key, context); } - template < typename Key = typename BasicJsonType::object_t::key_type, - enable_if_t < !std::is_convertible::value, int > = 0 > - void write_cbor_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& /*context*/) - { - write_cbor(BasicJsonType(key)); - } - - /// @brief write an object key as a MessagePack str, as in @ref write_cbor_key - template::value, int> = 0> + /// @brief write an object key as a MessagePack str void write_msgpack_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& context) { - write_msgpack_string(key, context); - } - - template < typename Key = typename BasicJsonType::object_t::key_type, - enable_if_t < !std::is_convertible::value, int > = 0 > - void write_msgpack_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& /*context*/) - { - write_msgpack(BasicJsonType(key)); + write_object_key(key, context); } /// the longest string that is written, with its head of one or two @@ -3165,9 +3115,7 @@ class binary_writer write_cbor_head(0x60, sanitized.size()); // step 2: write the string - oa.write_characters( - reinterpret_cast(sanitized.data()), - sanitized.size()); + write_bytes(sanitized.data(), sanitized.size()); } ///////////// @@ -3248,9 +3196,7 @@ class binary_writer } // step 2: write the string - oa.write_characters( - reinterpret_cast(sanitized.data()), - sanitized.size()); + write_bytes(sanitized.data(), sanitized.size()); } //////////// @@ -3533,9 +3479,7 @@ class binary_writer oa.write_character(to_char_type('S')); } write_number_with_ubjson_prefix(sanitized.size(), true, use_bjdata); - oa.write_characters( - reinterpret_cast(sanitized.data()), - sanitized.size()); + write_bytes(sanitized.data(), sanitized.size()); } /*! @@ -4335,7 +4279,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_unsigned))) { - throw_integer_out_of_range(j, "BON8", "int64"); + throw_integer_out_of_range(j, input_format_t::bon8, "int64"); } write_bon8_integer(static_cast(j.m_data.m_value.number_unsigned)); string_open = false; @@ -4346,7 +4290,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "BON8", "int64"); + throw_integer_out_of_range(j, input_format_t::bon8, "int64"); } write_bon8_integer(static_cast(j.m_data.m_value.number_integer)); string_open = false; @@ -4414,7 +4358,7 @@ class binary_writer case value_t::discarded: default: - throw_on_discarded(j, "BON8"); + throw_on_discarded(j, input_format_t::bon8); } } @@ -4425,7 +4369,7 @@ class binary_writer A scalar, and an empty array or object, are written out in full and not pushed. - @sa @ref write_cbor_value_or_push + @sa @ref write_container_value_or_push */ void write_bon8_value_or_push(const BasicJsonType& j, bool& string_open, std::vector& stack) { @@ -4811,7 +4755,7 @@ class binary_writer } else { - oa.write_characters(reinterpret_cast(s.data()), s.size()); + write_bytes(s.data(), s.size()); string_open = true; } } @@ -4902,6 +4846,31 @@ class binary_writer } } + /*! + @brief the write-pass counterpart of @ref sanitize_utf8_for_write for BSON + + The size pass (@ref calc_bson_entry_header_size, @ref + calc_bson_string_size) has already validated @a s, and thrown for @ref + error_handler_t::strict if it was ill-formed. Here, @ref + error_handler_t::strict therefore behaves like @ref error_handler_t::keep, + and @ref error_handler_t::replace / @ref error_handler_t::ignore produce + exactly the bytes the size pass measured. Hence no context is needed. + + @param[in] s the string (value or object key) to write + @param[out] storage backing storage for a sanitized copy + + @return a reference to @a s, or to @a storage once it holds a sanitized copy + */ + const string_t& sanitize_utf8_for_bson_write(const string_t& s, string_t& storage) const + { + if (error_handler == error_handler_t::keep || error_handler == error_handler_t::strict || is_valid_utf8(s)) + { + return s; // NOLINT(bugprone-return-const-ref-from-parameter): callers pass lvalues that outlive the call + } + storage = sanitize_utf8(s, error_handler); + return storage; + } + /// deleted: anything but a string_t would bind a temporary that dies before the returned reference is used template < typename T, enable_if_t < !std::is_same::value, int > = 0 > const string_t& sanitize_utf8_for_write(const T& /*s*/, const BasicJsonType& /*context*/, string_t& /*storage*/) const = delete; // NOLINT(hicpp-use-equals-delete,modernize-use-equals-delete): a private helper's guard, not part of the interface diff --git a/include/nlohmann/detail/output/serializer.hpp b/include/nlohmann/detail/output/serializer.hpp index 5eb9906a1..598fda98a 100644 --- a/include/nlohmann/detail/output/serializer.hpp +++ b/include/nlohmann/detail/output/serializer.hpp @@ -966,6 +966,28 @@ class serializer } } + /// write out the bytes pending in @ref string_buffer and reset @a bytes + JSON_HEDLEY_ALWAYS_INLINE void emit_pending(std::size_t& bytes) + { + if (bytes != 0) + { + put_buffer(string_buffer, bytes); + bytes = 0; + } + } + + /// write buffer and reset index if fewer than 13 bytes are left, as this + /// is the maximal number of bytes to be written ("\uxxxx\uxxxx\0") for one + /// code point + JSON_HEDLEY_ALWAYS_INLINE void reserve_string_buffer(std::size_t& bytes) + { + if (string_buffer.size() - bytes < 13) + { + put_buffer(string_buffer, bytes); + bytes = 0; + } + } + /*! @brief worker for @ref dump_escaped @@ -1024,11 +1046,7 @@ class serializer { // emit any bytes still pending in string_buffer first to // preserve output order, then write the run directly - if (bytes != 0) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + emit_pending(bytes); put_run(s, i, run); bytes_after_last_accept = 0; undumped_chars = 0; @@ -1049,11 +1067,10 @@ class serializer { // emit any bytes still pending in string_buffer first to // preserve output order - if (bytes != 0) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + emit_pending(bytes); + // deliberately not put_buffer(): a fixed 8-byte memcpy + // has no asserts and no write-through branch, which + // matters on the hot path const auto& escape = ascii_escapes()[data[i]]; if (JSON_HEDLEY_UNLIKELY(write_buffer.size() - write_buffer_pos < escape.size())) { @@ -1128,14 +1145,7 @@ class serializer string_buffer[bytes++] = s[i]; } - // write buffer and reset index; there must be 13 bytes - // left, as this is the maximal number of bytes to be - // written ("\uxxxx\uxxxx\0") for one code point - if (string_buffer.size() - bytes < 13) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + reserve_string_buffer(bytes); // remember the byte position of this accept bytes_after_last_accept = bytes; @@ -1187,14 +1197,7 @@ class serializer string_buffer[bytes++] = '\xBD'; } - // write buffer and reset index; there must be 13 bytes - // left, as this is the maximal number of bytes to be - // written ("\uxxxx\uxxxx\0") for one code point - if (string_buffer.size() - bytes < 13) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + reserve_string_buffer(bytes); bytes_after_last_accept = bytes; } @@ -1227,14 +1230,7 @@ class serializer string_buffer[bytes++] = s[i]; } - // write buffer and reset index; there must be 13 bytes - // left, as this is the maximal number of bytes to be - // written ("\uxxxx\uxxxx\0") for one code point - if (string_buffer.size() - bytes < 13) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + reserve_string_buffer(bytes); bytes_after_last_accept = bytes; undumped_chars = 0; @@ -1600,30 +1596,17 @@ class serializer */ static const std::array& ascii_copyable() noexcept { - static const std::array copyable = - { - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 0x50 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 0x70 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xA0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xB0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xC0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xD0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xE0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 0xF0 - } - }; + // constant-initialized from the predicate: no runtime loop, no guard + static constexpr std::array copyable = make_ascii_copyable(make_index_sequence<256> {}); return copyable; } + template + static constexpr std::array make_ascii_copyable(index_sequence /*unused*/) noexcept + { + return {{ static_cast(is_ascii_copyable(static_cast(Is)) ? 1 : 0)... }}; + } + /*! @brief the JSON form of each character below 0x60 @@ -1752,7 +1735,6 @@ class serializer */ JSON_HEDLEY_NEVER_INLINE std::size_t write_u_escapes(const unsigned char* data, std::size_t n) { - constexpr const char* nibble_to_hex = "0123456789abcdef"; std::size_t i = 0; while (i < n && data[i] >= 0x80) { @@ -1789,26 +1771,21 @@ class serializer char* d = write_buffer.data() + write_buffer_pos; if (codepoint > 0xFFFFu) { - d = put_u_escape(d, static_cast(0xD7C0u + (codepoint >> 10u)), nibble_to_hex); + d = put_u_escape(d, static_cast(0xD7C0u + (codepoint >> 10u))); codepoint = 0xDC00u + (codepoint & 0x3FFu); } - d = put_u_escape(d, static_cast(codepoint), nibble_to_hex); + d = put_u_escape(d, static_cast(codepoint)); write_buffer_pos = static_cast(d - write_buffer.data()); i += length; } return i; } - static char* put_u_escape(char* d, const std::uint16_t codeunit, const char* nibble_to_hex) noexcept + static char* put_u_escape(char* d, const std::uint16_t codeunit) noexcept { - const auto unit = static_cast(codeunit); d[0] = '\\'; d[1] = 'u'; - d[2] = nibble_to_hex[(unit >> 12u) & 0x0Fu]; - d[3] = nibble_to_hex[(unit >> 8u) & 0x0Fu]; - d[4] = nibble_to_hex[(unit >> 4u) & 0x0Fu]; - d[5] = nibble_to_hex[unit & 0x0Fu]; - return d + 6; + return hex4_lower(codeunit, d + 2); } /*! @@ -1817,7 +1794,7 @@ class serializer * Branch-free replacement for `snprintf(buf, 7, "\\u%04x", codeunit)` in the * string escaping hot path. It writes exactly six characters ('\\', 'u' and * four hex digits) at position @a pos of @a string_buffer via a nibble - * lookup table, avoiding the format-string parsing and locale machinery of + * lookup table (@ref hex4_lower), avoiding the format-string parsing and locale machinery of * `snprintf`. Advances @a pos by the number of bytes written (6). * * @param[in] pos position in @a string_buffer to write at; there must @@ -1827,13 +1804,9 @@ class serializer void write_u_escape(std::size_t& pos, std::uint16_t codeunit) noexcept { JSON_ASSERT(string_buffer.size() - pos >= 6); - constexpr const char* nibble_to_hex = "0123456789abcdef"; string_buffer[pos + 0] = '\\'; string_buffer[pos + 1] = 'u'; - string_buffer[pos + 2] = nibble_to_hex[(codeunit >> 12u) & 0x0Fu]; - string_buffer[pos + 3] = nibble_to_hex[(codeunit >> 8u) & 0x0Fu]; - string_buffer[pos + 4] = nibble_to_hex[(codeunit >> 4u) & 0x0Fu]; - string_buffer[pos + 5] = nibble_to_hex[codeunit & 0x0Fu]; + hex4_lower(codeunit, &string_buffer[pos + 2]); pos += 6; } diff --git a/include/nlohmann/detail/string_utils.hpp b/include/nlohmann/detail/string_utils.hpp index 0b17714c7..4b1213c80 100644 --- a/include/nlohmann/detail/string_utils.hpp +++ b/include/nlohmann/detail/string_utils.hpp @@ -49,6 +49,24 @@ inline std::string hex_byte(const std::uint8_t byte) return result; } +/*! +@brief write a UTF-16 code unit as four lowercase hexadecimal digits + +The lowercase counterpart of hex_byte(), used for the `\\uXXXX` escapes of the +serializers, whose output is lowercase. Writes exactly four characters to +@a out and returns the pointer past them. +*/ +JSON_HEDLEY_ALWAYS_INLINE char* hex4_lower(const std::uint16_t codeunit, char* out) noexcept +{ + constexpr const char* nibble_to_hex = "0123456789abcdef"; + const unsigned int unit = codeunit; + out[0] = nibble_to_hex[(unit >> 12u) & 0x0Fu]; + out[1] = nibble_to_hex[(unit >> 8u) & 0x0Fu]; + out[2] = nibble_to_hex[(unit >> 4u) & 0x0Fu]; + out[3] = nibble_to_hex[unit & 0x0Fu]; + return out + 4; +} + /////////////////// // UTF-8 encoding // /////////////////// @@ -104,6 +122,66 @@ void encode_utf8(std::uint32_t cp, const Out& out) } } +/*! +@brief the shape of a well-formed UTF-8 sequence, by its lead byte + +@a length is 1 for ASCII, 2 to 4 for a valid lead byte, and 0 for a byte that +cannot start a well-formed sequence (a continuation byte, 0xC0, 0xC1, or 0xF5 +to 0xFF). For a multibyte sequence, the second byte must lie in +[@a second_lo, @a second_hi], and every later byte in [0x80, 0xBF] +(RFC 3629, section 4; Unicode Table 3-7). The narrower second-byte ranges +exclude overlong forms (0xE0, 0xF0), surrogates (0xED), and code points +beyond U+10FFFF (0xF4). +*/ +struct utf8_sequence_shape +{ + std::uint8_t length; + std::uint8_t second_lo; + std::uint8_t second_hi; +}; + +/// @return the shape of a UTF-8 sequence starting with @a lead, see utf8_sequence_shape +inline utf8_sequence_shape utf8_shape(const std::uint8_t lead) noexcept +{ + if (lead < 0x80u) + { + return {1, 0, 0}; + } + if (lead < 0xC2u) + { + return {0, 0, 0}; + } + if (lead <= 0xDFu) + { + return {2, 0x80u, 0xBFu}; + } + if (lead == 0xE0u) + { + return {3, 0xA0u, 0xBFu}; + } + if (lead == 0xEDu) + { + return {3, 0x80u, 0x9Fu}; + } + if (lead <= 0xEFu) + { + return {3, 0x80u, 0xBFu}; + } + if (lead == 0xF0u) + { + return {4, 0x90u, 0xBFu}; + } + if (lead <= 0xF3u) + { + return {4, 0x80u, 0xBFu}; + } + if (lead == 0xF4u) + { + return {4, 0x80u, 0x8Fu}; + } + return {0, 0, 0}; +} + /////////////////// // UTF-8 decoding // /////////////////// @@ -119,7 +197,7 @@ This is a single-byte step of a "shift-based" UTF-8 decoder originally written by Björn Hoehrmann. See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. -The library checks UTF-8 well-formedness (RFC 3629, section 4) in three +The library checks UTF-8 well-formedness (RFC 3629, section 4) in five places, which differ in speed, diagnostics, and how they read the input: - decode() below: the serializer, to escape and, in strict mode, reject @@ -133,10 +211,15 @@ places, which differ in speed, diagnostics, and how they read the input: bulk string scan, the bulk path of the BON8 reader, and the BON8 writer. They must accept exactly what the lexer's switch accepts. - the byte path of binary_reader::get_bon8_string(): BON8 input without bulk - access, and the bytes the bulk path leaves to it. + access, and the bytes the bulk path leaves to it. It keeps its own ranges: + taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891). +- check_utf8() in detail/view/edit.hpp: strings stored in an editable + json_document, with the index of the offending byte. It also uses + utf8_shape(). -All four must accept the same set of sequences, so a change to one needs a -matching change to the others. +All five must accept the same set of sequences, which the test "UTF-8 +validators accept the same sequences" in unit-class_lexer.cpp checks +exhaustively against decode() and utf8_shape(). @param[in,out] state the current decoder state @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT) diff --git a/include/nlohmann/detail/value_t.hpp b/include/nlohmann/detail/value_t.hpp index 9f64cb90b..6cdff395b 100644 --- a/include/nlohmann/detail/value_t.hpp +++ b/include/nlohmann/detail/value_t.hpp @@ -177,6 +177,36 @@ inline bool owns_memory(const value_t t) noexcept } } +/*! +@brief whether a value of the given type is neither an object nor an array + +Unlike basic_json::is_primitive(), this includes value_t::discarded: it names +the types that iterators walk with a primitive_iterator_t (a single value, or no +value for null and discarded) and that JSON Pointer resolution and the +serializer treat as leaves. Every enumerator is listed so that -Wswitch-enum +flags this function when a new type is added. +*/ +inline bool is_primitive(const value_t t) noexcept +{ + switch (t) + { + case value_t::object: + case value_t::array: + return false; + + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: + default: + return true; + } +} + /*! @brief compare an integer with a floating point number without precision loss diff --git a/include/nlohmann/detail/view/edit.hpp b/include/nlohmann/detail/view/edit.hpp index ec18793c9..bad8d9da3 100644 --- a/include/nlohmann/detail/view/edit.hpp +++ b/include/nlohmann/detail/view/edit.hpp @@ -51,11 +51,6 @@ struct first_true : std::integral_constant < int, 1 + firs inline void check_utf8(const char* s, std::size_t n) { const auto* const p = reinterpret_cast(s); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) - const auto hex = [](unsigned char c) - { - constexpr const char* digits = "0123456789ABCDEF"; - return std::string{digits[c >> 4u], digits[c & 0xFu]}; - }; for (std::size_t i = 0; i < n;) { const unsigned char c = p[i]; @@ -64,39 +59,22 @@ inline void check_utf8(const char* s, std::size_t n) ++i; continue; } - std::size_t len = 0; - unsigned char lo = 0x80; - unsigned char hi = 0xBF; - if (c >= 0xC2 && c <= 0xDF) + const utf8_sequence_shape shape = utf8_shape(c); + const std::size_t len = shape.length; + if (len == 0) { - len = 2; - } - else if (c >= 0xE0 && c <= 0xEF) - { - len = 3; - lo = c == 0xE0 ? 0xA0 : 0x80; - hi = c == 0xED ? 0x9F : 0xBF; - } - else if (c >= 0xF0 && c <= 0xF4) - { - len = 4; - lo = c == 0xF0 ? 0x90 : 0x80; - hi = c == 0xF4 ? 0x8F : 0xBF; - } - else - { - throw_type_error(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex(c))); + throw_type_error(exception_id::invalid_utf8, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_byte(c))); } for (std::size_t k = 1; k < len; ++k) { if (i + k == n) { - throw_type_error(316, concat("incomplete UTF-8 string; last byte: 0x", hex(p[n - 1]))); + throw_type_error(exception_id::invalid_utf8, concat("incomplete UTF-8 string; last byte: 0x", hex_byte(p[n - 1]))); } const unsigned char b = p[i + k]; - if (b < (k == 1 ? lo : 0x80) || b > (k == 1 ? hi : 0xBF)) + if (b < (k == 1 ? shape.second_lo : 0x80) || b > (k == 1 ? shape.second_hi : 0xBF)) { - throw_type_error(316, concat("invalid UTF-8 byte at index ", std::to_string(i + k), ": 0x", hex(b))); + throw_type_error(exception_id::invalid_utf8, concat("invalid UTF-8 byte at index ", std::to_string(i + k), ": 0x", hex_byte(b))); } } i += len; @@ -144,7 +122,7 @@ class editor node* const o = own(object); if (o->kind != static_cast(value_t::object) && o->kind != static_cast(value_t::null)) { - throw_type_error(305, "cannot use operator[] with a string argument with ", object.type_name()); + throw_type_error(exception_id::subscript_wrong_type, "cannot use operator[] with a string argument with ", object.type_name()); } check_utf8(key.data(), key.size()); const encoded e = encode(std::forward(value)); @@ -195,7 +173,7 @@ class editor node* const a = own(array); if (a->kind != static_cast(value_t::array)) { - throw_type_error(305, "cannot use operator[] with a numeric argument with ", array.type_name()); + throw_type_error(exception_id::subscript_wrong_type, "cannot use operator[] with a numeric argument with ", array.type_name()); } check_index(idx, a->len); const encoded e = encode(std::forward(value)); @@ -212,7 +190,7 @@ class editor node* const a = own(array); if (a->kind != static_cast(value_t::array) && a->kind != static_cast(value_t::null)) { - throw_type_error(308, "cannot use push_back() with ", array.type_name()); + throw_type_error(exception_id::push_back_wrong_type, "cannot use push_back() with ", array.type_name()); } const encoded e = encode(std::forward(value)); if (a->kind == static_cast(value_t::null)) @@ -236,7 +214,7 @@ class editor node* const a = own(array); if (a->kind != static_cast(value_t::array)) { - throw_type_error(309, "cannot use insert() with ", array.type_name()); + throw_type_error(exception_id::insert_wrong_type, "cannot use insert() with ", array.type_name()); } check_index(idx, a->len + 1); const encoded e = encode(std::forward(value)); @@ -256,7 +234,7 @@ class editor node* const o = own(object); if (o->kind != static_cast(value_t::object)) { - throw_type_error(307, "cannot use erase() with ", object.type_name()); + throw_type_error(exception_id::erase_wrong_type, "cannot use erase() with ", object.type_name()); } for (const node* k = nav::first(m_doc, o), *end = nav::end(m_doc, o); k != end; k = document_data::after(k + 1)) { @@ -274,7 +252,7 @@ class editor node* const a = own(array); if (a->kind != static_cast(value_t::array)) { - throw_type_error(307, "cannot use erase() with ", array.type_name()); + throw_type_error(exception_id::erase_wrong_type, "cannot use erase() with ", array.type_name()); } check_index(idx, a->len); node* const h = block_of(m_doc, a, 0); @@ -297,7 +275,7 @@ class editor { if (NLOHMANN_VIEW_UNLIKELY(v.m_doc != &m_doc || v.m_node == nullptr)) { - throw_invalid_iterator(202, "view does not belong to this document"); + throw_invalid_iterator(exception_id::iterator_from_other_value, "view does not belong to this document"); } edit_state_of(m_doc); return const_cast(v.m_node); // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to this document @@ -307,7 +285,7 @@ class editor { if (idx >= limit) { - throw_out_of_range(401, concat("array index ", std::to_string(idx), " is out of range")); + throw_array_index_out_of_range(idx); } } @@ -448,7 +426,7 @@ class editor { if (NLOHMANN_VIEW_UNLIKELY(v.m_node == nullptr)) { - throw_type_error(302, "type must be a value, but is ", "discarded"); + throw_type_error(exception_id::type_mismatch, "type must be a value, but is ", "discarded"); } encoded r; if (!is_container(*v.m_node)) @@ -598,7 +576,7 @@ class editor { if (NLOHMANN_VIEW_UNLIKELY(len >= 0xFFFFFFFFu)) { - throw_out_of_range(416, "strings of 4 GiB or more are not supported by json_document"); // LCOV_EXCL_LINE + throw_out_of_range(exception_id::document_too_large, "strings of 4 GiB or more are not supported by json_document"); // LCOV_EXCL_LINE } node n = plain_node(value_t::string); n.flags = static_cast(n.flags | node_flags::edited); @@ -673,12 +651,12 @@ class editor return string_node(s.data(), s.size()); } case value_t::binary: - throw_type_error(319, "cannot store a binary value in a json_document", ""); + throw_type_error(exception_id::document_binary_value, "cannot store a binary value in a json_document", ""); case value_t::discarded: case value_t::object: case value_t::array: default: - throw_type_error(302, "type must be a value, but is ", "discarded"); + throw_type_error(exception_id::type_mismatch, "type must be a value, but is ", "discarded"); } } diff --git a/include/nlohmann/detail/view/edit_storage.hpp b/include/nlohmann/detail/view/edit_storage.hpp index 614a0fb0d..9a45bfebd 100644 --- a/include/nlohmann/detail/view/edit_storage.hpp +++ b/include/nlohmann/detail/view/edit_storage.hpp @@ -84,7 +84,7 @@ inline std::size_t text_capacity(std::size_t cap, std::size_t used, std::size_t constexpr std::size_t limit = 0xFFFFFFFFu; if (NLOHMANN_VIEW_UNLIKELY(used > limit || n > limit - used)) { - throw_out_of_range(416, "edits of 4 GiB or more are not supported by json_document"); + throw_out_of_range(exception_id::document_too_large, "edits of 4 GiB or more are not supported by json_document"); } const std::size_t needed = used + n; const std::size_t wanted = needed + (std::min)(limit - needed, std::size_t{256}); @@ -135,7 +135,7 @@ inline void reserve_moved(document_data& d) document_data::edit_state& e = edit_state_of(d); if (e.moved.size() >= 0xFFFFFFFFu) { - throw_out_of_range(416, "more than 4294967295 edited arrays and objects are not supported by json_document"); // LCOV_EXCL_LINE + throw_out_of_range(exception_id::document_too_large, "more than 4294967295 edited arrays and objects are not supported by json_document"); // LCOV_EXCL_LINE } if (e.moved.size() == e.moved.capacity() || e.moved_cap.size() == e.moved_cap.capacity()) { diff --git a/include/nlohmann/detail/view/errors.hpp b/include/nlohmann/detail/view/errors.hpp index 1a2542ab3..2c7270b32 100644 --- a/include/nlohmann/detail/view/errors.hpp +++ b/include/nlohmann/detail/view/errors.hpp @@ -25,32 +25,52 @@ namespace view // Exceptions are thrown out of line, so that the accessors that may throw stay // small enough to be inlined. -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(int id, const char* prefix, const char* type) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(exception_id id, const char* prefix, const char* type) { NLOHMANN_VIEW_THROW(type_error::create(id, concat(prefix, type), nullptr)); } -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(int id, const std::string& msg) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(exception_id id, const std::string& msg) { NLOHMANN_VIEW_THROW(type_error::create(id, msg, nullptr)); } -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_out_of_range(int id, const std::string& msg) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_out_of_range(exception_id id, const std::string& msg) { NLOHMANN_VIEW_THROW(out_of_range::create(id, msg, nullptr)); } -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_invalid_iterator(int id, const char* msg) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_invalid_iterator(exception_id id, const char* msg) { NLOHMANN_VIEW_THROW(invalid_iterator::create(id, msg, nullptr)); } /// a parse error without a position (as those of json_pointer) -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_parse_error(int id, const std::string& msg) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_parse_error(exception_id id, const std::string& msg) { NLOHMANN_VIEW_THROW(parse_error::create(id, 0, msg, nullptr)); } +/// out_of_range.401: the array index @a idx is not below the size +template +[[noreturn]] NLOHMANN_VIEW_NOINLINE void throw_array_index_out_of_range(const Index idx) +{ + throw_out_of_range(exception_id::array_index_out_of_range, concat("array index ", std::to_string(idx), " is out of range")); +} + +/// out_of_range.402: the JSON Pointer token '-' of an array of @a size elements +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_array_index_past_end(std::size_t size) +{ + throw_out_of_range(exception_id::pointer_past_the_end_index, concat("array index '-' (", std::to_string(size), ") is out of range")); +} + +/// out_of_range.403: the object has no member @a key +template +[[noreturn]] NLOHMANN_VIEW_NOINLINE void throw_key_not_found(const Key& key) +{ + throw_out_of_range(exception_id::key_not_found, concat("key '", key, "' not found")); +} + /// SAX handler of the error replay: accepts every event without storing it, /// and throws the exception the parser reports template @@ -159,7 +179,7 @@ template if (f.code == error_code::input_too_large) { // (the limit is detail::view::max_input_size(): 4 GiB minus 16 bytes) - NLOHMANN_VIEW_THROW(out_of_range::create(416, "input of 4294967280 bytes or more is not supported by json_document", nullptr)); + NLOHMANN_VIEW_THROW(out_of_range::create(exception_id::document_too_large, "input of 4294967280 bytes or more is not supported by json_document", nullptr)); } throwing_sax sax; const bool accepted = BasicJsonType::sax_parse(src, src + size, &sax, input_format_t::json, true, ignore_comments, ignore_trailing_commas); @@ -179,7 +199,7 @@ template } } pos.chars_read_current_line = off + 1 - line_start; - NLOHMANN_VIEW_THROW(parse_error::create(101, pos, "syntax error while parsing value", nullptr)); + NLOHMANN_VIEW_THROW(parse_error::create(exception_id::syntax_error, pos, "syntax error while parsing value", nullptr)); // LCOV_EXCL_STOP } diff --git a/include/nlohmann/detail/view/image.hpp b/include/nlohmann/detail/view/image.hpp index 702a55530..af9994712 100644 --- a/include/nlohmann/detail/view/image.hpp +++ b/include/nlohmann/detail/view/image.hpp @@ -186,7 +186,7 @@ inline void compact_nodes(const document_data& d, std::size_t arena_size, std::v inline std::vector save_image(const document_data& d) { #if !NLOHMANN_VIEW_LITTLE_ENDIAN - throw_type_error(320, "json_document images need a little-endian target"); // LCOV_EXCL_LINE + throw_type_error(exception_id::document_image_unsupported, "json_document images need a little-endian target"); // LCOV_EXCL_LINE #endif const std::size_t arena_size = d.arena_size; std::vector compacted; @@ -203,7 +203,7 @@ inline std::vector save_image(const document_data& d) if (NLOHMANN_VIEW_UNLIKELY(text_size >= image_limit || total_arena >= image_limit || count >= image_limit)) { // LCOV_EXCL_START (4 GiB) - throw_out_of_range(416, "images of 4 GiB or more are not supported by json_document"); + throw_out_of_range(exception_id::document_too_large, "images of 4 GiB or more are not supported by json_document"); // LCOV_EXCL_STOP } image_header h{}; @@ -660,7 +660,7 @@ inline bool check_image(const node* nodes, std::size_t count, const unsigned cha [[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_invalid_image(const char* what) { - throw_parse_error(116, concat("invalid json_document image: ", what)); + throw_parse_error(exception_id::invalid_document_image, concat("invalid json_document image: ", what)); } /// Read an image into d. The text and the decoded strings stay in the image; @@ -668,7 +668,7 @@ inline bool check_image(const node* nodes, std::size_t count, const unsigned cha inline void load_image(document_data& d, const std::uint8_t* image, std::size_t size, image_check check) { #if !NLOHMANN_VIEW_LITTLE_ENDIAN - throw_type_error(320, "json_document images need a little-endian target"); // LCOV_EXCL_LINE + throw_type_error(exception_id::document_image_unsupported, "json_document images need a little-endian target"); // LCOV_EXCL_LINE #endif if (image == nullptr || size < sizeof(image_header)) { diff --git a/include/nlohmann/detail/view/iterator.hpp b/include/nlohmann/detail/view/iterator.hpp index 069f50fd1..9d8246210 100644 --- a/include/nlohmann/detail/view/iterator.hpp +++ b/include/nlohmann/detail/view/iterator.hpp @@ -110,7 +110,7 @@ class view_iterator { if (NLOHMANN_VIEW_UNLIKELY(m_value_offset == 0)) { - throw_invalid_iterator(207, "cannot use key() for non-object iterators"); + throw_invalid_iterator(exception_id::iterator_key_not_object, "cannot use key() for non-object iterators"); } return string_view_t(m_doc->str(*m_pos), m_pos->len); } diff --git a/include/nlohmann/detail/view/materialize.hpp b/include/nlohmann/detail/view/materialize.hpp index 006bdd0f3..62adb30a9 100644 --- a/include/nlohmann/detail/view/materialize.hpp +++ b/include/nlohmann/detail/view/materialize.hpp @@ -143,6 +143,8 @@ BasicJsonType materialize(const document_data& d, const node* n) case value_t::binary: case value_t::discarded: default: + // false positive: the parser's frame stack points into its own storage (never null) + // @infer-ignore NULLPTR_DEREFERENCE sax.null(); break; } diff --git a/include/nlohmann/detail/view/pointer.hpp b/include/nlohmann/detail/view/pointer.hpp index fb957b604..be5aa14de 100644 --- a/include/nlohmann/detail/view/pointer.hpp +++ b/include/nlohmann/detail/view/pointer.hpp @@ -32,51 +32,15 @@ enum class pointer_mode contains, ///< as basic_json::contains(): no exceptions at all }; -/// the outcome of reading an array index from a reference token -enum class index_status -{ - ok, - leading_zero, ///< parse_error.106 - not_number, ///< parse_error.109 - unresolved, ///< out_of_range.404 - too_large, ///< out_of_range.410 -}; +/// the outcome of reading an array index: detail::parse_array_index(), shared with json_pointer +using index_status = detail::array_index_status; /// reads an array index like json_pointer::array_index (RFC 6901, Sect. 4), /// but reports errors instead of throwing them template index_status array_index(const StringType& s, std::size_t& idx) noexcept { - // as basic_json: the empty token and "-" (past the last element) are - // unresolved rather than not a number - if (s.empty() || (s.size() == 1 && s[0] == '-')) - { - return index_status::unresolved; - } - if (s[0] < '0' || s[0] > '9') - { - return index_status::not_number; - } - if (s.size() > 1 && s[0] == '0') - { - return index_status::leading_zero; - } - std::uint64_t v = 0; - for (const auto c : s) - { - const auto d = static_cast(static_cast(c)) - '0'; - if (d > 9 || v > ((std::numeric_limits::max)() - d) / 10) - { - return index_status::unresolved; // not a number, or beyond unsigned long long - } - v = (v * 10) + d; - } - if (v >= (std::numeric_limits::max)()) // (std::size_t converts to std::uint64_t implicitly) - { - return index_status::too_large; - } - idx = static_cast(v); - return index_status::ok; + return detail::parse_array_index(s, idx); } /// throws the exception json_pointer::array_index throws for this status @@ -86,15 +50,15 @@ template switch (status) { case index_status::leading_zero: - throw_parse_error(106, concat("array index '", s, "' must not begin with '0'")); - case index_status::not_number: - throw_parse_error(109, concat("array index '", s, "' is not a number")); - case index_status::too_large: - throw_out_of_range(410, concat("array index ", s, " exceeds size_type")); // LCOV_EXCL_LINE + throw_parse_error(exception_id::pointer_index_leading_zero, concat("array index '", s, "' must not begin with '0'")); + case index_status::not_a_number: + throw_parse_error(exception_id::pointer_index_not_a_number, concat("array index '", s, "' is not a number")); + case index_status::exceeds_size_type: + throw_out_of_range(exception_id::value_out_of_range, concat("array index ", s, " exceeds size_type")); // LCOV_EXCL_LINE case index_status::unresolved: case index_status::ok: default: - throw_out_of_range(404, concat("unresolved reference token '", s, "'")); + throw_out_of_range(exception_id::pointer_unresolved, concat("unresolved reference token '", s, "'")); } } @@ -119,7 +83,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) { if (mode == pointer_mode::checked) { - throw_out_of_range(403, concat("key '", token, "' not found")); + throw_key_not_found(token); } return View(); } @@ -131,7 +95,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) { if (throwing) { - throw_out_of_range(402, concat("array index '-' (", std::to_string(cur.size()), ") is out of range")); + throw_array_index_past_end(cur.size()); } return View(); } @@ -139,7 +103,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) const index_status status = array_index(token, idx); if (status != index_status::ok) { - const bool parse_error = status == index_status::leading_zero || status == index_status::not_number; + const bool parse_error = status == index_status::leading_zero || status == index_status::not_a_number; if (throwing || (mode == pointer_mode::value && parse_error)) { throw_array_index_error(status, token); @@ -150,7 +114,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) { if (mode == pointer_mode::checked) { - throw_out_of_range(401, concat("array index ", std::to_string(idx), " is out of range")); + throw_array_index_out_of_range(idx); } return View(); } @@ -160,7 +124,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) { if (throwing) { - throw_out_of_range(404, concat("unresolved reference token '", token, "'")); + throw_out_of_range(exception_id::pointer_unresolved, concat("unresolved reference token '", token, "'")); } return View(); } diff --git a/include/nlohmann/detail/view/serializer.hpp b/include/nlohmann/detail/view/serializer.hpp index dbaca6171..76df8f89b 100644 --- a/include/nlohmann/detail/view/serializer.hpp +++ b/include/nlohmann/detail/view/serializer.hpp @@ -1073,21 +1073,17 @@ class view_serializer m_out.put("null"); return; } - static constexpr bool is_ieee_single_or_double - = std::numeric_limits::is_iec559 - && ((std::numeric_limits::digits == 24 && std::numeric_limits::max_exponent == 128) - || (std::numeric_limits::digits == 53 && std::numeric_limits::max_exponent == 1024)); - write_float(x, std::integral_constant()); + write_float(x, std::integral_constant::value>()); } - void write_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/) + void write_float(number_float_t x, std::true_type /*has_native_float_format*/) { std::array buf{}; const char* const end = ::nlohmann::detail::to_chars(buf.data(), buf.data() + buf.size(), x); m_out.put(buf.data(), static_cast(end - buf.data())); } - void write_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/) + void write_float(number_float_t x, std::false_type /*has_native_float_format*/) { // other types (e.g. long double) are rare: the library writes them const string_t s = BasicJsonType(x).dump(); @@ -1148,8 +1144,8 @@ class view_serializer void write_u_escape(std::uint32_t u) { - static constexpr const char* hex = "0123456789abcdef"; - const std::array e = {{'\\', 'u', hex[(u >> 12u) & 0xFu], hex[(u >> 8u) & 0xFu], hex[(u >> 4u) & 0xFu], hex[u & 0xFu]}}; + std::array e = {{'\\', 'u', 0, 0, 0, 0}}; + hex4_lower(static_cast(u), &e[2]); m_out.put(e.data(), e.size()); } diff --git a/include/nlohmann/detail/view/value.hpp b/include/nlohmann/detail/view/value.hpp index 1f1eafde4..945071705 100644 --- a/include/nlohmann/detail/view/value.hpp +++ b/include/nlohmann/detail/view/value.hpp @@ -60,7 +60,7 @@ NLOHMANN_VIEW_ALWAYS_INLINE T arithmetic_value(const document_data& d, const nod case value_t::binary: case value_t::discarded: default: - throw_type_error(302, "type must be number, but is ", value_type_name(static_cast(n.kind))); + throw_type_error(exception_id::type_mismatch, "type must be number, but is ", value_type_name(static_cast(n.kind))); } } @@ -70,7 +70,7 @@ std::vector vector_value(const View& v) { if (NLOHMANN_VIEW_UNLIKELY(!v.is_array())) { - throw_type_error(302, "type must be array, but is ", v.type_name()); + throw_type_error(exception_id::type_mismatch, "type must be array, but is ", v.type_name()); } std::vector r; r.reserve(v.size()); @@ -88,7 +88,7 @@ Map map_value(const View& v) { if (NLOHMANN_VIEW_UNLIKELY(!v.is_object())) { - throw_type_error(302, "type must be object, but is ", v.type_name()); + throw_type_error(exception_id::type_mismatch, "type must be object, but is ", v.type_name()); } Map r; for (auto it = v.begin(); it != v.end(); ++it) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 56b466baf..6dc2d7492 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -55,7 +55,7 @@ #include // IWYU pragma: export #include // IWYU pragma: keep #include // IWYU pragma: keep -#include +#include // IWYU pragma: export #include #include #include @@ -376,9 +376,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec result["name"] = "JSON for Modern C++"; result["url"] = "https://github.com/nlohmann/json"; result["version"]["string"] = - detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.', - std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.', - std::to_string(NLOHMANN_JSON_VERSION_PATCH)); + detail::concat(detail::to_string(static_cast(NLOHMANN_JSON_VERSION_MAJOR)), '.', + detail::to_string(static_cast(NLOHMANN_JSON_VERSION_MINOR)), '.', + detail::to_string(static_cast(NLOHMANN_JSON_VERSION_PATCH))); result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR; result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR; result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH; @@ -400,10 +400,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #elif defined(__clang__) result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}}; #elif defined(__GNUC__) || defined(__GNUG__) - result["compiler"] = {{"family", "gcc"}, {"version", detail::concat( - std::to_string(__GNUC__), '.', - std::to_string(__GNUC_MINOR__), '.', - std::to_string(__GNUC_PATCHLEVEL__)) + result["compiler"] = {{"family", "gcc"}, {"version", detail::concat( + detail::to_string(static_cast(__GNUC__)), '.', + detail::to_string(static_cast(__GNUC_MINOR__)), '.', + detail::to_string(static_cast(__GNUC_PATCHLEVEL__))) } }; #elif defined(__HP_aCC) @@ -421,9 +421,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #endif #if defined(_MSVC_LANG) - result["compiler"]["c++"] = std::to_string(_MSVC_LANG); + result["compiler"]["c++"] = detail::to_string(static_cast(_MSVC_LANG)); #elif defined(__cplusplus) - result["compiler"]["c++"] = std::to_string(__cplusplus); + result["compiler"]["c++"] = detail::to_string(static_cast(__cplusplus)); #else result["compiler"]["c++"] = "unknown"; #endif @@ -734,23 +734,15 @@ private: // true if v is not an array/object, or is an already-empty one static bool has_no_children(const basic_json& v) noexcept { - switch (v.m_data.m_type) + if (v.m_data.m_type == value_t::array) { - case value_t::array: - return v.m_data.m_value.array->empty(); - case value_t::object: - return v.m_data.m_value.object->empty(); - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return true; + return v.m_data.m_value.array->empty(); } + if (v.m_data.m_type == value_t::object) + { + return v.m_data.m_value.object->empty(); + } + return true; } // true if no child of the non-empty array/object v has children of @@ -855,28 +847,17 @@ private: } public: - void destroy_string() noexcept + template + static void destroy_pointer(T*& ptr) noexcept { - if (string == nullptr) + if (ptr == nullptr) { // not initialized (e.g., due to exception in the ctor) return; } - AllocatorType alloc; - std::allocator_traits::destroy(alloc, string); - std::allocator_traits::deallocate(alloc, string, 1); - } - - void destroy_binary() noexcept - { - if (binary == nullptr) - { - // not initialized (e.g., due to exception in the ctor) - return; - } - AllocatorType alloc; - std::allocator_traits::destroy(alloc, binary); - std::allocator_traits::deallocate(alloc, binary, 1); + AllocatorType alloc; + std::allocator_traits::destroy(alloc, ptr); + std::allocator_traits::deallocate(alloc, ptr, 1); } // t must be value_t::array or value_t::object @@ -1042,11 +1023,11 @@ public: switch (t) { case value_t::string: - destroy_string(); + destroy_pointer(string); break; case value_t::binary: - destroy_binary(); + destroy_pointer(binary); break; case value_t::object: @@ -1108,36 +1089,19 @@ public: void set_parents() { #if JSON_DIAGNOSTICS - switch (m_data.m_type) + if (m_data.m_type == value_t::array) { - case value_t::array: + for (auto& element : *m_data.m_value.array) { - for (auto& element : *m_data.m_value.array) - { - element.m_parent = this; - } - break; + element.m_parent = this; } - - case value_t::object: + } + else if (m_data.m_type == value_t::object) + { + for (auto& element : *m_data.m_value.object) { - for (auto& element : *m_data.m_value.object) - { - element.second.m_parent = this; - } - break; + element.second.m_parent = this; } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - break; } #endif } @@ -2585,8 +2549,7 @@ public: case value_t::string: case value_t::binary: { - if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() - || !last.m_it.primitive_iterator.is_end())) + if (JSON_HEDLEY_UNLIKELY(!primitive_range_is_complete(first, last))) { detail::throw_literal(detail::exception_id::iterator_range_out_of_range, "iterators out of range", first.m_object); } @@ -3060,6 +3023,135 @@ public: JSON_THROW(std::length_error(detail::concat("array index ", std::to_string(idx), " exceeds size_type"))); } + /// @throw type_error.309 always, as insert() is not defined for the type of @a j + JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE + static void throw_insert_wrong_type(const basic_json& j) + { + detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", j); + } + + /// @throw invalid_iterator.202 always, as an iterator belongs to another value than @a obj + JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE + static void throw_iterator_from_other_value(const basic_json* obj) + { + detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", obj); + } + + /// @throw type_error.310 always, as swap() with the given operand is not defined for the type of @a j + JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE + static void throw_swap_wrong_type(const char* operation, const basic_json& j) + { + detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, operation, j); + } + + /// exchange the stored @a Stored value (if the value has that type) with @a other + template + void swap_impl(Other& other, const char* operation) + { + Stored* const stored = get_impl_ptr(static_cast(nullptr)); + if (JSON_HEDLEY_UNLIKELY(stored == nullptr)) + { + throw_swap_wrong_type(operation, *this); + } + + using std::swap; + swap(*stored, other); + } + + /// whether [first, last) spans the whole of a primitive value + template + JSON_HEDLEY_ALWAYS_INLINE + static bool primitive_range_is_complete(const IteratorType& first, const IteratorType& last) noexcept + { + return first.m_it.primitive_iterator.is_begin() && last.m_it.primitive_iterator.is_end(); + } + + /// destroy the current (primitive) value and become null + void reset_to_null_impl() + { + m_data.m_value.destroy(m_data.m_type); + m_data.m_value = {}; + m_data.m_type = value_t::null; + assert_invariant(); + } + + /// append @a val (moved or copied) to the array; a null value becomes an array + template + JSON_HEDLEY_ALWAYS_INLINE + void push_back_array_impl(ValueType&& val) + { + // push_back only works for null objects or arrays + if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) + { + detail::throw_cannot_use_with(detail::exception_id::push_back_wrong_type, "push_back()", *this); + } + + // transform a null object into an array + if (is_null()) + { + convert_null_to(); + } + + // add the element to the array + const auto old_capacity = container_capacity(); + m_data.m_value.array->push_back(std::forward(val)); + set_parent_after_insert(m_data.m_value.array->back(), old_capacity); + } + + /// parse the input behind an already created adapter + template + static basic_json parse_impl(InputAdapterType&& adapter, + parser_callback_t cb, + const bool allow_exceptions, + const bool ignore_comments, + const bool ignore_trailing_commas) + { + basic_json result; + parser(std::forward(adapter), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas).parse(true, result); + return result; + } + + /// check the input behind an already created adapter + template + static bool accept_impl(InputAdapterType&& adapter, + const bool ignore_comments, + const bool ignore_trailing_commas) + { + return parser(std::forward(adapter), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true); + } + + /// generate SAX events from the input behind an already created adapter + template + static bool sax_parse_impl(InputAdapterType ia, SAX* sax, + const input_format_t format, + const bool strict, + const bool ignore_comments, + const bool ignore_trailing_commas, + const cbor_tag_handler_t tag_handler) + { + if (format == input_format_t::json) + { + return parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict); + } + // the SAX parser may recover from errors (see parse_error()) + return detail::binary_reader(std::move(ia), format).sax_parse(sax, strict, tag_handler); + } + + /// serialize @a j into a new vector; @a write receives the vector-backed writer + template + static std::vector to_binary_vector(const basic_json& j, const error_handler_t error_handler, WriteFunction write) + { + std::vector result; + result.reserve(detail::binary_initial_capacity(j)); + { + // the sink sets the size of result when it is destroyed, which + // must happen before result is returned (without NRVO, it is moved) + auto writer = vector_writer(result, error_handler); + write(writer); + } + return result; + } + /// @throw out_of_range.401 always, as @a idx is out of range of @a j JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE static void throw_array_index_out_of_range(size_type idx, const basic_json& j) @@ -4042,7 +4134,7 @@ public: // make sure the iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } IteratorType result = end(); @@ -4061,10 +4153,7 @@ public: detail::throw_literal(detail::exception_id::iterator_out_of_range, "iterator out of range", this); } - m_data.m_value.destroy(m_data.m_type); - m_data.m_value = {}; - m_data.m_type = value_t::null; - assert_invariant(); + reset_to_null_impl(); break; } @@ -4114,16 +4203,12 @@ public: case value_t::string: case value_t::binary: { - if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() - || !last.m_it.primitive_iterator.is_end())) + if (JSON_HEDLEY_UNLIKELY(!primitive_range_is_complete(first, last))) { detail::throw_literal(detail::exception_id::iterator_range_out_of_range, "iterators out of range", this); } - m_data.m_value.destroy(m_data.m_type); - m_data.m_value = {}; - m_data.m_type = value_t::null; - assert_invariant(); + reset_to_null_impl(); break; } @@ -4604,34 +4689,20 @@ public: JSON_HEDLEY_WARN_UNUSED_RESULT size_type max_size() const noexcept { - switch (m_data.m_type) + if (m_data.m_type == value_t::array) { - case value_t::array: - { - // delegate call to array_t::max_size() - return m_data.m_value.array->max_size(); - } - - case value_t::object: - { - // delegate call to object_t::max_size() - return m_data.m_value.object->max_size(); - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // all other types have max_size() == size() - return size(); - } + // delegate call to array_t::max_size() + return m_data.m_value.array->max_size(); } + + if (m_data.m_type == value_t::object) + { + // delegate call to object_t::max_size() + return m_data.m_value.object->max_size(); + } + + // all other types have max_size() == size() + return size(); } /// @} @@ -4709,22 +4780,7 @@ public: /// @sa https://json.nlohmann.me/api/basic_json/push_back/ void push_back(basic_json&& val) { - // push_back only works for null objects or arrays - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) - { - detail::throw_cannot_use_with(detail::exception_id::push_back_wrong_type, "push_back()", *this); - } - - // transform a null object into an array - if (is_null()) - { - convert_null_to(); - } - - // add the element to the array (move semantics) - const auto old_capacity = container_capacity(); - m_data.m_value.array->push_back(std::move(val)); - set_parent_after_insert(m_data.m_value.array->back(), old_capacity); + push_back_array_impl(std::move(val)); // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor } @@ -4740,22 +4796,7 @@ public: /// @sa https://json.nlohmann.me/api/basic_json/push_back/ void push_back(const basic_json& val) { - // push_back only works for null objects or arrays - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) - { - detail::throw_cannot_use_with(detail::exception_id::push_back_wrong_type, "push_back()", *this); - } - - // transform a null object into an array - if (is_null()) - { - convert_null_to(); - } - - // add the element to the array - const auto old_capacity = container_capacity(); - m_data.m_value.array->push_back(val); - set_parent_after_insert(m_data.m_value.array->back(), old_capacity); + push_back_array_impl(val); } /// @brief add an object to an array @@ -4901,13 +4942,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // insert to array and return iterator @@ -4921,13 +4962,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // moving into a local first keeps this safe even if val aliases @@ -4943,13 +4984,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // insert to array and return iterator @@ -4963,13 +5004,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // check if range iterators belong to the same JSON object @@ -5000,13 +5041,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // copy the values first: ilist may refer to elements of this array @@ -5028,7 +5069,7 @@ public: // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if range iterators belong to the same JSON object @@ -5270,82 +5311,37 @@ public: /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) { - // swap only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - using std::swap; - swap(*(m_data.m_value.array), other); - set_parents(); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(array_t&)", *this); - } + swap_impl(other, "swap(array_t&)"); + set_parents(); } /// @brief exchanges the values /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) { - // swap only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - using std::swap; - swap(*(m_data.m_value.object), other); - set_parents(); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(object_t&)", *this); - } + swap_impl(other, "swap(object_t&)"); + set_parents(); } /// @brief exchanges the values /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) { - // swap only works for strings - if (JSON_HEDLEY_LIKELY(is_string())) - { - using std::swap; - swap(*(m_data.m_value.string), other); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(string_t&)", *this); - } + swap_impl(other, "swap(string_t&)"); } /// @brief exchanges the values /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) { - // swap only works for binary values - if (JSON_HEDLEY_LIKELY(is_binary())) - { - using std::swap; - swap(*(m_data.m_value.binary), other); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(binary_t&)", *this); - } + swap_impl(other, "swap(binary_t&)"); } /// @brief exchanges the values /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape) { - // swap only works for binary values - if (JSON_HEDLEY_LIKELY(is_binary())) - { - using std::swap; - swap(*(m_data.m_value.binary), other); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(binary_t::container_type&)", *this); - } + swap_impl(other, "swap(binary_t::container_type&)"); } /// @} @@ -6026,10 +6022,7 @@ public: const bool ignore_comments = false, const bool ignore_trailing_commas = false) { - basic_json result; - auto p = parser(detail::input_adapter(std::forward(i)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); - p.parse(true, result); - return result; + return parse_impl(detail::input_adapter(std::forward(i)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); } /// @brief deserialize from a pair of character iterators (or an iterator+sentinel pair, C++20 ranges support) @@ -6044,10 +6037,7 @@ public: const bool ignore_comments = false, const bool ignore_trailing_commas = false) { - basic_json result; - auto p = parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); - p.parse(true, result); - return result; + return parse_impl(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); } JSON_HEDLEY_WARN_UNUSED_RESULT @@ -6061,10 +6051,7 @@ public: = delete; #else { - basic_json result; - auto p = parser(i.get(), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); - p.parse(true, result); - return result; + return parse_impl(i.get(), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); } #endif @@ -6076,7 +6063,7 @@ public: const bool ignore_comments = false, const bool ignore_trailing_commas = false) { - return parser(detail::input_adapter(std::forward(i)), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true); + return accept_impl(detail::input_adapter(std::forward(i)), ignore_comments, ignore_trailing_commas); } /// @brief check if the input is valid JSON (iterator pair, or iterator+sentinel pair for C++20 ranges support) @@ -6088,7 +6075,7 @@ public: const bool ignore_comments = false, const bool ignore_trailing_commas = false) { - return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true); + return accept_impl(detail::input_adapter(std::move(first), std::move(last)), ignore_comments, ignore_trailing_commas); } JSON_HEDLEY_WARN_UNUSED_RESULT @@ -6100,7 +6087,7 @@ public: = delete; #else { - return parser(i.get(), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true); + return accept_impl(i.get(), ignore_comments, ignore_trailing_commas); } #endif @@ -6115,10 +6102,7 @@ public: const bool ignore_trailing_commas = false, const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { - auto ia = detail::input_adapter(std::forward(i)); - return format == input_format_t::json - ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(sax, strict, tag_handler); + return sax_parse_impl(detail::input_adapter(std::forward(i)), sax, format, strict, ignore_comments, ignore_trailing_commas, tag_handler); } /// @brief generate SAX events (iterator pair, or iterator+sentinel pair for C++20 ranges support) @@ -6133,10 +6117,7 @@ public: const bool ignore_trailing_commas = false, const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { - auto ia = detail::input_adapter(std::move(first), std::move(last)); - return format == input_format_t::json - ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(sax, strict, tag_handler); + return sax_parse_impl(detail::input_adapter(std::move(first), std::move(last)), sax, format, strict, ignore_comments, ignore_trailing_commas, tag_handler); } /// @brief generate SAX events @@ -6170,12 +6151,7 @@ public: = delete; #else { - auto ia = i.get(); - return format == input_format_t::json - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict) - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - : detail::binary_reader(std::move(ia), format).sax_parse(sax, strict, tag_handler); + return sax_parse_impl(i.get(), sax, format, strict, ignore_comments, ignore_trailing_commas, tag_handler); } #endif #if defined(__clang__) @@ -6323,10 +6299,10 @@ public: static std::vector to_cbor(const basic_json& j, const error_handler_t error_handler = detail::binary_writer_default_error_handler()) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_cbor(j); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_cbor(j); + }); } /// @brief create a CBOR serialization of a given JSON value @@ -6354,10 +6330,10 @@ public: static std::vector to_msgpack(const basic_json& j, const error_handler_t error_handler = error_handler_t::keep) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_msgpack(j); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_msgpack(j); + }); } /// @brief create a MessagePack serialization of a given JSON value @@ -6387,10 +6363,10 @@ public: const bool use_type = false, const error_handler_t error_handler = detail::binary_writer_default_error_handler()) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_ubjson(j, use_size, use_type); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_ubjson(j, use_size, use_type); + }); } /// @brief create a UBJSON serialization of a given JSON value @@ -6423,10 +6399,10 @@ public: const bjdata_version_t version = bjdata_version_t::draft2, const error_handler_t error_handler = detail::binary_writer_default_error_handler()) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_ubjson(j, use_size, use_type, true, true, version); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_ubjson(j, use_size, use_type, true, true, version); + }); } /// @brief create a BJData serialization of a given JSON value @@ -6458,10 +6434,10 @@ public: static std::vector to_bson(const basic_json& j, const error_handler_t error_handler = detail::binary_writer_default_error_handler()) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_bson(j); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_bson(j); + }); } /// @brief create a BSON serialization of a given JSON value @@ -6488,10 +6464,10 @@ public: /// @sa https://json.nlohmann.me/api/basic_json/to_bon8/ static std::vector to_bon8(const basic_json& j) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result).write_bon8(j); - return result; + return to_binary_vector(j, detail::binary_writer_default_error_handler(), [&](vector_binary_writer& w) + { + w.write_bon8(j); + }); } /// @brief create a BON8 serialization of a given JSON value @@ -7827,82 +7803,68 @@ public: return; } - switch (source.type()) + if (detail::is_primitive(source.type())) { - case value_t::array: + // both primitive types: replace value + diff_replace(result, path, target); + return; + } + + if (source.is_array()) + { + const std::size_t path_length = path.size(); + + // first pass: traverse common elements + std::size_t i = 0; + while (i < source.size() && i < target.size()) { + // recursive call to compare array values at index i + if (!(source[i] == target[i])) + { + diff_append_index(path, i); + diff_unequal(result, source[i], target[i], path, depth + 1); + path.resize(path_length); + } + ++i; + } + + // We now reached the end of at least one array + // in a second pass, traverse the remaining elements + diff_array_tails(result, source, target, path, i); + } + else + { + diff_target_values target_values; + std::vector added; + if (diff_object_keys(result, source, target, path, target_values, added)) + { + // fast path: target_values holds, for every member of + // source in order, its value in target -- so no further + // lookup is needed. Deleted keys are interleaved here + // too, in source's original order, to match the + // historical (pre-reordering-aware) output order. const std::size_t path_length = path.size(); - - // first pass: traverse common elements std::size_t i = 0; - while (i < source.size() && i < target.size()) + for (auto it = source.cbegin(); it != source.cend(); ++it, ++i) { - // recursive call to compare array values at index i - if (!(source[i] == target[i])) + if (target_values[i] != nullptr) { - diff_append_index(path, i); - diff_unequal(result, source[i], target[i], path, depth + 1); - path.resize(path_length); - } - ++i; - } - - // We now reached the end of at least one array - // in a second pass, traverse the remaining elements - diff_array_tails(result, source, target, path, i); - break; - } - - case value_t::object: - { - diff_target_values target_values; - std::vector added; - if (diff_object_keys(result, source, target, path, target_values, added)) - { - // fast path: target_values holds, for every member of - // source in order, its value in target -- so no further - // lookup is needed. Deleted keys are interleaved here - // too, in source's original order, to match the - // historical (pre-reordering-aware) output order. - const std::size_t path_length = path.size(); - std::size_t i = 0; - for (auto it = source.cbegin(); it != source.cend(); ++it, ++i) - { - if (target_values[i] != nullptr) + if (!(it.value() == *target_values[i])) { - if (!(it.value() == *target_values[i])) - { - diff_append_key(path, it.key()); - diff_unequal(result, it.value(), *target_values[i], path, depth + 1); - path.resize(path_length); - } - } - else - { - // found a key that is not in target -> remove it - diff_remove(result, diff_key_path(path, it.key())); + diff_append_key(path, it.key()); + diff_unequal(result, it.value(), *target_values[i], path, depth + 1); + path.resize(path_length); } } - - // append the "add" operations for brand-new keys - diff_added(result, added, path); + else + { + // found a key that is not in target -> remove it + diff_remove(result, diff_key_path(path, it.key())); + } } - break; - } - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // both primitive types: replace value - diff_replace(result, path, target); - break; + // append the "add" operations for brand-new keys + diff_added(result, added, path); } } } @@ -7948,46 +7910,31 @@ public: return; } - switch (s.type()) + if (detail::is_primitive(s.type())) { - case value_t::array: - { - stack.emplace_back(&s, &t, current_path.size()); - return; - } - - case value_t::object: - { - std::vector target_values; - std::vector added; - if (diff_object_keys(result, s, t, current_path, target_values, added)) - { - // fast path: the frame walks source in lockstep with - // common_keys, as diff_recursively does, and appends - // added_ops once all members are done - stack.emplace_back(&s, &t, current_path.size()); - stack.back().member = s.cbegin(); - stack.back().target_values = std::move(target_values); - stack.back().added = std::move(added); - } - return; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // both primitive types: replace value - diff_replace(result, current_path, t); - return; - } + // both primitive types: replace value + diff_replace(result, current_path, t); + return; } + + if (s.is_array()) + { + stack.emplace_back(&s, &t, current_path.size()); + return; + } + std::vector target_values; + std::vector added; + if (diff_object_keys(result, s, t, current_path, target_values, added)) + { + // fast path: the frame walks source in lockstep with + // common_keys, as diff_recursively does, and appends + // added_ops once all members are done + stack.emplace_back(&s, &t, current_path.size()); + stack.back().member = s.cbegin(); + stack.back().target_values = std::move(target_values); + stack.back().added = std::move(added); + } + return; }; enter(source, target); diff --git a/include/nlohmann/json_view.hpp b/include/nlohmann/json_view.hpp index 86481d884..85026008a 100644 --- a/include/nlohmann/json_view.hpp +++ b/include/nlohmann/json_view.hpp @@ -268,7 +268,7 @@ class basic_json_view { return {}; } - detail::view::throw_type_error(305, "cannot use operator[] with a string argument with ", type_name()); + detail::view::throw_type_error(detail::exception_id::subscript_wrong_type, "cannot use operator[] with a string argument with ", type_name()); } return lookup(key); } @@ -294,7 +294,7 @@ class basic_json_view { return {}; } - detail::view::throw_type_error(305, "cannot use operator[] with a numeric argument with ", type_name()); + detail::view::throw_type_error(detail::exception_id::subscript_wrong_type, "cannot use operator[] with a numeric argument with ", type_name()); } return idx < m_node->len ? element(idx) : basic_json_view(); } @@ -327,12 +327,12 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_object())) { - detail::view::throw_type_error(304, "cannot use at() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::at_wrong_type, "cannot use at() with ", type_name()); } const basic_json_view r = lookup(key); if (NLOHMANN_VIEW_UNLIKELY(r.is_discarded())) { - detail::view::throw_out_of_range(403, detail::concat("key '", std::string(key.data(), key.size()), "' not found")); + detail::view::throw_key_not_found(std::string(key.data(), key.size())); } return r; } @@ -353,11 +353,11 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_array())) { - detail::view::throw_type_error(304, "cannot use at() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::at_wrong_type, "cannot use at() with ", type_name()); } if (NLOHMANN_VIEW_UNLIKELY(idx >= m_node->len)) { - detail::view::throw_out_of_range(401, detail::concat("array index ", std::to_string(idx), " is out of range")); + detail::view::throw_array_index_out_of_range(idx); } return element(idx); } @@ -385,7 +385,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_object())) { - detail::view::throw_type_error(306, "cannot use value() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name()); } const basic_json_view r = lookup(key); return r.is_discarded() ? default_value : r.template get(); @@ -395,7 +395,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_object())) { - detail::view::throw_type_error(306, "cannot use value() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name()); } // (the default is made only if there is no such member) const basic_json_view r = lookup(key); @@ -410,7 +410,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_structured())) { - detail::view::throw_type_error(306, "cannot use value() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name()); } const basic_json_view r = detail::view::resolve_pointer(*this, detail::json_pointer_access::reference_tokens(ptr), detail::view::pointer_mode::value); return r.is_discarded() ? default_value : r.template get(); @@ -420,7 +420,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_structured())) { - detail::view::throw_type_error(306, "cannot use value() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name()); } const basic_json_view r = detail::view::resolve_pointer(*this, detail::json_pointer_access::reference_tokens(ptr), detail::view::pointer_mode::value); return r.is_discarded() ? string_t(default_value) : r.template get(); @@ -433,7 +433,7 @@ class basic_json_view const iterator it = begin(); if (NLOHMANN_VIEW_UNLIKELY(it == end())) { - detail::view::throw_invalid_iterator(214, "cannot get value"); + detail::view::throw_invalid_iterator(detail::exception_id::iterator_value_unavailable, "cannot get value"); } return *it; } @@ -586,7 +586,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_string())) { - detail::view::throw_type_error(302, "type must be string, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be string, but is ", type_name()); } return {m_doc->str(*m_node), m_node->len}; } @@ -598,7 +598,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_number())) { - detail::view::throw_type_error(302, "type must be number, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be number, but is ", type_name()); } return {m_doc->str(*m_node), detail::view::number_length(*m_node)}; } @@ -855,7 +855,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_boolean())) { - detail::view::throw_type_error(302, "type must be boolean, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be boolean, but is ", type_name()); } return (m_node->flags & detail::view::node_flags::is_true) != 0; } @@ -865,7 +865,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(m_node == nullptr)) { - detail::view::throw_type_error(302, "type must be number, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be number, but is ", type_name()); } return detail::view::arithmetic_value(*m_doc, *m_node); } @@ -874,7 +874,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_null())) { - detail::view::throw_type_error(302, "type must be null, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be null, but is ", type_name()); } return nullptr; } @@ -1183,7 +1183,7 @@ class basic_json_document { if (NLOHMANN_VIEW_UNLIKELY(!m_data || m_data->discarded)) { - detail::view::throw_type_error(320, "cannot save a discarded json_document"); + detail::view::throw_type_error(detail::exception_id::document_image_unsupported, "cannot save a discarded json_document"); } return detail::view::save_image(*m_data); } @@ -1284,7 +1284,7 @@ class basic_json_document const std::size_t idx = pointer_index(token); if (idx >= 0xFFFFFFFFu) { - detail::view::throw_out_of_range(401, detail::concat("array index ", std::to_string(idx), " is out of range")); + detail::view::throw_array_index_out_of_range(idx); } for (std::size_t i = 0; i < idx; ++i) { @@ -1340,7 +1340,7 @@ class basic_json_document { if (ptr.empty()) { - detail::view::throw_out_of_range(405, "JSON pointer has no parent"); + detail::view::throw_out_of_range(detail::exception_id::patch_on_root, "JSON pointer has no parent"); } const view_type parent = root().at(ptr.parent_pointer()); const auto& token = ptr.back(); @@ -1360,7 +1360,7 @@ class basic_json_document static_assert(Editable, "only an editable document can be edited: use basic_json_document (json_editable_document)"); if (NLOHMANN_VIEW_UNLIKELY(!m_data || m_data->discarded)) { - detail::view::throw_invalid_iterator(202, "view does not belong to this document"); + detail::view::throw_invalid_iterator(detail::exception_id::iterator_from_other_value, "view does not belong to this document"); } return detail::view::editor(*m_data); } @@ -1377,7 +1377,7 @@ class basic_json_document { if (idx < 0) { - detail::view::throw_out_of_range(401, detail::concat("array index ", std::to_string(idx), " is out of range")); + detail::view::throw_array_index_out_of_range(idx); } return static_cast(idx); } diff --git a/include/nlohmann/ordered_map.hpp b/include/nlohmann/ordered_map.hpp index e5f9164f6..831261219 100644 --- a/include/nlohmann/ordered_map.hpp +++ b/include/nlohmann/ordered_map.hpp @@ -125,6 +125,19 @@ private: Container::pop_back(); } + /// @brief remove the entry with the given key; returns the number of removed entries + template + size_type erase_impl(const K& key) + { + const auto it = find_impl(*this, key); + if (it != this->end()) + { + erase_at(it); + return 1; + } + return 0; + } + public: template::value, int> = 0> @@ -214,26 +227,14 @@ public: size_type erase(const key_type& key) { - const auto it = find_impl(*this, key); - if (it != this->end()) - { - erase_at(it); - return 1; - } - return 0; + return erase_impl(key); } template::value, int> = 0> size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward) { - const auto it = find_impl(*this, key); - if (it != this->end()) - { - erase_at(it); - return 1; - } - return 0; + return erase_impl(key); } iterator erase(iterator pos) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 1bfd56c43..097809b05 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -284,7 +284,7 @@ -#include // transform +#include // min, transform #include // array #include // forward_list #include // inserter, front_inserter, end @@ -3470,6 +3470,36 @@ inline bool owns_memory(const value_t t) noexcept } } +/*! +@brief whether a value of the given type is neither an object nor an array + +Unlike basic_json::is_primitive(), this includes value_t::discarded: it names +the types that iterators walk with a primitive_iterator_t (a single value, or no +value for null and discarded) and that JSON Pointer resolution and the +serializer treat as leaves. Every enumerator is listed so that -Wswitch-enum +flags this function when a new type is added. +*/ +inline bool is_primitive(const value_t t) noexcept +{ + switch (t) + { + case value_t::object: + case value_t::array: + return false; + + case value_t::null: + case value_t::string: + case value_t::boolean: + case value_t::number_integer: + case value_t::number_unsigned: + case value_t::number_float: + case value_t::binary: + case value_t::discarded: + default: + return true; + } +} + /*! @brief compare an integer with a floating point number without precision loss @@ -5510,6 +5540,7 @@ enum class exception_id : int invalid_string_or_size = 113, ///< a binary input contains an invalid string or size specification bson_unsupported_type = 114, ///< a BSON record type is not supported invalid_high_precision_number = 115, ///< a UBJSON/BJData high-precision number cannot be parsed + invalid_document_image = 116, ///< json_document::load() rejects an image // invalid_iterator iterators_incompatible = 201, ///< the iterators of a range belong to different values iterator_from_other_value = 202, ///< an iterator does not belong to the value it is used with @@ -5544,6 +5575,8 @@ enum class exception_id : int invalid_utf8 = 316, ///< dump() finds a string that is not valid UTF-8 type_not_serializable = 317, ///< a value cannot be serialized to the requested binary format enum_key_duplicate = 318, ///< an enum-keyed map has two keys that serialize to the same string + document_binary_value = 319, ///< a binary value is stored in a json_document + document_image_unsupported = 320, ///< a json_document image cannot be saved or loaded (discarded document, big-endian target) discarded_value_used = 321, ///< a discarded value is used // out_of_range array_index_out_of_range = 401, ///< an array index is out of range @@ -5561,6 +5594,7 @@ enum class exception_id : int patch_remove_parent_not_container = 413, ///< the parent of a JSON Patch 'remove' target is not a container patch_move_into_child = 414, ///< a JSON Patch 'move' moves a value into one of its children subtype_out_of_range = 415, ///< a binary subtype does not fit into one byte + document_too_large = 416, ///< a json_document input, its edit storage, or an image reaches the 32-bit (4 GiB) limit // other_error internal_error = 500, ///< unreachable code was reached patch_test_failed = 501, ///< a JSON Patch 'test' operation failed @@ -5590,6 +5624,13 @@ class exception : public std::exception return concat("[json.exception.", ename, '.', std::to_string(id_), "] "); } + /// build the message "[json.exception..] " + template + static std::string what_impl(const std::string& ename, int id_, const std::string& what_arg, BasicJsonContext context) + { + return concat(name(ename, id_), diagnostics(context), what_arg); + } + static std::string diagnostics(std::nullptr_t /*leaf_element*/) { return ""; @@ -5602,48 +5643,33 @@ class exception : public std::exception std::vector tokens; for (const auto* current = leaf_element; current != nullptr && current->m_parent != nullptr; current = current->m_parent) { - switch (current->m_parent->type()) + const auto* parent = current->m_parent; + if (parent->is_array()) { - case value_t::array: + for (std::size_t i = 0; i < parent->m_data.m_value.array->size(); ++i) { - for (std::size_t i = 0; i < current->m_parent->m_data.m_value.array->size(); ++i) + if (&parent->m_data.m_value.array->operator[](i) == current) { - if (¤t->m_parent->m_data.m_value.array->operator[](i) == current) - { - tokens.emplace_back(std::to_string(i)); - break; - } + tokens.emplace_back(std::to_string(i)); + break; } - break; } - - case value_t::object: - { - for (const auto& element : *current->m_parent->m_data.m_value.object) - { - if (&element.second == current) - { - // data() is null-terminated, so a key containing - // a null byte is cut short here rather than - // truncating the whole message at what() - tokens.emplace_back(element.first.data()); - break; - } - } - break; - } - - case value_t::null: // LCOV_EXCL_LINE - case value_t::string: // LCOV_EXCL_LINE - case value_t::boolean: // LCOV_EXCL_LINE - case value_t::number_integer: // LCOV_EXCL_LINE - case value_t::number_unsigned: // LCOV_EXCL_LINE - case value_t::number_float: // LCOV_EXCL_LINE - case value_t::binary: // LCOV_EXCL_LINE - case value_t::discarded: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - break; // LCOV_EXCL_LINE } + else if (parent->is_object()) + { + for (const auto& element : *parent->m_data.m_value.object) + { + if (&element.second == current) + { + // data() is null-terminated, so a key containing + // a null byte is cut short here rather than + // truncating the whole message at what() + tokens.emplace_back(element.first.data()); + break; + } + } + } + // all other parent types (primitives) contribute no token } if (tokens.empty()) @@ -5759,7 +5785,7 @@ class invalid_iterator : public exception template::value, int> = 0> static invalid_iterator create(int id_, const std::string& what_arg, BasicJsonContext context) { - const std::string w = concat(exception::name("invalid_iterator", id_), exception::diagnostics(context), what_arg); + const std::string w = exception::what_impl("invalid_iterator", id_, what_arg, context); return {id_, w.c_str()}; } @@ -5783,7 +5809,7 @@ class type_error : public exception template::value, int> = 0> static type_error create(int id_, const std::string& what_arg, BasicJsonContext context) { - const std::string w = concat(exception::name("type_error", id_), exception::diagnostics(context), what_arg); + const std::string w = exception::what_impl("type_error", id_, what_arg, context); return {id_, w.c_str()}; } @@ -5806,7 +5832,7 @@ class out_of_range : public exception template::value, int> = 0> static out_of_range create(int id_, const std::string& what_arg, BasicJsonContext context) { - const std::string w = concat(exception::name("out_of_range", id_), exception::diagnostics(context), what_arg); + const std::string w = exception::what_impl("out_of_range", id_, what_arg, context); return {id_, w.c_str()}; } @@ -5829,7 +5855,7 @@ class other_error : public exception template::value, int> = 0> static other_error create(int id_, const std::string& what_arg, BasicJsonContext context) { - const std::string w = concat(exception::name("other_error", id_), exception::diagnostics(context), what_arg); + const std::string w = exception::what_impl("other_error", id_, what_arg, context); return {id_, w.c_str()}; } @@ -6078,12 +6104,10 @@ void from_json(const BasicJsonType& j, std::optional& opt) } #endif // JSON_HAS_CPP_17 -// overloads for basic_json template parameters -template < typename BasicJsonType, typename ArithmeticType, - enable_if_t < std::is_arithmetic::value&& - !std::is_same::value, - int > = 0 > -void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) +// read a number value of any of the three number types into val; +// returns false (leaving val untouched) if j is not a number +template +bool get_number_value(const BasicJsonType& j, ArithmeticType& val) { switch (static_cast(j)) { @@ -6094,17 +6118,17 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) const auto* ptr = j.template get_ptr(); JSON_ASSERT(ptr != nullptr); val = static_cast(*ptr); - break; + return true; } case value_t::number_integer: { val = static_cast(*j.template get_ptr()); - break; + return true; } case value_t::number_float: { val = static_cast(*j.template get_ptr()); - break; + return true; } case value_t::null: @@ -6115,7 +6139,20 @@ void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) case value_t::binary: case value_t::discarded: default: - throw_type_must_be("number", j); + return false; + } +} + +// overloads for basic_json template parameters +template < typename BasicJsonType, typename ArithmeticType, + enable_if_t < std::is_arithmetic::value&& + !std::is_same::value, + int > = 0 > +void get_arithmetic_value(const BasicJsonType& j, ArithmeticType& val) +{ + if (JSON_HEDLEY_UNLIKELY(!get_number_value(j, val))) + { + throw_type_must_be("number", j); } } @@ -6254,29 +6291,44 @@ inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType: arr = *j.template get_ptr(); } +// fill a std::array from the JSON array j: the size is checked once and the +// elements are read in order, so a short array converts the elements it has and +// then throws the out_of_range error at() would throw for the first missing one +template +void from_json_std_array(const BasicJsonType& j, std::array& arr) +{ + const auto& inner_array = *j.template get_ptr(); + const std::size_t size = (std::min)(N, inner_array.size()); + for (std::size_t i = 0; i < size; ++i) + { + arr[i] = inner_array[i].template get(); + } + if (JSON_HEDLEY_UNLIKELY(size < N)) + { + static_cast(j.at(size)); + } +} + template auto from_json_array_impl(const BasicJsonType& j, std::array& arr, priority_tag<2> /*unused*/) -> decltype(j.template get(), void()) { - for (std::size_t i = 0; i < N; ++i) - { - arr[i] = j.at(i).template get(); - } + from_json_std_array(j, arr); } -// reserve() is called through this pair (modeled on from_json_object_reserve) -// so from_json_array_impl below has a single body for both ConstructibleArrayType -// that support reserve() and those that don't. -template -auto from_json_array_reserve(ConstructibleArrayType& arr, typename ConstructibleArrayType::size_type size, priority_tag<1> /*unused*/) --> decltype(arr.reserve(size), void()) +// reserve() is called through this pair (used for arrays and objects alike) +// so from_json_array_impl and the object from_json have a single body for both +// containers that support reserve() and those that don't. +template +auto reserve_if_possible(ConstructibleContainerType& c, typename ConstructibleContainerType::size_type size, priority_tag<1> /*unused*/) +-> decltype(c.reserve(size), void()) { - arr.reserve(size); + c.reserve(size); } -template -void from_json_array_reserve(ConstructibleArrayType& /*arr*/, std::size_t /*size*/, priority_tag<0> /*unused*/) +template +void reserve_if_possible(ConstructibleContainerType& /*c*/, std::size_t /*size*/, priority_tag<0> /*unused*/) {} // append through push_back() where available; containers without it (such as @@ -6306,7 +6358,7 @@ auto from_json_array_impl(const BasicJsonType& j, ConstructibleArrayType& arr, p { ConstructibleArrayType ret; const auto* inner_array = j.template get_ptr(); - from_json_array_reserve(ret, inner_array->size(), priority_tag<1> {}); + reserve_if_possible(ret, inner_array->size(), priority_tag<1> {}); for (const auto& elem : *inner_array) { // get() returns *this, this won't call a from_json @@ -6345,6 +6397,37 @@ std::array from_json_inplace_array_impl(const BasicJsonType& return { { j.at(Idx).template get()... } }; } +// std::is_trivial is deprecated in C++26 (see binary_writer::to_char_type); +// a struct rather than an alias, as astyle indents an #ifdef block of aliases +template +struct is_trivial_element +#ifdef JSON_HAS_CPP_26 + : conjunction, std::is_trivially_default_constructible> +#else + : std::is_trivial +#endif +{}; + +// the element type may not be default constructible: construct every element +// in place (a pack expansion that is unrolled N times) +template +std::array from_json_make_array(const BasicJsonType& j, identity_tag> tag, + std::false_type /*is_trivial_element*/) +{ + return from_json_inplace_array_impl(j, tag, make_index_sequence {}); +} + +// a trivial element type cannot tell default construction plus assignment from +// construction in place, so use a loop instead of unrolling N conversions +template +std::array from_json_make_array(const BasicJsonType& j, identity_tag> /*unused*/, + std::true_type /*is_trivial_element*/) +{ + std::array arr; // NOLINT(cppcoreguidelines-pro-type-member-init,hicpp-member-init): every element is assigned or an exception is thrown + from_json_std_array(j, arr); + return arr; +} + template < typename BasicJsonType, typename T, std::size_t N > auto from_json(const BasicJsonType& j, identity_tag> tag) -> decltype(from_json_inplace_array_impl(j, tag, make_index_sequence {})) @@ -6354,7 +6437,7 @@ auto from_json(const BasicJsonType& j, identity_tag> tag) throw_type_must_be("array", j); } - return from_json_inplace_array_impl(j, tag, make_index_sequence {}); + return from_json_make_array(j, tag, is_trivial_element {}); } template @@ -6387,17 +6470,6 @@ inline void from_json(const BasicJsonType& j, CompatibleArrayType& bin) } } -template -auto from_json_object_reserve(ConstructibleObjectType& obj, typename ConstructibleObjectType::size_type size, priority_tag<1> /*unused*/) --> decltype(obj.reserve(size), void()) -{ - obj.reserve(size); -} - -template -inline void from_json_object_reserve(ConstructibleObjectType& /*obj*/, std::size_t /*size*/, priority_tag<0> /*unused*/) -{} - // the keys of the source are sorted for the usual object types, so hinting the // end makes every insertion constant-time (for containers that order their keys // the same way); containers without emplace_hint() (such as ordered_map) fall @@ -6426,7 +6498,7 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj) ConstructibleObjectType ret; const auto* inner_object = j.template get_ptr(); - from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {}); + reserve_if_possible(ret, inner_object->size(), priority_tag<1> {}); for (const auto& p : *inner_object) { from_json_object_emplace(ret, p.first, p.second.template get(), priority_tag<1> {}); @@ -6446,42 +6518,17 @@ template < typename BasicJsonType, typename ArithmeticType, int > = 0 > inline void from_json(const BasicJsonType& j, ArithmeticType& val) { - switch (static_cast(j)) + if (get_number_value(j, val)) { - case value_t::number_unsigned: - { - // never null: a basic_json whose number_unsigned_t is number_integer_t - // stores no number_unsigned values (#2573) - const auto* ptr = j.template get_ptr(); - JSON_ASSERT(ptr != nullptr); - val = static_cast(*ptr); - break; - } - case value_t::number_integer: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::number_float: - { - val = static_cast(*j.template get_ptr()); - break; - } - case value_t::boolean: - { - val = static_cast(*j.template get_ptr()); - break; - } - - case value_t::null: - case value_t::object: - case value_t::array: - case value_t::string: - case value_t::binary: - case value_t::discarded: - default: - throw_type_must_be("number", j); + return; } + + // unlike get_arithmetic_value(), a boolean is accepted here + if (JSON_HEDLEY_UNLIKELY(!j.is_boolean())) + { + throw_type_must_be("number", j); + } + val = static_cast(*j.template get_ptr()); } template @@ -6841,6 +6888,24 @@ inline std::string hex_byte(const std::uint8_t byte) return result; } +/*! +@brief write a UTF-16 code unit as four lowercase hexadecimal digits + +The lowercase counterpart of hex_byte(), used for the `\\uXXXX` escapes of the +serializers, whose output is lowercase. Writes exactly four characters to +@a out and returns the pointer past them. +*/ +JSON_HEDLEY_ALWAYS_INLINE char* hex4_lower(const std::uint16_t codeunit, char* out) noexcept +{ + constexpr const char* nibble_to_hex = "0123456789abcdef"; + const unsigned int unit = codeunit; + out[0] = nibble_to_hex[(unit >> 12u) & 0x0Fu]; + out[1] = nibble_to_hex[(unit >> 8u) & 0x0Fu]; + out[2] = nibble_to_hex[(unit >> 4u) & 0x0Fu]; + out[3] = nibble_to_hex[unit & 0x0Fu]; + return out + 4; +} + /////////////////// // UTF-8 encoding // /////////////////// @@ -6896,6 +6961,66 @@ void encode_utf8(std::uint32_t cp, const Out& out) } } +/*! +@brief the shape of a well-formed UTF-8 sequence, by its lead byte + +@a length is 1 for ASCII, 2 to 4 for a valid lead byte, and 0 for a byte that +cannot start a well-formed sequence (a continuation byte, 0xC0, 0xC1, or 0xF5 +to 0xFF). For a multibyte sequence, the second byte must lie in +[@a second_lo, @a second_hi], and every later byte in [0x80, 0xBF] +(RFC 3629, section 4; Unicode Table 3-7). The narrower second-byte ranges +exclude overlong forms (0xE0, 0xF0), surrogates (0xED), and code points +beyond U+10FFFF (0xF4). +*/ +struct utf8_sequence_shape +{ + std::uint8_t length; + std::uint8_t second_lo; + std::uint8_t second_hi; +}; + +/// @return the shape of a UTF-8 sequence starting with @a lead, see utf8_sequence_shape +inline utf8_sequence_shape utf8_shape(const std::uint8_t lead) noexcept +{ + if (lead < 0x80u) + { + return {1, 0, 0}; + } + if (lead < 0xC2u) + { + return {0, 0, 0}; + } + if (lead <= 0xDFu) + { + return {2, 0x80u, 0xBFu}; + } + if (lead == 0xE0u) + { + return {3, 0xA0u, 0xBFu}; + } + if (lead == 0xEDu) + { + return {3, 0x80u, 0x9Fu}; + } + if (lead <= 0xEFu) + { + return {3, 0x80u, 0xBFu}; + } + if (lead == 0xF0u) + { + return {4, 0x90u, 0xBFu}; + } + if (lead <= 0xF3u) + { + return {4, 0x80u, 0xBFu}; + } + if (lead == 0xF4u) + { + return {4, 0x80u, 0x8Fu}; + } + return {0, 0, 0}; +} + /////////////////// // UTF-8 decoding // /////////////////// @@ -6911,7 +7036,7 @@ This is a single-byte step of a "shift-based" UTF-8 decoder originally written by Björn Hoehrmann. See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details. -The library checks UTF-8 well-formedness (RFC 3629, section 4) in three +The library checks UTF-8 well-formedness (RFC 3629, section 4) in five places, which differ in speed, diagnostics, and how they read the input: - decode() below: the serializer, to escape and, in strict mode, reject @@ -6925,10 +7050,15 @@ places, which differ in speed, diagnostics, and how they read the input: bulk string scan, the bulk path of the BON8 reader, and the BON8 writer. They must accept exactly what the lexer's switch accepts. - the byte path of binary_reader::get_bon8_string(): BON8 input without bulk - access, and the bytes the bulk path leaves to it. + access, and the bytes the bulk path leaves to it. It keeps its own ranges: + taking them from utf8_shape() made BON8 decoding 2-3% slower (#5891). +- check_utf8() in detail/view/edit.hpp: strings stored in an editable + json_document, with the index of the offending byte. It also uses + utf8_shape(). -All four must accept the same set of sequences, so a change to one needs a -matching change to the others. +All five must accept the same set of sequences, which the test "UTF-8 +validators accept the same sequences" in unit-class_lexer.cpp checks +exhaustively against decode() and utf8_shape(). @param[in,out] state the current decoder state @param[in,out] codep codepoint (valid only if resulting state is UTF8_ACCEPT) @@ -7194,35 +7324,25 @@ template class iteration_proxy_value { JSON_ASSERT(anchor.m_object != nullptr); - switch (anchor.m_object->type()) + if (anchor.m_object->is_array()) { // use integer array index as key - case value_t::array: + if (array_index != array_index_last) { - if (array_index != array_index_last) - { - int_to_string( array_index_str, array_index ); - array_index_last = array_index; - } - return array_index_str; + int_to_string( array_index_str, array_index ); + array_index_last = array_index; } - - // use key from the object - case value_t::object: - return anchor.key(); - - // use an empty key for all primitive types - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return empty_str; + return array_index_str; } + + if (anchor.m_object->is_object()) + { + // use key from the object + return anchor.key(); + } + + // use an empty key for all primitive types + return empty_str; } /// return value of the iterator @@ -8165,7 +8285,7 @@ NLOHMANN_JSON_NAMESPACE_END // #include // IWYU pragma: keep // #include - +// IWYU pragma: export // #include // __ _____ _____ _____ // __| | __| | | | JSON for Modern C++ @@ -8545,6 +8665,36 @@ namespace detail /// the supported input formats enum class input_format_t { json, cbor, msgpack, ubjson, bson, bjdata, bon8 }; +/// @return the name of a binary input format, as used in exception messages +inline const char* input_format_name(const input_format_t format) noexcept +{ + switch (format) + { + case input_format_t::cbor: + return "CBOR"; + case input_format_t::msgpack: + return "MessagePack"; + case input_format_t::ubjson: + return "UBJSON"; + case input_format_t::bson: + return "BSON"; + case input_format_t::bjdata: + return "BJData"; + case input_format_t::bon8: + return "BON8"; + case input_format_t::json: // LCOV_EXCL_LINE + default: // LCOV_EXCL_LINE + JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE + return ""; // LCOV_EXCL_LINE + } +} + +/// @return the message of the parse_error thrown or reported for an empty input +inline const char* empty_input_message() noexcept +{ + return "attempting to parse an empty input; check that your input string or stream contains the expected JSON"; +} + //////////////////// // input adapters // //////////////////// @@ -9751,7 +9901,7 @@ inline file_input_adapter input_adapter(std::FILE* file) { if (file == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } return file_input_adapter(file); } @@ -9760,7 +9910,7 @@ inline input_stream_adapter input_adapter(std::istream& stream) { if (stream.rdbuf() == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } return input_stream_adapter(stream); } @@ -9785,7 +9935,7 @@ contiguous_bytes_input_adapter input_adapter(CharT b) { if (b == nullptr) { - JSON_THROW(parse_error::create(exception_id::syntax_error, 0, "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr)); + JSON_THROW(parse_error::create(exception_id::syntax_error, 0, empty_input_message(), nullptr)); } auto length = std::strlen(reinterpret_cast(b)); const auto* ptr = reinterpret_cast(b); @@ -12041,7 +12191,7 @@ JSON_HEDLEY_ALWAYS_INLINE std::size_t find_string_special(const unsigned char* d // printable ASCII except '"' and '\\' - is emitted unchanged. Note this differs // from is_string_special() only in that 0x7F is also a stop (it is escaped as // \u007f under ensure_ascii). -inline bool is_ascii_copyable(unsigned char c) noexcept +constexpr bool is_ascii_copyable(unsigned char c) noexcept { return c >= 0x20u && c < 0x7Fu && c != '"' && c != '\\'; } @@ -13116,225 +13266,74 @@ class lexer : public lexer_base // invalid control characters case 0x00: - { - error_message = "invalid string: control character U+0000 (NUL) must be escaped to \\u0000"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x01: - { - error_message = "invalid string: control character U+0001 (SOH) must be escaped to \\u0001"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x02: - { - error_message = "invalid string: control character U+0002 (STX) must be escaped to \\u0002"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x03: - { - error_message = "invalid string: control character U+0003 (ETX) must be escaped to \\u0003"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x04: - { - error_message = "invalid string: control character U+0004 (EOT) must be escaped to \\u0004"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x05: - { - error_message = "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x06: - { - error_message = "invalid string: control character U+0006 (ACK) must be escaped to \\u0006"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x07: - { - error_message = "invalid string: control character U+0007 (BEL) must be escaped to \\u0007"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x08: - { - error_message = "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x09: - { - error_message = "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0A: - { - error_message = "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0B: - { - error_message = "invalid string: control character U+000B (VT) must be escaped to \\u000B"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0C: - { - error_message = "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0D: - { - error_message = "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0E: - { - error_message = "invalid string: control character U+000E (SO) must be escaped to \\u000E"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x0F: - { - error_message = "invalid string: control character U+000F (SI) must be escaped to \\u000F"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x10: - { - error_message = "invalid string: control character U+0010 (DLE) must be escaped to \\u0010"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x11: - { - error_message = "invalid string: control character U+0011 (DC1) must be escaped to \\u0011"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x12: - { - error_message = "invalid string: control character U+0012 (DC2) must be escaped to \\u0012"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x13: - { - error_message = "invalid string: control character U+0013 (DC3) must be escaped to \\u0013"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x14: - { - error_message = "invalid string: control character U+0014 (DC4) must be escaped to \\u0014"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x15: - { - error_message = "invalid string: control character U+0015 (NAK) must be escaped to \\u0015"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x16: - { - error_message = "invalid string: control character U+0016 (SYN) must be escaped to \\u0016"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x17: - { - error_message = "invalid string: control character U+0017 (ETB) must be escaped to \\u0017"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x18: - { - error_message = "invalid string: control character U+0018 (CAN) must be escaped to \\u0018"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x19: - { - error_message = "invalid string: control character U+0019 (EM) must be escaped to \\u0019"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1A: - { - error_message = "invalid string: control character U+001A (SUB) must be escaped to \\u001A"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1B: - { - error_message = "invalid string: control character U+001B (ESC) must be escaped to \\u001B"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1C: - { - error_message = "invalid string: control character U+001C (FS) must be escaped to \\u001C"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1D: - { - error_message = "invalid string: control character U+001D (GS) must be escaped to \\u001D"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1E: - { - error_message = "invalid string: control character U+001E (RS) must be escaped to \\u001E"; - error_cause = error_kind::string; - return token_type::parse_error; - } - case 0x1F: { - error_message = "invalid string: control character U+001F (US) must be escaped to \\u001F"; + static const std::array control_character_messages = {{ + "invalid string: control character U+0000 (NUL) must be escaped to \\u0000", + "invalid string: control character U+0001 (SOH) must be escaped to \\u0001", + "invalid string: control character U+0002 (STX) must be escaped to \\u0002", + "invalid string: control character U+0003 (ETX) must be escaped to \\u0003", + "invalid string: control character U+0004 (EOT) must be escaped to \\u0004", + "invalid string: control character U+0005 (ENQ) must be escaped to \\u0005", + "invalid string: control character U+0006 (ACK) must be escaped to \\u0006", + "invalid string: control character U+0007 (BEL) must be escaped to \\u0007", + "invalid string: control character U+0008 (BS) must be escaped to \\u0008 or \\b", + "invalid string: control character U+0009 (HT) must be escaped to \\u0009 or \\t", + "invalid string: control character U+000A (LF) must be escaped to \\u000A or \\n", + "invalid string: control character U+000B (VT) must be escaped to \\u000B", + "invalid string: control character U+000C (FF) must be escaped to \\u000C or \\f", + "invalid string: control character U+000D (CR) must be escaped to \\u000D or \\r", + "invalid string: control character U+000E (SO) must be escaped to \\u000E", + "invalid string: control character U+000F (SI) must be escaped to \\u000F", + "invalid string: control character U+0010 (DLE) must be escaped to \\u0010", + "invalid string: control character U+0011 (DC1) must be escaped to \\u0011", + "invalid string: control character U+0012 (DC2) must be escaped to \\u0012", + "invalid string: control character U+0013 (DC3) must be escaped to \\u0013", + "invalid string: control character U+0014 (DC4) must be escaped to \\u0014", + "invalid string: control character U+0015 (NAK) must be escaped to \\u0015", + "invalid string: control character U+0016 (SYN) must be escaped to \\u0016", + "invalid string: control character U+0017 (ETB) must be escaped to \\u0017", + "invalid string: control character U+0018 (CAN) must be escaped to \\u0018", + "invalid string: control character U+0019 (EM) must be escaped to \\u0019", + "invalid string: control character U+001A (SUB) must be escaped to \\u001A", + "invalid string: control character U+001B (ESC) must be escaped to \\u001B", + "invalid string: control character U+001C (FS) must be escaped to \\u001C", + "invalid string: control character U+001D (GS) must be escaped to \\u001D", + "invalid string: control character U+001E (RS) must be escaped to \\u001E", + "invalid string: control character U+001F (US) must be escaped to \\u001F" + } + }; + error_message = control_character_messages[static_cast(current)]; error_cause = error_kind::string; return token_type::parse_error; } @@ -16103,6 +16102,52 @@ struct diagnostic_positions }; #endif +/*! +@brief error state shared by the DOM-building SAX parsers + +Holds whether a parse error occurred and whether errors are thrown, and +implements the parse_error() and is_errored() members of the SAX interface for +json_sax_dom_parser and json_sax_dom_callback_parser. +*/ +class json_sax_dom_error_state +{ + public: + explicit json_sax_dom_error_state(const bool allow_exceptions_) noexcept + : allow_exceptions(allow_exceptions_) + {} + + // make class move-only + json_sax_dom_error_state(const json_sax_dom_error_state&) = delete; + json_sax_dom_error_state(json_sax_dom_error_state&&) = default; // NOLINT(hicpp-noexcept-move,performance-noexcept-move-constructor) + json_sax_dom_error_state& operator=(const json_sax_dom_error_state&) = delete; + json_sax_dom_error_state& operator=(json_sax_dom_error_state&&) = delete; // the const member prevents assignment + ~json_sax_dom_error_state() = default; + + template + bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, + const Exception& ex) + { + errored = true; + static_cast(ex); + if (allow_exceptions) + { + JSON_THROW(ex); + } + return false; + } + + bool is_errored() const + { + return errored; + } + + private: + /// whether a syntax error occurred + bool errored = false; + /// whether to throw exceptions in case of errors + const bool allow_exceptions = true; +}; + /*! @brief a hash table that finds the position of a key in an object whose elements are kept in a vector (ordered_map) @@ -16214,7 +16259,7 @@ read up to it. not use the value after a parse error) */ template -class json_sax_dom_parser +class json_sax_dom_parser : public json_sax_dom_error_state { public: using number_integer_t = typename BasicJsonType::number_integer_t; @@ -16230,7 +16275,7 @@ class json_sax_dom_parser @param[in] allow_exceptions_ whether parse errors yield exceptions */ explicit json_sax_dom_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr) - : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_) + : json_sax_dom_error_state(allow_exceptions_), root(r), m_lexer_ref(lexer_) {} // make class move-only @@ -16381,24 +16426,6 @@ class json_sax_dom_parser return true; } - template - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const Exception& ex) - { - errored = true; - static_cast(ex); - if (allow_exceptions) - { - JSON_THROW(ex); - } - return false; - } - - constexpr bool is_errored() const - { - return errored; - } - private: using object_t = typename BasicJsonType::object_t; using array_t = typename BasicJsonType::array_t; @@ -16696,7 +16723,7 @@ class json_sax_dom_parser /// directly if the value was the one too many. void place_container(const frame& f, frame* enclosing, BasicJsonType&& value, const bool enclosing_is_innermost) { - // a container without a slot of its own is collected for an enclosing one + // only a container with a slot may have no enclosing container JSON_ASSERT(f.slot != nullptr || enclosing != nullptr); if (f.slot != nullptr) { @@ -17114,10 +17141,6 @@ class json_sax_dom_parser /// the array that stores the elements of the innermost array as they /// arrive (frames.back().array), or nullptr array_t* stored_array = nullptr; - /// whether a syntax error occurred - bool errored = false; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; /// the lexer reference to obtain the current position lexer_t* m_lexer_ref = nullptr; }; @@ -17142,7 +17165,7 @@ constructor contains the parsed value. @tparam BasicJsonType the JSON type */ template -class json_sax_dom_binary_parser +class json_sax_dom_binary_parser : public json_sax_dom_error_state { public: using number_integer_t = typename BasicJsonType::number_integer_t; @@ -17158,7 +17181,7 @@ class json_sax_dom_binary_parser @param[in] allow_exceptions_ whether parse errors yield exceptions */ explicit json_sax_dom_binary_parser(BasicJsonType& r, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr) - : root(r), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_) + : json_sax_dom_error_state(allow_exceptions_), root(r), m_lexer_ref(lexer_) {} // make class move-only @@ -17287,24 +17310,6 @@ class json_sax_dom_binary_parser return true; } - template - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const Exception& ex) - { - errored = true; - static_cast(ex); - if (allow_exceptions) - { - JSON_THROW(ex); - } - return false; - } - - constexpr bool is_errored() const - { - return errored; - } - private: /*! @@ -17358,16 +17363,12 @@ class json_sax_dom_binary_parser std::vector ref_stack {}; /// helper to hold the reference for the next object element BasicJsonType* object_element = nullptr; - /// whether a syntax error occurred - bool errored = false; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; /// the lexer reference to obtain the current position lexer_t* m_lexer_ref = nullptr; }; template -class json_sax_dom_callback_parser +class json_sax_dom_callback_parser : public json_sax_dom_error_state { public: using number_integer_t = typename BasicJsonType::number_integer_t; @@ -17383,7 +17384,7 @@ class json_sax_dom_callback_parser parser_callback_t cb, const bool allow_exceptions_ = true, lexer_t* lexer_ = nullptr) - : root(r), callback(std::move(cb)), allow_exceptions(allow_exceptions_), m_lexer_ref(lexer_) + : json_sax_dom_error_state(allow_exceptions_), root(r), callback(std::move(cb)), m_lexer_ref(lexer_) { keep_stack.push_back(true); } @@ -17647,24 +17648,6 @@ class json_sax_dom_callback_parser return true; } - template - bool parse_error(std::size_t /*unused*/, const std::string& /*unused*/, - const Exception& ex) - { - errored = true; - static_cast(ex); - if (allow_exceptions) - { - JSON_THROW(ex); - } - return false; - } - - constexpr bool is_errored() const - { - return errored; - } - private: /*! @@ -17931,12 +17914,8 @@ class json_sax_dom_callback_parser /// value is later rejected by the callback, instead of erasing the /// member entirely std::vector> duplicate_key_stash {}; - /// whether a syntax error occurred - bool errored = false; /// callback function const parser_callback_t callback = nullptr; - /// whether to throw exceptions in case of errors - const bool allow_exceptions = true; /// a discarded value for the callback BasicJsonType discarded = BasicJsonType::value_t::discarded; /// the value of the key event for the callback (see key_value()) @@ -18468,6 +18447,26 @@ class binary_reader return is_object ? sax->end_object() : sax->end_array(); } + /*! + @brief close the innermost open container and report whether that + completed the value being read + + The value begun by the driver is complete once its container is not + inside another one. + + @param[out] value_complete whether no container is left open + @return whether the SAX parser accepted the end event + */ + bool leave_container_and_check(bool& value_complete) + { + if (JSON_HEDLEY_UNLIKELY(!leave_container())) + { + return false; + } + value_complete = container_stack.empty(); + return true; + } + ////////// // BSON // ////////// @@ -19405,6 +19404,34 @@ class binary_reader return last_byte_error(exception_id::invalid_string_or_size, concat("indefinite-length ", type_name, " is not allowed inside indefinite-length ", type_name, "; last byte: 0x", get_token_string()), context); } + /*! + @brief report an object key that is not a string + + @param[in] found description of what was found instead of a string key + @return false + */ + bool unsupported_key_type(const char* found) + { + return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + } + + /*! + @brief read the length of a CBOR string or byte array and then its bytes + + @tparam NumberType the type of the length field + @tparam ContainerType the destination container (string_t or binary_t) + @param[in] context further context information (for diagnostics) + @param[out] result container the bytes are appended to + + @return whether reading completed + */ + template + bool get_cbor_sized_bytes(const char* context, ContainerType& result) + { + NumberType len{}; + return get_number(len) && get_bytes(len, context, result); + } + /*! @brief reads a definite-length CBOR string @@ -19455,26 +19482,22 @@ class binary_reader case 0x78: // UTF-8 string (one-byte uint8_t for n follows) { - std::uint8_t len{}; - return get_number(len) && get_string(len, result); + return get_cbor_sized_bytes("string", result); } case 0x79: // UTF-8 string (two-byte uint16_t for n follow) { - std::uint16_t len{}; - return get_number(len) && get_string(len, result); + return get_cbor_sized_bytes("string", result); } case 0x7A: // UTF-8 string (four-byte uint32_t for n follow) { - std::uint32_t len{}; - return get_number(len) && get_string(len, result); + return get_cbor_sized_bytes("string", result); } case 0x7B: // UTF-8 string (eight-byte uint64_t for n follow) { - std::uint64_t len{}; - return get_number(len) && get_string(len, result); + return get_cbor_sized_bytes("string", result); } default: @@ -19613,7 +19636,7 @@ class binary_reader // a key that begins an item is skipped with its value when recovering // (see resync); a break stop code or a reserved byte begins none skip_requested = is_cbor_item_head(static_cast(current)); - return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + return unsupported_key_type(found); } /*! @@ -19666,30 +19689,22 @@ class binary_reader case 0x58: // Binary data (one-byte uint8_t for n follows) { - std::uint8_t len{}; - return get_number(len) && - get_binary(len, result); + return get_cbor_sized_bytes("binary", result); } case 0x59: // Binary data (two-byte uint16_t for n follow) { - std::uint16_t len{}; - return get_number(len) && - get_binary(len, result); + return get_cbor_sized_bytes("binary", result); } case 0x5A: // Binary data (four-byte uint32_t for n follow) { - std::uint32_t len{}; - return get_number(len) && - get_binary(len, result); + return get_cbor_sized_bytes("binary", result); } case 0x5B: // Binary data (eight-byte uint64_t for n follow) { - std::uint64_t len{}; - return get_number(len) && - get_binary(len, result); + return get_cbor_sized_bytes("binary", result); } default: @@ -19904,12 +19919,12 @@ class binary_reader if (at_end) { - if (JSON_HEDLEY_UNLIKELY(!leave_container())) + bool value_complete = false; + if (JSON_HEDLEY_UNLIKELY(!leave_container_and_check(value_complete))) { return false; } - // the value begun here is complete once its container is - if (container_stack.empty()) + if (value_complete) { return true; } @@ -20510,20 +20525,17 @@ class binary_reader case 0xD9: // str 8 { - std::uint8_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 0xDA: // str 16 { - std::uint16_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 0xDB: // str 32 { - std::uint32_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } default: @@ -20623,7 +20635,7 @@ class binary_reader // the key is skipped with its value when recovering (see resync) skip_requested = true; - return last_byte_error(exception_id::invalid_string_or_size, "only string keys are supported, but found ", "object key", found); + return unsupported_key_type(found); } /*! @@ -20768,12 +20780,12 @@ class binary_reader if (container_stack.back().remaining == 0) { - if (JSON_HEDLEY_UNLIKELY(!leave_container())) + bool value_complete = false; + if (JSON_HEDLEY_UNLIKELY(!leave_container_and_check(value_complete))) { return false; } - // the value begun here is complete once its container is - if (container_stack.empty()) + if (value_complete) { return true; } @@ -21118,8 +21130,7 @@ class binary_reader { case 'U': { - std::uint8_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 'i': @@ -21152,8 +21163,7 @@ class binary_reader { break; } - std::uint16_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 'm': @@ -21162,8 +21172,7 @@ class binary_reader { break; } - std::uint32_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } case 'M': @@ -21172,8 +21181,7 @@ class binary_reader { break; } - std::uint64_t len{}; - return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + return get_string_with_length(result, context); } default: @@ -22079,12 +22087,12 @@ class binary_reader if (at_end) { - if (JSON_HEDLEY_UNLIKELY(!leave_container())) + bool value_complete = false; + if (JSON_HEDLEY_UNLIKELY(!leave_container_and_check(value_complete))) { return false; } - // the value begun here is complete once its container is - if (container_stack.empty()) + if (value_complete) { return true; } @@ -22970,6 +22978,22 @@ class binary_reader return true; } + /*! + @brief read a length of type @a NumberType, then a string of that many bytes + + @tparam NumberType the type of the length field + @param[out] result string the bytes are appended to + @param[in] context further context information (for diagnostics) + + @return whether reading and validating the string succeeded + */ + template + bool get_string_with_length(string_t& result, const char* context) + { + NumberType len{}; + return get_number(len) && get_string(len, result) && check_string_utf8(result, context); + } + /*! @brief create a byte array by reading bytes from the input @@ -23471,37 +23495,7 @@ class binary_reader const std::string& context) const { std::string error_msg = "syntax error while parsing "; - - switch (input_format) - { - case input_format_t::cbor: - error_msg += "CBOR"; - break; - - case input_format_t::msgpack: - error_msg += "MessagePack"; - break; - - case input_format_t::ubjson: - error_msg += "UBJSON"; - break; - - case input_format_t::bson: - error_msg += "BSON"; - break; - - case input_format_t::bjdata: - error_msg += "BJData"; - break; - - case input_format_t::bon8: - error_msg += "BON8"; - break; - - case input_format_t::json: // LCOV_EXCL_LINE - default: // LCOV_EXCL_LINE - JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE - } + error_msg += input_format_name(input_format); return concat(error_msg, ' ', context, ": ", detail); } @@ -23870,6 +23864,39 @@ class parser return m_lexer.get_string(); } + /*! + @brief apply the end-of-input handling after a value was parsed + + In strict mode, the next token must be the end of the input; otherwise + parse_error.101 is reported through @a sax. In non-strict mode, the + lookahead is released so the caller can keep reading the input right after + the parsed value. + + @param[in,out] sax the SAX parser to report an error to + @param[in] strict whether to expect the last token to be EOF + @return true, or what @a sax returned for the error + */ + template + bool finish_input(SAX& sax, const bool strict) + { + if (strict) + { + // strict mode: next byte must be EOF + if (get_token() != token_type::end_of_input) + { + return syntax_error(sax, exception_message(token_type::end_of_input, "value")); + } + } + else + { + // the caller keeps using the input: position it right after + // the value by leaving the character that terminated it + m_lexer.release_lookahead(); + } + + return true; + } + /*! @brief run a DOM SAX parser to completion and position the lexer @@ -23889,20 +23916,7 @@ class parser { sax_parse_internal(&sdp); - if (strict) - { - // in strict mode, input must be completely read - if (get_token() != token_type::end_of_input) - { - syntax_error(sdp, exception_message(token_type::end_of_input, "value")); - } - } - else - { - // the caller keeps using the input: position it right after - // the value by leaving the character that terminated it - m_lexer.release_lookahead(); - } + static_cast(finish_input(sdp, strict)); return !sdp.is_errored(); } @@ -24127,7 +24141,7 @@ class parser { // there is nothing to recover static_cast(report_error(sax, parse_error::create(exception_id::syntax_error, m_lexer.get_position(), - "attempting to parse an empty input; check that your input string or stream contains the expected JSON", nullptr), allow_recovery)); + empty_input_message(), nullptr), allow_recovery)); return false; } @@ -25235,33 +25249,18 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci { JSON_ASSERT(m_object != nullptr); - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - { - m_it.object_iterator = typename object_t::iterator(); - break; - } - - case value_t::array: - { - m_it.array_iterator = typename array_t::iterator(); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator = primitive_iterator_t(); - break; - } + m_it.object_iterator = typename object_t::iterator(); + } + else if (m_object->is_array()) + { + m_it.array_iterator = typename array_t::iterator(); + } + else + { + // primitive (is_primitive(), including discarded) + m_it.primitive_iterator = primitive_iterator_t(); } } @@ -25376,33 +25375,28 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci { JSON_ASSERT(m_object != nullptr); - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - { - m_it.object_iterator = m_object->m_data.m_value.object->end(); - break; - } + m_it.object_iterator = m_object->m_data.m_value.object->end(); + } + else if (m_object->is_array()) + { + m_it.array_iterator = m_object->m_data.m_value.array->end(); + } + else + { + // primitive (is_primitive(), including discarded) + m_it.primitive_iterator.set_end(); + } + } - case value_t::array: - { - m_it.array_iterator = m_object->m_data.m_value.array->end(); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator.set_end(); - break; - } + /// @throw invalid_iterator.212 if @a other points to another container + template + JSON_HEDLEY_ALWAYS_INLINE void check_same_container(const IterImpl& other) const + { + if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) + { + throw_literal(exception_id::iterators_compare_different_values, "cannot compare iterators of different containers", m_object); } } @@ -25551,10 +25545,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci bool operator==(const IterImpl& other) const { // if objects are not the same, the comparison is undefined - if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) - { - throw_literal(exception_id::iterators_compare_different_values, "cannot compare iterators of different containers", m_object); - } + check_same_container(other); // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493 if (m_object == nullptr) @@ -25593,10 +25584,7 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci bool operator<(const iter_impl& other) const { // if objects are not the same, the comparison is undefined - if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object)) - { - throw_literal(exception_id::iterators_compare_different_values, "cannot compare iterators of different containers", m_object); - } + check_same_container(other); // value-initialized forward iterators can be compared, and must compare equal to other value-initialized iterators of the same type #4493 if (m_object == nullptr) @@ -25605,25 +25593,18 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci return false; } - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - throw_literal(exception_id::iterator_order_on_object, "cannot compare order of object iterators", m_object); - - case value_t::array: - return (m_it.array_iterator < other.m_it.array_iterator); - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return (m_it.primitive_iterator < other.m_it.primitive_iterator); + throw_literal(exception_id::iterator_order_on_object, "cannot compare order of object iterators", m_object); } + + if (m_object->is_array()) + { + return (m_it.array_iterator < other.m_it.array_iterator); + } + + // primitive (is_primitive(), including discarded) + return (m_it.primitive_iterator < other.m_it.primitive_iterator); } /*! @@ -25661,30 +25642,19 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci { JSON_ASSERT(m_object != nullptr); - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - throw_literal(exception_id::iterator_arithmetic_on_object, "cannot use offsets with object iterators", m_object); + throw_literal(exception_id::iterator_arithmetic_on_object, "cannot use offsets with object iterators", m_object); + } - case value_t::array: - { - std::advance(m_it.array_iterator, i); - break; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - m_it.primitive_iterator += i; - break; - } + if (m_object->is_array()) + { + std::advance(m_it.array_iterator, i); + } + else + { + // primitive (is_primitive(), including discarded) + m_it.primitive_iterator += i; } return *this; @@ -25740,25 +25710,18 @@ class iter_impl // NOLINT(cppcoreguidelines-special-member-functions,hicpp-speci { JSON_ASSERT(m_object != nullptr); - switch (m_object->m_data.m_type) + if (m_object->is_object()) { - case value_t::object: - throw_literal(exception_id::iterator_arithmetic_on_object, "cannot use offsets with object iterators", m_object); - - case value_t::array: - return m_it.array_iterator - other.m_it.array_iterator; - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return m_it.primitive_iterator - other.m_it.primitive_iterator; + throw_literal(exception_id::iterator_arithmetic_on_object, "cannot use offsets with object iterators", m_object); } + + if (m_object->is_array()) + { + return m_it.array_iterator - other.m_it.array_iterator; + } + + // primitive (is_primitive(), including discarded) + return m_it.primitive_iterator - other.m_it.primitive_iterator; } /*! @@ -26022,6 +25985,102 @@ NLOHMANN_JSON_NAMESPACE_BEGIN namespace detail { struct json_pointer_access; + +/*! +@brief result of parse_array_index() + +json_pointer::array_index() maps each value to the corresponding +parse_error/out_of_range exception; json_pointer::contains() and +get_checked_or_null(), which must not throw for an out-of-range or +unrepresentable index, and the json_view pointer resolution switch on it +directly instead. +*/ +enum class array_index_status +{ + ok, ///< @a s is a valid, representable array index + leading_zero, ///< @a s begins with '0' but has more than one character + not_a_number, ///< @a s is neither empty nor "-" and does not begin with a digit + unresolved, ///< @a s could not be converted to an integer + exceeds_size_type ///< @a s converts to an integer that exceeds SizeType +}; + +/*! +@brief convert a JSON Pointer reference token into an array index (RFC 6901, Sect. 4) + +@param[in] s reference token to be converted into an array index; its data() + must be NUL-terminated +@param[out] idx the integer representation of @a s if array_index_status::ok + is returned; left unchanged otherwise + +@return whether @a s is a valid array index, and if not, why + +@note this function never throws; json_pointer::array_index(), the callers that + must not throw, and the json_view pointer resolution build on it instead + of each re-implementing the RFC 6901 digit rules and the range check +@note force-inlined: as a free function template it is no longer inlined into + the pointer resolution, which makes JsonPointerAt up to 17% slower (#5891) +*/ +template +JSON_HEDLEY_ALWAYS_INLINE array_index_status parse_array_index(const StringType& s, SizeType& idx) noexcept +{ + // error condition (cf. RFC 6901, Sect. 4) + if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) + { + return array_index_status::leading_zero; + } + + // error condition (cf. RFC 6901, Sect. 4); this also covers single- + // character tokens, so "/x" and "/xy" fail alike; "-" and the empty + // token are left to the conversion below and are reported as unresolved + if (JSON_HEDLEY_UNLIKELY(!s.empty() && !(s.size() == 1 && s[0] == '-') && !(s[0] >= '0' && s[0] <= '9'))) + { + return array_index_status::not_a_number; + } + + // fast path: up to 18 decimal digits cannot overflow unsigned long long or + // SizeType (9 digits for a 32-bit SizeType), so strtoull and errno are not needed + constexpr std::size_t max_fast_digits = sizeof(SizeType) >= 8 ? 18 : 9; + if (!s.empty() && s.size() <= max_fast_digits) + { + SizeType acc = 0; + for (std::size_t i = 0; i < s.size(); ++i) + { + const char c = s[i]; + if (JSON_HEDLEY_UNLIKELY(c < '0' || c > '9')) + { + break; // not a plain number: let strtoull decide + } + acc = static_cast((acc * 10) + static_cast(c - '0')); + if (JSON_HEDLEY_UNLIKELY(i == s.size() - 1)) + { + idx = acc; + return array_index_status::ok; + } + } + } + + const char* p = s.data(); + char* p_end = nullptr; // NOLINT(misc-const-correctness) + errno = 0; // strtoull doesn't reset errno + const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int) + if (p == p_end // invalid input or empty string + || errno == ERANGE // out of range + || JSON_HEDLEY_UNLIKELY(static_cast(p_end - p) != s.size())) // incomplete read + { + return array_index_status::unresolved; + } + + // the index does not fit into SizeType; on 64-bit platforms this is + // only SIZE_MAX itself (see #2203 and #5395) + if (res >= static_cast((std::numeric_limits::max)())) // NOLINT(runtime/int) + { + return array_index_status::exceeds_size_type; + } + + idx = static_cast(res); + return array_index_status::ok; +} + } // namespace detail /// @brief JSON Pointer defines a string syntax for identifying a specific value within a JSON document @@ -26286,6 +26345,28 @@ class json_pointer JSON_THROW(detail::out_of_range::create(detail::exception_id::patch_on_root, "JSON pointer has no parent", nullptr)); } + /// whether the '~' at @a pos of @a s is not followed by '0' or '1' + JSON_HEDLEY_ALWAYS_INLINE static bool is_invalid_escape(const string_t& s, std::size_t pos) + { + return pos == s.size() - 1 || (s[pos + 1] != '0' && s[pos + 1] != '1'); + } + + /// @throw parse_error.107 always + JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE static void throw_missing_slash(const string_t& pointer) + { + static_cast(pointer); // unused when JSON_NOEXCEPTION is defined + JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_missing_slash, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", pointer, "'"), nullptr)); + } + + /// @throw parse_error.107 if a nonempty @a pointer does not begin with '/' + JSON_HEDLEY_ALWAYS_INLINE static void check_begins_with_slash(const string_t& pointer) + { + if (JSON_HEDLEY_UNLIKELY(pointer[0] != '/')) + { + throw_missing_slash(pointer); + } + } + /// @throw out_of_range.404 always template JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE static void throw_unresolved(const string_t& reference_token, BasicJsonContext context) @@ -26295,21 +26376,7 @@ class json_pointer JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_unresolved, detail::concat("unresolved reference token '", reference_token, "'"), context)); } - /*! - @brief result of @ref parse_array_index - - @ref array_index maps each value to the corresponding parse_error/out_of_range - exception; @ref contains and @ref get_checked_or_null, which must not throw for - an out-of-range or unrepresentable index, switch on it directly instead. - */ - enum class array_index_status - { - ok, ///< @a s is a valid, representable array index - leading_zero, ///< @a s begins with '0' but has more than one character - not_a_number, ///< @a s is neither empty nor "-" and does not begin with a digit - unresolved, ///< @a s could not be converted to an integer - exceeds_size_type ///< @a s converts to an integer that exceeds size_type - }; + using array_index_status = detail::array_index_status; /// whether a reference token is "-" (a plain comparison with a string literal is not always inlined) static bool is_dash(const string_t& s) noexcept @@ -26317,78 +26384,11 @@ class json_pointer return s.size() == 1 && s[0] == '-'; } - /*! - @param[in] s reference token to be converted into an array index - @param[out] idx the integer representation of @a s if @ref array_index_status::ok - is returned; left unchanged otherwise - - @return whether @a s is a valid array index, and if not, why - - @note this function never throws; @ref array_index and the callers that must not - throw (@ref contains, @ref get_checked_or_null) build on it instead of each - re-implementing the RFC 6901 digit rules and the @a size_type range check - */ + /// detail::parse_array_index() for the size_type of @a BasicJsonType template static array_index_status parse_array_index(const string_t& s, typename BasicJsonType::size_type& idx) noexcept { - using size_type = typename BasicJsonType::size_type; - - // error condition (cf. RFC 6901, Sect. 4) - if (JSON_HEDLEY_UNLIKELY(s.size() > 1 && s[0] == '0')) - { - return array_index_status::leading_zero; - } - - // error condition (cf. RFC 6901, Sect. 4); this also covers single- - // character tokens, so "/x" and "/xy" fail alike; "-" and the empty - // token are left to the conversion below and are reported as unresolved - if (JSON_HEDLEY_UNLIKELY(!s.empty() && !is_dash(s) && !(s[0] >= '0' && s[0] <= '9'))) - { - return array_index_status::not_a_number; - } - - // fast path: up to 18 decimal digits cannot overflow unsigned long long or - // size_type (9 digits for a 32-bit size_type), so strtoull and errno are not needed - constexpr std::size_t max_fast_digits = sizeof(size_type) >= 8 ? 18 : 9; - if (!s.empty() && s.size() <= max_fast_digits) - { - size_type acc = 0; - for (std::size_t i = 0; i < s.size(); ++i) - { - const char c = s[i]; - if (JSON_HEDLEY_UNLIKELY(c < '0' || c > '9')) - { - break; // not a plain number: let strtoull decide - } - acc = static_cast((acc * 10) + static_cast(c - '0')); - if (JSON_HEDLEY_UNLIKELY(i == s.size() - 1)) - { - idx = acc; - return array_index_status::ok; - } - } - } - - const char* p = s.data(); - char* p_end = nullptr; // NOLINT(misc-const-correctness) - errno = 0; // strtoull doesn't reset errno - const unsigned long long res = std::strtoull(p, &p_end, 10); // NOLINT(runtime/int) - if (p == p_end // invalid input or empty string - || errno == ERANGE // out of range - || JSON_HEDLEY_UNLIKELY(static_cast(p_end - p) != s.size())) // incomplete read - { - return array_index_status::unresolved; - } - - // the index does not fit into size_type; on 64-bit platforms this is - // only SIZE_MAX itself (see #2203 and #5395) - if (res >= static_cast((std::numeric_limits::max)())) // NOLINT(runtime/int) - { - return array_index_status::exceeds_size_type; - } - - idx = static_cast(res); - return array_index_status::ok; + return detail::parse_array_index(s, idx); } /*! @@ -26438,10 +26438,7 @@ class json_pointer return reference_tokens.empty(); } - if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) - { - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_missing_slash, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr)); - } + check_begins_with_slash(reference_string); // the whole string is validated, whether or not it matches const std::size_t size = reference_string.size(); @@ -26449,7 +26446,7 @@ class json_pointer pos != string_t::npos; pos = reference_string.find_first_of('~', pos + 1)) { - if (JSON_HEDLEY_UNLIKELY(pos == size - 1 || (reference_string[pos + 1] != '0' && reference_string[pos + 1] != '1'))) + if (JSON_HEDLEY_UNLIKELY(is_invalid_escape(reference_string, pos))) { JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_invalid_escape, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); } @@ -26727,10 +26724,7 @@ class json_pointer } const char* const data = key.data(); - if (JSON_HEDLEY_UNLIKELY(data[0] != '/')) - { - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_missing_slash, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", key, "'"), nullptr)); - } + check_begins_with_slash(key); // The beginning of the key that it has in common with the previous // one, which was checked already, is skipped: the scan starts with @@ -26763,7 +26757,7 @@ class json_pointer else if (data[i] == '~') { // ~ must be followed by 0 or 1 - if (JSON_HEDLEY_UNLIKELY(i + 1 == size || (data[i + 1] != '0' && data[i + 1] != '1'))) + if (JSON_HEDLEY_UNLIKELY(is_invalid_escape(key, i))) { JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_invalid_escape, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); } @@ -26862,58 +26856,46 @@ class json_pointer ++end; } - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + token.clear(); + token.append(begin, static_cast(end - begin)); + const auto it = ptr->m_data.m_value.object->find(token); + if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) { - token.clear(); - token.append(begin, static_cast(end - begin)); - const auto it = ptr->m_data.m_value.object->find(token); - if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) - { - return nullptr; - } - ptr = &it->second; - break; - } - - case detail::value_t::array: - { - // an index of up to 18 digits without a leading zero fits into size_type - // (9 digits for a 32-bit size_type); everything else is left to json_pointer - constexpr std::size_t max_digits = sizeof(typename BasicJsonType::size_type) >= 8 ? 18 : 9; - const auto digits = static_cast(end - begin); - if (JSON_HEDLEY_UNLIKELY(digits == 0 || digits > max_digits || (digits > 1 && *begin == '0'))) - { - return nullptr; - } - typename BasicJsonType::size_type idx = 0; - for (const char* c = begin; c != end; ++c) - { - if (JSON_HEDLEY_UNLIKELY(*c < '0' || *c > '9')) - { - return nullptr; - } - idx = static_cast((idx * 10) + static_cast(*c - '0')); - } - if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) - { - return nullptr; - } - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: return nullptr; + } + ptr = &it->second; + } + else if (ptr->is_array()) + { + // an index of up to 18 digits without a leading zero fits into size_type + // (9 digits for a 32-bit size_type); everything else is left to json_pointer + constexpr std::size_t max_digits = sizeof(typename BasicJsonType::size_type) >= 8 ? 18 : 9; + const auto digits = static_cast(end - begin); + if (JSON_HEDLEY_UNLIKELY(digits == 0 || digits > max_digits || (digits > 1 && *begin == '0'))) + { + return nullptr; + } + typename BasicJsonType::size_type idx = 0; + for (const char* c = begin; c != end; ++c) + { + if (JSON_HEDLEY_UNLIKELY(*c < '0' || *c > '9')) + { + return nullptr; + } + idx = static_cast((idx * 10) + static_cast(*c - '0')); + } + if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) + { + return nullptr; + } + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // primitive value with a reference token left + return nullptr; } pos = static_cast(end - path.data()); steps.emplace_back(pos, ptr); @@ -26963,40 +26945,28 @@ class json_pointer : detail::value_t::object; } - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + // use unchecked object access + ptr = &object_member(*ptr, reference_token); + } + else if (ptr->is_array()) + { + if (reference_token == "-") { - // use unchecked object access - ptr = &object_member(*ptr, reference_token); - break; + // explicitly treat "-" as index beyond the end + ptr = &ptr->operator[](ptr->m_data.m_value.array->size()); } - - case detail::value_t::array: + else { - if (reference_token == "-") - { - // explicitly treat "-" as index beyond the end - ptr = &ptr->operator[](ptr->m_data.m_value.array->size()); - } - else - { - // convert array index to number; unchecked access - ptr = &ptr->operator[](array_index(reference_token)); - } - break; + // convert array index to number; unchecked access + ptr = &ptr->operator[](array_index(reference_token)); } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - throw_unresolved(reference_token, ptr); + } + else + { + // primitive value with a reference token left + throw_unresolved(reference_token, ptr); } } @@ -27014,51 +26984,39 @@ class json_pointer { for (const auto& reference_token : reference_tokens) { - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + const auto it = ptr->m_data.m_value.object->find(reference_token); + if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) { - const auto it = ptr->m_data.m_value.object->find(reference_token); - if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) - { - // at() throws out_of_range.403 with the usual message - ptr->at(reference_token); - } - ptr = &it->second; - break; + // at() throws out_of_range.403 with the usual message + ptr->at(reference_token); + } + ptr = &it->second; + } + else if (ptr->is_array()) + { + if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) + { + // "-" always fails the range check + JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_past_the_end_index, detail::concat( + "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), + ") is out of range"), ptr)); } - case detail::value_t::array: + const auto idx = array_index(reference_token); + // Bounds check before access to avoid exception with JSON_NOEXCEPTION + if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) { - if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) - { - // "-" always fails the range check - JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_past_the_end_index, detail::concat( - "array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), - ") is out of range"), ptr)); - } - - const auto idx = array_index(reference_token); - // Bounds check before access to avoid exception with JSON_NOEXCEPTION - if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) - { - JSON_THROW(detail::out_of_range::create(detail::exception_id::array_index_out_of_range, detail::concat( - "array index ", std::to_string(idx), " is out of range"), ptr)); - } - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; + JSON_THROW(detail::out_of_range::create(detail::exception_id::array_index_out_of_range, detail::concat( + "array index ", std::to_string(idx), " is out of range"), ptr)); } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - throw_unresolved(reference_token, ptr); + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // primitive value with a reference token left + throw_unresolved(reference_token, ptr); } } @@ -27087,44 +27045,32 @@ class json_pointer { for (const auto& reference_token : reference_tokens) { - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + // use unchecked object access; the const operator[] + // asserts that the key exists + const auto it = ptr->m_data.m_value.object->find(reference_token); + JSON_ASSERT(it != ptr->m_data.m_value.object->end()); + ptr = &it->second; + } + else if (ptr->is_array()) + { + if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) { - // use unchecked object access; the const operator[] - // asserts that the key exists - const auto it = ptr->m_data.m_value.object->find(reference_token); - JSON_ASSERT(it != ptr->m_data.m_value.object->end()); - ptr = &it->second; - break; + // "-" cannot be used for const access + JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_past_the_end_index, detail::concat("array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), ") is out of range"), ptr)); } - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) - { - // "-" cannot be used for const access - JSON_THROW(detail::out_of_range::create(detail::exception_id::pointer_past_the_end_index, detail::concat("array index '-' (", std::to_string(ptr->m_data.m_value.array->size()), ") is out of range"), ptr)); - } - - // use unchecked array access; the const operator[] - // asserts that the index exists - const auto idx = array_index(reference_token); - JSON_ASSERT(idx < ptr->m_data.m_value.array->size()); - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - throw_unresolved(reference_token, ptr); + // use unchecked array access; the const operator[] + // asserts that the index exists + const auto idx = array_index(reference_token); + JSON_ASSERT(idx < ptr->m_data.m_value.array->size()); + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // primitive value with a reference token left + throw_unresolved(reference_token, ptr); } } @@ -27148,66 +27094,54 @@ class json_pointer { for (const auto& reference_token : reference_tokens) { - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + const auto it = ptr->m_data.m_value.object->find(reference_token); + if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) { - const auto it = ptr->m_data.m_value.object->find(reference_token); - if (JSON_HEDLEY_UNLIKELY(it == ptr->m_data.m_value.object->end())) - { - return nullptr; - } - ptr = &it->second; - break; - } - - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) - { - // "-" always fails the range check - return nullptr; - } - - // a malformed index throws parse_error.106/109; an - // index that is syntactically valid but cannot be - // represented (out_of_range.404/410) is treated like an - // out-of-range index below - typename BasicJsonType::size_type idx{}; - switch (parse_array_index(reference_token, idx)) - { - // the branches differ in their messages, not after JSON_THROW's expansion - // NOLINTNEXTLINE(bugprone-branch-clone) - case array_index_status::leading_zero: - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_index_leading_zero, 0, detail::concat("array index '", reference_token, "' must not begin with '0'"), nullptr)); - case array_index_status::not_a_number: - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_index_not_a_number, 0, detail::concat("array index '", reference_token, "' is not a number"), nullptr)); - case array_index_status::unresolved: - case array_index_status::exceeds_size_type: - return nullptr; - case array_index_status::ok: - default: - break; - } - - if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) - { - return nullptr; - } - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: return nullptr; + } + ptr = &it->second; + } + else if (ptr->is_array()) + { + if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) + { + // "-" always fails the range check + return nullptr; + } + + // a malformed index throws parse_error.106/109; an + // index that is syntactically valid but cannot be + // represented (out_of_range.404/410) is treated like an + // out-of-range index below + typename BasicJsonType::size_type idx{}; + switch (parse_array_index(reference_token, idx)) + { + // the branches differ in their messages, not after JSON_THROW's expansion + // NOLINTNEXTLINE(bugprone-branch-clone) + case array_index_status::leading_zero: + JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_index_leading_zero, 0, detail::concat("array index '", reference_token, "' must not begin with '0'"), nullptr)); + case array_index_status::not_a_number: + JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_index_not_a_number, 0, detail::concat("array index '", reference_token, "' is not a number"), nullptr)); + case array_index_status::unresolved: + case array_index_status::exceeds_size_type: + return nullptr; + case array_index_status::ok: + default: + break; + } + + if (JSON_HEDLEY_UNLIKELY(idx >= ptr->m_data.m_value.array->size())) + { + return nullptr; + } + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // primitive value with a reference token left + return nullptr; } } @@ -27223,63 +27157,48 @@ class json_pointer { for (const auto& reference_token : reference_tokens) { - switch (ptr->type()) + if (ptr->is_object()) { - case detail::value_t::object: + const auto it = ptr->m_data.m_value.object->find(reference_token); + if (it == ptr->m_data.m_value.object->end()) { - const auto it = ptr->m_data.m_value.object->find(reference_token); - if (it == ptr->m_data.m_value.object->end()) - { - // we did not find the key in the object - return false; - } - - ptr = &it->second; - break; - } - - case detail::value_t::array: - { - if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) - { - // "-" always fails the range check - return false; - } - - // any parse failure (malformed index, or one that is syntactically - // valid but not representable as size_type) means the reference - // token cannot denote an existing array element -- contains() must - // not throw (see #5395), so it is treated as "not found" - typename BasicJsonType::size_type idx{}; - if (JSON_HEDLEY_UNLIKELY(parse_array_index(reference_token, idx) != array_index_status::ok)) - { - return false; - } - - if (idx >= ptr->m_data.m_value.array->size()) - { - // index out of range - return false; - } - - ptr = &(*ptr->m_data.m_value.array)[idx]; - break; - } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: - { - // we do not expect primitive values if there is still a - // reference token to process + // we did not find the key in the object return false; } + + ptr = &it->second; + } + else if (ptr->is_array()) + { + if (JSON_HEDLEY_UNLIKELY(is_dash(reference_token))) + { + // "-" always fails the range check + return false; + } + + // any parse failure (malformed index, or one that is syntactically + // valid but not representable as size_type) means the reference + // token cannot denote an existing array element -- contains() must + // not throw (see #5395), so it is treated as "not found" + typename BasicJsonType::size_type idx{}; + if (JSON_HEDLEY_UNLIKELY(parse_array_index(reference_token, idx) != array_index_status::ok)) + { + return false; + } + + if (idx >= ptr->m_data.m_value.array->size()) + { + // index out of range + return false; + } + + ptr = &(*ptr->m_data.m_value.array)[idx]; + } + else + { + // we do not expect primitive values if there is still a + // reference token to process + return false; } } @@ -27307,10 +27226,7 @@ class json_pointer } // check if a nonempty reference string begins with slash - if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) - { - JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_missing_slash, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr)); - } + check_begins_with_slash(reference_string); // every slash starts a reference token const std::size_t size = reference_string.size(); @@ -27351,9 +27267,7 @@ class json_pointer JSON_ASSERT(reference_token[pos] == '~'); // ~ must be followed by 0 or 1 - if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || - (reference_token[pos + 1] != '0' && - reference_token[pos + 1] != '1'))) + if (JSON_HEDLEY_UNLIKELY(is_invalid_escape(reference_token, pos))) { JSON_THROW(detail::parse_error::create(detail::exception_id::pointer_invalid_escape, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); } @@ -27510,50 +27424,34 @@ class json_pointer auto hint = object.end(); const auto enter = [&stack, &path, &object, &hint](const BasicJsonType & v) { - switch (v.type()) + if (v.is_array()) { - case detail::value_t::array: + if (v.m_data.m_value.array->empty()) { - if (v.m_data.m_value.array->empty()) - { - // flatten empty array as null - add_flattened(object, hint, path, nullptr, detail::priority_tag<2> {}); - } - else - { - stack.emplace_back(&v, object_const_iterator(), path.size()); - } - return; + // flatten empty array as null + add_flattened(object, hint, path, nullptr, detail::priority_tag<2> {}); } - - case detail::value_t::object: + else { - if (v.m_data.m_value.object->empty()) - { - // flatten empty object as null - add_flattened(object, hint, path, nullptr, detail::priority_tag<2> {}); - } - else - { - stack.emplace_back(&v, v.m_data.m_value.object->begin(), path.size()); - } - return; + stack.emplace_back(&v, object_const_iterator(), path.size()); } - - case detail::value_t::null: - case detail::value_t::string: - case detail::value_t::boolean: - case detail::value_t::number_integer: - case detail::value_t::number_unsigned: - case detail::value_t::number_float: - case detail::value_t::binary: - case detail::value_t::discarded: - default: + } + else if (v.is_object()) + { + if (v.m_data.m_value.object->empty()) { - // add a primitive value with its reference string - add_flattened(object, hint, path, v, detail::priority_tag<2> {}); - return; + // flatten empty object as null + add_flattened(object, hint, path, nullptr, detail::priority_tag<2> {}); } + else + { + stack.emplace_back(&v, v.m_data.m_value.object->begin(), path.size()); + } + } + else + { + // add a primitive value with its reference string + add_flattened(object, hint, path, v, detail::priority_tag<2> {}); } }; @@ -28116,6 +28014,8 @@ NLOHMANN_JSON_NAMESPACE_END // #include +// #include + // #include // #include @@ -29021,9 +28921,7 @@ class binary_writer write_cbor_head(0x40, N); // step 2: write each element - oa.write_characters( - reinterpret_cast(j.m_data.m_value.binary->data()), - N); + write_bytes(j.m_data.m_value.binary->data(), N); break; } @@ -29036,7 +28934,7 @@ class binary_writer case value_t::discarded: default: - throw_on_discarded(j, "CBOR"); + throw_on_discarded(j, input_format_t::cbor); } } @@ -29236,9 +29134,7 @@ class binary_writer } // step 2: write the byte string - oa.write_characters( - reinterpret_cast(j.m_data.m_value.binary->data()), - N); + write_bytes(j.m_data.m_value.binary->data(), N); break; } @@ -29252,7 +29148,7 @@ class binary_writer case value_t::discarded: default: - throw_on_discarded(j, "MessagePack"); + throw_on_discarded(j, input_format_t::msgpack); } } @@ -29336,7 +29232,7 @@ class binary_writer case value_t::array: { bool prefix_required = true; - const bool write_closer = write_ubjson_start_array(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); + const bool write_closer = write_ubjson_start_container(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); { const auto& array = *j.m_data.m_value.array; @@ -29386,9 +29282,7 @@ class binary_writer if (use_type) { - oa.write_characters( - reinterpret_cast(j.m_data.m_value.binary->data()), - j.m_data.m_value.binary->size()); + write_bytes(j.m_data.m_value.binary->data(), j.m_data.m_value.binary->size()); } else { @@ -29414,7 +29308,7 @@ class binary_writer } bool prefix_required = true; - const bool write_closer = write_ubjson_start_object(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); + const bool write_closer = write_ubjson_start_container(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); write_ubjson_members(j, use_count, use_type, prefix_required, use_bjdata, bjdata_version, depth); @@ -29428,7 +29322,7 @@ class binary_writer case value_t::discarded: default: - throw_on_discarded(j, use_bjdata ? "BJData" : "UBJSON"); + throw_on_discarded(j, use_bjdata ? input_format_t::bjdata : input_format_t::ubjson); } } @@ -29454,11 +29348,11 @@ class binary_writer @brief throws because @a j is discarded and cannot be serialized @throw type_error.321 always */ - JSON_HEDLEY_NO_RETURN static void throw_on_discarded(const BasicJsonType& j, const char* format_name) + JSON_HEDLEY_NO_RETURN static void throw_on_discarded(const BasicJsonType& j, const input_format_t format) { static_cast(j); // unused when JSON_NOEXCEPTION is defined - static_cast(format_name); - JSON_THROW(type_error::create(exception_id::discarded_value_used, concat("cannot serialize discarded value to ", format_name), &j)); + static_cast(format); + JSON_THROW(type_error::create(exception_id::discarded_value_used, concat("cannot serialize discarded value to ", input_format_name(format)), &j)); } /// how the range of integer type @a NumberType relates to that of @a TargetType @@ -29578,11 +29472,11 @@ class binary_writer } /*! - @brief throws because the integer @a j is too large for @a format_name + @brief throws because the integer @a j is too large for @a format @throw out_of_range.407 always */ JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE - static void throw_integer_out_of_range(const BasicJsonType& j, const char* format_name, const char* range) + static void throw_integer_out_of_range(const BasicJsonType& j, const input_format_t format, const char* range) { // neither std::to_string(), which has no overload for integer types // wider than 64 bits, nor dump(), which would also instantiate the @@ -29593,9 +29487,9 @@ class binary_writer ? integer_to_string(j.m_data.m_value.number_unsigned, std::integral_constant()) : integer_to_string(j.m_data.m_value.number_integer, std::integral_constant()); static_cast(number); // unused when JSON_NOEXCEPTION is defined - static_cast(format_name); + static_cast(format); static_cast(range); - JSON_THROW(out_of_range::create(exception_id::integer_too_large, concat("integer number ", number, " cannot be represented by ", format_name, " as it does not fit ", range), &j)); + JSON_THROW(out_of_range::create(exception_id::integer_too_large, concat("integer number ", number, " cannot be represented by ", input_format_name(format), " as it does not fit ", range), &j)); } // The exceptions below are thrown from functions of their own, out of line: @@ -29638,53 +29532,56 @@ class binary_writer JSON_THROW(other_error::create(exception_id::size_marker_required, "use_type requires use_size = true", &j)); } + /// @brief write @a size bytes starting at @a data to the output adapter + JSON_HEDLEY_ALWAYS_INLINE + void write_bytes(const void* data, const std::size_t size) + { + oa.write_characters(static_cast(data), size); + } + + /// @brief write the MessagePack prefix of an array or object with @a N + /// elements: the fix marker @a fix_marker | n, or @a marker_16 or + /// @a marker_32 followed by the length + JSON_HEDLEY_ALWAYS_INLINE + void write_msgpack_container_prefix(const std::size_t N, const BasicJsonType& j, + const std::uint8_t fix_marker, const std::uint8_t marker_16, const std::uint8_t marker_32) + { + const auto n = to_msgpack_length(N, j); + if (n <= 15) + { + // fixarray, fixmap + oa.write_character(to_char_type(static_cast(fix_marker | n))); + } + else if (n <= (std::numeric_limits::max)()) + { + // array 16, map 16 + oa.write_character(to_char_type(marker_16)); + write_number(static_cast(n)); + } + else + { + // array 32, map 32 + oa.write_character(to_char_type(marker_32)); + write_number(static_cast(n)); + } + } + + JSON_HEDLEY_ALWAYS_INLINE void write_msgpack_array_prefix(const std::size_t N, const BasicJsonType& j) { - const auto n = to_msgpack_length(N, j); - if (n <= 15) - { - // fixarray - oa.write_character(to_char_type(static_cast(0x90 | n))); - } - else if (n <= (std::numeric_limits::max)()) - { - // array 16 - oa.write_character(to_char_type(0xDC)); - write_number(static_cast(n)); - } - else - { - // array 32 - oa.write_character(to_char_type(0xDD)); - write_number(static_cast(n)); - } + write_msgpack_container_prefix(N, j, 0x90, 0xDC, 0xDD); } + JSON_HEDLEY_ALWAYS_INLINE void write_msgpack_object_prefix(const std::size_t N, const BasicJsonType& j) { - const auto n = to_msgpack_length(N, j); - if (n <= 15) - { - // fixmap - oa.write_character(to_char_type(static_cast(0x80 | (n & 0xF)))); - } - else if (n <= (std::numeric_limits::max)()) - { - // map 16 - oa.write_character(to_char_type(0xDE)); - write_number(static_cast(n)); - } - else - { - // map 32 - oa.write_character(to_char_type(0xDF)); - write_number(static_cast(n)); - } + write_msgpack_container_prefix(N, j, 0x80, 0xDE, 0xDF); } - /// @brief a CBOR, MessagePack, or BON8 array or object whose elements - /// @ref write_cbor_iterative, @ref write_msgpack_iterative, or - /// @ref write_bon8_iterative is still writing + /// @brief a CBOR, MessagePack, BON8, or (as the base of @ref ubjson_frame) + /// UBJSON array or object whose elements @ref write_container_iterative, + /// @ref write_bon8_iterative, or @ref write_ubjson_iterative is still + /// writing struct binary_container_frame { explicit binary_container_frame(const BasicJsonType* value_) noexcept @@ -29720,19 +29617,82 @@ class binary_writer typename BasicJsonType::array_t::const_iterator array_it{}; }; + /// @brief the CBOR specifics of @ref write_container_iterative + struct cbor_walk + { + static void array_head(binary_writer& w, const BasicJsonType& j) + { + w.write_cbor_head(0x80, j.m_data.m_value.array->size()); + } + + static void object_head(binary_writer& w, const BasicJsonType& j) + { + w.write_cbor_head(0xA0, j.m_data.m_value.object->size()); + } + + static void scalar(binary_writer& w, const BasicJsonType& j) + { + w.write_cbor(j); + } + + static void string(binary_writer& w, const string_t& key, const BasicJsonType& context) + { + w.write_cbor_string(key, context); + } + + template + static void key(binary_writer& w, const KeyType& k, const BasicJsonType& object) + { + w.write_cbor_key(k, object); + } + }; + + /// @brief the MessagePack specifics of @ref write_container_iterative + struct msgpack_walk + { + static void array_head(binary_writer& w, const BasicJsonType& j) + { + w.write_msgpack_array_prefix(j.m_data.m_value.array->size(), j); + } + + static void object_head(binary_writer& w, const BasicJsonType& j) + { + w.write_msgpack_object_prefix(j.m_data.m_value.object->size(), j); + } + + static void scalar(binary_writer& w, const BasicJsonType& j) + { + w.write_msgpack(j); + } + + static void string(binary_writer& w, const string_t& key, const BasicJsonType& context) + { + w.write_msgpack_string(key, context); + } + + template + static void key(binary_writer& w, const KeyType& k, const BasicJsonType& object) + { + w.write_msgpack_key(k, object); + } + }; + /*! - @brief write @a j with @ref write_cbor, or write its header and push a - frame for @ref write_cbor_iterative to continue with its elements + @brief write @a j with @a Walk::scalar, or write its header and push a + frame for @ref write_container_iterative to continue with its + elements A scalar, and an empty array or object, are written out in full: there is - nothing below them for @ref write_cbor_iterative to come back to, so + nothing below them for @ref write_container_iterative to come back to, so nothing is pushed for them. */ - void write_cbor_value_or_push(const BasicJsonType& j, std::vector& stack) + template + JSON_HEDLEY_ALWAYS_INLINE + void write_container_value_or_push(const BasicJsonType& j, std::vector& stack) { if (j.is_array()) { - write_cbor_head(0x80, j.m_data.m_value.array->size()); + Walk::array_head(*this, j); if (!j.m_data.m_value.array->empty()) { stack.emplace_back(&j); @@ -29742,7 +29702,7 @@ class binary_writer if (j.is_object()) { - write_cbor_head(0xA0, j.m_data.m_value.object->size()); + Walk::object_head(*this, j); if (!j.m_data.m_value.object->empty()) { stack.emplace_back(&j); @@ -29750,22 +29710,26 @@ class binary_writer return; } - write_cbor(j); + Walk::scalar(*this, j); } /*! @brief write out @a root and everything below it without the call stack - Emits the same bytes as @ref write_cbor, keeping the containers it has + Emits the same bytes as the recursive writer, keeping the containers it has entered on an explicit stack instead of descending into them. Only reached for values nested deeper than @ref recursion_depth_limit, which is why it - is not written for speed. + is not written for speed. Shared by CBOR and MessagePack, which differ only + in the @a Walk policy (@ref cbor_walk, @ref msgpack_walk); BON8 has its own + walker, as it tracks an open string run and closes a container with a + marker. */ - void write_cbor_iterative(const BasicJsonType& root) + template + void write_container_iterative(const BasicJsonType& root) { - // only a container with elements is ever pushed; see write_cbor_value_or_push + // only a container with elements is ever pushed; see write_container_value_or_push std::vector stack; - write_cbor_value_or_push(root, stack); + write_container_value_or_push(root, stack); while (!stack.empty()) { @@ -29783,7 +29747,7 @@ class binary_writer // read the child before pushing: entering it can move every frame const BasicJsonType* child = &(*current.array_it); ++stack.back().array_it; - write_cbor_value_or_push(*child, stack); + write_container_value_or_push(*child, stack); } else { @@ -29795,110 +29759,53 @@ class binary_writer } // the key is written directly (not via a temporary basic_json), - // with the object as diagnostics context, as in write_cbor - write_cbor_key(current.object_it->first, *current.value); + // with the object as diagnostics context, as in the recursive writer + Walk::key(*this, current.object_it->first, *current.value); const BasicJsonType* child = &(current.object_it->second); ++stack.back().object_it; - write_cbor_value_or_push(*child, stack); + write_container_value_or_push(*child, stack); } } } - /*! - @brief write @a j with @ref write_msgpack, or write its header and push a - frame for @ref write_msgpack_iterative to continue with its elements - - @sa @ref write_cbor_value_or_push - */ - void write_msgpack_value_or_push(const BasicJsonType& j, std::vector& stack) + void write_cbor_iterative(const BasicJsonType& root) { - if (j.is_array()) - { - write_msgpack_array_prefix(j.m_data.m_value.array->size(), j); - if (!j.m_data.m_value.array->empty()) - { - stack.emplace_back(&j); - } - return; - } - - if (j.is_object()) - { - write_msgpack_object_prefix(j.m_data.m_value.object->size(), j); - if (!j.m_data.m_value.object->empty()) - { - stack.emplace_back(&j); - } - return; - } - - write_msgpack(j); + write_container_iterative(root); } - /*! - @brief write out @a root and everything below it without the call stack - - @sa @ref write_cbor_iterative - */ void write_msgpack_iterative(const BasicJsonType& root) { - std::vector stack; - write_msgpack_value_or_push(root, stack); - - while (!stack.empty()) - { - const binary_container_frame current = stack.back(); - - if (current.value->is_array()) - { - const auto& array = *current.value->m_data.m_value.array; - if (current.array_it == array.cend()) - { - stack.pop_back(); - continue; - } - - const BasicJsonType* child = &(*current.array_it); - ++stack.back().array_it; - write_msgpack_value_or_push(*child, stack); - } - else - { - const auto& object = *current.value->m_data.m_value.object; - if (current.object_it == object.cend()) - { - stack.pop_back(); - continue; - } - - // as in write_cbor_iterative, the key is written directly with - // the object as diagnostics context - write_msgpack_key(current.object_it->first, *current.value); - const BasicJsonType* child = &(current.object_it->second); - ++stack.back().object_it; - write_msgpack_value_or_push(*child, stack); - } - } + write_container_iterative(root); } - /// @return true when a closing ']' still has to be written after the elements - bool write_ubjson_start_array(const BasicJsonType& j, const bool use_count, const bool use_type, - const bool add_prefix, const bool use_bjdata, bool& prefix_required) + /*! + @brief write the start of an array (@a IsArray) or object: its opening + marker, the optimized type, and the count + + @return true when a closing ']' or '}' still has to be written after the + elements + */ + template + bool write_ubjson_start_container(const BasicJsonType& j, const bool use_count, const bool use_type, + const bool add_prefix, const bool use_bjdata, bool& prefix_required) { prefix_required = true; if (add_prefix) { - oa.write_character(to_char_type('[')); + oa.write_character(to_char_type(IsArray ? '[' : '{')); } - if (use_type && !j.m_data.m_value.array->empty()) + const std::size_t size = IsArray ? j.m_data.m_value.array->size() : j.m_data.m_value.object->size(); + + if (use_type && size != 0) { if (!use_count) { throw_size_marker_required(j); } const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata); - const bool same_prefix = std::all_of(j.begin() + 1, j.end(), + // the first element of an array has been compared with itself + const bool same_prefix = std::all_of(IsArray ? j.begin() + 1 : j.begin(), j.end(), [this, first_prefix, use_bjdata](const BasicJsonType & v) { return ubjson_prefix(v, use_bjdata) == first_prefix; @@ -29910,8 +29817,9 @@ class binary_writer // those, at one byte per element, so the result can be read back. // Objects are not affected: every element is preceded by its key. const bool valueless_type = (first_prefix == 'Z' || first_prefix == 'T' || first_prefix == 'F'); - const bool excessive_valueless = valueless_type - && j.m_data.m_value.array->size() > detail::max_valueless_container_size; + const bool excessive_valueless = IsArray + && valueless_type + && size > detail::max_valueless_container_size; if (same_prefix && !excessive_valueless && !(use_bjdata && is_bjdata_excluded_type_marker(first_prefix))) @@ -29924,46 +29832,7 @@ class binary_writer if (use_count) { - write_ubjson_count(j.m_data.m_value.array->size(), use_bjdata); - } - - return !use_count; - } - - /// @return true when a closing '}' still has to be written after the elements - bool write_ubjson_start_object(const BasicJsonType& j, const bool use_count, const bool use_type, - const bool add_prefix, const bool use_bjdata, bool& prefix_required) - { - prefix_required = true; - if (add_prefix) - { - oa.write_character(to_char_type('{')); - } - - if (use_type && !j.m_data.m_value.object->empty()) - { - if (!use_count) - { - throw_size_marker_required(j); - } - const CharType first_prefix = ubjson_prefix(j.front(), use_bjdata); - const bool same_prefix = std::all_of(j.begin(), j.end(), - [this, first_prefix, use_bjdata](const BasicJsonType & v) - { - return ubjson_prefix(v, use_bjdata) == first_prefix; - }); - - if (same_prefix && !(use_bjdata && is_bjdata_excluded_type_marker(first_prefix))) - { - prefix_required = false; - oa.write_character(to_char_type('$')); - oa.write_character(first_prefix); - } - } - - if (use_count) - { - write_ubjson_count(j.m_data.m_value.object->size(), use_bjdata); + write_ubjson_count(size, use_bjdata); } return !use_count; @@ -29991,21 +29860,12 @@ class binary_writer /// @brief an object or array @ref write_ubjson_iterative is still writing /// the elements of - struct ubjson_frame + struct ubjson_frame : binary_container_frame { ubjson_frame(const BasicJsonType* value_, const bool prefix_required_) noexcept - : value(value_) + : binary_container_frame(value_) , prefix_required(prefix_required_) - { - if (value->is_object()) - { - object_it = value->m_data.m_value.object->cbegin(); - } - else - { - array_it = value->m_data.m_value.array->cbegin(); - } - } + {} // declared for GCC's -Weffc++, which asks for them in a class with // pointer members and a non-trivial destructor; the exception @@ -30017,13 +29877,9 @@ class binary_writer ubjson_frame& operator=(ubjson_frame&&) = default; ~ubjson_frame() = default; - /// the array or object being written - const BasicJsonType* value; /// whether value's elements each carry their own type marker; an /// optimized ($type) container writes it once for all of them instead bool prefix_required; - typename BasicJsonType::object_t::const_iterator object_it{}; - typename BasicJsonType::array_t::const_iterator array_it{}; }; /*! @@ -30034,7 +29890,7 @@ class binary_writer elements of an optimized container, and everything below the top level, never repeat it) - @sa @ref write_cbor_value_or_push + @sa @ref write_container_value_or_push */ void write_ubjson_value_or_push(const BasicJsonType& j, const bool add_prefix, const bool use_count, const bool use_type, const bool use_bjdata, const bjdata_version_t bjdata_version, @@ -30057,11 +29913,11 @@ class binary_writer bool prefix_required = true; if (is_array) { - write_ubjson_start_array(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); + write_ubjson_start_container(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); } else { - write_ubjson_start_object(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); + write_ubjson_start_container(j, use_count, use_type, add_prefix, use_bjdata, prefix_required); } const bool empty = is_array ? j.m_data.m_value.array->empty() : j.m_data.m_value.object->empty(); @@ -30071,7 +29927,7 @@ class binary_writer return; } - // write_ubjson_start_array/_object return !use_count, i.e. whether a + // write_ubjson_start_container returns !use_count, i.e. whether a // closer still has to be written; use_count is constant for the whole // document, so that is recomputed here instead of being carried along if (!use_count) @@ -30260,14 +30116,15 @@ class binary_writer { oa.write_character(to_char_type(element_type)); - if (error_handler == error_handler_t::keep || error_handler == error_handler_t::strict || is_valid_utf8(name)) + if (JSON_HEDLEY_LIKELY(error_handler == error_handler_t::keep || error_handler == error_handler_t::strict)) { - oa.write_characters(reinterpret_cast(name.data()), name.size()); + write_bytes(name.data(), name.size()); } else { - const string_t sanitized = sanitize_utf8(name, error_handler); - oa.write_characters(reinterpret_cast(sanitized.data()), sanitized.size()); + string_t storage; + const string_t& written = sanitize_utf8_for_bson_write(name, storage); + write_bytes(written.data(), written.size()); } // the terminating null byte is written explicitly rather than taken @@ -30343,16 +30200,11 @@ class binary_writer { write_bson_entry_header(name, 0x02); - const bool sanitize = error_handler != error_handler_t::keep - && error_handler != error_handler_t::strict - && !is_valid_utf8(value); - const string_t sanitized = sanitize ? sanitize_utf8(value, error_handler) : string_t{}; - const string_t& written = sanitize ? sanitized : value; + string_t storage; + const string_t& written = sanitize_utf8_for_bson_write(value, storage); write_number(to_bson_length(written.size() + 1ul), true); - oa.write_characters( - reinterpret_cast(written.data()), - written.size()); + write_bytes(written.data(), written.size()); // the terminating null byte is written explicitly rather than taken // from the buffer, so that string_t::data() need not be null-terminated oa.write_character(to_char_type(0x00)); @@ -30458,7 +30310,7 @@ class binary_writer write_number(value.has_subtype() ? static_cast(value.subtype()) : static_cast(0x00)); - oa.write_characters(reinterpret_cast(value.data()), value.size()); + write_bytes(value.data(), value.size()); } /*! @@ -30488,14 +30340,14 @@ class binary_writer case value_t::number_integer: if (JSON_HEDLEY_UNLIKELY(!integer_fits(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "BSON", "int64"); + throw_integer_out_of_range(j, input_format_t::bson, "int64"); } return calc_bson_integer_size(static_cast(j.m_data.m_value.number_integer)); case value_t::number_unsigned: if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_unsigned))) { - throw_integer_out_of_range(j, "BSON", "uint64"); + throw_integer_out_of_range(j, input_format_t::bson, "uint64"); } return calc_bson_unsigned_size(conditional_static_cast(j.m_data.m_value.number_unsigned)); @@ -30506,7 +30358,7 @@ class binary_writer return 0ul; case value_t::discarded: - throw_on_discarded(j, "BSON"); + throw_on_discarded(j, input_format_t::bson); // LCOV_EXCL_START case value_t::object: @@ -30670,7 +30522,7 @@ class binary_writer // the terminating null byte is written explicitly rather than // taken from the buffer, so that string_t::data() need not be // null-terminated - oa.write_characters(reinterpret_cast(value.data()), value.size()); + write_bytes(value.data(), value.size()); oa.write_character(to_char_type(0x00)); return true; } @@ -30687,7 +30539,7 @@ class binary_writer p += sizeof(std::int32_t); *p++ = value.has_subtype() ? to_char_type(static_cast(value.subtype())) : to_char_type(0x00); oa.commit(static_cast(p - out)); - oa.write_characters(reinterpret_cast(value.data()), value.size()); + write_bytes(value.data(), value.size()); return true; } @@ -30735,7 +30587,7 @@ class binary_writer return write_bson_null(name); case value_t::discarded: - throw_on_discarded(j, "BSON"); + throw_on_discarded(j, input_format_t::bson); // LCOV_EXCL_START case value_t::object: @@ -31516,7 +31368,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_unsigned))) { - throw_integer_out_of_range(j, "CBOR", "uint64"); + throw_integer_out_of_range(j, input_format_t::cbor, "uint64"); } write_cbor_head(0x00, conditional_static_cast(j.m_data.m_value.number_unsigned)); return; @@ -31526,7 +31378,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "CBOR", "[-2^64, 2^64-1]"); + throw_integer_out_of_range(j, input_format_t::cbor, "[-2^64, 2^64-1]"); } // CBOR does not differentiate between positive signed // integers and unsigned integers @@ -31537,7 +31389,7 @@ class binary_writer // a negative integer n is encoded as -1 - n if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(-1 - j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "CBOR", "[-2^64, 2^64-1]"); + throw_integer_out_of_range(j, input_format_t::cbor, "[-2^64, 2^64-1]"); } write_cbor_head(0x20, static_cast(-1 - j.m_data.m_value.number_integer)); } @@ -31613,7 +31465,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_unsigned))) { - throw_integer_out_of_range(j, "MessagePack", "uint64"); + throw_integer_out_of_range(j, input_format_t::msgpack, "uint64"); } write_msgpack_unsigned(static_cast(j.m_data.m_value.number_unsigned)); return; @@ -31623,7 +31475,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "MessagePack", "[-2^63, 2^64-1]"); + throw_integer_out_of_range(j, input_format_t::msgpack, "[-2^63, 2^64-1]"); } // MessagePack does not differentiate between positive // signed integers and unsigned integers. @@ -31633,7 +31485,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_min(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "MessagePack", "[-2^63, 2^64-1]"); + throw_integer_out_of_range(j, input_format_t::msgpack, "[-2^63, 2^64-1]"); } if (j.m_data.m_value.number_integer >= -32) { @@ -31767,40 +31619,37 @@ class binary_writer } /*! - @brief write an object key as a CBOR text string + @brief write an object key as a CBOR or MessagePack string (@a Walk) A key convertible to string_t is written directly. Other key types (only an explicit conversion, or only a to_json overload) go through a temporary basic_json, as in version 3.12.0; the temporary is then the diagnostics context for strict UTF-8 checks. */ - template::value, int> = 0> + void write_object_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& context) + { + Walk::string(*this, key, context); + } + + template>::value, int> = 0> + void write_object_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& /*context*/) + { + Walk::scalar(*this, BasicJsonType(key)); + } + + /// @brief write an object key as a CBOR text string void write_cbor_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& context) { - write_cbor_string(key, context); + write_object_key(key, context); } - template < typename Key = typename BasicJsonType::object_t::key_type, - enable_if_t < !std::is_convertible::value, int > = 0 > - void write_cbor_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& /*context*/) - { - write_cbor(BasicJsonType(key)); - } - - /// @brief write an object key as a MessagePack str, as in @ref write_cbor_key - template::value, int> = 0> + /// @brief write an object key as a MessagePack str void write_msgpack_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& context) { - write_msgpack_string(key, context); - } - - template < typename Key = typename BasicJsonType::object_t::key_type, - enable_if_t < !std::is_convertible::value, int > = 0 > - void write_msgpack_key(const typename BasicJsonType::object_t::key_type& key, const BasicJsonType& /*context*/) - { - write_msgpack(BasicJsonType(key)); + write_object_key(key, context); } /// the longest string that is written, with its head of one or two @@ -31875,9 +31724,7 @@ class binary_writer write_cbor_head(0x60, sanitized.size()); // step 2: write the string - oa.write_characters( - reinterpret_cast(sanitized.data()), - sanitized.size()); + write_bytes(sanitized.data(), sanitized.size()); } ///////////// @@ -31958,9 +31805,7 @@ class binary_writer } // step 2: write the string - oa.write_characters( - reinterpret_cast(sanitized.data()), - sanitized.size()); + write_bytes(sanitized.data(), sanitized.size()); } //////////// @@ -32243,9 +32088,7 @@ class binary_writer oa.write_character(to_char_type('S')); } write_number_with_ubjson_prefix(sanitized.size(), true, use_bjdata); - oa.write_characters( - reinterpret_cast(sanitized.data()), - sanitized.size()); + write_bytes(sanitized.data(), sanitized.size()); } /*! @@ -33045,7 +32888,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits_max(j.m_data.m_value.number_unsigned))) { - throw_integer_out_of_range(j, "BON8", "int64"); + throw_integer_out_of_range(j, input_format_t::bon8, "int64"); } write_bon8_integer(static_cast(j.m_data.m_value.number_unsigned)); string_open = false; @@ -33056,7 +32899,7 @@ class binary_writer { if (JSON_HEDLEY_UNLIKELY(!integer_fits(j.m_data.m_value.number_integer))) { - throw_integer_out_of_range(j, "BON8", "int64"); + throw_integer_out_of_range(j, input_format_t::bon8, "int64"); } write_bon8_integer(static_cast(j.m_data.m_value.number_integer)); string_open = false; @@ -33124,7 +32967,7 @@ class binary_writer case value_t::discarded: default: - throw_on_discarded(j, "BON8"); + throw_on_discarded(j, input_format_t::bon8); } } @@ -33135,7 +32978,7 @@ class binary_writer A scalar, and an empty array or object, are written out in full and not pushed. - @sa @ref write_cbor_value_or_push + @sa @ref write_container_value_or_push */ void write_bon8_value_or_push(const BasicJsonType& j, bool& string_open, std::vector& stack) { @@ -33521,7 +33364,7 @@ class binary_writer } else { - oa.write_characters(reinterpret_cast(s.data()), s.size()); + write_bytes(s.data(), s.size()); string_open = true; } } @@ -33612,6 +33455,31 @@ class binary_writer } } + /*! + @brief the write-pass counterpart of @ref sanitize_utf8_for_write for BSON + + The size pass (@ref calc_bson_entry_header_size, @ref + calc_bson_string_size) has already validated @a s, and thrown for @ref + error_handler_t::strict if it was ill-formed. Here, @ref + error_handler_t::strict therefore behaves like @ref error_handler_t::keep, + and @ref error_handler_t::replace / @ref error_handler_t::ignore produce + exactly the bytes the size pass measured. Hence no context is needed. + + @param[in] s the string (value or object key) to write + @param[out] storage backing storage for a sanitized copy + + @return a reference to @a s, or to @a storage once it holds a sanitized copy + */ + const string_t& sanitize_utf8_for_bson_write(const string_t& s, string_t& storage) const + { + if (error_handler == error_handler_t::keep || error_handler == error_handler_t::strict || is_valid_utf8(s)) + { + return s; // NOLINT(bugprone-return-const-ref-from-parameter): callers pass lvalues that outlive the call + } + storage = sanitize_utf8(s, error_handler); + return storage; + } + /// deleted: anything but a string_t would bind a temporary that dies before the returned reference is used template < typename T, enable_if_t < !std::is_same::value, int > = 0 > const string_t& sanitize_utf8_for_write(const T& /*s*/, const BasicJsonType& /*context*/, string_t& /*storage*/) const = delete; // NOLINT(hicpp-use-equals-delete,modernize-use-equals-delete): a private helper's guard, not part of the interface @@ -36701,6 +36569,28 @@ class serializer } } + /// write out the bytes pending in @ref string_buffer and reset @a bytes + JSON_HEDLEY_ALWAYS_INLINE void emit_pending(std::size_t& bytes) + { + if (bytes != 0) + { + put_buffer(string_buffer, bytes); + bytes = 0; + } + } + + /// write buffer and reset index if fewer than 13 bytes are left, as this + /// is the maximal number of bytes to be written ("\uxxxx\uxxxx\0") for one + /// code point + JSON_HEDLEY_ALWAYS_INLINE void reserve_string_buffer(std::size_t& bytes) + { + if (string_buffer.size() - bytes < 13) + { + put_buffer(string_buffer, bytes); + bytes = 0; + } + } + /*! @brief worker for @ref dump_escaped @@ -36759,11 +36649,7 @@ class serializer { // emit any bytes still pending in string_buffer first to // preserve output order, then write the run directly - if (bytes != 0) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + emit_pending(bytes); put_run(s, i, run); bytes_after_last_accept = 0; undumped_chars = 0; @@ -36784,11 +36670,10 @@ class serializer { // emit any bytes still pending in string_buffer first to // preserve output order - if (bytes != 0) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + emit_pending(bytes); + // deliberately not put_buffer(): a fixed 8-byte memcpy + // has no asserts and no write-through branch, which + // matters on the hot path const auto& escape = ascii_escapes()[data[i]]; if (JSON_HEDLEY_UNLIKELY(write_buffer.size() - write_buffer_pos < escape.size())) { @@ -36863,14 +36748,7 @@ class serializer string_buffer[bytes++] = s[i]; } - // write buffer and reset index; there must be 13 bytes - // left, as this is the maximal number of bytes to be - // written ("\uxxxx\uxxxx\0") for one code point - if (string_buffer.size() - bytes < 13) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + reserve_string_buffer(bytes); // remember the byte position of this accept bytes_after_last_accept = bytes; @@ -36922,14 +36800,7 @@ class serializer string_buffer[bytes++] = '\xBD'; } - // write buffer and reset index; there must be 13 bytes - // left, as this is the maximal number of bytes to be - // written ("\uxxxx\uxxxx\0") for one code point - if (string_buffer.size() - bytes < 13) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + reserve_string_buffer(bytes); bytes_after_last_accept = bytes; } @@ -36962,14 +36833,7 @@ class serializer string_buffer[bytes++] = s[i]; } - // write buffer and reset index; there must be 13 bytes - // left, as this is the maximal number of bytes to be - // written ("\uxxxx\uxxxx\0") for one code point - if (string_buffer.size() - bytes < 13) - { - put_buffer(string_buffer, bytes); - bytes = 0; - } + reserve_string_buffer(bytes); bytes_after_last_accept = bytes; undumped_chars = 0; @@ -37335,30 +37199,17 @@ class serializer */ static const std::array& ascii_copyable() noexcept { - static const std::array copyable = - { - { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x00 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x10 - 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x20 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x30 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x40 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, // 0x50 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x60 - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, // 0x70 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x80 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x90 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xA0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xB0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xC0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xD0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0xE0 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 // 0xF0 - } - }; + // constant-initialized from the predicate: no runtime loop, no guard + static constexpr std::array copyable = make_ascii_copyable(make_index_sequence<256> {}); return copyable; } + template + static constexpr std::array make_ascii_copyable(index_sequence /*unused*/) noexcept + { + return {{ static_cast(is_ascii_copyable(static_cast(Is)) ? 1 : 0)... }}; + } + /*! @brief the JSON form of each character below 0x60 @@ -37487,7 +37338,6 @@ class serializer */ JSON_HEDLEY_NEVER_INLINE std::size_t write_u_escapes(const unsigned char* data, std::size_t n) { - constexpr const char* nibble_to_hex = "0123456789abcdef"; std::size_t i = 0; while (i < n && data[i] >= 0x80) { @@ -37524,26 +37374,21 @@ class serializer char* d = write_buffer.data() + write_buffer_pos; if (codepoint > 0xFFFFu) { - d = put_u_escape(d, static_cast(0xD7C0u + (codepoint >> 10u)), nibble_to_hex); + d = put_u_escape(d, static_cast(0xD7C0u + (codepoint >> 10u))); codepoint = 0xDC00u + (codepoint & 0x3FFu); } - d = put_u_escape(d, static_cast(codepoint), nibble_to_hex); + d = put_u_escape(d, static_cast(codepoint)); write_buffer_pos = static_cast(d - write_buffer.data()); i += length; } return i; } - static char* put_u_escape(char* d, const std::uint16_t codeunit, const char* nibble_to_hex) noexcept + static char* put_u_escape(char* d, const std::uint16_t codeunit) noexcept { - const auto unit = static_cast(codeunit); d[0] = '\\'; d[1] = 'u'; - d[2] = nibble_to_hex[(unit >> 12u) & 0x0Fu]; - d[3] = nibble_to_hex[(unit >> 8u) & 0x0Fu]; - d[4] = nibble_to_hex[(unit >> 4u) & 0x0Fu]; - d[5] = nibble_to_hex[unit & 0x0Fu]; - return d + 6; + return hex4_lower(codeunit, d + 2); } /*! @@ -37552,7 +37397,7 @@ class serializer * Branch-free replacement for `snprintf(buf, 7, "\\u%04x", codeunit)` in the * string escaping hot path. It writes exactly six characters ('\\', 'u' and * four hex digits) at position @a pos of @a string_buffer via a nibble - * lookup table, avoiding the format-string parsing and locale machinery of + * lookup table (@ref hex4_lower), avoiding the format-string parsing and locale machinery of * `snprintf`. Advances @a pos by the number of bytes written (6). * * @param[in] pos position in @a string_buffer to write at; there must @@ -37562,13 +37407,9 @@ class serializer void write_u_escape(std::size_t& pos, std::uint16_t codeunit) noexcept { JSON_ASSERT(string_buffer.size() - pos >= 6); - constexpr const char* nibble_to_hex = "0123456789abcdef"; string_buffer[pos + 0] = '\\'; string_buffer[pos + 1] = 'u'; - string_buffer[pos + 2] = nibble_to_hex[(codeunit >> 12u) & 0x0Fu]; - string_buffer[pos + 3] = nibble_to_hex[(codeunit >> 8u) & 0x0Fu]; - string_buffer[pos + 4] = nibble_to_hex[(codeunit >> 4u) & 0x0Fu]; - string_buffer[pos + 5] = nibble_to_hex[codeunit & 0x0Fu]; + hex4_lower(codeunit, &string_buffer[pos + 2]); pos += 6; } @@ -38084,6 +37925,19 @@ private: Container::pop_back(); } + /// @brief remove the entry with the given key; returns the number of removed entries + template + size_type erase_impl(const K& key) + { + const auto it = find_impl(*this, key); + if (it != this->end()) + { + erase_at(it); + return 1; + } + return 0; + } + public: template::value, int> = 0> @@ -38173,26 +38027,14 @@ public: size_type erase(const key_type& key) { - const auto it = find_impl(*this, key); - if (it != this->end()) - { - erase_at(it); - return 1; - } - return 0; + return erase_impl(key); } template::value, int> = 0> size_type erase(KeyType && key) // NOLINT(cppcoreguidelines-missing-std-forward) { - const auto it = find_impl(*this, key); - if (it != this->end()) - { - erase_at(it); - return 1; - } - return 0; + return erase_impl(key); } iterator erase(iterator pos) @@ -38686,9 +38528,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec result["name"] = "JSON for Modern C++"; result["url"] = "https://github.com/nlohmann/json"; result["version"]["string"] = - detail::concat(std::to_string(NLOHMANN_JSON_VERSION_MAJOR), '.', - std::to_string(NLOHMANN_JSON_VERSION_MINOR), '.', - std::to_string(NLOHMANN_JSON_VERSION_PATCH)); + detail::concat(detail::to_string(static_cast(NLOHMANN_JSON_VERSION_MAJOR)), '.', + detail::to_string(static_cast(NLOHMANN_JSON_VERSION_MINOR)), '.', + detail::to_string(static_cast(NLOHMANN_JSON_VERSION_PATCH))); result["version"]["major"] = NLOHMANN_JSON_VERSION_MAJOR; result["version"]["minor"] = NLOHMANN_JSON_VERSION_MINOR; result["version"]["patch"] = NLOHMANN_JSON_VERSION_PATCH; @@ -38710,10 +38552,10 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #elif defined(__clang__) result["compiler"] = {{"family", "clang"}, {"version", __clang_version__}}; #elif defined(__GNUC__) || defined(__GNUG__) - result["compiler"] = {{"family", "gcc"}, {"version", detail::concat( - std::to_string(__GNUC__), '.', - std::to_string(__GNUC_MINOR__), '.', - std::to_string(__GNUC_PATCHLEVEL__)) + result["compiler"] = {{"family", "gcc"}, {"version", detail::concat( + detail::to_string(static_cast(__GNUC__)), '.', + detail::to_string(static_cast(__GNUC_MINOR__)), '.', + detail::to_string(static_cast(__GNUC_PATCHLEVEL__))) } }; #elif defined(__HP_aCC) @@ -38731,9 +38573,9 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec #endif #if defined(_MSVC_LANG) - result["compiler"]["c++"] = std::to_string(_MSVC_LANG); + result["compiler"]["c++"] = detail::to_string(static_cast(_MSVC_LANG)); #elif defined(__cplusplus) - result["compiler"]["c++"] = std::to_string(__cplusplus); + result["compiler"]["c++"] = detail::to_string(static_cast(__cplusplus)); #else result["compiler"]["c++"] = "unknown"; #endif @@ -39044,23 +38886,15 @@ private: // true if v is not an array/object, or is an already-empty one static bool has_no_children(const basic_json& v) noexcept { - switch (v.m_data.m_type) + if (v.m_data.m_type == value_t::array) { - case value_t::array: - return v.m_data.m_value.array->empty(); - case value_t::object: - return v.m_data.m_value.object->empty(); - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - return true; + return v.m_data.m_value.array->empty(); } + if (v.m_data.m_type == value_t::object) + { + return v.m_data.m_value.object->empty(); + } + return true; } // true if no child of the non-empty array/object v has children of @@ -39165,28 +38999,17 @@ private: } public: - void destroy_string() noexcept + template + static void destroy_pointer(T*& ptr) noexcept { - if (string == nullptr) + if (ptr == nullptr) { // not initialized (e.g., due to exception in the ctor) return; } - AllocatorType alloc; - std::allocator_traits::destroy(alloc, string); - std::allocator_traits::deallocate(alloc, string, 1); - } - - void destroy_binary() noexcept - { - if (binary == nullptr) - { - // not initialized (e.g., due to exception in the ctor) - return; - } - AllocatorType alloc; - std::allocator_traits::destroy(alloc, binary); - std::allocator_traits::deallocate(alloc, binary, 1); + AllocatorType alloc; + std::allocator_traits::destroy(alloc, ptr); + std::allocator_traits::deallocate(alloc, ptr, 1); } // t must be value_t::array or value_t::object @@ -39352,11 +39175,11 @@ public: switch (t) { case value_t::string: - destroy_string(); + destroy_pointer(string); break; case value_t::binary: - destroy_binary(); + destroy_pointer(binary); break; case value_t::object: @@ -39418,36 +39241,19 @@ public: void set_parents() { #if JSON_DIAGNOSTICS - switch (m_data.m_type) + if (m_data.m_type == value_t::array) { - case value_t::array: + for (auto& element : *m_data.m_value.array) { - for (auto& element : *m_data.m_value.array) - { - element.m_parent = this; - } - break; + element.m_parent = this; } - - case value_t::object: + } + else if (m_data.m_type == value_t::object) + { + for (auto& element : *m_data.m_value.object) { - for (auto& element : *m_data.m_value.object) - { - element.second.m_parent = this; - } - break; + element.second.m_parent = this; } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - break; } #endif } @@ -40895,8 +40701,7 @@ public: case value_t::string: case value_t::binary: { - if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() - || !last.m_it.primitive_iterator.is_end())) + if (JSON_HEDLEY_UNLIKELY(!primitive_range_is_complete(first, last))) { detail::throw_literal(detail::exception_id::iterator_range_out_of_range, "iterators out of range", first.m_object); } @@ -41370,6 +41175,135 @@ public: JSON_THROW(std::length_error(detail::concat("array index ", std::to_string(idx), " exceeds size_type"))); } + /// @throw type_error.309 always, as insert() is not defined for the type of @a j + JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE + static void throw_insert_wrong_type(const basic_json& j) + { + detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", j); + } + + /// @throw invalid_iterator.202 always, as an iterator belongs to another value than @a obj + JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE + static void throw_iterator_from_other_value(const basic_json* obj) + { + detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", obj); + } + + /// @throw type_error.310 always, as swap() with the given operand is not defined for the type of @a j + JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE + static void throw_swap_wrong_type(const char* operation, const basic_json& j) + { + detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, operation, j); + } + + /// exchange the stored @a Stored value (if the value has that type) with @a other + template + void swap_impl(Other& other, const char* operation) + { + Stored* const stored = get_impl_ptr(static_cast(nullptr)); + if (JSON_HEDLEY_UNLIKELY(stored == nullptr)) + { + throw_swap_wrong_type(operation, *this); + } + + using std::swap; + swap(*stored, other); + } + + /// whether [first, last) spans the whole of a primitive value + template + JSON_HEDLEY_ALWAYS_INLINE + static bool primitive_range_is_complete(const IteratorType& first, const IteratorType& last) noexcept + { + return first.m_it.primitive_iterator.is_begin() && last.m_it.primitive_iterator.is_end(); + } + + /// destroy the current (primitive) value and become null + void reset_to_null_impl() + { + m_data.m_value.destroy(m_data.m_type); + m_data.m_value = {}; + m_data.m_type = value_t::null; + assert_invariant(); + } + + /// append @a val (moved or copied) to the array; a null value becomes an array + template + JSON_HEDLEY_ALWAYS_INLINE + void push_back_array_impl(ValueType&& val) + { + // push_back only works for null objects or arrays + if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) + { + detail::throw_cannot_use_with(detail::exception_id::push_back_wrong_type, "push_back()", *this); + } + + // transform a null object into an array + if (is_null()) + { + convert_null_to(); + } + + // add the element to the array + const auto old_capacity = container_capacity(); + m_data.m_value.array->push_back(std::forward(val)); + set_parent_after_insert(m_data.m_value.array->back(), old_capacity); + } + + /// parse the input behind an already created adapter + template + static basic_json parse_impl(InputAdapterType&& adapter, + parser_callback_t cb, + const bool allow_exceptions, + const bool ignore_comments, + const bool ignore_trailing_commas) + { + basic_json result; + parser(std::forward(adapter), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas).parse(true, result); + return result; + } + + /// check the input behind an already created adapter + template + static bool accept_impl(InputAdapterType&& adapter, + const bool ignore_comments, + const bool ignore_trailing_commas) + { + return parser(std::forward(adapter), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true); + } + + /// generate SAX events from the input behind an already created adapter + template + static bool sax_parse_impl(InputAdapterType ia, SAX* sax, + const input_format_t format, + const bool strict, + const bool ignore_comments, + const bool ignore_trailing_commas, + const cbor_tag_handler_t tag_handler) + { + if (format == input_format_t::json) + { + return parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict); + } + // the SAX parser may recover from errors (see parse_error()) + return detail::binary_reader(std::move(ia), format).sax_parse(sax, strict, tag_handler); + } + + /// serialize @a j into a new vector; @a write receives the vector-backed writer + template + static std::vector to_binary_vector(const basic_json& j, const error_handler_t error_handler, WriteFunction write) + { + std::vector result; + result.reserve(detail::binary_initial_capacity(j)); + { + // the sink sets the size of result when it is destroyed, which + // must happen before result is returned (without NRVO, it is moved) + auto writer = vector_writer(result, error_handler); + write(writer); + } + return result; + } + /// @throw out_of_range.401 always, as @a idx is out of range of @a j JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE static void throw_array_index_out_of_range(size_type idx, const basic_json& j) @@ -42352,7 +42286,7 @@ public: // make sure the iterator fits the current value if (JSON_HEDLEY_UNLIKELY(this != pos.m_object)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } IteratorType result = end(); @@ -42371,10 +42305,7 @@ public: detail::throw_literal(detail::exception_id::iterator_out_of_range, "iterator out of range", this); } - m_data.m_value.destroy(m_data.m_type); - m_data.m_value = {}; - m_data.m_type = value_t::null; - assert_invariant(); + reset_to_null_impl(); break; } @@ -42424,16 +42355,12 @@ public: case value_t::string: case value_t::binary: { - if (JSON_HEDLEY_UNLIKELY(!first.m_it.primitive_iterator.is_begin() - || !last.m_it.primitive_iterator.is_end())) + if (JSON_HEDLEY_UNLIKELY(!primitive_range_is_complete(first, last))) { detail::throw_literal(detail::exception_id::iterator_range_out_of_range, "iterators out of range", this); } - m_data.m_value.destroy(m_data.m_type); - m_data.m_value = {}; - m_data.m_type = value_t::null; - assert_invariant(); + reset_to_null_impl(); break; } @@ -42914,34 +42841,20 @@ public: JSON_HEDLEY_WARN_UNUSED_RESULT size_type max_size() const noexcept { - switch (m_data.m_type) + if (m_data.m_type == value_t::array) { - case value_t::array: - { - // delegate call to array_t::max_size() - return m_data.m_value.array->max_size(); - } - - case value_t::object: - { - // delegate call to object_t::max_size() - return m_data.m_value.object->max_size(); - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // all other types have max_size() == size() - return size(); - } + // delegate call to array_t::max_size() + return m_data.m_value.array->max_size(); } + + if (m_data.m_type == value_t::object) + { + // delegate call to object_t::max_size() + return m_data.m_value.object->max_size(); + } + + // all other types have max_size() == size() + return size(); } /// @} @@ -43019,22 +42932,7 @@ public: /// @sa https://json.nlohmann.me/api/basic_json/push_back/ void push_back(basic_json&& val) { - // push_back only works for null objects or arrays - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) - { - detail::throw_cannot_use_with(detail::exception_id::push_back_wrong_type, "push_back()", *this); - } - - // transform a null object into an array - if (is_null()) - { - convert_null_to(); - } - - // add the element to the array (move semantics) - const auto old_capacity = container_capacity(); - m_data.m_value.array->push_back(std::move(val)); - set_parent_after_insert(m_data.m_value.array->back(), old_capacity); + push_back_array_impl(std::move(val)); // if val is moved from, basic_json move constructor marks it null, so we do not call the destructor } @@ -43050,22 +42948,7 @@ public: /// @sa https://json.nlohmann.me/api/basic_json/push_back/ void push_back(const basic_json& val) { - // push_back only works for null objects or arrays - if (JSON_HEDLEY_UNLIKELY(!(is_null() || is_array()))) - { - detail::throw_cannot_use_with(detail::exception_id::push_back_wrong_type, "push_back()", *this); - } - - // transform a null object into an array - if (is_null()) - { - convert_null_to(); - } - - // add the element to the array - const auto old_capacity = container_capacity(); - m_data.m_value.array->push_back(val); - set_parent_after_insert(m_data.m_value.array->back(), old_capacity); + push_back_array_impl(val); } /// @brief add an object to an array @@ -43211,13 +43094,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // insert to array and return iterator @@ -43231,13 +43114,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // moving into a local first keeps this safe even if val aliases @@ -43253,13 +43136,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // insert to array and return iterator @@ -43273,13 +43156,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // check if range iterators belong to the same JSON object @@ -43310,13 +43193,13 @@ public: // insert only works for arrays if (JSON_HEDLEY_UNLIKELY(!is_array())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if iterator pos fits to this JSON value if (JSON_HEDLEY_UNLIKELY(pos.m_object != this)) { - detail::throw_literal(detail::exception_id::iterator_from_other_value, "iterator does not fit current value", this); + throw_iterator_from_other_value(this); } // copy the values first: ilist may refer to elements of this array @@ -43338,7 +43221,7 @@ public: // insert only works for objects if (JSON_HEDLEY_UNLIKELY(!is_object())) { - detail::throw_cannot_use_with(detail::exception_id::insert_wrong_type, "insert()", *this); + throw_insert_wrong_type(*this); } // check if range iterators belong to the same JSON object @@ -43580,82 +43463,37 @@ public: /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(array_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) { - // swap only works for arrays - if (JSON_HEDLEY_LIKELY(is_array())) - { - using std::swap; - swap(*(m_data.m_value.array), other); - set_parents(); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(array_t&)", *this); - } + swap_impl(other, "swap(array_t&)"); + set_parents(); } /// @brief exchanges the values /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(object_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) { - // swap only works for objects - if (JSON_HEDLEY_LIKELY(is_object())) - { - using std::swap; - swap(*(m_data.m_value.object), other); - set_parents(); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(object_t&)", *this); - } + swap_impl(other, "swap(object_t&)"); + set_parents(); } /// @brief exchanges the values /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(string_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) { - // swap only works for strings - if (JSON_HEDLEY_LIKELY(is_string())) - { - using std::swap; - swap(*(m_data.m_value.string), other); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(string_t&)", *this); - } + swap_impl(other, "swap(string_t&)"); } /// @brief exchanges the values /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(binary_t& other) // NOLINT(bugprone-exception-escape,cppcoreguidelines-noexcept-swap,performance-noexcept-swap) { - // swap only works for binary values - if (JSON_HEDLEY_LIKELY(is_binary())) - { - using std::swap; - swap(*(m_data.m_value.binary), other); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(binary_t&)", *this); - } + swap_impl(other, "swap(binary_t&)"); } /// @brief exchanges the values /// @sa https://json.nlohmann.me/api/basic_json/swap/ void swap(typename binary_t::container_type& other) // NOLINT(bugprone-exception-escape) { - // swap only works for binary values - if (JSON_HEDLEY_LIKELY(is_binary())) - { - using std::swap; - swap(*(m_data.m_value.binary), other); - } - else - { - detail::throw_cannot_use_with(detail::exception_id::swap_wrong_type, "swap(binary_t::container_type&)", *this); - } + swap_impl(other, "swap(binary_t::container_type&)"); } /// @} @@ -44336,10 +44174,7 @@ public: const bool ignore_comments = false, const bool ignore_trailing_commas = false) { - basic_json result; - auto p = parser(detail::input_adapter(std::forward(i)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); - p.parse(true, result); - return result; + return parse_impl(detail::input_adapter(std::forward(i)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); } /// @brief deserialize from a pair of character iterators (or an iterator+sentinel pair, C++20 ranges support) @@ -44354,10 +44189,7 @@ public: const bool ignore_comments = false, const bool ignore_trailing_commas = false) { - basic_json result; - auto p = parser(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); - p.parse(true, result); - return result; + return parse_impl(detail::input_adapter(std::move(first), std::move(last)), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); } JSON_HEDLEY_WARN_UNUSED_RESULT @@ -44371,10 +44203,7 @@ public: = delete; #else { - basic_json result; - auto p = parser(i.get(), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); - p.parse(true, result); - return result; + return parse_impl(i.get(), std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas); } #endif @@ -44386,7 +44215,7 @@ public: const bool ignore_comments = false, const bool ignore_trailing_commas = false) { - return parser(detail::input_adapter(std::forward(i)), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true); + return accept_impl(detail::input_adapter(std::forward(i)), ignore_comments, ignore_trailing_commas); } /// @brief check if the input is valid JSON (iterator pair, or iterator+sentinel pair for C++20 ranges support) @@ -44398,7 +44227,7 @@ public: const bool ignore_comments = false, const bool ignore_trailing_commas = false) { - return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true); + return accept_impl(detail::input_adapter(std::move(first), std::move(last)), ignore_comments, ignore_trailing_commas); } JSON_HEDLEY_WARN_UNUSED_RESULT @@ -44410,7 +44239,7 @@ public: = delete; #else { - return parser(i.get(), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true); + return accept_impl(i.get(), ignore_comments, ignore_trailing_commas); } #endif @@ -44425,10 +44254,7 @@ public: const bool ignore_trailing_commas = false, const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { - auto ia = detail::input_adapter(std::forward(i)); - return format == input_format_t::json - ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(sax, strict, tag_handler); + return sax_parse_impl(detail::input_adapter(std::forward(i)), sax, format, strict, ignore_comments, ignore_trailing_commas, tag_handler); } /// @brief generate SAX events (iterator pair, or iterator+sentinel pair for C++20 ranges support) @@ -44443,10 +44269,7 @@ public: const bool ignore_trailing_commas = false, const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error) { - auto ia = detail::input_adapter(std::move(first), std::move(last)); - return format == input_format_t::json - ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict) - : detail::binary_reader(std::move(ia), format).sax_parse(sax, strict, tag_handler); + return sax_parse_impl(detail::input_adapter(std::move(first), std::move(last)), sax, format, strict, ignore_comments, ignore_trailing_commas, tag_handler); } /// @brief generate SAX events @@ -44480,12 +44303,7 @@ public: = delete; #else { - auto ia = i.get(); - return format == input_format_t::json - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - ? parser(std::move(ia), nullptr, true, ignore_comments, ignore_trailing_commas).sax_parse(sax, strict) - // NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg) - : detail::binary_reader(std::move(ia), format).sax_parse(sax, strict, tag_handler); + return sax_parse_impl(i.get(), sax, format, strict, ignore_comments, ignore_trailing_commas, tag_handler); } #endif #if defined(__clang__) @@ -44633,10 +44451,10 @@ public: static std::vector to_cbor(const basic_json& j, const error_handler_t error_handler = detail::binary_writer_default_error_handler()) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_cbor(j); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_cbor(j); + }); } /// @brief create a CBOR serialization of a given JSON value @@ -44664,10 +44482,10 @@ public: static std::vector to_msgpack(const basic_json& j, const error_handler_t error_handler = error_handler_t::keep) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_msgpack(j); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_msgpack(j); + }); } /// @brief create a MessagePack serialization of a given JSON value @@ -44697,10 +44515,10 @@ public: const bool use_type = false, const error_handler_t error_handler = detail::binary_writer_default_error_handler()) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_ubjson(j, use_size, use_type); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_ubjson(j, use_size, use_type); + }); } /// @brief create a UBJSON serialization of a given JSON value @@ -44733,10 +44551,10 @@ public: const bjdata_version_t version = bjdata_version_t::draft2, const error_handler_t error_handler = detail::binary_writer_default_error_handler()) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_ubjson(j, use_size, use_type, true, true, version); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_ubjson(j, use_size, use_type, true, true, version); + }); } /// @brief create a BJData serialization of a given JSON value @@ -44768,10 +44586,10 @@ public: static std::vector to_bson(const basic_json& j, const error_handler_t error_handler = detail::binary_writer_default_error_handler()) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result, error_handler).write_bson(j); - return result; + return to_binary_vector(j, error_handler, [&](vector_binary_writer& w) + { + w.write_bson(j); + }); } /// @brief create a BSON serialization of a given JSON value @@ -44798,10 +44616,10 @@ public: /// @sa https://json.nlohmann.me/api/basic_json/to_bon8/ static std::vector to_bon8(const basic_json& j) { - std::vector result; - result.reserve(detail::binary_initial_capacity(j)); - vector_writer(result).write_bon8(j); - return result; + return to_binary_vector(j, detail::binary_writer_default_error_handler(), [&](vector_binary_writer& w) + { + w.write_bon8(j); + }); } /// @brief create a BON8 serialization of a given JSON value @@ -46137,82 +45955,68 @@ public: return; } - switch (source.type()) + if (detail::is_primitive(source.type())) { - case value_t::array: + // both primitive types: replace value + diff_replace(result, path, target); + return; + } + + if (source.is_array()) + { + const std::size_t path_length = path.size(); + + // first pass: traverse common elements + std::size_t i = 0; + while (i < source.size() && i < target.size()) { + // recursive call to compare array values at index i + if (!(source[i] == target[i])) + { + diff_append_index(path, i); + diff_unequal(result, source[i], target[i], path, depth + 1); + path.resize(path_length); + } + ++i; + } + + // We now reached the end of at least one array + // in a second pass, traverse the remaining elements + diff_array_tails(result, source, target, path, i); + } + else + { + diff_target_values target_values; + std::vector added; + if (diff_object_keys(result, source, target, path, target_values, added)) + { + // fast path: target_values holds, for every member of + // source in order, its value in target -- so no further + // lookup is needed. Deleted keys are interleaved here + // too, in source's original order, to match the + // historical (pre-reordering-aware) output order. const std::size_t path_length = path.size(); - - // first pass: traverse common elements std::size_t i = 0; - while (i < source.size() && i < target.size()) + for (auto it = source.cbegin(); it != source.cend(); ++it, ++i) { - // recursive call to compare array values at index i - if (!(source[i] == target[i])) + if (target_values[i] != nullptr) { - diff_append_index(path, i); - diff_unequal(result, source[i], target[i], path, depth + 1); - path.resize(path_length); - } - ++i; - } - - // We now reached the end of at least one array - // in a second pass, traverse the remaining elements - diff_array_tails(result, source, target, path, i); - break; - } - - case value_t::object: - { - diff_target_values target_values; - std::vector added; - if (diff_object_keys(result, source, target, path, target_values, added)) - { - // fast path: target_values holds, for every member of - // source in order, its value in target -- so no further - // lookup is needed. Deleted keys are interleaved here - // too, in source's original order, to match the - // historical (pre-reordering-aware) output order. - const std::size_t path_length = path.size(); - std::size_t i = 0; - for (auto it = source.cbegin(); it != source.cend(); ++it, ++i) - { - if (target_values[i] != nullptr) + if (!(it.value() == *target_values[i])) { - if (!(it.value() == *target_values[i])) - { - diff_append_key(path, it.key()); - diff_unequal(result, it.value(), *target_values[i], path, depth + 1); - path.resize(path_length); - } - } - else - { - // found a key that is not in target -> remove it - diff_remove(result, diff_key_path(path, it.key())); + diff_append_key(path, it.key()); + diff_unequal(result, it.value(), *target_values[i], path, depth + 1); + path.resize(path_length); } } - - // append the "add" operations for brand-new keys - diff_added(result, added, path); + else + { + // found a key that is not in target -> remove it + diff_remove(result, diff_key_path(path, it.key())); + } } - break; - } - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // both primitive types: replace value - diff_replace(result, path, target); - break; + // append the "add" operations for brand-new keys + diff_added(result, added, path); } } } @@ -46258,46 +46062,31 @@ public: return; } - switch (s.type()) + if (detail::is_primitive(s.type())) { - case value_t::array: - { - stack.emplace_back(&s, &t, current_path.size()); - return; - } - - case value_t::object: - { - std::vector target_values; - std::vector added; - if (diff_object_keys(result, s, t, current_path, target_values, added)) - { - // fast path: the frame walks source in lockstep with - // common_keys, as diff_recursively does, and appends - // added_ops once all members are done - stack.emplace_back(&s, &t, current_path.size()); - stack.back().member = s.cbegin(); - stack.back().target_values = std::move(target_values); - stack.back().added = std::move(added); - } - return; - } - - case value_t::null: - case value_t::string: - case value_t::boolean: - case value_t::number_integer: - case value_t::number_unsigned: - case value_t::number_float: - case value_t::binary: - case value_t::discarded: - default: - { - // both primitive types: replace value - diff_replace(result, current_path, t); - return; - } + // both primitive types: replace value + diff_replace(result, current_path, t); + return; } + + if (s.is_array()) + { + stack.emplace_back(&s, &t, current_path.size()); + return; + } + std::vector target_values; + std::vector added; + if (diff_object_keys(result, s, t, current_path, target_values, added)) + { + // fast path: the frame walks source in lockstep with + // common_keys, as diff_recursively does, and appends + // added_ops once all members are done + stack.emplace_back(&s, &t, current_path.size()); + stack.back().member = s.cbegin(); + stack.back().target_values = std::move(target_values); + stack.back().added = std::move(added); + } + return; }; enter(source, target); diff --git a/single_include/nlohmann/json_view.hpp b/single_include/nlohmann/json_view.hpp index d05a9fe64..4298c901b 100644 --- a/single_include/nlohmann/json_view.hpp +++ b/single_include/nlohmann/json_view.hpp @@ -2819,32 +2819,52 @@ namespace view // Exceptions are thrown out of line, so that the accessors that may throw stay // small enough to be inlined. -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(int id, const char* prefix, const char* type) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(exception_id id, const char* prefix, const char* type) { NLOHMANN_VIEW_THROW(type_error::create(id, concat(prefix, type), nullptr)); } -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(int id, const std::string& msg) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_type_error(exception_id id, const std::string& msg) { NLOHMANN_VIEW_THROW(type_error::create(id, msg, nullptr)); } -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_out_of_range(int id, const std::string& msg) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_out_of_range(exception_id id, const std::string& msg) { NLOHMANN_VIEW_THROW(out_of_range::create(id, msg, nullptr)); } -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_invalid_iterator(int id, const char* msg) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_invalid_iterator(exception_id id, const char* msg) { NLOHMANN_VIEW_THROW(invalid_iterator::create(id, msg, nullptr)); } /// a parse error without a position (as those of json_pointer) -[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_parse_error(int id, const std::string& msg) +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_parse_error(exception_id id, const std::string& msg) { NLOHMANN_VIEW_THROW(parse_error::create(id, 0, msg, nullptr)); } +/// out_of_range.401: the array index @a idx is not below the size +template +[[noreturn]] NLOHMANN_VIEW_NOINLINE void throw_array_index_out_of_range(const Index idx) +{ + throw_out_of_range(exception_id::array_index_out_of_range, concat("array index ", std::to_string(idx), " is out of range")); +} + +/// out_of_range.402: the JSON Pointer token '-' of an array of @a size elements +[[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_array_index_past_end(std::size_t size) +{ + throw_out_of_range(exception_id::pointer_past_the_end_index, concat("array index '-' (", std::to_string(size), ") is out of range")); +} + +/// out_of_range.403: the object has no member @a key +template +[[noreturn]] NLOHMANN_VIEW_NOINLINE void throw_key_not_found(const Key& key) +{ + throw_out_of_range(exception_id::key_not_found, concat("key '", key, "' not found")); +} + /// SAX handler of the error replay: accepts every event without storing it, /// and throws the exception the parser reports template @@ -2953,7 +2973,7 @@ template if (f.code == error_code::input_too_large) { // (the limit is detail::view::max_input_size(): 4 GiB minus 16 bytes) - NLOHMANN_VIEW_THROW(out_of_range::create(416, "input of 4294967280 bytes or more is not supported by json_document", nullptr)); + NLOHMANN_VIEW_THROW(out_of_range::create(exception_id::document_too_large, "input of 4294967280 bytes or more is not supported by json_document", nullptr)); } throwing_sax sax; const bool accepted = BasicJsonType::sax_parse(src, src + size, &sax, input_format_t::json, true, ignore_comments, ignore_trailing_commas); @@ -2973,7 +2993,7 @@ template } } pos.chars_read_current_line = off + 1 - line_start; - NLOHMANN_VIEW_THROW(parse_error::create(101, pos, "syntax error while parsing value", nullptr)); + NLOHMANN_VIEW_THROW(parse_error::create(exception_id::syntax_error, pos, "syntax error while parsing value", nullptr)); // LCOV_EXCL_STOP } @@ -3048,7 +3068,7 @@ inline std::size_t text_capacity(std::size_t cap, std::size_t used, std::size_t constexpr std::size_t limit = 0xFFFFFFFFu; if (NLOHMANN_VIEW_UNLIKELY(used > limit || n > limit - used)) { - throw_out_of_range(416, "edits of 4 GiB or more are not supported by json_document"); + throw_out_of_range(exception_id::document_too_large, "edits of 4 GiB or more are not supported by json_document"); } const std::size_t needed = used + n; const std::size_t wanted = needed + (std::min)(limit - needed, std::size_t{256}); @@ -3099,7 +3119,7 @@ inline void reserve_moved(document_data& d) document_data::edit_state& e = edit_state_of(d); if (e.moved.size() >= 0xFFFFFFFFu) { - throw_out_of_range(416, "more than 4294967295 edited arrays and objects are not supported by json_document"); // LCOV_EXCL_LINE + throw_out_of_range(exception_id::document_too_large, "more than 4294967295 edited arrays and objects are not supported by json_document"); // LCOV_EXCL_LINE } if (e.moved.size() == e.moved.capacity() || e.moved_cap.size() == e.moved_cap.capacity()) { @@ -3729,11 +3749,6 @@ struct first_true : std::integral_constant < int, 1 + firs inline void check_utf8(const char* s, std::size_t n) { const auto* const p = reinterpret_cast(s); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) - const auto hex = [](unsigned char c) - { - constexpr const char* digits = "0123456789ABCDEF"; - return std::string{digits[c >> 4u], digits[c & 0xFu]}; - }; for (std::size_t i = 0; i < n;) { const unsigned char c = p[i]; @@ -3742,39 +3757,22 @@ inline void check_utf8(const char* s, std::size_t n) ++i; continue; } - std::size_t len = 0; - unsigned char lo = 0x80; - unsigned char hi = 0xBF; - if (c >= 0xC2 && c <= 0xDF) + const utf8_sequence_shape shape = utf8_shape(c); + const std::size_t len = shape.length; + if (len == 0) { - len = 2; - } - else if (c >= 0xE0 && c <= 0xEF) - { - len = 3; - lo = c == 0xE0 ? 0xA0 : 0x80; - hi = c == 0xED ? 0x9F : 0xBF; - } - else if (c >= 0xF0 && c <= 0xF4) - { - len = 4; - lo = c == 0xF0 ? 0x90 : 0x80; - hi = c == 0xF4 ? 0x8F : 0xBF; - } - else - { - throw_type_error(316, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex(c))); + throw_type_error(exception_id::invalid_utf8, concat("invalid UTF-8 byte at index ", std::to_string(i), ": 0x", hex_byte(c))); } for (std::size_t k = 1; k < len; ++k) { if (i + k == n) { - throw_type_error(316, concat("incomplete UTF-8 string; last byte: 0x", hex(p[n - 1]))); + throw_type_error(exception_id::invalid_utf8, concat("incomplete UTF-8 string; last byte: 0x", hex_byte(p[n - 1]))); } const unsigned char b = p[i + k]; - if (b < (k == 1 ? lo : 0x80) || b > (k == 1 ? hi : 0xBF)) + if (b < (k == 1 ? shape.second_lo : 0x80) || b > (k == 1 ? shape.second_hi : 0xBF)) { - throw_type_error(316, concat("invalid UTF-8 byte at index ", std::to_string(i + k), ": 0x", hex(b))); + throw_type_error(exception_id::invalid_utf8, concat("invalid UTF-8 byte at index ", std::to_string(i + k), ": 0x", hex_byte(b))); } } i += len; @@ -3822,7 +3820,7 @@ class editor node* const o = own(object); if (o->kind != static_cast(value_t::object) && o->kind != static_cast(value_t::null)) { - throw_type_error(305, "cannot use operator[] with a string argument with ", object.type_name()); + throw_type_error(exception_id::subscript_wrong_type, "cannot use operator[] with a string argument with ", object.type_name()); } check_utf8(key.data(), key.size()); const encoded e = encode(std::forward(value)); @@ -3873,7 +3871,7 @@ class editor node* const a = own(array); if (a->kind != static_cast(value_t::array)) { - throw_type_error(305, "cannot use operator[] with a numeric argument with ", array.type_name()); + throw_type_error(exception_id::subscript_wrong_type, "cannot use operator[] with a numeric argument with ", array.type_name()); } check_index(idx, a->len); const encoded e = encode(std::forward(value)); @@ -3890,7 +3888,7 @@ class editor node* const a = own(array); if (a->kind != static_cast(value_t::array) && a->kind != static_cast(value_t::null)) { - throw_type_error(308, "cannot use push_back() with ", array.type_name()); + throw_type_error(exception_id::push_back_wrong_type, "cannot use push_back() with ", array.type_name()); } const encoded e = encode(std::forward(value)); if (a->kind == static_cast(value_t::null)) @@ -3914,7 +3912,7 @@ class editor node* const a = own(array); if (a->kind != static_cast(value_t::array)) { - throw_type_error(309, "cannot use insert() with ", array.type_name()); + throw_type_error(exception_id::insert_wrong_type, "cannot use insert() with ", array.type_name()); } check_index(idx, a->len + 1); const encoded e = encode(std::forward(value)); @@ -3934,7 +3932,7 @@ class editor node* const o = own(object); if (o->kind != static_cast(value_t::object)) { - throw_type_error(307, "cannot use erase() with ", object.type_name()); + throw_type_error(exception_id::erase_wrong_type, "cannot use erase() with ", object.type_name()); } for (const node* k = nav::first(m_doc, o), *end = nav::end(m_doc, o); k != end; k = document_data::after(k + 1)) { @@ -3952,7 +3950,7 @@ class editor node* const a = own(array); if (a->kind != static_cast(value_t::array)) { - throw_type_error(307, "cannot use erase() with ", array.type_name()); + throw_type_error(exception_id::erase_wrong_type, "cannot use erase() with ", array.type_name()); } check_index(idx, a->len); node* const h = block_of(m_doc, a, 0); @@ -3975,7 +3973,7 @@ class editor { if (NLOHMANN_VIEW_UNLIKELY(v.m_doc != &m_doc || v.m_node == nullptr)) { - throw_invalid_iterator(202, "view does not belong to this document"); + throw_invalid_iterator(exception_id::iterator_from_other_value, "view does not belong to this document"); } edit_state_of(m_doc); return const_cast(v.m_node); // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to this document @@ -3985,7 +3983,7 @@ class editor { if (idx >= limit) { - throw_out_of_range(401, concat("array index ", std::to_string(idx), " is out of range")); + throw_array_index_out_of_range(idx); } } @@ -4126,7 +4124,7 @@ class editor { if (NLOHMANN_VIEW_UNLIKELY(v.m_node == nullptr)) { - throw_type_error(302, "type must be a value, but is ", "discarded"); + throw_type_error(exception_id::type_mismatch, "type must be a value, but is ", "discarded"); } encoded r; if (!is_container(*v.m_node)) @@ -4276,7 +4274,7 @@ class editor { if (NLOHMANN_VIEW_UNLIKELY(len >= 0xFFFFFFFFu)) { - throw_out_of_range(416, "strings of 4 GiB or more are not supported by json_document"); // LCOV_EXCL_LINE + throw_out_of_range(exception_id::document_too_large, "strings of 4 GiB or more are not supported by json_document"); // LCOV_EXCL_LINE } node n = plain_node(value_t::string); n.flags = static_cast(n.flags | node_flags::edited); @@ -4351,12 +4349,12 @@ class editor return string_node(s.data(), s.size()); } case value_t::binary: - throw_type_error(319, "cannot store a binary value in a json_document", ""); + throw_type_error(exception_id::document_binary_value, "cannot store a binary value in a json_document", ""); case value_t::discarded: case value_t::object: case value_t::array: default: - throw_type_error(302, "type must be a value, but is ", "discarded"); + throw_type_error(exception_id::type_mismatch, "type must be a value, but is ", "discarded"); } } @@ -4984,7 +4982,7 @@ inline void compact_nodes(const document_data& d, std::size_t arena_size, std::v inline std::vector save_image(const document_data& d) { #if !NLOHMANN_VIEW_LITTLE_ENDIAN - throw_type_error(320, "json_document images need a little-endian target"); // LCOV_EXCL_LINE + throw_type_error(exception_id::document_image_unsupported, "json_document images need a little-endian target"); // LCOV_EXCL_LINE #endif const std::size_t arena_size = d.arena_size; std::vector compacted; @@ -5001,7 +4999,7 @@ inline std::vector save_image(const document_data& d) if (NLOHMANN_VIEW_UNLIKELY(text_size >= image_limit || total_arena >= image_limit || count >= image_limit)) { // LCOV_EXCL_START (4 GiB) - throw_out_of_range(416, "images of 4 GiB or more are not supported by json_document"); + throw_out_of_range(exception_id::document_too_large, "images of 4 GiB or more are not supported by json_document"); // LCOV_EXCL_STOP } image_header h{}; @@ -5458,7 +5456,7 @@ inline bool check_image(const node* nodes, std::size_t count, const unsigned cha [[noreturn]] NLOHMANN_VIEW_NOINLINE inline void throw_invalid_image(const char* what) { - throw_parse_error(116, concat("invalid json_document image: ", what)); + throw_parse_error(exception_id::invalid_document_image, concat("invalid json_document image: ", what)); } /// Read an image into d. The text and the decoded strings stay in the image; @@ -5466,7 +5464,7 @@ inline bool check_image(const node* nodes, std::size_t count, const unsigned cha inline void load_image(document_data& d, const std::uint8_t* image, std::size_t size, image_check check) { #if !NLOHMANN_VIEW_LITTLE_ENDIAN - throw_type_error(320, "json_document images need a little-endian target"); // LCOV_EXCL_LINE + throw_type_error(exception_id::document_image_unsupported, "json_document images need a little-endian target"); // LCOV_EXCL_LINE #endif if (image == nullptr || size < sizeof(image_header)) { @@ -5854,7 +5852,7 @@ class view_iterator { if (NLOHMANN_VIEW_UNLIKELY(m_value_offset == 0)) { - throw_invalid_iterator(207, "cannot use key() for non-object iterators"); + throw_invalid_iterator(exception_id::iterator_key_not_object, "cannot use key() for non-object iterators"); } return string_view_t(m_doc->str(*m_pos), m_pos->len); } @@ -6162,6 +6160,8 @@ BasicJsonType materialize(const document_data& d, const node* n) case value_t::binary: case value_t::discarded: default: + // false positive: the parser's frame stack points into its own storage (never null) + // @infer-ignore NULLPTR_DEREFERENCE sax.null(); break; } @@ -6246,51 +6246,15 @@ enum class pointer_mode contains, ///< as basic_json::contains(): no exceptions at all }; -/// the outcome of reading an array index from a reference token -enum class index_status -{ - ok, - leading_zero, ///< parse_error.106 - not_number, ///< parse_error.109 - unresolved, ///< out_of_range.404 - too_large, ///< out_of_range.410 -}; +/// the outcome of reading an array index: detail::parse_array_index(), shared with json_pointer +using index_status = detail::array_index_status; /// reads an array index like json_pointer::array_index (RFC 6901, Sect. 4), /// but reports errors instead of throwing them template index_status array_index(const StringType& s, std::size_t& idx) noexcept { - // as basic_json: the empty token and "-" (past the last element) are - // unresolved rather than not a number - if (s.empty() || (s.size() == 1 && s[0] == '-')) - { - return index_status::unresolved; - } - if (s[0] < '0' || s[0] > '9') - { - return index_status::not_number; - } - if (s.size() > 1 && s[0] == '0') - { - return index_status::leading_zero; - } - std::uint64_t v = 0; - for (const auto c : s) - { - const auto d = static_cast(static_cast(c)) - '0'; - if (d > 9 || v > ((std::numeric_limits::max)() - d) / 10) - { - return index_status::unresolved; // not a number, or beyond unsigned long long - } - v = (v * 10) + d; - } - if (v >= (std::numeric_limits::max)()) // (std::size_t converts to std::uint64_t implicitly) - { - return index_status::too_large; - } - idx = static_cast(v); - return index_status::ok; + return detail::parse_array_index(s, idx); } /// throws the exception json_pointer::array_index throws for this status @@ -6300,15 +6264,15 @@ template switch (status) { case index_status::leading_zero: - throw_parse_error(106, concat("array index '", s, "' must not begin with '0'")); - case index_status::not_number: - throw_parse_error(109, concat("array index '", s, "' is not a number")); - case index_status::too_large: - throw_out_of_range(410, concat("array index ", s, " exceeds size_type")); // LCOV_EXCL_LINE + throw_parse_error(exception_id::pointer_index_leading_zero, concat("array index '", s, "' must not begin with '0'")); + case index_status::not_a_number: + throw_parse_error(exception_id::pointer_index_not_a_number, concat("array index '", s, "' is not a number")); + case index_status::exceeds_size_type: + throw_out_of_range(exception_id::value_out_of_range, concat("array index ", s, " exceeds size_type")); // LCOV_EXCL_LINE case index_status::unresolved: case index_status::ok: default: - throw_out_of_range(404, concat("unresolved reference token '", s, "'")); + throw_out_of_range(exception_id::pointer_unresolved, concat("unresolved reference token '", s, "'")); } } @@ -6333,7 +6297,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) { if (mode == pointer_mode::checked) { - throw_out_of_range(403, concat("key '", token, "' not found")); + throw_key_not_found(token); } return View(); } @@ -6345,7 +6309,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) { if (throwing) { - throw_out_of_range(402, concat("array index '-' (", std::to_string(cur.size()), ") is out of range")); + throw_array_index_past_end(cur.size()); } return View(); } @@ -6353,7 +6317,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) const index_status status = array_index(token, idx); if (status != index_status::ok) { - const bool parse_error = status == index_status::leading_zero || status == index_status::not_number; + const bool parse_error = status == index_status::leading_zero || status == index_status::not_a_number; if (throwing || (mode == pointer_mode::value && parse_error)) { throw_array_index_error(status, token); @@ -6364,7 +6328,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) { if (mode == pointer_mode::checked) { - throw_out_of_range(401, concat("array index ", std::to_string(idx), " is out of range")); + throw_array_index_out_of_range(idx); } return View(); } @@ -6374,7 +6338,7 @@ View resolve_pointer(View cur, const Tokens& tokens, pointer_mode mode) { if (throwing) { - throw_out_of_range(404, concat("unresolved reference token '", token, "'")); + throw_out_of_range(exception_id::pointer_unresolved, concat("unresolved reference token '", token, "'")); } return View(); } @@ -7467,21 +7431,17 @@ class view_serializer m_out.put("null"); return; } - static constexpr bool is_ieee_single_or_double - = std::numeric_limits::is_iec559 - && ((std::numeric_limits::digits == 24 && std::numeric_limits::max_exponent == 128) - || (std::numeric_limits::digits == 53 && std::numeric_limits::max_exponent == 1024)); - write_float(x, std::integral_constant()); + write_float(x, std::integral_constant::value>()); } - void write_float(number_float_t x, std::true_type /*is_ieee_single_or_double*/) + void write_float(number_float_t x, std::true_type /*has_native_float_format*/) { std::array buf{}; const char* const end = ::nlohmann::detail::to_chars(buf.data(), buf.data() + buf.size(), x); m_out.put(buf.data(), static_cast(end - buf.data())); } - void write_float(number_float_t x, std::false_type /*is_ieee_single_or_double*/) + void write_float(number_float_t x, std::false_type /*has_native_float_format*/) { // other types (e.g. long double) are rare: the library writes them const string_t s = BasicJsonType(x).dump(); @@ -7542,8 +7502,8 @@ class view_serializer void write_u_escape(std::uint32_t u) { - static constexpr const char* hex = "0123456789abcdef"; - const std::array e = {{'\\', 'u', hex[(u >> 12u) & 0xFu], hex[(u >> 8u) & 0xFu], hex[(u >> 4u) & 0xFu], hex[u & 0xFu]}}; + std::array e = {{'\\', 'u', 0, 0, 0, 0}}; + hex4_lower(static_cast(u), &e[2]); m_out.put(e.data(), e.size()); } @@ -7740,7 +7700,7 @@ NLOHMANN_VIEW_ALWAYS_INLINE T arithmetic_value(const document_data& d, const nod case value_t::binary: case value_t::discarded: default: - throw_type_error(302, "type must be number, but is ", value_type_name(static_cast(n.kind))); + throw_type_error(exception_id::type_mismatch, "type must be number, but is ", value_type_name(static_cast(n.kind))); } } @@ -7750,7 +7710,7 @@ std::vector vector_value(const View& v) { if (NLOHMANN_VIEW_UNLIKELY(!v.is_array())) { - throw_type_error(302, "type must be array, but is ", v.type_name()); + throw_type_error(exception_id::type_mismatch, "type must be array, but is ", v.type_name()); } std::vector r; r.reserve(v.size()); @@ -7768,7 +7728,7 @@ Map map_value(const View& v) { if (NLOHMANN_VIEW_UNLIKELY(!v.is_object())) { - throw_type_error(302, "type must be object, but is ", v.type_name()); + throw_type_error(exception_id::type_mismatch, "type must be object, but is ", v.type_name()); } Map r; for (auto it = v.begin(); it != v.end(); ++it) @@ -8007,7 +7967,7 @@ class basic_json_view { return {}; } - detail::view::throw_type_error(305, "cannot use operator[] with a string argument with ", type_name()); + detail::view::throw_type_error(detail::exception_id::subscript_wrong_type, "cannot use operator[] with a string argument with ", type_name()); } return lookup(key); } @@ -8033,7 +7993,7 @@ class basic_json_view { return {}; } - detail::view::throw_type_error(305, "cannot use operator[] with a numeric argument with ", type_name()); + detail::view::throw_type_error(detail::exception_id::subscript_wrong_type, "cannot use operator[] with a numeric argument with ", type_name()); } return idx < m_node->len ? element(idx) : basic_json_view(); } @@ -8066,12 +8026,12 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_object())) { - detail::view::throw_type_error(304, "cannot use at() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::at_wrong_type, "cannot use at() with ", type_name()); } const basic_json_view r = lookup(key); if (NLOHMANN_VIEW_UNLIKELY(r.is_discarded())) { - detail::view::throw_out_of_range(403, detail::concat("key '", std::string(key.data(), key.size()), "' not found")); + detail::view::throw_key_not_found(std::string(key.data(), key.size())); } return r; } @@ -8092,11 +8052,11 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_array())) { - detail::view::throw_type_error(304, "cannot use at() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::at_wrong_type, "cannot use at() with ", type_name()); } if (NLOHMANN_VIEW_UNLIKELY(idx >= m_node->len)) { - detail::view::throw_out_of_range(401, detail::concat("array index ", std::to_string(idx), " is out of range")); + detail::view::throw_array_index_out_of_range(idx); } return element(idx); } @@ -8124,7 +8084,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_object())) { - detail::view::throw_type_error(306, "cannot use value() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name()); } const basic_json_view r = lookup(key); return r.is_discarded() ? default_value : r.template get(); @@ -8134,7 +8094,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_object())) { - detail::view::throw_type_error(306, "cannot use value() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name()); } // (the default is made only if there is no such member) const basic_json_view r = lookup(key); @@ -8149,7 +8109,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_structured())) { - detail::view::throw_type_error(306, "cannot use value() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name()); } const basic_json_view r = detail::view::resolve_pointer(*this, detail::json_pointer_access::reference_tokens(ptr), detail::view::pointer_mode::value); return r.is_discarded() ? default_value : r.template get(); @@ -8159,7 +8119,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_structured())) { - detail::view::throw_type_error(306, "cannot use value() with ", type_name()); + detail::view::throw_type_error(detail::exception_id::value_wrong_type, "cannot use value() with ", type_name()); } const basic_json_view r = detail::view::resolve_pointer(*this, detail::json_pointer_access::reference_tokens(ptr), detail::view::pointer_mode::value); return r.is_discarded() ? string_t(default_value) : r.template get(); @@ -8172,7 +8132,7 @@ class basic_json_view const iterator it = begin(); if (NLOHMANN_VIEW_UNLIKELY(it == end())) { - detail::view::throw_invalid_iterator(214, "cannot get value"); + detail::view::throw_invalid_iterator(detail::exception_id::iterator_value_unavailable, "cannot get value"); } return *it; } @@ -8325,7 +8285,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_string())) { - detail::view::throw_type_error(302, "type must be string, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be string, but is ", type_name()); } return {m_doc->str(*m_node), m_node->len}; } @@ -8337,7 +8297,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_number())) { - detail::view::throw_type_error(302, "type must be number, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be number, but is ", type_name()); } return {m_doc->str(*m_node), detail::view::number_length(*m_node)}; } @@ -8594,7 +8554,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_boolean())) { - detail::view::throw_type_error(302, "type must be boolean, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be boolean, but is ", type_name()); } return (m_node->flags & detail::view::node_flags::is_true) != 0; } @@ -8604,7 +8564,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(m_node == nullptr)) { - detail::view::throw_type_error(302, "type must be number, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be number, but is ", type_name()); } return detail::view::arithmetic_value(*m_doc, *m_node); } @@ -8613,7 +8573,7 @@ class basic_json_view { if (NLOHMANN_VIEW_UNLIKELY(!is_null())) { - detail::view::throw_type_error(302, "type must be null, but is ", type_name()); + detail::view::throw_type_error(detail::exception_id::type_mismatch, "type must be null, but is ", type_name()); } return nullptr; } @@ -8922,7 +8882,7 @@ class basic_json_document { if (NLOHMANN_VIEW_UNLIKELY(!m_data || m_data->discarded)) { - detail::view::throw_type_error(320, "cannot save a discarded json_document"); + detail::view::throw_type_error(detail::exception_id::document_image_unsupported, "cannot save a discarded json_document"); } return detail::view::save_image(*m_data); } @@ -9023,7 +8983,7 @@ class basic_json_document const std::size_t idx = pointer_index(token); if (idx >= 0xFFFFFFFFu) { - detail::view::throw_out_of_range(401, detail::concat("array index ", std::to_string(idx), " is out of range")); + detail::view::throw_array_index_out_of_range(idx); } for (std::size_t i = 0; i < idx; ++i) { @@ -9079,7 +9039,7 @@ class basic_json_document { if (ptr.empty()) { - detail::view::throw_out_of_range(405, "JSON pointer has no parent"); + detail::view::throw_out_of_range(detail::exception_id::patch_on_root, "JSON pointer has no parent"); } const view_type parent = root().at(ptr.parent_pointer()); const auto& token = ptr.back(); @@ -9099,7 +9059,7 @@ class basic_json_document static_assert(Editable, "only an editable document can be edited: use basic_json_document (json_editable_document)"); if (NLOHMANN_VIEW_UNLIKELY(!m_data || m_data->discarded)) { - detail::view::throw_invalid_iterator(202, "view does not belong to this document"); + detail::view::throw_invalid_iterator(detail::exception_id::iterator_from_other_value, "view does not belong to this document"); } return detail::view::editor(*m_data); } @@ -9116,7 +9076,7 @@ class basic_json_document { if (idx < 0) { - detail::view::throw_out_of_range(401, detail::concat("array index ", std::to_string(idx), " is out of range")); + detail::view::throw_array_index_out_of_range(idx); } return static_cast(idx); } diff --git a/tests/src/unit-class_lexer.cpp b/tests/src/unit-class_lexer.cpp index fac88687f..2926ef88b 100644 --- a/tests/src/unit-class_lexer.cpp +++ b/tests/src/unit-class_lexer.cpp @@ -2042,3 +2042,118 @@ TEST_CASE("string scanning kernels") CHECK(nlohmann::detail::read_eight_bytes(bytes.data() + 8) == 0xFF0F0E0D0C0B0A09u); CHECK(nlohmann::detail::read_eight_bytes(reinterpret_cast(bytes.data()) + 3) == 0x0B0A090807060504u); } + +TEST_CASE("find_ascii_copyable_run agrees with is_ascii_copyable for every byte") +{ + // the SWAR mask in find_ascii_copyable_run() encodes the predicate a + // second time; check every byte value at every position of a 24-byte buffer + // (three words) and for several lengths, with the byte at the end or inside + for (unsigned int value = 0; value < 256; ++value) + { + for (std::size_t position = 0; position < 24; ++position) + { + std::array buffer{}; + buffer.fill(static_cast('a')); + buffer[position] = static_cast(value); + const bool copyable = nlohmann::detail::is_ascii_copyable(static_cast(value)); + for (const std::size_t n : + { + position, position + 1, std::size_t{24} + }) + { + if (n > 24) + { + continue; + } + const std::size_t expected = (copyable || n <= position) ? n : position; + CAPTURE(value) + CAPTURE(position) + CAPTURE(n) + CHECK(nlohmann::detail::find_ascii_copyable_run(buffer.data(), n) == expected); + } + } + } +} + +TEST_CASE("UTF-8 validators accept the same sequences") +{ + // The library checks UTF-8 in several places: detail::decode (serializer), + // the per-lead-byte switch of the lexer (json::accept), validate_one_utf8 + // (bulk string scan), utf8_shape, and the byte path of the BON8 reader. + // They must accept exactly the same sequences (RFC 3629, section 4). + using nlohmann::json; + + // bytes for the third and fourth position: the first two pairs are checked + // against all validators, the others only against the cheap ones + const std::array, 6> tails = + { + { + {{0x80, 0x80}}, {{0xBF, 0xBF}}, {{0x7F, 0x80}}, {{0xC0, 0x80}}, {{0x80, 0x7F}}, {{0x80, 0xC0}} + } + }; + + for (unsigned lead = 0x80; lead <= 0xFF; ++lead) + { + const auto shape = nlohmann::detail::utf8_shape(static_cast(lead)); + for (unsigned second = 0; second <= 0xFF; ++second) + { + for (std::size_t t = 0; t < tails.size(); ++t) + { + const std::array bytes = {{static_cast(lead), static_cast(second), tails[t][0], tails[t][1]}}; + + // expectation from the shape: the sequence is bytes[0..length) + const std::size_t length = shape.length; + bool expected = length >= 2 && second >= shape.second_lo && second <= shape.second_hi; + for (std::size_t i = 2; expected && i < length; ++i) + { + expected = bytes[i] >= 0x80 && bytes[i] <= 0xBF; + } + + // the DFA of the serializer + bool dfa = length >= 2; + if (dfa) + { + std::uint8_t state = nlohmann::detail::UTF8_ACCEPT; + std::uint32_t codep = 0; + for (std::size_t i = 0; i < length; ++i) + { + nlohmann::detail::decode(state, codep, bytes[i]); + } + dfa = state == nlohmann::detail::UTF8_ACCEPT; + } + + // the bulk path: exactly the sequence, and one byte short + const std::size_t bulk = nlohmann::detail::validate_one_utf8(bytes.data(), length); + const std::size_t bulk_short = length > 0 ? nlohmann::detail::validate_one_utf8(bytes.data(), length - 1) : 0; + + CAPTURE(lead) + CAPTURE(second) + CAPTURE(t) + CHECK((dfa == expected && bulk == (expected ? length : 0) && bulk_short == 0)); + + if (t < 2 && length >= 2) + { + // the lexer's byte path + const std::string sequence(reinterpret_cast(bytes.data()), length); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast) + CHECK(json::accept("\"" + sequence + "\"") == expected); + + // the byte path of the BON8 reader (a string ends at 0xFF) + if (lead >= 0xC2 && lead <= 0xF7) + { + std::vector packed(bytes.begin(), bytes.begin() + static_cast(length)); + packed.push_back(0xFF); + const json j = json::from_bon8(packed, true, false); + if (expected) + { + CHECK(j == json(sequence)); + } + else + { + CHECK_FALSE((j.is_string() && j.get() == sequence)); + } + } + } + } + } + } +} diff --git a/tests/src/unit-constructor1.cpp b/tests/src/unit-constructor1.cpp index 600eb0069..96a64e721 100644 --- a/tests/src/unit-constructor1.cpp +++ b/tests/src/unit-constructor1.cpp @@ -361,6 +361,40 @@ TEST_CASE("constructors") CHECK_THROWS_WITH_AS((j.get>()), "[json.exception.out_of_range.401] array index 1 is out of range", json::out_of_range&); CHECK_THROWS_WITH_AS((j.get>()), "[json.exception.out_of_range.401] array index 1 is out of range", json::out_of_range&); CHECK_THROWS_WITH_AS((j.get>()), "[json.exception.out_of_range.401] array index 1 is out of range", json::out_of_range&); +#if JSON_DIAGNOSTICS + // the exception names the element that has the wrong type + CHECK_THROWS_WITH_AS((j.get>()), "[json.exception.type_error.302] (/0) type must be string, but is number", json::type_error&); +#else + CHECK_THROWS_WITH_AS((j.get>()), "[json.exception.type_error.302] type must be string, but is number", json::type_error&); +#endif + + // the elements are converted in order, so a type error comes before a missing element + json const j2{1, "two"}; + std::array a{{0, 0, 0}}; +#if JSON_DIAGNOSTICS + CHECK_THROWS_WITH_AS((j2.get>()), "[json.exception.type_error.302] (/1) type must be number, but is string", json::type_error&); + CHECK_THROWS_WITH_AS(j2.get_to(a), "[json.exception.type_error.302] (/1) type must be number, but is string", json::type_error&); +#else + CHECK_THROWS_WITH_AS((j2.get>()), "[json.exception.type_error.302] type must be number, but is string", json::type_error&); + CHECK_THROWS_WITH_AS(j2.get_to(a), "[json.exception.type_error.302] type must be number, but is string", json::type_error&); +#endif + CHECK_THROWS_WITH_AS(j.get_to(a), "[json.exception.out_of_range.401] array index 1 is out of range", json::out_of_range&); +#if !defined(JSON_NOEXCEPTION) // without exceptions, the get_to calls in CHECK_THROWS_WITH_AS do not run + CHECK(a[0] == 1); +#endif + + json const j3{"one", "two"}; + CHECK_THROWS_WITH_AS((j3.get>()), "[json.exception.out_of_range.401] array index 2 is out of range", json::out_of_range&); + } + + SECTION("std::array from a longer array or from booleans") + { + json const j{1, 2.5, true, 4u}; + CHECK(j.get>() == (std::array {{1, 2, 1}})); + std::array a{{0, 0, 0, 0}}; + j.get_to(a); + CHECK(a == (std::array {{1, 2, 1, 4}})); + CHECK(j.get>().empty()); } SECTION("std::forward_list")