mirror of
https://github.com/nlohmann/json.git
synced 2026-11-17 09:01:28 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b0a4218e38 |
No files matched your search
+1
-1
@@ -66,7 +66,7 @@ labels:
|
||||
- "tests/src/fuzzer-(parse_json_view|json_view_image)\\.cpp"
|
||||
- "tests/benchmarks/json_view/.*"
|
||||
- "tools/amalgamate/config_json_view\\.json"
|
||||
- "docs/mkdocs/docs/features/json_view/.*"
|
||||
- "docs/mkdocs/docs/features/json_view\\.md"
|
||||
- "docs/mkdocs/docs/api/basic_json_(document|view)/.*"
|
||||
- "docs/mkdocs/docs/api/(ordered_)?json_(editable_)?(document|view)\\.md"
|
||||
- "docs/mkdocs/docs/examples/(basic_json_(document|view)__|(ordered_)?json_(editable_)?(document|view)).*"
|
||||
|
||||
@@ -115,7 +115,9 @@ jobs:
|
||||
|
||||
python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json.json -s .
|
||||
python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json_fwd.json -s .
|
||||
python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json_view.json -s .
|
||||
# the configuration of json_view.hpp comes with the pull request until
|
||||
# it is on develop; the tool itself is still develop's
|
||||
python3 $TOOL_DIR/amalgamate.py -c $MAIN_DIR/tools/amalgamate/config_json_view.json -s .
|
||||
|
||||
# the header list of the Bazel "json" target must match the files in include/
|
||||
cmake -P cmake/scripts/gen_bazel_build_file.cmake
|
||||
|
||||
+3
-2
@@ -20,7 +20,7 @@
|
||||
|
||||
# fuzzers
|
||||
/tests/corpus_*
|
||||
/tests/*_fuzzer
|
||||
/tests/parse_*_fuzzer
|
||||
|
||||
# documentation
|
||||
/docs/docset/JSON_for_Modern_C++.docset/
|
||||
@@ -47,10 +47,11 @@ nlohmann_json.spdx
|
||||
# Bazel-related
|
||||
MODULE.bazel.lock
|
||||
|
||||
# backups left by `make check-amalgamation` if it is interrupted
|
||||
# backups left by `make check-amalgamation` or the ci_test_amalgamation target if they are interrupted
|
||||
/BUILD.bazel~
|
||||
/nlohmann_json.natvis~
|
||||
/single_include/nlohmann/*.hpp~
|
||||
/venv_astyle/
|
||||
|
||||
# release outputs of `make release`
|
||||
/include.zip
|
||||
|
||||
@@ -28,7 +28,6 @@ The [GitHub Actions](https://docs.github.com/en/actions) workflows that build, t
|
||||
- `publish_documentation.yml` — build and publish the documentation on pushes to the `develop` branch that touch the documentation sources (or the files it embeds).
|
||||
- `check_docs_links.yml` — check the links of the documentation weekly, as external links can break without any change in this repository.
|
||||
- `cancel_closed_pr_runs.yml` — cancel unfinished workflow runs of a pull request once it is closed or merged.
|
||||
- `json_view_benchmarks.yml` — compare the performance of `json_view` with other JSON libraries on GitHub-hosted x86-64 and AArch64 Linux runners, on demand or when a pull request gets the label "benchmark".
|
||||
|
||||
Further documentation:
|
||||
|
||||
|
||||
@@ -215,7 +215,7 @@ release: include.zip json.tar.xz
|
||||
|
||||
# clean up
|
||||
clean:
|
||||
rm -fr *.dSYM tests/*.dSYM tests/*_fuzzer
|
||||
rm -fr *.dSYM tests/*.dSYM tests/parse_*_fuzzer
|
||||
rm -fr cmake-build-benchmarks cmake-build-pvs-studio release_files
|
||||
$(MAKE) clean -Cdocs
|
||||
|
||||
|
||||
@@ -406,6 +406,61 @@ add_custom_target(ci_test_clang_sanitizer
|
||||
COMMENT "Compile and test with sanitizers"
|
||||
)
|
||||
|
||||
###############################################################################
|
||||
# Check if header is amalgamated and sources are properly indented.
|
||||
###############################################################################
|
||||
|
||||
# Same file set as .github/workflows/check_amalgamation.yml, so this CMake target (which can be run locally) is held
|
||||
# to the same standard as the workflow (which runs for pull requests and for pushes to develop/master/release/*).
|
||||
file(GLOB_RECURSE INDENT_FILES
|
||||
${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.hpp
|
||||
${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.cpp
|
||||
${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.cu
|
||||
${PROJECT_SOURCE_DIR}/include/*.hpp
|
||||
${PROJECT_SOURCE_DIR}/include/*.cpp
|
||||
${PROJECT_SOURCE_DIR}/include/*.cu
|
||||
${PROJECT_SOURCE_DIR}/tests/*.hpp
|
||||
${PROJECT_SOURCE_DIR}/tests/*.cpp
|
||||
${PROJECT_SOURCE_DIR}/tests/*.cu
|
||||
)
|
||||
list(FILTER INDENT_FILES EXCLUDE REGEX "/tests/thirdparty/|/tests/abi/include/nlohmann/")
|
||||
|
||||
set(include_dir ${PROJECT_SOURCE_DIR}/single_include/nlohmann)
|
||||
set(tool_dir ${PROJECT_SOURCE_DIR}/tools/amalgamate)
|
||||
add_custom_target(ci_test_amalgamation
|
||||
COMMAND rm -fr ${include_dir}/json.hpp~ ${include_dir}/json_fwd.hpp~ ${include_dir}/json_literals.hpp~ ${include_dir}/json_view.hpp~
|
||||
COMMAND cp ${include_dir}/json.hpp ${include_dir}/json.hpp~
|
||||
COMMAND cp ${include_dir}/json_fwd.hpp ${include_dir}/json_fwd.hpp~
|
||||
COMMAND cp ${include_dir}/json_literals.hpp ${include_dir}/json_literals.hpp~
|
||||
COMMAND cp ${include_dir}/json_view.hpp ${include_dir}/json_view.hpp~
|
||||
COMMAND cp ${PROJECT_SOURCE_DIR}/BUILD.bazel ${PROJECT_SOURCE_DIR}/BUILD.bazel~
|
||||
|
||||
COMMAND ${Python3_EXECUTABLE} -mvenv venv_astyle
|
||||
COMMAND venv_astyle/bin/pip3 --quiet install -r ${CMAKE_SOURCE_DIR}/tools/astyle/requirements.txt
|
||||
COMMAND venv_astyle/bin/astyle --version
|
||||
|
||||
COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json.json -s .
|
||||
COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json_fwd.json -s .
|
||||
COMMAND cp ${PROJECT_SOURCE_DIR}/include/nlohmann/json_literals.hpp ${include_dir}/json_literals.hpp
|
||||
COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json_view.json -s .
|
||||
COMMAND venv_astyle/bin/astyle --project=tools/astyle/.astylerc --suffix=none ${include_dir}/json.hpp ${include_dir}/json_fwd.hpp ${include_dir}/json_literals.hpp ${include_dir}/json_view.hpp
|
||||
COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/scripts/gen_bazel_build_file.cmake
|
||||
|
||||
COMMAND diff ${include_dir}/json.hpp~ ${include_dir}/json.hpp
|
||||
COMMAND diff ${include_dir}/json_fwd.hpp~ ${include_dir}/json_fwd.hpp
|
||||
COMMAND diff ${include_dir}/json_literals.hpp~ ${include_dir}/json_literals.hpp
|
||||
COMMAND diff ${include_dir}/json_view.hpp~ ${include_dir}/json_view.hpp
|
||||
COMMAND diff ${PROJECT_SOURCE_DIR}/BUILD.bazel~ ${PROJECT_SOURCE_DIR}/BUILD.bazel
|
||||
|
||||
COMMAND venv_astyle/bin/astyle --project=tools/astyle/.astylerc --suffix=orig ${INDENT_FILES}
|
||||
COMMAND for FILE in `find . -name '*.orig'`\; do false \; done
|
||||
|
||||
COMMAND rm -f ${include_dir}/json.hpp~ ${include_dir}/json_fwd.hpp~ ${include_dir}/json_literals.hpp~ ${include_dir}/json_view.hpp~ ${PROJECT_SOURCE_DIR}/BUILD.bazel~
|
||||
|
||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
||||
COMMENT "Check amalgamation, formatting, and BUILD.bazel"
|
||||
)
|
||||
|
||||
###############################################################################
|
||||
# Build and test using the amalgamated header
|
||||
###############################################################################
|
||||
@@ -637,6 +692,19 @@ else()
|
||||
)
|
||||
endif()
|
||||
|
||||
###############################################################################
|
||||
# Benchmarks
|
||||
###############################################################################
|
||||
|
||||
add_custom_target(ci_benchmarks
|
||||
COMMAND ${CMAKE_COMMAND}
|
||||
-DCMAKE_BUILD_TYPE=Release -GNinja
|
||||
-S${PROJECT_SOURCE_DIR}/tests/benchmarks -B${PROJECT_BINARY_DIR}/build_benchmarks
|
||||
COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_benchmarks --target json_benchmarks
|
||||
COMMAND cd ${PROJECT_BINARY_DIR}/build_benchmarks && ./json_benchmarks
|
||||
COMMENT "Run benchmarks"
|
||||
)
|
||||
|
||||
###############################################################################
|
||||
# CMake flags
|
||||
###############################################################################
|
||||
@@ -897,3 +965,12 @@ add_custom_target(ci_test_documentation_mermaid
|
||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/docs/mkdocs
|
||||
COMMENT "Check the Mermaid diagrams of the documentation"
|
||||
)
|
||||
|
||||
###############################################################################
|
||||
# Clean up all generated files.
|
||||
###############################################################################
|
||||
|
||||
add_custom_target(ci_clean
|
||||
COMMAND rm -fr ${PROJECT_BINARY_DIR}/build_* ${PROJECT_BINARY_DIR}/cmake-3.5.0 ${PROJECT_BINARY_DIR}/cmake-3.31.6 ${PROJECT_BINARY_DIR}/cmake-4.0.0 ${JSON_CMAKE_FLAG_BUILD_DIRS} ${single_binaries}
|
||||
COMMENT "Clean generated directories"
|
||||
)
|
||||
@@ -2,21 +2,12 @@
|
||||
|
||||
```cpp
|
||||
namespace std {
|
||||
void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2) noexcept(
|
||||
is_nothrow_move_constructible<nlohmann::basic_json>::value &&
|
||||
is_nothrow_move_assignable<nlohmann::basic_json>::value
|
||||
);
|
||||
void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2);
|
||||
}
|
||||
```
|
||||
|
||||
Exchanges the values of two JSON objects.
|
||||
|
||||
This specialization is only provided before C++20, which no longer allows specializing function templates in
|
||||
namespace `std`. With C++20 (and in generic code with any standard), use the idiom `#!cpp using std::swap; swap(j1, j2);`: it finds
|
||||
the [`swap`](swap.md) friend function (overload 2) by argument-dependent lookup. A qualified `#!cpp std::swap(j1, j2)`
|
||||
still compiles with C++20, but uses the generic `std::swap`, which swaps by three moves instead of exchanging the
|
||||
contents.
|
||||
|
||||
## Parameters
|
||||
|
||||
`j1` (in, out)
|
||||
@@ -27,9 +18,7 @@ contents.
|
||||
|
||||
## Exception safety
|
||||
|
||||
No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the
|
||||
[`json_base_class_t`](json_base_class_t.md) can throw. With the default base class, this function never throws
|
||||
exceptions.
|
||||
No-throw guarantee: this function never throws exceptions.
|
||||
|
||||
## Complexity
|
||||
|
||||
@@ -68,4 +57,3 @@ void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2)
|
||||
|
||||
- Added in version 1.0.0.
|
||||
- Extended for arbitrary basic_json types in version 3.10.5.
|
||||
- Not provided with C++20 since version 3.9.0.
|
||||
@@ -75,12 +75,8 @@ void swap(typename binary_t::container_type& other);
|
||||
|
||||
## Exception safety
|
||||
|
||||
1. No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the
|
||||
[`json_base_class_t`](json_base_class_t.md) can throw (moving the type tag and the stored value never throws).
|
||||
With the default base class, this function never throws exceptions.
|
||||
2. No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the
|
||||
[`json_base_class_t`](json_base_class_t.md) can throw (moving the type tag and the stored value never throws).
|
||||
With the default base class, this function never throws exceptions.
|
||||
1. No-throw guarantee: this function never throws exceptions.
|
||||
2. No-throw guarantee: this function never throws exceptions.
|
||||
3. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||
4. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||
5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||
@@ -89,8 +85,8 @@ void swap(typename binary_t::container_type& other);
|
||||
|
||||
## Exceptions
|
||||
|
||||
1. Throws only what moving the [`json_base_class_t`](json_base_class_t.md) throws; nothing with the default base class.
|
||||
2. Throws only what moving the [`json_base_class_t`](json_base_class_t.md) throws; nothing with the default base class.
|
||||
1. No-throw guarantee: this function never throws exceptions.
|
||||
2. No-throw guarantee: this function never throws exceptions.
|
||||
3. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
|
||||
arrays; example: `"cannot use swap(array_t&) with boolean"`
|
||||
4. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
|
||||
|
||||
@@ -57,8 +57,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
||||
## Exceptions
|
||||
|
||||
- Throws [`other_error.502`](../../home/exceptions.md#jsonexceptionother_error502) if `use_type` is true and `use_size`
|
||||
is false, and `j` contains a non-empty array, object, or binary value. Scalars and empty arrays, objects, or binary
|
||||
values do not throw.
|
||||
is false, and `j` contains a non-empty array, object, or binary value.
|
||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||
not valid UTF-8 and `error_handler` is `strict` (the default only if
|
||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled)
|
||||
|
||||
@@ -66,8 +66,7 @@ has `erase`; calling it on a read-only `basic_json_document` fails to compile (`
|
||||
[`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) (a leading `#!cpp '0'`),
|
||||
[`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) (not a number),
|
||||
[`out_of_range.410`](../../home/exceptions.md#jsonexceptionout_of_range410) (too large for `size_type`), or
|
||||
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) (an empty token, or the token
|
||||
`#!cpp "-"`, which never names an existing element); otherwise (an
|
||||
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) (an empty token); otherwise (an
|
||||
object, or a primitive value the pointer's parent resolves to) throws what 1. throws.
|
||||
|
||||
Overloads 1. and 2. also throw [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202)
|
||||
|
||||
@@ -62,9 +62,10 @@ view.
|
||||
|
||||
!!! info "Overload 2 never throws"
|
||||
|
||||
Like [`BasicJsonType::contains(const json_pointer&)`](../basic_json/contains.md), overload 2 never throws: a
|
||||
missing key, an out-of-range or malformed array index, a `#!cpp "-"` index, or a reference token used on a
|
||||
primitive all simply make it return `#!cpp false`.
|
||||
Unlike [`BasicJsonType::contains(const json_pointer&)`](../basic_json/contains.md), which can throw for certain
|
||||
malformed pointers (for instance an empty array-index reference token), overload 2 never throws: a missing key,
|
||||
an out-of-range or malformed array index, a `#!cpp "-"` index, or a reference token used on a primitive all
|
||||
simply make it return `#!cpp false`.
|
||||
|
||||
## Examples
|
||||
|
||||
|
||||
@@ -27,10 +27,9 @@ for (const auto [key, value] : v.items())
|
||||
}
|
||||
```
|
||||
|
||||
Taking `item` by value (`#!cpp const auto`) is recommended: unlike `BasicJsonType::items()`, whose elements are
|
||||
references into an existing object, a view's `item` is produced on the fly for each step of the iteration and is
|
||||
cheap to copy. A `#!cpp const auto&` (as in the first snippet) also works, as it binds to that temporary, but a
|
||||
non-const `#!cpp auto&` does not compile.
|
||||
Note the `#!cpp const auto` (by value), not `#!cpp const auto&`: unlike `BasicJsonType::items()`, whose elements are
|
||||
references into an existing object, a view's `item` is produced on the fly for each step of the iteration, so there
|
||||
is nothing for a reference to bind to.
|
||||
|
||||
## Return value
|
||||
|
||||
|
||||
@@ -7,10 +7,6 @@ bool operator==(const basic_json_view& lhs, const basic_json_view& rhs);
|
||||
// (2)
|
||||
bool operator==(const basic_json_view& lhs, const BasicJsonType& rhs);
|
||||
bool operator==(const BasicJsonType& lhs, const basic_json_view& rhs);
|
||||
|
||||
// (3)
|
||||
template<view_mode M>
|
||||
bool operator==(const basic_json_view& lhs, const basic_json_view<BasicJsonType, M>& rhs);
|
||||
```
|
||||
|
||||
1. Compares two views for equality: whether the values [`BasicJsonType::parse()`](../basic_json/parse.md) would
|
||||
@@ -18,12 +14,8 @@ bool operator==(const basic_json_view& lhs, const basic_json_view<BasicJsonType,
|
||||
2. Compares a view and a `BasicJsonType` value for equality, in either order: whether the value `parse()` would
|
||||
produce for the view and the other operand are equal, according to `BasicJsonType`'s
|
||||
[`operator==`](../basic_json/operator_eq.md).
|
||||
3. Like overload 1, for two views of the same `BasicJsonType` but of different [view modes](../view_mode.md), in
|
||||
either order: a view of a read-only document (such as a [`json_view`](../json_view.md)) and a view of an editable
|
||||
document (such as a [`json_editable_view`](../json_editable_view.md)). This overload only takes part in overload
|
||||
resolution if `M` is not the mode of `lhs`.
|
||||
|
||||
No overload builds a `BasicJsonType` value for a view to do the comparison (see [Notes](#notes) below). Numbers
|
||||
Neither overload builds a `BasicJsonType` value for a view to do the comparison (see [Notes](#notes) below). Numbers
|
||||
compare by value across their types (`#!cpp 1 == 1.0`), and an object compares by its members, with duplicate keys
|
||||
resolved exactly as `parse()` resolves them — the last value, at the position of the first occurrence of the key.
|
||||
This is not what [`operator[]`](operator[].md) and the other lookups return for a duplicate key: they find the *first*
|
||||
|
||||
@@ -7,17 +7,11 @@ bool operator!=(const basic_json_view& lhs, const basic_json_view& rhs);
|
||||
// (2)
|
||||
bool operator!=(const basic_json_view& lhs, const BasicJsonType& rhs);
|
||||
bool operator!=(const BasicJsonType& lhs, const basic_json_view& rhs);
|
||||
|
||||
// (3)
|
||||
template<view_mode M>
|
||||
bool operator!=(const basic_json_view& lhs, const basic_json_view<BasicJsonType, M>& rhs);
|
||||
```
|
||||
|
||||
1. Compares two views for inequality. Returns `#!cpp !(lhs == rhs)`, see [operator==](operator_eq.md).
|
||||
2. Compares a view and a `BasicJsonType` value for inequality, in either order. Returns `#!cpp !(lhs == rhs)` (or,
|
||||
for the reversed order, `#!cpp !(rhs == lhs)`), see [operator==](operator_eq.md).
|
||||
3. Compares a read-only and an editable view of the same `BasicJsonType` for inequality, in either order. Returns
|
||||
`#!cpp !(lhs == rhs)`, see [operator==](operator_eq.md).
|
||||
|
||||
Since `operator!=` is defined as the negation of [`operator==`](operator_eq.md), it follows the same rules for
|
||||
special cases: for instance, since a [discarded](is_discarded.md) view is never equal to anything by default (see
|
||||
|
||||
@@ -15,10 +15,6 @@ materializing anything.
|
||||
- `#!cpp static_cast<std::size_t>(-1)` for a [discarded](is_discarded.md) view, and for a string that contains
|
||||
escapes — such a string was decoded once into the document's own buffer, so there is no single byte range in
|
||||
`source()` left to point at.
|
||||
- `#!cpp static_cast<std::size_t>(-1)` in an [editable document](../json_editable_document.md) for a value written
|
||||
by an edit ([`set`](../basic_json_document/set.md), [`push_back`](../basic_json_document/push_back.md),
|
||||
[`insert`](../basic_json_document/insert.md)), and for an array or object whose elements an edit added or removed.
|
||||
The values an edit did not touch keep their offsets.
|
||||
|
||||
## Exception safety
|
||||
|
||||
|
||||
@@ -8,9 +8,8 @@ using json_editable_document = basic_json_document<json, view_mode::editable>;
|
||||
|
||||
This type is an **editable** [`basic_json_document`](basic_json_document/index.md) of the default
|
||||
[`json`](json.md) specialization: in addition to everything [`json_document`](json_document.md) offers,
|
||||
[`set`](basic_json_document/set.md), [`push_back`](basic_json_document/push_back.md),
|
||||
[`insert`](basic_json_document/insert.md), and [`erase`](basic_json_document/erase.md) change values after parsing,
|
||||
without ever rewriting the source text — see [Edits](basic_json_document/index.md#edits) and
|
||||
[`set`](basic_json_document/set.md) and [`push_back`](basic_json_document/push_back.md) change values after
|
||||
parsing, without ever rewriting the source text — see [Edits](basic_json_document/index.md#edits) and
|
||||
[Editing a document](../features/json_view/editing.md).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -9,8 +9,7 @@ using json_editable_view = basic_json_view<json, view_mode::editable>;
|
||||
This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a
|
||||
[`json_editable_document`](json_editable_document.md). It offers the same read-only interface as
|
||||
[`json_view`](json_view.md); what is different is what it can be a view *of* — a value that
|
||||
[`set`](basic_json_document/set.md), [`push_back`](basic_json_document/push_back.md),
|
||||
[`insert`](basic_json_document/insert.md), and [`erase`](basic_json_document/erase.md) can change, with every view
|
||||
[`set`](basic_json_document/set.md) and [`push_back`](basic_json_document/push_back.md) can change, with every view
|
||||
still referring to it seeing the change, see [Edits](basic_json_document/index.md#edits).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -171,7 +171,7 @@ See the examples below for the concrete generated code.
|
||||
|
||||
The macro is equivalent to:
|
||||
|
||||
```cpp hl_lines="22 23 24 25 26 27 28"
|
||||
```cpp hl_lines="22 22 23 24 25 26 27 28"
|
||||
--8<-- "examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp"
|
||||
```
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ For further information please refer to the corresponding macros without `WITH_N
|
||||
|
||||
The macro is equivalent to:
|
||||
|
||||
```cpp hl_lines="16 17 18 19 20 21 22 23 24 25 26 27 28 29 30"
|
||||
```cpp hl_lines="16 17 18 19 20 21 22 23 24 25 26 27 28"
|
||||
--8<-- "examples/nlohmann_define_type_non_intrusive_with_names_explicit.cpp"
|
||||
```
|
||||
|
||||
|
||||
@@ -98,7 +98,7 @@ serialization fails by default. The fourth argument of `dump` selects an
|
||||
|
||||
## Using `std::format`, `std::print`, and `fmt`
|
||||
|
||||
Since version 3.13.0, JSON values can be formatted directly with C++20's
|
||||
Since version 3.12.0, JSON values can be formatted directly with C++20's
|
||||
[`std::format`](https://en.cppreference.com/w/cpp/utility/format/format) whenever the standard library provides the
|
||||
`<format>` header (controlled by [`JSON_HAS_STD_FORMAT`](../api/macros/json_has_std_format.md)). This is enabled by the
|
||||
[`std::formatter<basic_json>`](../api/basic_json/std_formatter.md) specialization, which also makes JSON values work with
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# Recipes for Common Types
|
||||
|
||||
The library converts the standard containers, strings, numbers, tuples, `std::optional` (since version 3.12.0), and
|
||||
`std::filesystem::path` (since version 3.10.5), the last two with C++17, out of the box, and your own types once you write conversion functions or use
|
||||
The library converts the standard containers, strings, numbers, tuples, `std::optional`, and `std::filesystem::path`
|
||||
(the last two since version 3.12.0, C++17) out of the box, and your own types once you write conversion functions or use
|
||||
the macros (see [Arbitrary type conversions](arbitrary_types.md)). It deliberately does **not** convert smart pointers,
|
||||
`std::variant`, `std::chrono` types, or class hierarchies, because there is more than one reasonable JSON
|
||||
representation for each of them. This page shows a representation for each that you can copy and adapt.
|
||||
|
||||
@@ -987,9 +987,7 @@ contains other characters (e.g., `1a`), or `-` where it cannot be used.
|
||||
|
||||
The JSON Patch operations 'remove' and 'move' cannot be applied to the root element of the JSON value. The exception is
|
||||
also thrown when the parent or a reference token of an empty JSON Pointer is requested, for instance by
|
||||
[`front`](../api/json_pointer/front.md), [`back`](../api/json_pointer/back.md),
|
||||
[`pop_front`](../api/json_pointer/pop_front.md), or [`pop_back`](../api/json_pointer/pop_back.md), and when
|
||||
[`basic_json_document::erase`](../api/basic_json_document/erase.md) is called with an empty JSON Pointer.
|
||||
[`back`](../api/json_pointer/back.md) or [`pop_back`](../api/json_pointer/pop_back.md).
|
||||
|
||||
!!! failure "Example message"
|
||||
|
||||
@@ -1265,9 +1263,9 @@ A JSON Patch operation 'test' failed. The unsuccessful operation is also printed
|
||||
### json.exception.other_error.502
|
||||
|
||||
[`to_ubjson`](../api/basic_json/to_ubjson.md) and [`to_bjdata`](../api/basic_json/to_bjdata.md) were called with
|
||||
`use_type = true` but `use_size = false`, and the value contains a non-empty array, object, or binary value (with
|
||||
BJData Draft 3, any binary value, including an empty one). UBJSON requires a size marker (`#`) after a type marker
|
||||
(`$`); scalars and empty arrays or objects do not throw.
|
||||
`use_type = true` but `use_size = false`, and the value contains a non-empty array or object (or, with BJData Draft 3,
|
||||
any binary value). UBJSON requires a size marker (`#`) after a type marker (`$`); scalars and empty arrays or objects
|
||||
do not throw.
|
||||
|
||||
!!! failure "Example message"
|
||||
|
||||
|
||||
@@ -1927,6 +1927,21 @@ class binary_reader
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief move past a value of @a size bytes, starting with @a head, that a
|
||||
bulk run reads, as get() and get_number() would
|
||||
|
||||
Called before the value is passed on, so that an error reported while
|
||||
passing it on (see @ref emit_float) has the position and the last byte
|
||||
that reading the value byte by byte gives.
|
||||
*/
|
||||
void bulk_advance(const std::size_t size, const unsigned char head)
|
||||
{
|
||||
ia.bulk_skip(size);
|
||||
chars_read += size;
|
||||
current = head;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief read the integers, floats, booleans, and nulls that follow in a
|
||||
definite-length array, without the per-element bookkeeping of
|
||||
@@ -1952,71 +1967,70 @@ class binary_reader
|
||||
{
|
||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||
const unsigned char head = data[0];
|
||||
std::size_t size = 0;
|
||||
bool accepted = false;
|
||||
switch (head)
|
||||
{
|
||||
case 0x18:
|
||||
size = 2;
|
||||
bulk_advance(2, head);
|
||||
accepted = emit_unsigned(data[1]);
|
||||
break;
|
||||
case 0x19:
|
||||
size = 3;
|
||||
bulk_advance(3, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||
break;
|
||||
case 0x1A:
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||
break;
|
||||
case 0x1B:
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||
break;
|
||||
case 0x38:
|
||||
size = 2;
|
||||
bulk_advance(2, head);
|
||||
accepted = emit_cbor_negative_integer(data[1]);
|
||||
break;
|
||||
case 0x39:
|
||||
size = 3;
|
||||
bulk_advance(3, head);
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
||||
break;
|
||||
case 0x3A:
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
||||
break;
|
||||
case 0x3B:
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
||||
break;
|
||||
case 0xF4:
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->boolean(false);
|
||||
break;
|
||||
case 0xF5:
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->boolean(true);
|
||||
break;
|
||||
case 0xF6:
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->null();
|
||||
break;
|
||||
case 0xFA:
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||
break;
|
||||
case 0xFB:
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||
break;
|
||||
default:
|
||||
if (head <= 0x17)
|
||||
{
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||
}
|
||||
else if (head >= 0x20 && head <= 0x37)
|
||||
{
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
||||
}
|
||||
else
|
||||
@@ -2026,10 +2040,6 @@ class binary_reader
|
||||
}
|
||||
break;
|
||||
}
|
||||
// as if read by get() and get_number(): current is the head byte
|
||||
ia.bulk_skip(size);
|
||||
chars_read += size;
|
||||
current = head;
|
||||
--remaining;
|
||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||
{
|
||||
@@ -2867,73 +2877,72 @@ class binary_reader
|
||||
{
|
||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||
const unsigned char head = data[0];
|
||||
std::size_t size = 0;
|
||||
bool accepted = false;
|
||||
switch (head)
|
||||
{
|
||||
case 0xC0: // nil
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->null();
|
||||
break;
|
||||
case 0xC2: // false
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->boolean(false);
|
||||
break;
|
||||
case 0xC3: // true
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->boolean(true);
|
||||
break;
|
||||
case 0xCA: // float 32
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||
break;
|
||||
case 0xCB: // float 64
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||
break;
|
||||
case 0xCC: // uint 8
|
||||
size = 2;
|
||||
bulk_advance(2, head);
|
||||
accepted = emit_unsigned(data[1]);
|
||||
break;
|
||||
case 0xCD: // uint 16
|
||||
size = 3;
|
||||
bulk_advance(3, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||
break;
|
||||
case 0xCE: // uint 32
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||
break;
|
||||
case 0xCF: // uint 64
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||
break;
|
||||
case 0xD0: // int 8
|
||||
size = 2;
|
||||
bulk_advance(2, head);
|
||||
accepted = emit_signed(static_cast<std::int8_t>(data[1]));
|
||||
break;
|
||||
case 0xD1: // int 16
|
||||
size = 3;
|
||||
bulk_advance(3, head);
|
||||
accepted = emit_signed(load_big_endian<std::int16_t>(data + 1));
|
||||
break;
|
||||
case 0xD2: // int 32
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_signed(load_big_endian<std::int32_t>(data + 1));
|
||||
break;
|
||||
case 0xD3: // int 64
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_signed(load_big_endian<std::int64_t>(data + 1));
|
||||
break;
|
||||
default:
|
||||
if (head <= 0x7F)
|
||||
{
|
||||
// positive fixint
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||
}
|
||||
else if (head >= 0xE0)
|
||||
{
|
||||
// negative fixint
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->number_integer(static_cast<std::int8_t>(head));
|
||||
}
|
||||
else
|
||||
@@ -2943,10 +2952,6 @@ class binary_reader
|
||||
}
|
||||
break;
|
||||
}
|
||||
// as if read by get() and get_number(): current is the last byte
|
||||
ia.bulk_skip(size);
|
||||
chars_read += size;
|
||||
current = data[size - 1];
|
||||
--remaining;
|
||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||
{
|
||||
@@ -3850,6 +3855,7 @@ class binary_reader
|
||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||
// UBJSON stores numbers big-endian, BJData little-endian
|
||||
const bool swap = is_little_endian != (input_format == input_format_t::bjdata);
|
||||
const std::size_t start = chars_read;
|
||||
std::size_t read = 0;
|
||||
bool accepted = true;
|
||||
while (read < count && accepted)
|
||||
@@ -3861,16 +3867,39 @@ class binary_reader
|
||||
byte_swap(number);
|
||||
}
|
||||
++read;
|
||||
// where passing the value on can report an error (see
|
||||
// emit_float), the position is that after the value, as if it
|
||||
// was read by get_number()
|
||||
set_chars_read(start + (read * size), std::integral_constant<bool, may_overflow_number_float<NumberType>()> {});
|
||||
accepted = emit_ubjson_number(number);
|
||||
}
|
||||
// as if read by get_number(): current is the last byte read
|
||||
// get_number() does not change current
|
||||
ia.bulk_skip(read * size);
|
||||
chars_read += read * size;
|
||||
current = data[(read * size) - 1];
|
||||
chars_read = start + (read * size);
|
||||
remaining -= read;
|
||||
return accepted;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief whether a value of type @a NumberType can be finite and still
|
||||
overflow number_float_t, which @ref emit_float reports as an error
|
||||
|
||||
False for the default number types, so that the loop in @ref
|
||||
parse_ubjson_typed_run need not track the position per value.
|
||||
*/
|
||||
template<typename NumberType>
|
||||
static constexpr bool may_overflow_number_float() noexcept
|
||||
{
|
||||
return static_cast<long double>((std::numeric_limits<number_float_t>::max)()) < static_cast<long double>((std::numeric_limits<NumberType>::max)());
|
||||
}
|
||||
|
||||
void set_chars_read(const std::size_t position, std::true_type /*track*/) noexcept
|
||||
{
|
||||
chars_read = position;
|
||||
}
|
||||
|
||||
void set_chars_read(const std::size_t /*position*/, std::false_type /*track*/) const noexcept {}
|
||||
|
||||
template<typename NumberType, enable_if_t<std::is_floating_point<NumberType>::value, int> = 0>
|
||||
bool emit_ubjson_number(const NumberType number)
|
||||
{
|
||||
|
||||
+1
-1
@@ -50,7 +50,6 @@
|
||||
#undef JSON_HEDLEY_EMSCRIPTEN_VERSION
|
||||
#undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
|
||||
#undef JSON_HEDLEY_END_C_DECLS
|
||||
#undef JSON_HEDLEY_FALL_THROUGH
|
||||
#undef JSON_HEDLEY_FLAGS
|
||||
#undef JSON_HEDLEY_FLAGS_CAST
|
||||
#undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
|
||||
@@ -159,3 +158,4 @@
|
||||
#undef JSON_HEDLEY_WARNING
|
||||
#undef JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||
#undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
|
||||
#undef JSON_HEDLEY_FALL_THROUGH
|
||||
Generated
+73
-44
@@ -20605,6 +20605,21 @@ class binary_reader
|
||||
return result;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief move past a value of @a size bytes, starting with @a head, that a
|
||||
bulk run reads, as get() and get_number() would
|
||||
|
||||
Called before the value is passed on, so that an error reported while
|
||||
passing it on (see @ref emit_float) has the position and the last byte
|
||||
that reading the value byte by byte gives.
|
||||
*/
|
||||
void bulk_advance(const std::size_t size, const unsigned char head)
|
||||
{
|
||||
ia.bulk_skip(size);
|
||||
chars_read += size;
|
||||
current = head;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief read the integers, floats, booleans, and nulls that follow in a
|
||||
definite-length array, without the per-element bookkeeping of
|
||||
@@ -20630,71 +20645,70 @@ class binary_reader
|
||||
{
|
||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||
const unsigned char head = data[0];
|
||||
std::size_t size = 0;
|
||||
bool accepted = false;
|
||||
switch (head)
|
||||
{
|
||||
case 0x18:
|
||||
size = 2;
|
||||
bulk_advance(2, head);
|
||||
accepted = emit_unsigned(data[1]);
|
||||
break;
|
||||
case 0x19:
|
||||
size = 3;
|
||||
bulk_advance(3, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||
break;
|
||||
case 0x1A:
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||
break;
|
||||
case 0x1B:
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||
break;
|
||||
case 0x38:
|
||||
size = 2;
|
||||
bulk_advance(2, head);
|
||||
accepted = emit_cbor_negative_integer(data[1]);
|
||||
break;
|
||||
case 0x39:
|
||||
size = 3;
|
||||
bulk_advance(3, head);
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
||||
break;
|
||||
case 0x3A:
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
||||
break;
|
||||
case 0x3B:
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
||||
break;
|
||||
case 0xF4:
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->boolean(false);
|
||||
break;
|
||||
case 0xF5:
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->boolean(true);
|
||||
break;
|
||||
case 0xF6:
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->null();
|
||||
break;
|
||||
case 0xFA:
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||
break;
|
||||
case 0xFB:
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||
break;
|
||||
default:
|
||||
if (head <= 0x17)
|
||||
{
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||
}
|
||||
else if (head >= 0x20 && head <= 0x37)
|
||||
{
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
||||
}
|
||||
else
|
||||
@@ -20704,10 +20718,6 @@ class binary_reader
|
||||
}
|
||||
break;
|
||||
}
|
||||
// as if read by get() and get_number(): current is the head byte
|
||||
ia.bulk_skip(size);
|
||||
chars_read += size;
|
||||
current = head;
|
||||
--remaining;
|
||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||
{
|
||||
@@ -21545,73 +21555,72 @@ class binary_reader
|
||||
{
|
||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||
const unsigned char head = data[0];
|
||||
std::size_t size = 0;
|
||||
bool accepted = false;
|
||||
switch (head)
|
||||
{
|
||||
case 0xC0: // nil
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->null();
|
||||
break;
|
||||
case 0xC2: // false
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->boolean(false);
|
||||
break;
|
||||
case 0xC3: // true
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->boolean(true);
|
||||
break;
|
||||
case 0xCA: // float 32
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||
break;
|
||||
case 0xCB: // float 64
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||
break;
|
||||
case 0xCC: // uint 8
|
||||
size = 2;
|
||||
bulk_advance(2, head);
|
||||
accepted = emit_unsigned(data[1]);
|
||||
break;
|
||||
case 0xCD: // uint 16
|
||||
size = 3;
|
||||
bulk_advance(3, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||
break;
|
||||
case 0xCE: // uint 32
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||
break;
|
||||
case 0xCF: // uint 64
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||
break;
|
||||
case 0xD0: // int 8
|
||||
size = 2;
|
||||
bulk_advance(2, head);
|
||||
accepted = emit_signed(static_cast<std::int8_t>(data[1]));
|
||||
break;
|
||||
case 0xD1: // int 16
|
||||
size = 3;
|
||||
bulk_advance(3, head);
|
||||
accepted = emit_signed(load_big_endian<std::int16_t>(data + 1));
|
||||
break;
|
||||
case 0xD2: // int 32
|
||||
size = 5;
|
||||
bulk_advance(5, head);
|
||||
accepted = emit_signed(load_big_endian<std::int32_t>(data + 1));
|
||||
break;
|
||||
case 0xD3: // int 64
|
||||
size = 9;
|
||||
bulk_advance(9, head);
|
||||
accepted = emit_signed(load_big_endian<std::int64_t>(data + 1));
|
||||
break;
|
||||
default:
|
||||
if (head <= 0x7F)
|
||||
{
|
||||
// positive fixint
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||
}
|
||||
else if (head >= 0xE0)
|
||||
{
|
||||
// negative fixint
|
||||
size = 1;
|
||||
bulk_advance(1, head);
|
||||
accepted = sax->number_integer(static_cast<std::int8_t>(head));
|
||||
}
|
||||
else
|
||||
@@ -21621,10 +21630,6 @@ class binary_reader
|
||||
}
|
||||
break;
|
||||
}
|
||||
// as if read by get() and get_number(): current is the last byte
|
||||
ia.bulk_skip(size);
|
||||
chars_read += size;
|
||||
current = data[size - 1];
|
||||
--remaining;
|
||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||
{
|
||||
@@ -22528,6 +22533,7 @@ class binary_reader
|
||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||
// UBJSON stores numbers big-endian, BJData little-endian
|
||||
const bool swap = is_little_endian != (input_format == input_format_t::bjdata);
|
||||
const std::size_t start = chars_read;
|
||||
std::size_t read = 0;
|
||||
bool accepted = true;
|
||||
while (read < count && accepted)
|
||||
@@ -22539,16 +22545,39 @@ class binary_reader
|
||||
byte_swap(number);
|
||||
}
|
||||
++read;
|
||||
// where passing the value on can report an error (see
|
||||
// emit_float), the position is that after the value, as if it
|
||||
// was read by get_number()
|
||||
set_chars_read(start + (read * size), std::integral_constant<bool, may_overflow_number_float<NumberType>()> {});
|
||||
accepted = emit_ubjson_number(number);
|
||||
}
|
||||
// as if read by get_number(): current is the last byte read
|
||||
// get_number() does not change current
|
||||
ia.bulk_skip(read * size);
|
||||
chars_read += read * size;
|
||||
current = data[(read * size) - 1];
|
||||
chars_read = start + (read * size);
|
||||
remaining -= read;
|
||||
return accepted;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief whether a value of type @a NumberType can be finite and still
|
||||
overflow number_float_t, which @ref emit_float reports as an error
|
||||
|
||||
False for the default number types, so that the loop in @ref
|
||||
parse_ubjson_typed_run need not track the position per value.
|
||||
*/
|
||||
template<typename NumberType>
|
||||
static constexpr bool may_overflow_number_float() noexcept
|
||||
{
|
||||
return static_cast<long double>((std::numeric_limits<number_float_t>::max)()) < static_cast<long double>((std::numeric_limits<NumberType>::max)());
|
||||
}
|
||||
|
||||
void set_chars_read(const std::size_t position, std::true_type /*track*/) noexcept
|
||||
{
|
||||
chars_read = position;
|
||||
}
|
||||
|
||||
void set_chars_read(const std::size_t /*position*/, std::false_type /*track*/) const noexcept {}
|
||||
|
||||
template<typename NumberType, enable_if_t<std::is_floating_point<NumberType>::value, int> = 0>
|
||||
bool emit_ubjson_number(const NumberType number)
|
||||
{
|
||||
@@ -48757,7 +48786,6 @@ struct formatter<nlohmann::NLOHMANN_BASIC_JSON_TPL, char> // NOLINT(cert-dcl58-c
|
||||
#undef JSON_HEDLEY_EMSCRIPTEN_VERSION
|
||||
#undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
|
||||
#undef JSON_HEDLEY_END_C_DECLS
|
||||
#undef JSON_HEDLEY_FALL_THROUGH
|
||||
#undef JSON_HEDLEY_FLAGS
|
||||
#undef JSON_HEDLEY_FLAGS_CAST
|
||||
#undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
|
||||
@@ -48866,6 +48894,7 @@ struct formatter<nlohmann::NLOHMANN_BASIC_JSON_TPL, char> // NOLINT(cert-dcl58-c
|
||||
#undef JSON_HEDLEY_WARNING
|
||||
#undef JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||
#undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
|
||||
#undef JSON_HEDLEY_FALL_THROUGH
|
||||
|
||||
// IWYU pragma: keep
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ using nlohmann::json;
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <limits>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -367,3 +368,125 @@ TEST_CASE("Binary formats with narrow number types")
|
||||
CHECK(format.decode(format.encode(json(1e300)), false).is_discarded());
|
||||
}
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
// records the SAX events of narrow_json, including the arguments of parse_error
|
||||
struct narrow_event_recorder
|
||||
{
|
||||
using number_integer_t = narrow_json::number_integer_t;
|
||||
using number_unsigned_t = narrow_json::number_unsigned_t;
|
||||
using number_float_t = narrow_json::number_float_t;
|
||||
using string_t = narrow_json::string_t;
|
||||
using binary_t = narrow_json::binary_t;
|
||||
|
||||
bool null()
|
||||
{
|
||||
events.emplace_back("null");
|
||||
return true;
|
||||
}
|
||||
bool boolean(bool val)
|
||||
{
|
||||
events.emplace_back(val ? "true" : "false");
|
||||
return true;
|
||||
}
|
||||
bool number_integer(number_integer_t val)
|
||||
{
|
||||
events.push_back(std::to_string(val));
|
||||
return true;
|
||||
}
|
||||
bool number_unsigned(number_unsigned_t val)
|
||||
{
|
||||
events.push_back(std::to_string(val));
|
||||
return true;
|
||||
}
|
||||
bool number_float(number_float_t val, const string_t& /*unused*/)
|
||||
{
|
||||
events.push_back(std::to_string(val));
|
||||
return true;
|
||||
}
|
||||
bool string(string_t& val)
|
||||
{
|
||||
events.push_back(val);
|
||||
return true;
|
||||
}
|
||||
bool binary(binary_t& /*unused*/)
|
||||
{
|
||||
events.emplace_back("binary");
|
||||
return true;
|
||||
}
|
||||
bool start_object(std::size_t /*unused*/)
|
||||
{
|
||||
events.emplace_back("{");
|
||||
return true;
|
||||
}
|
||||
bool key(string_t& val)
|
||||
{
|
||||
events.push_back(val);
|
||||
return true;
|
||||
}
|
||||
bool end_object()
|
||||
{
|
||||
events.emplace_back("}");
|
||||
return true;
|
||||
}
|
||||
bool start_array(std::size_t /*unused*/)
|
||||
{
|
||||
events.emplace_back("[");
|
||||
return true;
|
||||
}
|
||||
bool end_array()
|
||||
{
|
||||
events.emplace_back("]");
|
||||
return true;
|
||||
}
|
||||
bool parse_error(std::size_t position, const std::string& last_token, const nlohmann::detail::exception& ex)
|
||||
{
|
||||
events.push_back("parse_error(" + std::to_string(position) + ", " + last_token + ", " + ex.what() + ")");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> events {}; // NOLINT(readability-redundant-member-init)
|
||||
};
|
||||
} // namespace
|
||||
|
||||
TEST_CASE("Binary formats with narrow number types from contiguous and non-contiguous input")
|
||||
{
|
||||
// contiguous input reads runs of numbers in one loop; a number that
|
||||
// overflows number_float_t must be reported with the same position and
|
||||
// last byte as when the input is read byte by byte
|
||||
const json values = {1, 70000, -70000, 1.5, 2.5, 1e300, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5};
|
||||
const json doubles = {1.5, 2.5, 3.5, 1e300, 4.5, 5.5, 6.5, 7.5, 8.5};
|
||||
|
||||
struct binary_input
|
||||
{
|
||||
const char* name;
|
||||
json::input_format_t format;
|
||||
bytes data;
|
||||
};
|
||||
const std::vector<binary_input> inputs =
|
||||
{
|
||||
{"CBOR", json::input_format_t::cbor, json::to_cbor(values)},
|
||||
{"CBOR doubles", json::input_format_t::cbor, json::to_cbor(doubles)},
|
||||
{"MessagePack", json::input_format_t::msgpack, json::to_msgpack(values)},
|
||||
{"MessagePack doubles", json::input_format_t::msgpack, json::to_msgpack(doubles)},
|
||||
{"UBJSON", json::input_format_t::ubjson, json::to_ubjson(values, true)},
|
||||
{"UBJSON typed doubles", json::input_format_t::ubjson, json::to_ubjson(doubles, true, true)},
|
||||
{"BJData", json::input_format_t::bjdata, json::to_bjdata(values, true)},
|
||||
{"BJData typed doubles", json::input_format_t::bjdata, json::to_bjdata(doubles, true, true)},
|
||||
};
|
||||
|
||||
for (const auto& input : inputs)
|
||||
{
|
||||
const std::string name = input.name;
|
||||
CAPTURE(name)
|
||||
const std::list<std::uint8_t> bytewise(input.data.begin(), input.data.end());
|
||||
narrow_event_recorder from_contiguous;
|
||||
narrow_event_recorder from_bytewise;
|
||||
CHECK_FALSE(narrow_json::sax_parse(input.data, &from_contiguous, input.format));
|
||||
CHECK_FALSE(narrow_json::sax_parse(bytewise.begin(), bytewise.end(), &from_bytewise, input.format));
|
||||
CHECK(from_contiguous.events == from_bytewise.events);
|
||||
REQUIRE(!from_contiguous.events.empty());
|
||||
CHECK(from_contiguous.events.back().find("out_of_range.406") != std::string::npos);
|
||||
}
|
||||
}
|
||||
+19
-30
@@ -15,7 +15,6 @@ using nlohmann::json;
|
||||
#include <fstream>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "make_test_data_available.hpp"
|
||||
#include "test_utils.hpp"
|
||||
|
||||
@@ -266,50 +265,39 @@ TEST_CASE("Unicode")
|
||||
|
||||
CHECK_THROWS_WITH_AS(_ = json::parse("\"\\uD800\\uE000\""), "[json.exception.parse_error.101] parse error at line 1, column 13: syntax error while parsing value - invalid string: surrogate U+D800..U+DBFF must be followed by U+DC00..U+DFFF; last read: '\"\\uD800\\uE000'", json::parse_error&);
|
||||
}
|
||||
}
|
||||
|
||||
#if 0 // NOLINT(readability-avoid-unconditional-preprocessor-if)
|
||||
SECTION("incorrect sequences")
|
||||
{
|
||||
SECTION("high surrogate without low surrogate")
|
||||
{
|
||||
// D800..DBFF are high surrogates and must be followed by low
|
||||
// surrogates DC00..DFFF; here, nothing follows
|
||||
json _;
|
||||
for (std::size_t cp = 0xD800u; cp <= 0xDBFFu; ++cp)
|
||||
{
|
||||
const std::string json_text = "\"" + codepoint_to_unicode(cp) + "\"";
|
||||
std::string json_text = "\"" + codepoint_to_unicode(cp) + "\"";
|
||||
CAPTURE(json_text)
|
||||
CHECK_THROWS_AS(_ = json::parse(json_text), json::parse_error&);
|
||||
CHECK_THROWS_AS(json::parse(json_text), json::parse_error&);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("high surrogate with wrong low surrogate")
|
||||
{
|
||||
// D800..DBFF are high surrogates and must be followed by low
|
||||
// surrogates DC00..DFFF; here a different code unit follows.
|
||||
// All pairs would be 1024 * 64512 parses, so every high
|
||||
// surrogate is paired with the boundaries of the ranges around
|
||||
// DC00..DFFF, and one high surrogate with every code unit
|
||||
// (with accept(), which does not throw and is much faster)
|
||||
const auto escaped_pair = [&](std::size_t cp1, std::size_t cp2)
|
||||
{
|
||||
return "\"" + codepoint_to_unicode(cp1) + codepoint_to_unicode(cp2) + "\"";
|
||||
};
|
||||
const std::vector<std::size_t> boundaries = {0x0000u, 0x0041u, 0xD7FFu, 0xD800u, 0xDBFFu, 0xE000u, 0xFFFFu};
|
||||
json _;
|
||||
// surrogates DC00..DFFF; here a different sequence follows
|
||||
for (std::size_t cp1 = 0xD800u; cp1 <= 0xDBFFu; ++cp1)
|
||||
{
|
||||
for (const std::size_t cp2 : boundaries)
|
||||
for (std::size_t cp2 = 0x0000u; cp2 <= 0xFFFFu; ++cp2)
|
||||
{
|
||||
const std::string json_text = escaped_pair(cp1, cp2);
|
||||
if (0xDC00u <= cp2 && cp2 <= 0xDFFFu)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
std::string json_text = "\"" + codepoint_to_unicode(cp1) + codepoint_to_unicode(cp2) + "\"";
|
||||
CAPTURE(json_text)
|
||||
CHECK_THROWS_AS(_ = json::parse(json_text), json::parse_error&);
|
||||
}
|
||||
}
|
||||
for (std::size_t cp2 = 0x0000u; cp2 <= 0xFFFFu; ++cp2)
|
||||
{
|
||||
if (cp2 < 0xDC00u || cp2 > 0xDFFFu)
|
||||
{
|
||||
const std::string json_text = escaped_pair(0xDA00u, cp2);
|
||||
CAPTURE(json_text)
|
||||
CHECK_FALSE(json::accept(json_text));
|
||||
CHECK_THROWS_AS(json::parse(json_text), json::parse_error&);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -318,15 +306,16 @@ TEST_CASE("Unicode")
|
||||
{
|
||||
// low surrogates DC00..DFFF must follow high surrogates; here,
|
||||
// they occur alone
|
||||
json _;
|
||||
for (std::size_t cp = 0xDC00u; cp <= 0xDFFFu; ++cp)
|
||||
{
|
||||
const std::string json_text = "\"" + codepoint_to_unicode(cp) + "\"";
|
||||
std::string json_text = "\"" + codepoint_to_unicode(cp) + "\"";
|
||||
CAPTURE(json_text)
|
||||
CHECK_THROWS_AS(_ = json::parse(json_text), json::parse_error&);
|
||||
CHECK_THROWS_AS(json::parse(json_text), json::parse_error&);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("read all unicode characters")
|
||||
|
||||
Reference in new issue
Block a user