mirror of
https://github.com/nlohmann/json.git
synced 2026-08-10 19:23:19 +00:00
Compare commits
36
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bb8c13048f | ||
|
|
f61a40e7f1 | ||
|
|
7d2bc4a39a | ||
|
|
4818446af9 | ||
|
|
c4a3b4d63f | ||
|
|
d8858677ad | ||
|
|
8d7828aff0 | ||
|
|
387d766f4e | ||
|
|
813e8c9564 | ||
|
|
78fb2b676e | ||
|
|
c7b035b661 | ||
|
|
91929ecac1 | ||
|
|
51b5c250dc | ||
|
|
9a86ad483c | ||
|
|
52f7e7af23 | ||
|
|
18acf8ac46 | ||
|
|
21a88c46e3 | ||
|
|
7681caa4d7 | ||
|
|
ad94fb01cc | ||
|
|
c2e1cc50e0 | ||
|
|
173f2a7407 | ||
|
|
1c63a120b6 | ||
|
|
85889e8843 | ||
|
|
3c0a9a99fd | ||
|
|
e82724d87f | ||
|
|
78821cd9c2 | ||
|
|
68f0722a19 | ||
|
|
5f121d8c50 | ||
|
|
585929bff9 | ||
|
|
31ba5208c8 | ||
|
|
2222d386c9 | ||
|
|
eaedec859a | ||
|
|
d94cbd99dc | ||
|
|
bc48951128 | ||
|
|
fd72ecfc8c | ||
|
|
de8a099ba5 |
@@ -38,14 +38,14 @@ jobs:
|
|||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
uses: github/codeql-action/init@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4.37.3
|
||||||
with:
|
with:
|
||||||
languages: c-cpp
|
languages: c-cpp
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
- name: Autobuild
|
- name: Autobuild
|
||||||
uses: github/codeql-action/autobuild@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
uses: github/codeql-action/autobuild@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4.37.3
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
- name: Perform CodeQL Analysis
|
||||||
uses: github/codeql-action/analyze@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
uses: github/codeql-action/analyze@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4.37.3
|
||||||
|
|||||||
@@ -43,6 +43,6 @@ jobs:
|
|||||||
output: 'flawfinder_results.sarif'
|
output: 'flawfinder_results.sarif'
|
||||||
|
|
||||||
- name: Upload analysis results to GitHub Security tab
|
- name: Upload analysis results to GitHub Security tab
|
||||||
uses: github/codeql-action/upload-sarif@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
uses: github/codeql-action/upload-sarif@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4.37.3
|
||||||
with:
|
with:
|
||||||
sarif_file: ${{github.workspace}}/flawfinder_results.sarif
|
sarif_file: ${{github.workspace}}/flawfinder_results.sarif
|
||||||
|
|||||||
@@ -76,6 +76,6 @@ jobs:
|
|||||||
|
|
||||||
# Upload the results to GitHub's code scanning dashboard.
|
# Upload the results to GitHub's code scanning dashboard.
|
||||||
- name: "Upload to code-scanning"
|
- name: "Upload to code-scanning"
|
||||||
uses: github/codeql-action/upload-sarif@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
uses: github/codeql-action/upload-sarif@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4.37.3
|
||||||
with:
|
with:
|
||||||
sarif_file: results.sarif
|
sarif_file: results.sarif
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ jobs:
|
|||||||
|
|
||||||
# Upload SARIF file generated in previous step
|
# Upload SARIF file generated in previous step
|
||||||
- name: Upload SARIF file
|
- name: Upload SARIF file
|
||||||
uses: github/codeql-action/upload-sarif@e0647621c2984b5ed2f768cb892365bf2a616ad1 # v4.37.2
|
uses: github/codeql-action/upload-sarif@e4fba868fa4b1b91e1fdab776edc8cfbe6e9fb81 # v4.37.3
|
||||||
with:
|
with:
|
||||||
sarif_file: semgrep.sarif
|
sarif_file: semgrep.sarif
|
||||||
if: always()
|
if: always()
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
- uses: actions/stale@1e223db275d687790206a7acac4d1a11bd6fe629 # v10.4.0
|
- uses: actions/stale@4391f3da665fdf50b6810c1a66712fb9ba21aa93 # v11.0.0
|
||||||
with:
|
with:
|
||||||
stale-issue-label: 'state: stale'
|
stale-issue-label: 'state: stale'
|
||||||
stale-pr-label: 'state: stale'
|
stale-pr-label: 'state: stale'
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ jobs:
|
|||||||
name: code-coverage-report
|
name: code-coverage-report
|
||||||
path: ${{ github.workspace }}/build/html
|
path: ${{ github.workspace }}/build/html
|
||||||
- name: Publish report to Coveralls
|
- name: Publish report to Coveralls
|
||||||
uses: coverallsapp/github-action@5cbfd81b66ca5d10c19b062c04de0199c215fb6e # v2.3.7
|
uses: coverallsapp/github-action@8d6379e14d29928660c4ba802d8e85393440b329 # v2.3.8
|
||||||
with:
|
with:
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
path-to-lcov: ${{ github.workspace }}/build/json.info.filtered.noexcept
|
path-to-lcov: ${{ github.workspace }}/build/json.info.filtered.noexcept
|
||||||
|
|||||||
@@ -1801,13 +1801,13 @@ The library itself consists of a single header file licensed under the MIT licen
|
|||||||
- [**amalgamate.py - Amalgamate C source and header files**](https://github.com/edlund/amalgamate) to create a single header file
|
- [**amalgamate.py - Amalgamate C source and header files**](https://github.com/edlund/amalgamate) to create a single header file
|
||||||
- [**American fuzzy lop**](https://lcamtuf.coredump.cx/afl/) for fuzz testing
|
- [**American fuzzy lop**](https://lcamtuf.coredump.cx/afl/) for fuzz testing
|
||||||
- [**AppVeyor**](https://www.appveyor.com) for [continuous integration](https://ci.appveyor.com/project/nlohmann/json) on Windows
|
- [**AppVeyor**](https://www.appveyor.com) for [continuous integration](https://ci.appveyor.com/project/nlohmann/json) on Windows
|
||||||
- [**Artistic Style**](http://astyle.sourceforge.net) for automatic source code indentation
|
- [**Artistic Style**](https://astyle.sourceforge.net) for automatic source code indentation
|
||||||
- [**Clang**](https://clang.llvm.org) for compilation with code sanitizers
|
- [**Clang**](https://clang.llvm.org) for compilation with code sanitizers
|
||||||
- [**CMake**](https://cmake.org) for build automation
|
- [**CMake**](https://cmake.org) for build automation
|
||||||
- [**Codacy**](https://www.codacy.com) for further [code analysis](https://app.codacy.com/gh/nlohmann/json/dashboard)
|
- [**Codacy**](https://www.codacy.com) for further [code analysis](https://app.codacy.com/gh/nlohmann/json/dashboard)
|
||||||
- [**Coveralls**](https://coveralls.io) to measure [code coverage](https://coveralls.io/github/nlohmann/json)
|
- [**Coveralls**](https://coveralls.io) to measure [code coverage](https://coveralls.io/github/nlohmann/json)
|
||||||
- [**Coverity Scan**](https://scan.coverity.com) for [static analysis](https://scan.coverity.com/projects/nlohmann-json)
|
- [**Coverity Scan**](https://scan.coverity.com) for [static analysis](https://scan.coverity.com/projects/nlohmann-json)
|
||||||
- [**cppcheck**](http://cppcheck.sourceforge.net) for static analysis
|
- [**cppcheck**](https://cppcheck.sourceforge.io) for static analysis
|
||||||
- [**doctest**](https://github.com/onqtam/doctest) for the unit tests
|
- [**doctest**](https://github.com/onqtam/doctest) for the unit tests
|
||||||
- [**GitHub Changelog Generator**](https://github.com/skywinder/github-changelog-generator) to generate the [ChangeLog](https://github.com/nlohmann/json/blob/develop/ChangeLog.md)
|
- [**GitHub Changelog Generator**](https://github.com/skywinder/github-changelog-generator) to generate the [ChangeLog](https://github.com/nlohmann/json/blob/develop/ChangeLog.md)
|
||||||
- [**Google Benchmark**](https://github.com/google/benchmark) to implement the benchmarks
|
- [**Google Benchmark**](https://github.com/google/benchmark) to implement the benchmarks
|
||||||
@@ -1822,6 +1822,15 @@ The library itself consists of a single header file licensed under the MIT licen
|
|||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
### Standards compliance
|
||||||
|
|
||||||
|
The library targets strict conformance with [RFC 8259](https://tools.ietf.org/html/rfc8259.html). Both the original [JSONTestSuite](https://github.com/nst/JSONTestSuite) and its updated revision are exercised in CI; their test data is downloaded from [`nlohmann/json_test_data`](https://github.com/nlohmann/json_test_data) at configure time rather than committed to this repository (see [`tests/src/unit-testsuites.cpp`](https://github.com/nlohmann/json/blob/develop/tests/src/unit-testsuites.cpp)):
|
||||||
|
|
||||||
|
- The updated revision runs all mandatory `y_` (must-accept) and `n_` (must-reject) cases through the strict [`parse()`](https://json.nlohmann.me/api/basic_json/parse/) entry point; the original suite runs its `n_` cases through `parse()` and its `y_` cases through [`operator>>`](https://json.nlohmann.me/api/operator_gtgt/).
|
||||||
|
- The `i_` (implementation-defined) cases are, by RFC 8259, free to be accepted *or* rejected, so "passing all `i_` cases" is not a meaningful conformance metric. The library makes deliberate, documented choices there: nesting depth is not artificially limited, a leading UTF-8 byte order mark is silently ignored, [Unicode noncharacters](https://www.unicode.org/faq/private_use.html#nonchar1) are forwarded unchanged, invalid UTF-8 and lone/unpaired UTF-16 surrogates are rejected (stricter than required), and a number that cannot be stored without becoming `NaN`/`INF` raises [`out_of_range.406`](https://json.nlohmann.me/home/exceptions/#jsonexceptionout_of_range406).
|
||||||
|
|
||||||
|
One behavioral nuance is worth calling out, because a superficial test often misreads it as non-compliance: [`parse()`](https://json.nlohmann.me/api/basic_json/parse/) is strict and rejects trailing data after a value, whereas [`operator>>`](https://json.nlohmann.me/api/operator_gtgt/) follows relaxed iostream semantics — it parses a single value and leaves the stream positioned right after it. Feeding "a valid document followed by trailing bytes" through `operator>>` reports success; the same input through `parse()` is rejected. This is a documented two-API design, not a conformance gap. See [**parsing**](https://json.nlohmann.me/features/parsing/) for details.
|
||||||
|
|
||||||
### Character encoding
|
### Character encoding
|
||||||
|
|
||||||
The library supports **Unicode input** as follows:
|
The library supports **Unicode input** as follows:
|
||||||
|
|||||||
@@ -5,6 +5,9 @@
|
|||||||
# -Wno-extra-semi-stmt The library uses assert which triggers this warning.
|
# -Wno-extra-semi-stmt The library uses assert which triggers this warning.
|
||||||
# -Wno-padded We do not care about padding warnings.
|
# -Wno-padded We do not care about padding warnings.
|
||||||
# -Wno-covered-switch-default All switches list all cases and a default case.
|
# -Wno-covered-switch-default All switches list all cases and a default case.
|
||||||
|
# -Wno-c2y-extensions Clang 22.1 diagnoses __COUNTER__ as a C2y extension, also in
|
||||||
|
# C++ mode. The library does not use __COUNTER__; the warnings
|
||||||
|
# all come from vendored Doctest (SECTION/TEST_CASE macros).
|
||||||
# -Wno-unsafe-buffer-usage Pervasive: the library's own low-level numeric/buffer code
|
# -Wno-unsafe-buffer-usage Pervasive: the library's own low-level numeric/buffer code
|
||||||
# (to_chars, serializer, lexer, binary reader/writer, input
|
# (to_chars, serializer, lexer, binary reader/writer, input
|
||||||
# adapters, json_pointer) plus vendored Doctest itself (~208
|
# adapters, json_pointer) plus vendored Doctest itself (~208
|
||||||
@@ -20,5 +23,6 @@ set(CLANG_CXXFLAGS
|
|||||||
-Wno-extra-semi-stmt
|
-Wno-extra-semi-stmt
|
||||||
-Wno-padded
|
-Wno-padded
|
||||||
-Wno-covered-switch-default
|
-Wno-covered-switch-default
|
||||||
|
-Wno-c2y-extensions
|
||||||
-Wno-unsafe-buffer-usage
|
-Wno-unsafe-buffer-usage
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -29,7 +29,14 @@ Discarding a value (i.e., returning `#!cpp false`) has different effects dependi
|
|||||||
called:
|
called:
|
||||||
|
|
||||||
- Discarded values in structured types are skipped. That is, the parser will behave as if the discarded value was never
|
- Discarded values in structured types are skipped. That is, the parser will behave as if the discarded value was never
|
||||||
read.
|
read. This holds for every value type and for both kinds of parent: a discarded element is removed from the
|
||||||
|
surrounding array, and a discarded member is removed from the surrounding object together with its key.
|
||||||
|
- Arrays and objects can be discarded either at their `parse_event_t::array_start`/`parse_event_t::object_start` event
|
||||||
|
or at their `parse_event_t::array_end`/`parse_event_t::object_end` event, and both remove the whole value. Discarding
|
||||||
|
it at the start event also means the callback is called neither for the content of the value nor for its matching end
|
||||||
|
event.
|
||||||
|
- Discarding a `parse_event_t::key` event discards the whole object member. The callback is still called for the
|
||||||
|
associated value, but its return value has no further effect.
|
||||||
- In case a value outside a structured type is skipped, it is replaced with `null`. This case happens if the top-level
|
- In case a value outside a structured type is skipped, it is replaced with `null`. This case happens if the top-level
|
||||||
element is skipped.
|
element is skipped.
|
||||||
|
|
||||||
@@ -49,7 +56,7 @@ called:
|
|||||||
## Return value
|
## Return value
|
||||||
|
|
||||||
Whether the JSON value which called the function during parsing should be kept (`#!cpp true`) or not (`#!cpp false`). In
|
Whether the JSON value which called the function during parsing should be kept (`#!cpp true`) or not (`#!cpp false`). In
|
||||||
the latter case, it is either skipped completely or replaced by an empty discarded object.
|
the latter case, it is skipped completely, or replaced by `null` if it is the top-level value.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
@@ -68,6 +75,21 @@ the latter case, it is either skipped completely or replaced by an empty discard
|
|||||||
--8<-- "examples/parse__string__parser_callback_t.output"
|
--8<-- "examples/parse__string__parser_callback_t.output"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
??? example
|
||||||
|
|
||||||
|
The example below shows where discarded values are removed. The array and the number are discarded in different
|
||||||
|
ways, but in each case the parse result contains neither the value nor its key.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
--8<-- "examples/parser_callback_t.cpp"
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```json
|
||||||
|
--8<-- "examples/parser_callback_t.output"
|
||||||
|
```
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
|
|
||||||
- [parse](parse.md) deserialize from a compatible input
|
- [parse](parse.md) deserialize from a compatible input
|
||||||
@@ -76,3 +98,5 @@ the latter case, it is either skipped completely or replaced by an empty discard
|
|||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 1.0.0.
|
- Added in version 1.0.0.
|
||||||
|
- Fixed in version 3.13.0 to also remove discarded values from a parent object; before, discarding an array or a value
|
||||||
|
stored under an object key left a discarded member behind, which made the parse result serialize to invalid JSON.
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ header. See also the [macro overview page](../../features/macros.md).
|
|||||||
- [**JSON_NO_IO**](json_no_io.md) - switch off functions relying on certain C++ I/O headers
|
- [**JSON_NO_IO**](json_no_io.md) - switch off functions relying on certain C++ I/O headers
|
||||||
- [**JSON_SKIP_UNSUPPORTED_COMPILER_CHECK**](json_skip_unsupported_compiler_check.md) - do not warn about unsupported compilers
|
- [**JSON_SKIP_UNSUPPORTED_COMPILER_CHECK**](json_skip_unsupported_compiler_check.md) - do not warn about unsupported compilers
|
||||||
- [**JSON_USE_GLOBAL_UDLS**](json_use_global_udls.md) - place user-defined string literals (UDLs) into the global namespace
|
- [**JSON_USE_GLOBAL_UDLS**](json_use_global_udls.md) - place user-defined string literals (UDLs) into the global namespace
|
||||||
|
- [**JSON_USE_SIMDUTF**](json_use_simdutf.md) - use the simdutf library to accelerate UTF-8 validation
|
||||||
|
|
||||||
## Library version
|
## Library version
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,52 @@
|
|||||||
|
# JSON_USE_SIMDUTF
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#define JSON_USE_SIMDUTF
|
||||||
|
```
|
||||||
|
|
||||||
|
When defined, the parser validates the UTF-8 content of JSON strings that come from a **contiguous byte input**
|
||||||
|
(`std::string`, `std::vector<char>`/`<std::uint8_t>`, string literals, `const char*` ranges, …) using the
|
||||||
|
[simdutf](https://github.com/simdutf/simdutf) library instead of the built-in scalar validator. On text with many
|
||||||
|
non-ASCII characters (e.g. CJK or emoji) this can validate several times faster.
|
||||||
|
|
||||||
|
This is an **opt-in external dependency**. The library itself remains header-only and its behavior is unchanged: the
|
||||||
|
same input is accepted or rejected either way, and every parse error is reported at the same position with the same
|
||||||
|
message (simdutf is only used to fast-path *valid* runs; anything it flags falls back to the scalar path so the exact
|
||||||
|
diagnostic is preserved). Streaming inputs (files, `std::istream`, wide strings, user-defined adapters) always use the
|
||||||
|
scalar path.
|
||||||
|
|
||||||
|
When `JSON_USE_SIMDUTF` is defined you must make the `simdutf.h` header available on the include path and link the
|
||||||
|
simdutf library. When it is not defined, no simdutf header is included and there is no dependency.
|
||||||
|
|
||||||
|
## Default definition
|
||||||
|
|
||||||
|
By default, `#!cpp JSON_USE_SIMDUTF` is not defined and the portable C++11 scalar validator is used.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#undef JSON_USE_SIMDUTF
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
??? example
|
||||||
|
|
||||||
|
The code below enables the simdutf backend for UTF-8 validation.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#define JSON_USE_SIMDUTF 1
|
||||||
|
#include <simdutf.h>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
...
|
||||||
|
```
|
||||||
|
|
||||||
|
The project must also link against simdutf, e.g. with CMake:
|
||||||
|
|
||||||
|
```cmake
|
||||||
|
target_compile_definitions(your_target PRIVATE JSON_USE_SIMDUTF)
|
||||||
|
target_link_libraries(your_target PRIVATE simdutf::simdutf)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Version history
|
||||||
|
|
||||||
|
- Added in version 3.12.1.
|
||||||
@@ -33,17 +33,44 @@ A UTF-8 byte order mark is silently ignored.
|
|||||||
Invalid Unicode escapes and unpaired surrogates in the input are reported as
|
Invalid Unicode escapes and unpaired surrogates in the input are reported as
|
||||||
[`parse_error.101`](../home/exceptions.md#jsonexceptionparse_error101) with a detailed message.
|
[`parse_error.101`](../home/exceptions.md#jsonexceptionparse_error101) with a detailed message.
|
||||||
|
|
||||||
`operator>>` parses exactly one JSON value and leaves the stream positioned right after it, so it can be called
|
`operator>>` parses exactly one JSON value, so it can be called repeatedly to read a sequence of concatenated JSON
|
||||||
repeatedly to read a sequence of concatenated JSON values from the same stream:
|
values from the same stream:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
json j1, j2;
|
json j1, j2;
|
||||||
input >> j1; // parses the first value, stream now positioned right after it
|
input >> j1; // parses the first value
|
||||||
input >> j2; // parses the next value
|
input >> j2; // parses the next value
|
||||||
```
|
```
|
||||||
|
|
||||||
Note this does **not** work for [JSON Lines](../features/parsing/json_lines.md) (newline-delimited JSON) input --
|
!!! warning "A number must be followed by whitespace"
|
||||||
see that page for why and for the recommended alternative.
|
|
||||||
|
A number is only terminated by the character that follows it. That character is read from the stream to detect the
|
||||||
|
end of the number, and it is **not** put back. When a value that is a number is immediately followed by the next
|
||||||
|
value, the first character of that next value is lost:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
std::istringstream input("1true");
|
||||||
|
json j1, j2;
|
||||||
|
input >> j1; // j1 == 1
|
||||||
|
input >> j2; // throws parse_error.101: the stream now starts at "rue"
|
||||||
|
```
|
||||||
|
|
||||||
|
Separating the values with whitespace avoids this, because the character that is eaten is then the separator:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
std::istringstream input("1 true");
|
||||||
|
json j1, j2;
|
||||||
|
input >> j1; // j1 == 1
|
||||||
|
input >> j2; // j2 == true
|
||||||
|
```
|
||||||
|
|
||||||
|
Only numbers are affected. Values ending in a self-delimiting character do not read past themselves, so
|
||||||
|
`truefalse`, `[1][2]`, `{"a":1}{"b":2}`, and `"a""b"` can be read back to back without a separator.
|
||||||
|
|
||||||
|
This is tracked in [#5340](https://github.com/nlohmann/json/issues/5340).
|
||||||
|
|
||||||
|
Note that reading concatenated values does **not** work for [JSON Lines](../features/parsing/json_lines.md)
|
||||||
|
(newline-delimited JSON) input -- see that page for why and for the recommended alternative.
|
||||||
|
|
||||||
!!! warning "Deprecation"
|
!!! warning "Deprecation"
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ Therefore, adding object elements can yield a reallocation in which case all ite
|
|||||||
[`end()`](basic_json/end.md) iterator) and all references to the elements are invalidated. Also, any iterator or
|
[`end()`](basic_json/end.md) iterator) and all references to the elements are invalidated. Also, any iterator or
|
||||||
reference after the insertion point will point to the same index, which is now a different value.
|
reference after the insertion point will point to the same index, which is now a different value.
|
||||||
|
|
||||||
|
## Complexity
|
||||||
|
|
||||||
|
[`ordered_map`](ordered_map.md) has no lookup index: every key-based object operation is a linear scan, so building or
|
||||||
|
parsing an object of `n` keys costs O(n²) rather than O(n log n). See
|
||||||
|
[`ordered_map` complexity](ordered_map.md#complexity) for the per-operation table and for measured numbers.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
??? example
|
??? example
|
||||||
|
|||||||
@@ -56,6 +56,48 @@ std::equal_to<> // since C++14
|
|||||||
- **find**
|
- **find**
|
||||||
- **insert**
|
- **insert**
|
||||||
|
|
||||||
|
## Complexity
|
||||||
|
|
||||||
|
Because the elements are stored in a `std::vector` in insertion order, there is no index to look a key up by. Every
|
||||||
|
key-based operation performs a **linear scan** over the stored elements. With `n` denoting the number of elements in the
|
||||||
|
container:
|
||||||
|
|
||||||
|
| Operation | Complexity | Note |
|
||||||
|
|----------------------------------------|----------------|----------------------------------------------------------|
|
||||||
|
| **emplace** | O(n) | scans for an existing key, then appends (amortized O(1)) |
|
||||||
|
| **operator\[\]** | O(n) | delegates to **emplace** (non-const) or **at** (const) |
|
||||||
|
| **at** | O(n) | throws `#!cpp std::out_of_range` if the key is not found |
|
||||||
|
| **find** | O(n) | |
|
||||||
|
| **count** | O(n) | the result is always 0 or 1 |
|
||||||
|
| **erase(key)** | O(n) | scan, then move the remaining elements one position down |
|
||||||
|
| **erase(pos)**, **erase(first, last)** | O(n) | moves all elements after the erased range |
|
||||||
|
| **insert(value)** | O(n) | equivalent to **emplace** |
|
||||||
|
| **insert(first, last)** | O((n + m) * m) | for `m` inserted elements |
|
||||||
|
|
||||||
|
This differs from `#!cpp std::map`, where the same operations are O(log n).
|
||||||
|
|
||||||
|
!!! warning "Quadratic cost of building large objects"
|
||||||
|
|
||||||
|
Because every insertion scans all elements inserted so far, building an object of `n` distinct keys costs
|
||||||
|
**O(n²)** in total. This applies to filling an [`ordered_json`](ordered_json.md) object key by key as well as to
|
||||||
|
parsing one, since the parser inserts each key as it is read.
|
||||||
|
|
||||||
|
The cost is negligible for the object sizes typically found in configuration files or API payloads, but it grows
|
||||||
|
steeply for machine-generated objects with many thousands of keys. Measured with `-O2 -DNDEBUG` for parsing a flat
|
||||||
|
object of `n` keys, relative to `#!cpp nlohmann::json` (which uses `#!cpp std::map`):
|
||||||
|
|
||||||
|
| `n` | `json` | `ordered_json` | factor |
|
||||||
|
|--------|--------|----------------|--------|
|
||||||
|
| 2000 | 0.7 ms | 3.6 ms | 5× |
|
||||||
|
| 4000 | 0.8 ms | 14.0 ms | 19× |
|
||||||
|
| 8000 | 1.6 ms | 67.8 ms | 43× |
|
||||||
|
| 16 000 | 3.3 ms | 181.6 ms | 54× |
|
||||||
|
|
||||||
|
If key order matters for objects of that size, consider a container with a lookup index, such as
|
||||||
|
[`tsl::ordered_map`](https://github.com/Tessil/ordered-map)
|
||||||
|
([integration](https://github.com/nlohmann/json/issues/546#issuecomment-304447518)), as the object type -- see
|
||||||
|
[object order](../features/object_order.md).
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
??? example
|
??? example
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// a JSON text with an array and a number inside an object
|
||||||
|
auto text = R"({"IDs": [116, 943], "Width": 800})";
|
||||||
|
|
||||||
|
// discard the array when the parser reads its opening bracket
|
||||||
|
json j_array_start = json::parse(text, [](int /*depth*/, json::parse_event_t event, json & /*parsed*/)
|
||||||
|
{
|
||||||
|
return event != json::parse_event_t::array_start;
|
||||||
|
});
|
||||||
|
|
||||||
|
// discard the same array when the parser reads its closing bracket
|
||||||
|
json j_array_end = json::parse(text, [](int /*depth*/, json::parse_event_t event, json & /*parsed*/)
|
||||||
|
{
|
||||||
|
return event != json::parse_event_t::array_end;
|
||||||
|
});
|
||||||
|
|
||||||
|
// discard the number, but keep its key
|
||||||
|
json j_value = json::parse(text, [](int /*depth*/, json::parse_event_t event, json & parsed)
|
||||||
|
{
|
||||||
|
return !(event == json::parse_event_t::value && parsed == json(800));
|
||||||
|
});
|
||||||
|
|
||||||
|
// discard the key of the number
|
||||||
|
json j_key = json::parse(text, [](int /*depth*/, json::parse_event_t event, json & parsed)
|
||||||
|
{
|
||||||
|
return !(event == json::parse_event_t::key && parsed == json("Width"));
|
||||||
|
});
|
||||||
|
|
||||||
|
// discard the top-level object
|
||||||
|
json j_root = json::parse(text, [](int /*depth*/, json::parse_event_t event, json & /*parsed*/)
|
||||||
|
{
|
||||||
|
return event != json::parse_event_t::object_end;
|
||||||
|
});
|
||||||
|
|
||||||
|
// in every case, the discarded value is removed together with its key
|
||||||
|
std::cout << j_array_start << '\n'
|
||||||
|
<< j_array_end << '\n'
|
||||||
|
<< j_value << '\n'
|
||||||
|
<< j_key << '\n'
|
||||||
|
<< j_root << '\n';
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{"Width":800}
|
||||||
|
{"Width":800}
|
||||||
|
{"IDs":[116,943]}
|
||||||
|
{"IDs":[116,943]}
|
||||||
|
null
|
||||||
@@ -35,6 +35,19 @@ The library uses the following mapping from JSON values types to BSON types:
|
|||||||
The mapping is **incomplete**, since only JSON-objects (and things contained therein) can be serialized to BSON.
|
The mapping is **incomplete**, since only JSON-objects (and things contained therein) can be serialized to BSON.
|
||||||
Also, keys may not contain U+0000, since they are serialized a zero-terminated c-strings.
|
Also, keys may not contain U+0000, since they are serialized a zero-terminated c-strings.
|
||||||
|
|
||||||
|
!!! warning "BSON type 0x11 interoperability"
|
||||||
|
|
||||||
|
The BSON specification defines type `0x11` as a Timestamp. This library uses marker `0x11` when serializing
|
||||||
|
`number_unsigned` values in the range `9223372036854775808..18446744073709551615`. Other BSON implementations may
|
||||||
|
therefore interpret these values as Timestamps instead of unsigned integers.
|
||||||
|
|
||||||
|
!!! info "Binary values without a subtype"
|
||||||
|
|
||||||
|
BSON requires every binary value to have a subtype. If a binary value has no subtype, this library serializes it
|
||||||
|
with the generic subtype `0x00`. After deserialization, `has_subtype()` returns `true` and `subtype()` returns `0`.
|
||||||
|
As a result, serializing and deserializing a JSON object containing such a value produces a different JSON object,
|
||||||
|
even though the binary data is unchanged.
|
||||||
|
|
||||||
??? example
|
??? example
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
@@ -82,8 +95,8 @@ The library maps BSON record types to JSON value types as follows:
|
|||||||
|
|
||||||
!!! note "Handling of BSON type 0x11"
|
!!! note "Handling of BSON type 0x11"
|
||||||
|
|
||||||
BSON type 0x11 is used to represent uint64 numbers. This library treats these values purely as uint64 numbers
|
This library deserializes BSON type `0x11` (Timestamp) as a `number_unsigned` value. The 64-bit value is preserved,
|
||||||
and does not parse them into date-related formats.
|
but the Timestamp type information is not.
|
||||||
|
|
||||||
??? example
|
??? example
|
||||||
|
|
||||||
|
|||||||
@@ -66,8 +66,14 @@ auto t = j.get<std::tuple<double, std::string, int>>(); // {1.0, "hello", 42}
|
|||||||
std::get<1>(refs) = "world"; // modifies j[1] in place
|
std::get<1>(refs) = "world"; // modifies j[1] in place
|
||||||
```
|
```
|
||||||
|
|
||||||
A referenced type must be one the library actually stores (or an arithmetic type it can convert to/from);
|
A referenced element must name the type the library actually *stores* — one of [`boolean_t`](../api/basic_json/boolean_t.md),
|
||||||
otherwise this is a compile error.
|
[`number_integer_t`](../api/basic_json/number_integer_t.md), [`number_unsigned_t`](../api/basic_json/number_unsigned_t.md),
|
||||||
|
[`number_float_t`](../api/basic_json/number_float_t.md), [`string_t`](../api/basic_json/string_t.md),
|
||||||
|
[`binary_t`](../api/basic_json/binary_t.md), [`array_t`](../api/basic_json/array_t.md), or
|
||||||
|
[`object_t`](../api/basic_json/object_t.md). There is nothing else to refer to, so a reference to any other type is a
|
||||||
|
compile error even when a conversion would exist: `#!cpp std::tuple<int&>` is rejected, because the library stores a
|
||||||
|
`#!cpp number_integer_t` (`#!cpp std::int64_t` by default) and not an `#!cpp int`. This restriction applies only to
|
||||||
|
reference elements — a plain `#!cpp std::tuple<int>` converts by value as usual.
|
||||||
|
|
||||||
## Implicit conversions
|
## Implicit conversions
|
||||||
|
|
||||||
@@ -116,17 +122,34 @@ which forces the explicit `get` form and can catch unintended conversions at com
|
|||||||
with a custom `adl_serializer<std::optional<T>>` specialization. Prefer `get<std::optional<T>>()`/`get_to()`
|
with a custom `adl_serializer<std::optional<T>>` specialization. Prefer `get<std::optional<T>>()`/`get_to()`
|
||||||
over `static_cast` for optional types.
|
over `static_cast` for optional types.
|
||||||
|
|
||||||
!!! warning "Converting to a fixed-size `std::array` does not check length"
|
!!! warning "Converting to a fixed-size destination does not check the array size"
|
||||||
|
|
||||||
Converting a JSON array to `#!cpp std::array<T, N>` does not check that the JSON array's size matches `N`:
|
Some destination types have a size that is fixed by their C++ type rather than by the JSON value:
|
||||||
if the JSON array is longer, the extra elements are silently dropped; if it is shorter, the remaining
|
`#!cpp std::pair<A, B>`, `#!cpp std::tuple<Ts...>`, `#!cpp std::array<T, N>`, C arrays `#!cpp T[N]`, and
|
||||||
`std::array` elements are left default-constructed. No exception is thrown in either case.
|
`#!cpp std::map`/`#!cpp std::unordered_map` with a non-string key type (which is read from an array of
|
||||||
|
two-element arrays). All of them read exactly as many elements as they need via
|
||||||
|
[`at`](../api/basic_json/at.md) and **never compare the JSON array's size to that number**. The two
|
||||||
|
mismatch directions therefore behave differently:
|
||||||
|
|
||||||
|
- The JSON array has **too many** elements: the surplus is **silently discarded**, and no exception is
|
||||||
|
thrown.
|
||||||
|
- The JSON array has **too few** elements: `at` throws
|
||||||
|
[`out_of_range.401`](../home/exceptions.md#jsonexceptionout_of_range401) for the first missing index --
|
||||||
|
an out-of-range error, not a [`type_error`](../home/exceptions.md#type-errors), even though the cause
|
||||||
|
is a shape mismatch.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
json j = {1, 2, 3, 4, 5};
|
json j = {1, 2, 3, 4, 5};
|
||||||
auto a = j.get<std::array<int, 3>>(); // {1, 2, 3} -- elements 4 and 5 silently dropped
|
|
||||||
|
auto a = j.get<std::array<int, 3>>(); // {1, 2, 3} -- elements 4 and 5 silently dropped
|
||||||
|
auto p = j.get<std::pair<int, int>>(); // (1, 2) -- elements 3, 4, and 5 silently dropped
|
||||||
|
|
||||||
|
json k = {1};
|
||||||
|
auto q = k.get<std::pair<int, int>>(); // ❌ throws out_of_range.401
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If a size mismatch is an error in your application, check the size yourself before converting.
|
||||||
|
|
||||||
## Omitting a field when serializing `std::optional`
|
## Omitting a field when serializing `std::optional`
|
||||||
|
|
||||||
By default, `to_json` for `std::optional<T>` writes either the value or `#!json null` -- there is no built-in way
|
By default, `to_json` for `std::optional<T>` writes either the value or `#!json null` -- there is no built-in way
|
||||||
|
|||||||
@@ -53,6 +53,12 @@ If you do want to preserve the **insertion order**, you can use the type [`nlohm
|
|||||||
|
|
||||||
Alternatively, you can use a more sophisticated ordered map like [`tsl::ordered_map`](https://github.com/Tessil/ordered-map) ([integration](https://github.com/nlohmann/json/issues/546#issuecomment-304447518)) or [`nlohmann::fifo_map`](https://github.com/nlohmann/fifo_map) ([integration](https://github.com/nlohmann/json/issues/485#issuecomment-333652309)).
|
Alternatively, you can use a more sophisticated ordered map like [`tsl::ordered_map`](https://github.com/Tessil/ordered-map) ([integration](https://github.com/nlohmann/json/issues/546#issuecomment-304447518)) or [`nlohmann::fifo_map`](https://github.com/nlohmann/fifo_map) ([integration](https://github.com/nlohmann/json/issues/485#issuecomment-333652309)).
|
||||||
|
|
||||||
|
The [`ordered_map`](../api/ordered_map.md) behind `nlohmann::ordered_json` is deliberately minimal and has no lookup
|
||||||
|
index, so every key access is a linear scan and building an object of `n` keys costs O(n²). This is unnoticeable at
|
||||||
|
typical object sizes but becomes significant for objects with many thousands of keys; see
|
||||||
|
[`ordered_map` complexity](../api/ordered_map.md#complexity). The alternatives above keep a lookup index and do not
|
||||||
|
have this cost.
|
||||||
|
|
||||||
### Notes on parsing
|
### Notes on parsing
|
||||||
|
|
||||||
Note that you also need to call the right [`parse`](../api/basic_json/parse.md) function when reading from a file.
|
Note that you also need to call the right [`parse`](../api/basic_json/parse.md) function when reading from a file.
|
||||||
|
|||||||
@@ -28,6 +28,22 @@ Inputs consisting of multiple values separated by newlines are handled by the [J
|
|||||||
By default, the library rejects comments and trailing commas. Both can be enabled with parameters of the `parse`
|
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).
|
function — see [comments](../comments.md) and [trailing commas](../trailing_commas.md).
|
||||||
|
|
||||||
|
## Strictness and trailing data
|
||||||
|
|
||||||
|
[`parse`](../../api/basic_json/parse.md) reads a single JSON value and requires the whole input to be consumed: any
|
||||||
|
non-whitespace data after the value is reported as a parse error. Use it when you want to guarantee that an input is
|
||||||
|
exactly one complete JSON document.
|
||||||
|
|
||||||
|
[`operator>>`](../../api/operator_gtgt.md) follows relaxed `#!cpp std::istream` semantics instead: it parses one JSON
|
||||||
|
value and leaves the stream positioned right after it, without requiring the rest of the stream to be consumed. This is
|
||||||
|
what makes it possible to read several concatenated values from the same stream, but it also means that "a valid
|
||||||
|
document followed by trailing bytes" is accepted rather than rejected. If you are validating conformance, or need to
|
||||||
|
reject any input that is not exactly one JSON document, prefer `parse`.
|
||||||
|
|
||||||
|
When using `operator>>` to read several concatenated values this way, a value that is a number must be followed by
|
||||||
|
whitespace, because `operator>>` consumes the character that terminates a number — see the
|
||||||
|
[`operator>>` notes](../../api/operator_gtgt.md#notes) for details and examples.
|
||||||
|
|
||||||
## SAX vs. DOM parsing
|
## SAX vs. DOM parsing
|
||||||
|
|
||||||
The library offers two parsing models:
|
The library offers two parsing models:
|
||||||
|
|||||||
@@ -49,4 +49,5 @@ JSON Lines input with more than one value is treated as invalid JSON by the [`pa
|
|||||||
with a JSON Lines input does not work, because the parser will try to parse one value after the last one.
|
with a JSON Lines input does not work, because the parser will try to parse one value after the last one.
|
||||||
|
|
||||||
This is different from parsing a stream of *concatenated* (non-newline-delimited) JSON values, for which
|
This is different from parsing a stream of *concatenated* (non-newline-delimited) JSON values, for which
|
||||||
`operator>>` does work -- see its [notes](../../api/operator_gtgt.md#notes) for details.
|
`operator>>` does work, provided that a value that is a number is followed by whitespace -- see its
|
||||||
|
[notes](../../api/operator_gtgt.md#notes) for details.
|
||||||
|
|||||||
@@ -291,9 +291,10 @@ A JSON Pointer array index must be a number.
|
|||||||
|
|
||||||
### json.exception.parse_error.110
|
### json.exception.parse_error.110
|
||||||
|
|
||||||
When parsing CBOR or MessagePack, the byte vector ends before the complete value has been read.
|
When parsing a [binary format](../features/binary_formats/index.md), the byte vector ends before the complete value has
|
||||||
|
been read.
|
||||||
|
|
||||||
!!! failure "Example message"
|
!!! failure "Example messages"
|
||||||
|
|
||||||
```
|
```
|
||||||
[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input
|
[json.exception.parse_error.110] parse error at byte 5: syntax error while parsing CBOR string: unexpected end of input
|
||||||
@@ -301,6 +302,9 @@ When parsing CBOR or MessagePack, the byte vector ends before the complete value
|
|||||||
```
|
```
|
||||||
[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing UBJSON value: expected end of input; last byte: 0x5A
|
[json.exception.parse_error.110] parse error at byte 2: syntax error while parsing UBJSON value: expected end of input; last byte: 0x5A
|
||||||
```
|
```
|
||||||
|
```
|
||||||
|
[json.exception.parse_error.110] parse error at byte 8: syntax error while parsing BSON number: unexpected end of input
|
||||||
|
```
|
||||||
|
|
||||||
### json.exception.parse_error.112
|
### json.exception.parse_error.112
|
||||||
|
|
||||||
@@ -329,10 +333,14 @@ An unexpected byte was read in a [binary format](../features/binary_formats/inde
|
|||||||
```
|
```
|
||||||
[json.exception.parse_error.112] parse error at byte 9: syntax error while parsing CBOR value: negative integer overflow
|
[json.exception.parse_error.112] parse error at byte 9: syntax error while parsing CBOR value: negative integer overflow
|
||||||
```
|
```
|
||||||
|
```
|
||||||
|
[json.exception.parse_error.112] parse error at byte 5: syntax error while parsing BSON document: document size 6 does not match the number of bytes read (5)
|
||||||
|
```
|
||||||
|
|
||||||
### json.exception.parse_error.113
|
### json.exception.parse_error.113
|
||||||
|
|
||||||
While parsing a map key, a value that is not a string has been read.
|
A string could not be read from a [binary format](../features/binary_formats/index.md): either a value that is not a
|
||||||
|
string was read where one was required (for instance as a map key), or the string's length specification is invalid.
|
||||||
|
|
||||||
!!! failure "Example messages"
|
!!! failure "Example messages"
|
||||||
|
|
||||||
@@ -345,6 +353,9 @@ While parsing a map key, a value that is not a string has been read.
|
|||||||
```
|
```
|
||||||
[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON char: byte after 'C' must be in range 0x00..0x7F; last byte: 0x82
|
[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON char: byte after 'C' must be in range 0x00..0x7F; last byte: 0x82
|
||||||
```
|
```
|
||||||
|
```
|
||||||
|
[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData string: string length must not be negative
|
||||||
|
```
|
||||||
|
|
||||||
### json.exception.parse_error.114
|
### json.exception.parse_error.114
|
||||||
|
|
||||||
@@ -853,13 +864,21 @@ and this exception no longer occurs.
|
|||||||
|
|
||||||
### json.exception.out_of_range.408
|
### json.exception.out_of_range.408
|
||||||
|
|
||||||
The size (following `#`) of an UBJSON array or object exceeds the maximal capacity.
|
The size of an array or object in a [binary format](../features/binary_formats/index.md) exceeds the maximal capacity:
|
||||||
|
the size following `#` for [UBJSON](../features/binary_formats/ubjson.md)/[BJData](../features/binary_formats/bjdata.md),
|
||||||
|
or the encoded length for [CBOR](../features/binary_formats/cbor.md).
|
||||||
|
|
||||||
!!! failure "Example message"
|
!!! failure "Example messages"
|
||||||
|
|
||||||
```
|
```
|
||||||
excessive array size: 8658170730974374167
|
excessive array size: 8658170730974374167
|
||||||
```
|
```
|
||||||
|
```
|
||||||
|
[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive array size
|
||||||
|
```
|
||||||
|
```
|
||||||
|
[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive map size
|
||||||
|
```
|
||||||
|
|
||||||
### json.exception.out_of_range.409
|
### json.exception.out_of_range.409
|
||||||
|
|
||||||
|
|||||||
@@ -296,6 +296,7 @@ nav:
|
|||||||
- 'JSON_USE_GLOBAL_UDLS': api/macros/json_use_global_udls.md
|
- 'JSON_USE_GLOBAL_UDLS': api/macros/json_use_global_udls.md
|
||||||
- 'JSON_USE_IMPLICIT_CONVERSIONS': api/macros/json_use_implicit_conversions.md
|
- 'JSON_USE_IMPLICIT_CONVERSIONS': api/macros/json_use_implicit_conversions.md
|
||||||
- 'JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON': api/macros/json_use_legacy_discarded_value_comparison.md
|
- 'JSON_USE_LEGACY_DISCARDED_VALUE_COMPARISON': api/macros/json_use_legacy_discarded_value_comparison.md
|
||||||
|
- 'JSON_USE_SIMDUTF': api/macros/json_use_simdutf.md
|
||||||
- 'NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE': api/macros/nlohmann_define_derived_type.md
|
- 'NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE': api/macros/nlohmann_define_derived_type.md
|
||||||
- 'NLOHMANN_DEFINE_TYPE_INTRUSIVE, NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE': api/macros/nlohmann_define_type_intrusive.md
|
- 'NLOHMANN_DEFINE_TYPE_INTRUSIVE, NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE': api/macros/nlohmann_define_type_intrusive.md
|
||||||
- 'NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE, NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE': api/macros/nlohmann_define_type_non_intrusive.md
|
- 'NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE, NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT, NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE': api/macros/nlohmann_define_type_non_intrusive.md
|
||||||
|
|||||||
@@ -704,13 +704,15 @@ class binary_reader
|
|||||||
case 0x9A: // array (four-byte uint32_t for n follow)
|
case 0x9A: // array (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
|
std::size_t size{};
|
||||||
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9B: // array (eight-byte uint64_t for n follow)
|
case 0x9B: // array (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_array(conditional_static_cast<std::size_t>(len), tag_handler);
|
std::size_t size{};
|
||||||
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "array") && get_cbor_array(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x9F: // array (indefinite length)
|
case 0x9F: // array (indefinite length)
|
||||||
@@ -758,13 +760,15 @@ class binary_reader
|
|||||||
case 0xBA: // map (four-byte uint32_t for n follow)
|
case 0xBA: // map (four-byte uint32_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
|
std::size_t size{};
|
||||||
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBB: // map (eight-byte uint64_t for n follow)
|
case 0xBB: // map (eight-byte uint64_t for n follow)
|
||||||
{
|
{
|
||||||
std::uint64_t len{};
|
std::uint64_t len{};
|
||||||
return get_number(input_format_t::cbor, len) && get_cbor_object(conditional_static_cast<std::size_t>(len), tag_handler);
|
std::size_t size{};
|
||||||
|
return get_number(input_format_t::cbor, len) && get_cbor_container_size(len, size, "map") && get_cbor_object(size, tag_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xBF: // map (indefinite length)
|
case 0xBF: // map (indefinite length)
|
||||||
@@ -807,25 +811,37 @@ class binary_reader
|
|||||||
case 0xD8:
|
case 0xD8:
|
||||||
{
|
{
|
||||||
std::uint8_t subtype_to_ignore{};
|
std::uint8_t subtype_to_ignore{};
|
||||||
get_number(input_format_t::cbor, subtype_to_ignore);
|
if (!get_number(input_format_t::cbor, subtype_to_ignore))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 0xD9:
|
case 0xD9:
|
||||||
{
|
{
|
||||||
std::uint16_t subtype_to_ignore{};
|
std::uint16_t subtype_to_ignore{};
|
||||||
get_number(input_format_t::cbor, subtype_to_ignore);
|
if (!get_number(input_format_t::cbor, subtype_to_ignore))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 0xDA:
|
case 0xDA:
|
||||||
{
|
{
|
||||||
std::uint32_t subtype_to_ignore{};
|
std::uint32_t subtype_to_ignore{};
|
||||||
get_number(input_format_t::cbor, subtype_to_ignore);
|
if (!get_number(input_format_t::cbor, subtype_to_ignore))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 0xDB:
|
case 0xDB:
|
||||||
{
|
{
|
||||||
std::uint64_t subtype_to_ignore{};
|
std::uint64_t subtype_to_ignore{};
|
||||||
get_number(input_format_t::cbor, subtype_to_ignore);
|
if (!get_number(input_format_t::cbor, subtype_to_ignore))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -843,28 +859,40 @@ class binary_reader
|
|||||||
case 0xD8:
|
case 0xD8:
|
||||||
{
|
{
|
||||||
std::uint8_t subtype{};
|
std::uint8_t subtype{};
|
||||||
get_number(input_format_t::cbor, subtype);
|
if (!get_number(input_format_t::cbor, subtype))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
|
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 0xD9:
|
case 0xD9:
|
||||||
{
|
{
|
||||||
std::uint16_t subtype{};
|
std::uint16_t subtype{};
|
||||||
get_number(input_format_t::cbor, subtype);
|
if (!get_number(input_format_t::cbor, subtype))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
|
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 0xDA:
|
case 0xDA:
|
||||||
{
|
{
|
||||||
std::uint32_t subtype{};
|
std::uint32_t subtype{};
|
||||||
get_number(input_format_t::cbor, subtype);
|
if (!get_number(input_format_t::cbor, subtype))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
|
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 0xDB:
|
case 0xDB:
|
||||||
{
|
{
|
||||||
std::uint64_t subtype{};
|
std::uint64_t subtype{};
|
||||||
get_number(input_format_t::cbor, subtype);
|
if (!get_number(input_format_t::cbor, subtype))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
|
b.set_subtype(detail::conditional_static_cast<typename binary_t::subtype_type>(subtype));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1155,6 +1183,31 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief narrow a definite CBOR array/map length to std::size_t
|
||||||
|
|
||||||
|
A definite length is rejected if it does not fit in std::size_t or if it
|
||||||
|
equals detail::unknown_size(), which is reserved to mark an indefinite-
|
||||||
|
length container and would otherwise make the length read as indefinite.
|
||||||
|
Both cases exceed any container's max_size(), so no representable input
|
||||||
|
is affected.
|
||||||
|
|
||||||
|
@param[in] len the declared length
|
||||||
|
@param[out] result the length narrowed to std::size_t
|
||||||
|
@param[in] context "array" or "map", for the error message
|
||||||
|
@return whether the length is usable
|
||||||
|
*/
|
||||||
|
bool get_cbor_container_size(const std::uint64_t len, std::size_t& result, const char* context)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!value_in_range_of<std::size_t>(len) || len == detail::unknown_size()))
|
||||||
|
{
|
||||||
|
return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
|
||||||
|
exception_message(input_format_t::cbor, concat("excessive ", context, " size"), "size"), nullptr));
|
||||||
|
}
|
||||||
|
result = conditional_static_cast<std::size_t>(len);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@param[in] len the length of the array or detail::unknown_size() for an
|
@param[in] len the length of the array or detail::unknown_size() for an
|
||||||
array of indefinite size
|
array of indefinite size
|
||||||
@@ -2825,7 +2878,17 @@ class binary_reader
|
|||||||
case token_type::value_unsigned:
|
case token_type::value_unsigned:
|
||||||
return sax->number_unsigned(number_lexer.get_number_unsigned());
|
return sax->number_unsigned(number_lexer.get_number_unsigned());
|
||||||
case token_type::value_float:
|
case token_type::value_float:
|
||||||
return sax->number_float(number_lexer.get_number_float(), std::move(number_string));
|
{
|
||||||
|
const auto parsed_float = number_lexer.get_number_float();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!std::isfinite(parsed_float)))
|
||||||
|
{
|
||||||
|
return sax->parse_error(
|
||||||
|
chars_read,
|
||||||
|
number_string,
|
||||||
|
out_of_range::create(406, concat("number overflow parsing '", number_string, '\''), nullptr));
|
||||||
|
}
|
||||||
|
return sax->number_float(parsed_float, std::move(number_string));
|
||||||
|
}
|
||||||
case token_type::uninitialized:
|
case token_type::uninitialized:
|
||||||
case token_type::literal_true:
|
case token_type::literal_true:
|
||||||
case token_type::literal_false:
|
case token_type::literal_false:
|
||||||
|
|||||||
@@ -231,6 +231,33 @@ class iterator_input_adapter
|
|||||||
std::is_same<IteratorType, SentinelType>::value && std::is_pointer<IteratorType>::value;
|
std::is_same<IteratorType, SentinelType>::value && std::is_pointer<IteratorType>::value;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Whether the remaining input is a single contiguous block of 1-byte
|
||||||
|
// elements that the lexer can inspect directly (used for the SWAR string
|
||||||
|
// fast path). Restricted to same-type iterator/sentinel pairs so that plain
|
||||||
|
// std::distance/std::advance are well-defined in all standards.
|
||||||
|
static constexpr bool supports_bulk_scan =
|
||||||
|
iterator_is_contiguous && std::is_same<IteratorType, SentinelType>::value && sizeof(char_type) == 1;
|
||||||
|
|
||||||
|
// Pointer to the next unread element; only valid when bulk_remaining() > 0.
|
||||||
|
const char_type* bulk_data() const
|
||||||
|
{
|
||||||
|
return &*current;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Number of unread elements available as one contiguous block.
|
||||||
|
std::size_t bulk_remaining() const
|
||||||
|
{
|
||||||
|
return static_cast<std::size_t>(std::distance(current, end));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Consume @a n elements previously inspected via bulk_data().
|
||||||
|
void bulk_skip(std::size_t n)
|
||||||
|
{
|
||||||
|
std::advance(current, static_cast<typename std::iterator_traits<IteratorType>::difference_type>(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
// contiguous fast path: bulk copy the remaining range with std::memcpy
|
// contiguous fast path: bulk copy the remaining range with std::memcpy
|
||||||
template<class T>
|
template<class T>
|
||||||
std::size_t get_elements_impl(T* dest, std::size_t count, std::true_type /*contiguous*/)
|
std::size_t get_elements_impl(T* dest, std::size_t count, std::true_type /*contiguous*/)
|
||||||
@@ -566,6 +593,24 @@ typename iterator_input_adapter_factory<IteratorType, SentinelType>::adapter_typ
|
|||||||
return factory_type::create(first, last);
|
return factory_type::create(first, last);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detect a container that stores its elements contiguously as single bytes
|
||||||
|
// (std::string, std::vector<char/unsigned char>, std::array<char, N>,
|
||||||
|
// std::string_view, ...). Such inputs are wrapped in a pointer-based adapter so
|
||||||
|
// they benefit from the contiguous fast paths (bulk string scanning, memcpy for
|
||||||
|
// binary formats) in every C++ standard - not only in C++20, where the standard
|
||||||
|
// library iterators model std::contiguous_iterator and are detected directly.
|
||||||
|
template<typename ContainerType, typename = void>
|
||||||
|
struct is_contiguous_byte_container : std::false_type {};
|
||||||
|
|
||||||
|
template<typename ContainerType>
|
||||||
|
struct is_contiguous_byte_container < ContainerType, void_t <
|
||||||
|
decltype(std::declval<const ContainerType&>().data()),
|
||||||
|
decltype(std::declval<const ContainerType&>().size()) >>
|
||||||
|
: std::integral_constant < bool,
|
||||||
|
std::is_pointer<decltype(std::declval<const ContainerType&>().data())>::value&&
|
||||||
|
std::is_integral<typename std::remove_pointer<decltype(std::declval<const ContainerType&>().data())>::type>::value&&
|
||||||
|
sizeof(typename std::remove_pointer<decltype(std::declval<const ContainerType&>().data())>::type) == 1 > {};
|
||||||
|
|
||||||
// Convenience shorthand from container to iterator
|
// Convenience shorthand from container to iterator
|
||||||
// Enables ADL on begin(container) and end(container)
|
// Enables ADL on begin(container) and end(container)
|
||||||
// Encloses the using declarations in namespace for not to leak them to outside scope
|
// Encloses the using declarations in namespace for not to leak them to outside scope
|
||||||
@@ -593,12 +638,32 @@ struct container_input_adapter_factory< ContainerType,
|
|||||||
|
|
||||||
} // namespace container_input_adapter_factory_impl
|
} // namespace container_input_adapter_factory_impl
|
||||||
|
|
||||||
template<typename ContainerType>
|
// General container path (iterator-based). Contiguous single-byte containers
|
||||||
typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(ContainerType&& container)
|
// are excluded here and routed through the pointer-based overload below.
|
||||||
|
template < typename ContainerType,
|
||||||
|
enable_if_t < !is_contiguous_byte_container<ContainerType>::value, int > = 0 >
|
||||||
|
typename container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::adapter_type input_adapter(ContainerType && container)
|
||||||
{
|
{
|
||||||
return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(std::forward<ContainerType>(container));
|
return container_input_adapter_factory_impl::container_input_adapter_factory<ContainerType>::create(std::forward<ContainerType>(container));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Contiguous single-byte containers (std::string, std::vector<char>, ...) are
|
||||||
|
// wrapped in a pointer-based adapter so the contiguous fast paths apply in every
|
||||||
|
// standard. The pointer keeps the container's own element type (const char* for
|
||||||
|
// std::string, const std::uint8_t* for std::vector<std::uint8_t>, ...), so the
|
||||||
|
// resulting char_type - and therefore the parsing behavior - is byte-for-byte
|
||||||
|
// identical to the iterator-based path; only the raw pointer additionally
|
||||||
|
// enables the bulk fast paths. The container outlives the adapter for the whole
|
||||||
|
// parse (temporaries live until the end of the full expression), exactly as the
|
||||||
|
// iterators it replaces did.
|
||||||
|
template < typename ContainerType,
|
||||||
|
enable_if_t < is_contiguous_byte_container<ContainerType>::value, int > = 0 >
|
||||||
|
auto input_adapter(const ContainerType& container)
|
||||||
|
-> decltype(input_adapter(container.data(), container.data() + container.size()))
|
||||||
|
{
|
||||||
|
return input_adapter(container.data(), container.data() + container.size());
|
||||||
|
}
|
||||||
|
|
||||||
// specialization for std::string
|
// specialization for std::string
|
||||||
using string_input_adapter_type = decltype(input_adapter(std::declval<std::string>()));
|
using string_input_adapter_type = decltype(input_adapter(std::declval<std::string>()));
|
||||||
|
|
||||||
|
|||||||
@@ -626,14 +626,7 @@ class json_sax_dom_callback_parser
|
|||||||
if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
|
if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_structured())
|
||||||
{
|
{
|
||||||
// remove discarded value
|
// remove discarded value
|
||||||
for (auto it = ref_stack.back()->begin(); it != ref_stack.back()->end(); ++it)
|
remove_discarded_value(*ref_stack.back());
|
||||||
{
|
|
||||||
if (it->is_discarded())
|
|
||||||
{
|
|
||||||
ref_stack.back()->erase(it);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -674,8 +667,9 @@ class json_sax_dom_callback_parser
|
|||||||
bool end_array()
|
bool end_array()
|
||||||
{
|
{
|
||||||
bool keep = true;
|
bool keep = true;
|
||||||
|
const bool stored = ref_stack.back() != nullptr;
|
||||||
|
|
||||||
if (ref_stack.back())
|
if (stored)
|
||||||
{
|
{
|
||||||
keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
|
keep = callback(static_cast<int>(ref_stack.size()) - 1, parse_event_t::array_end, *ref_stack.back());
|
||||||
if (keep)
|
if (keep)
|
||||||
@@ -709,9 +703,19 @@ class json_sax_dom_callback_parser
|
|||||||
keep_stack.pop_back();
|
keep_stack.pop_back();
|
||||||
|
|
||||||
// remove discarded value
|
// remove discarded value
|
||||||
if (!keep && !ref_stack.empty() && ref_stack.back()->is_array())
|
if (!ref_stack.empty() && ref_stack.back())
|
||||||
{
|
{
|
||||||
ref_stack.back()->m_data.m_value.array->pop_back();
|
if (!keep && ref_stack.back()->is_array())
|
||||||
|
{
|
||||||
|
ref_stack.back()->m_data.m_value.array->pop_back();
|
||||||
|
}
|
||||||
|
else if ((!keep || !stored) && ref_stack.back()->is_object())
|
||||||
|
{
|
||||||
|
// the array is either still stored under its key or was never
|
||||||
|
// stored, leaving the placeholder key() wrote; both show up as
|
||||||
|
// a discarded member of the parent object
|
||||||
|
remove_discarded_value(*ref_stack.back());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -801,6 +805,19 @@ class json_sax_dom_callback_parser
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/// remove the discarded value the callback rejected from its parent
|
||||||
|
static void remove_discarded_value(BasicJsonType& parent)
|
||||||
|
{
|
||||||
|
for (auto it = parent.begin(); it != parent.end(); ++it)
|
||||||
|
{
|
||||||
|
if (it->is_discarded())
|
||||||
|
{
|
||||||
|
parent.erase(it);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@param[in] v value to add to the JSON value we build during parsing
|
@param[in] v value to add to the JSON value we build during parsing
|
||||||
@param[in] skip_callback whether we should skip calling the callback
|
@param[in] skip_callback whether we should skip calling the callback
|
||||||
@@ -841,6 +858,18 @@ class json_sax_dom_callback_parser
|
|||||||
// do not handle this value if we just learnt it shall be discarded
|
// do not handle this value if we just learnt it shall be discarded
|
||||||
if (!keep)
|
if (!keep)
|
||||||
{
|
{
|
||||||
|
// if the value was to become an object member, key() already
|
||||||
|
// stored a placeholder for it that has to be removed again
|
||||||
|
if (!ref_stack.empty() && ref_stack.back() && ref_stack.back()->is_object())
|
||||||
|
{
|
||||||
|
JSON_ASSERT(!key_keep_stack.empty());
|
||||||
|
const bool placeholder_stored = key_keep_stack.back();
|
||||||
|
key_keep_stack.pop_back();
|
||||||
|
if (placeholder_stored)
|
||||||
|
{
|
||||||
|
remove_discarded_value(*ref_stack.back());
|
||||||
|
}
|
||||||
|
}
|
||||||
return {false, nullptr};
|
return {false, nullptr};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,9 @@
|
|||||||
#include <vector> // vector
|
#include <vector> // vector
|
||||||
|
|
||||||
#include <nlohmann/detail/input/input_adapters.hpp>
|
#include <nlohmann/detail/input/input_adapters.hpp>
|
||||||
|
#include <nlohmann/detail/input/number_parse.hpp>
|
||||||
#include <nlohmann/detail/input/position_t.hpp>
|
#include <nlohmann/detail/input/position_t.hpp>
|
||||||
|
#include <nlohmann/detail/input/string_scan.hpp>
|
||||||
#include <nlohmann/detail/macro_scope.hpp>
|
#include <nlohmann/detail/macro_scope.hpp>
|
||||||
#include <nlohmann/detail/meta/type_traits.hpp>
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
||||||
|
|
||||||
@@ -125,6 +127,25 @@ constexpr bool input_adapter_supports_seek(std::false_type /*detected*/)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detect whether an input adapter exposes a contiguous byte block that the
|
||||||
|
// lexer can scan directly (see iterator_input_adapter::supports_bulk_scan).
|
||||||
|
// Adapters without the flag - file, stream, wide-string, user-defined - fall
|
||||||
|
// back to the character-at-a-time string scanner.
|
||||||
|
template<typename InputAdapterType>
|
||||||
|
using detect_supports_bulk_scan = decltype(InputAdapterType::supports_bulk_scan);
|
||||||
|
|
||||||
|
template<typename InputAdapterType>
|
||||||
|
constexpr bool input_adapter_supports_bulk_scan(std::true_type /*detected*/)
|
||||||
|
{
|
||||||
|
return InputAdapterType::supports_bulk_scan;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename InputAdapterType>
|
||||||
|
constexpr bool input_adapter_supports_bulk_scan(std::false_type /*detected*/)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief lexical analysis
|
@brief lexical analysis
|
||||||
|
|
||||||
@@ -146,6 +167,14 @@ class lexer : public lexer_base<BasicJsonType>
|
|||||||
static constexpr bool lazy_token_string =
|
static constexpr bool lazy_token_string =
|
||||||
input_adapter_supports_seek<InputAdapterType>(is_detected<detect_supports_seek, InputAdapterType> {});
|
input_adapter_supports_seek<InputAdapterType>(is_detected<detect_supports_seek, InputAdapterType> {});
|
||||||
|
|
||||||
|
/// whether string scanning may bulk-consume runs of ordinary characters
|
||||||
|
/// directly from a contiguous input buffer (SWAR fast path). This requires
|
||||||
|
/// the token to be reconstructible lazily (lazy_token_string), so bypassing
|
||||||
|
/// the per-character capture in get() cannot lose error diagnostics.
|
||||||
|
static constexpr bool bulk_scan =
|
||||||
|
lazy_token_string
|
||||||
|
&& input_adapter_supports_bulk_scan<InputAdapterType>(is_detected<detect_supports_bulk_scan, InputAdapterType> {});
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using token_type = typename lexer_base<BasicJsonType>::token_type;
|
using token_type = typename lexer_base<BasicJsonType>::token_type;
|
||||||
|
|
||||||
@@ -265,6 +294,40 @@ class lexer : public lexer_base<BasicJsonType>
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// contiguous input: bulk-append the run of ordinary characters and complete
|
||||||
|
/// well-formed UTF-8 sequences starting at the current read position, leaving
|
||||||
|
/// the first byte that needs individual handling (the closing quote, an
|
||||||
|
/// escape, a control character, or an ill-formed UTF-8 byte) for get()
|
||||||
|
void scan_string_bulk(std::true_type /*bulk*/)
|
||||||
|
{
|
||||||
|
// a pending unget must be consumed through the normal path first
|
||||||
|
if (next_unget)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const std::size_t remaining = ia.bulk_remaining();
|
||||||
|
if (remaining == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
|
|
||||||
|
const std::size_t pos = string_bulk_run(data, remaining);
|
||||||
|
if (pos == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
token_buffer.append(reinterpret_cast<const typename string_t::value_type*>(data), pos);
|
||||||
|
ia.bulk_skip(pos);
|
||||||
|
// the run contains no newline (all bytes < 0x20 are treated as special),
|
||||||
|
// so only the flat character counters advance
|
||||||
|
position.chars_read_total += pos;
|
||||||
|
position.chars_read_current_line += pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// streaming input: no bulk fast path
|
||||||
|
void scan_string_bulk(std::false_type /*bulk*/) const noexcept {}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief scan a string literal
|
@brief scan a string literal
|
||||||
|
|
||||||
@@ -290,6 +353,10 @@ class lexer : public lexer_base<BasicJsonType>
|
|||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
// bulk-consume ordinary characters from contiguous input, then
|
||||||
|
// handle the next special byte through the switch below
|
||||||
|
scan_string_bulk(std::integral_constant<bool, bulk_scan> {});
|
||||||
|
|
||||||
// get the next character
|
// get the next character
|
||||||
switch (get())
|
switch (get())
|
||||||
{
|
{
|
||||||
@@ -1279,45 +1346,56 @@ scan_number_done:
|
|||||||
// we are done scanning a number)
|
// we are done scanning a number)
|
||||||
unget();
|
unget();
|
||||||
|
|
||||||
char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
|
return convert_number(number_type);
|
||||||
errno = 0;
|
}
|
||||||
|
|
||||||
// try to parse integers first and fall back to floats
|
/*!
|
||||||
|
@brief convert the number text in token_buffer to its value and token type
|
||||||
|
|
||||||
|
The digit sequence in token_buffer has already been validated (by the
|
||||||
|
scan_number() state machine or by the contiguous fast path) and holds the
|
||||||
|
locale decimal point in place of '.'. Integers are parsed first and fall
|
||||||
|
back to floating point on overflow. This is shared so both scanners produce
|
||||||
|
identical results.
|
||||||
|
*/
|
||||||
|
token_type convert_number(token_type number_type)
|
||||||
|
{
|
||||||
|
const char* const num_begin = token_buffer.data();
|
||||||
|
const char* const num_end = num_begin + token_buffer.size();
|
||||||
|
|
||||||
|
// try to parse integers first and fall back to floats; the digit
|
||||||
|
// sequence has already been validated, so a dedicated parser can avoid
|
||||||
|
// the locale/errno overhead of strtoull
|
||||||
if (number_type == token_type::value_unsigned)
|
if (number_type == token_type::value_unsigned)
|
||||||
{
|
{
|
||||||
const auto x = std::strtoull(token_buffer.data(), &endptr, 10);
|
if (parse_integer_unsigned(num_begin, num_end, value_unsigned))
|
||||||
|
|
||||||
// we checked the number format before
|
|
||||||
JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
|
|
||||||
|
|
||||||
if (errno != ERANGE)
|
|
||||||
{
|
{
|
||||||
value_unsigned = static_cast<number_unsigned_t>(x);
|
return token_type::value_unsigned;
|
||||||
if (value_unsigned == x)
|
|
||||||
{
|
|
||||||
return token_type::value_unsigned;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (number_type == token_type::value_integer)
|
else if (number_type == token_type::value_integer)
|
||||||
{
|
{
|
||||||
const auto x = std::strtoll(token_buffer.data(), &endptr, 10);
|
if (parse_integer_signed(num_begin, num_end, value_integer))
|
||||||
|
|
||||||
// we checked the number format before
|
|
||||||
JSON_ASSERT(endptr == token_buffer.data() + token_buffer.size());
|
|
||||||
|
|
||||||
if (errno != ERANGE)
|
|
||||||
{
|
{
|
||||||
value_integer = static_cast<number_integer_t>(x);
|
return token_type::value_integer;
|
||||||
if (value_integer == x)
|
|
||||||
{
|
|
||||||
return token_type::value_integer;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// this code is reached if we parse a floating-point number or if an
|
// this code is reached if we parse a floating-point number or if an
|
||||||
// integer conversion above failed
|
// integer conversion above overflowed. Prefer std::from_chars
|
||||||
|
// (Eisel-Lemire, locale-independent, correctly rounded) when available;
|
||||||
|
// otherwise the exact Clinger fast path (double only); otherwise the
|
||||||
|
// locale-aware strtof/strtod.
|
||||||
|
if (parse_float_from_chars(num_begin, num_end, value_float))
|
||||||
|
{
|
||||||
|
return token_type::value_float;
|
||||||
|
}
|
||||||
|
if (parse_float_fast(num_begin, num_end, decimal_point_char, value_float))
|
||||||
|
{
|
||||||
|
return token_type::value_float;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
|
||||||
strtof(value_float, token_buffer.data(), &endptr);
|
strtof(value_float, token_buffer.data(), &endptr);
|
||||||
|
|
||||||
// we checked the number format before
|
// we checked the number format before
|
||||||
@@ -1326,6 +1404,130 @@ scan_number_done:
|
|||||||
return token_type::value_float;
|
return token_type::value_float;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief contiguous fast path for scanning a number
|
||||||
|
|
||||||
|
Parses the whole number token straight from the input buffer, avoiding the
|
||||||
|
per-character get()/add() of scan_number(). On success it fills token_buffer
|
||||||
|
(with the locale decimal point substituted, as scan_number() does) and
|
||||||
|
returns the token type. On anything it does not fully recognize as a
|
||||||
|
well-formed number it makes no state change and returns
|
||||||
|
token_type::uninitialized, so the caller falls back to scan_number(), which
|
||||||
|
then produces the exact diagnostic. @a current is the first digit or the
|
||||||
|
leading minus (already read); the remaining bytes are taken from the adapter.
|
||||||
|
*/
|
||||||
|
token_type scan_number_bulk_contiguous()
|
||||||
|
{
|
||||||
|
// a pending unget offsets the buffer position from current; fall back
|
||||||
|
if (next_unget)
|
||||||
|
{
|
||||||
|
return token_type::uninitialized;
|
||||||
|
}
|
||||||
|
const std::size_t rem = ia.bulk_remaining();
|
||||||
|
if (rem == 0)
|
||||||
|
{
|
||||||
|
// the first digit is the last input byte; let scan_number() finish
|
||||||
|
return token_type::uninitialized;
|
||||||
|
}
|
||||||
|
// the byte before the next unread one is current (contiguous input)
|
||||||
|
const char* const data = reinterpret_cast<const char*>(ia.bulk_data()) - 1;
|
||||||
|
const std::size_t avail = rem + 1;
|
||||||
|
|
||||||
|
// validate + classify the number extent (mirrors scan_number()'s grammar)
|
||||||
|
std::size_t i = 0;
|
||||||
|
std::size_t dot_index = std::string::npos;
|
||||||
|
token_type number_type = token_type::value_unsigned;
|
||||||
|
if (data[0] == '-')
|
||||||
|
{
|
||||||
|
number_type = token_type::value_integer;
|
||||||
|
i = 1;
|
||||||
|
if (i >= avail)
|
||||||
|
{
|
||||||
|
return token_type::uninitialized;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (data[i] == '0')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
else if (data[i] >= '1' && data[i] <= '9')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
while (i < avail && data[i] >= '0' && data[i] <= '9')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return token_type::uninitialized;
|
||||||
|
}
|
||||||
|
if (i < avail && data[i] == '.')
|
||||||
|
{
|
||||||
|
number_type = token_type::value_float;
|
||||||
|
dot_index = i;
|
||||||
|
++i;
|
||||||
|
if (i >= avail || !(data[i] >= '0' && data[i] <= '9'))
|
||||||
|
{
|
||||||
|
return token_type::uninitialized;
|
||||||
|
}
|
||||||
|
while (i < avail && data[i] >= '0' && data[i] <= '9')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (i < avail && (data[i] == 'e' || data[i] == 'E'))
|
||||||
|
{
|
||||||
|
number_type = token_type::value_float;
|
||||||
|
++i;
|
||||||
|
if (i < avail && (data[i] == '+' || data[i] == '-'))
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
if (i >= avail || !(data[i] >= '0' && data[i] <= '9'))
|
||||||
|
{
|
||||||
|
return token_type::uninitialized;
|
||||||
|
}
|
||||||
|
while (i < avail && data[i] >= '0' && data[i] <= '9')
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const std::size_t len = i;
|
||||||
|
|
||||||
|
// materialize the token exactly as scan_number() would, substituting the
|
||||||
|
// locale decimal point so convert_number()'s strtof fallback stays valid.
|
||||||
|
// reset() already cleared token_buffer, so append() fills it (assign() is
|
||||||
|
// avoided because custom string_t types need not provide it)
|
||||||
|
reset();
|
||||||
|
token_buffer.append(reinterpret_cast<const typename string_t::value_type*>(data), len);
|
||||||
|
if (dot_index != std::string::npos)
|
||||||
|
{
|
||||||
|
token_buffer[dot_index] = static_cast<typename string_t::value_type>(decimal_point_char);
|
||||||
|
decimal_point_position = dot_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
// consume the remaining bytes of the number (current was already read)
|
||||||
|
ia.bulk_skip(len - 1);
|
||||||
|
position.chars_read_total += (len - 1);
|
||||||
|
position.chars_read_current_line += (len - 1);
|
||||||
|
|
||||||
|
return convert_number(number_type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// contiguous input: try the number fast path, else the byte-path scanner
|
||||||
|
token_type scan_number_dispatch(std::true_type /*bulk*/)
|
||||||
|
{
|
||||||
|
const token_type t = scan_number_bulk_contiguous();
|
||||||
|
return (t != token_type::uninitialized) ? t : scan_number();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// streaming input: always use the byte-path scanner
|
||||||
|
token_type scan_number_dispatch(std::false_type /*bulk*/)
|
||||||
|
{
|
||||||
|
return scan_number();
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@param[in] literal_text the literal text to expect
|
@param[in] literal_text the literal text to expect
|
||||||
@param[in] length the length of the passed literal text
|
@param[in] length the length of the passed literal text
|
||||||
@@ -1680,7 +1882,7 @@ scan_number_done:
|
|||||||
case '7':
|
case '7':
|
||||||
case '8':
|
case '8':
|
||||||
case '9':
|
case '9':
|
||||||
return scan_number();
|
return scan_number_dispatch(std::integral_constant<bool, bulk_scan> {});
|
||||||
|
|
||||||
// end of input (the null byte is needed when parsing from
|
// end of input (the null byte is needed when parsing from
|
||||||
// string literals)
|
// string literals)
|
||||||
|
|||||||
@@ -0,0 +1,302 @@
|
|||||||
|
// __ _____ _____ _____
|
||||||
|
// __| | __| | | | JSON for Modern C++
|
||||||
|
// | | |__ | | | | | | version 3.12.0
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <array> // array
|
||||||
|
#include <cfloat> // FLT_EVAL_METHOD
|
||||||
|
#include <cstddef> // size_t
|
||||||
|
#include <cstdint> // int64_t, uint64_t
|
||||||
|
#include <limits> // numeric_limits
|
||||||
|
|
||||||
|
#include <nlohmann/detail/macro_scope.hpp>
|
||||||
|
|
||||||
|
// std::from_chars lives in <charconv>, but being in C++17 mode does not
|
||||||
|
// guarantee the header exists: GCC 7 sets __cplusplus to C++17 yet ships no
|
||||||
|
// <charconv> (added in GCC 8; floating-point support in GCC 11). Guard the
|
||||||
|
// include with __has_include so such toolchains fall back to the scalar path.
|
||||||
|
#if defined(JSON_HAS_CPP_17) && defined(__has_include)
|
||||||
|
#if __has_include(<charconv>)
|
||||||
|
#include <charconv> // from_chars (only used when __cpp_lib_to_chars is defined)
|
||||||
|
#include <system_error> // errc
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// This file contains the value-conversion helpers used by the lexer to turn an
|
||||||
|
// already-validated number token into a value, without the locale/errno
|
||||||
|
// overhead of std::strtoull/std::strtod. They are free functions so the lexer
|
||||||
|
// stays focused on scanning; see lexer::convert_number().
|
||||||
|
|
||||||
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief fast integer parser for an already-validated unsigned integer
|
||||||
|
|
||||||
|
The number scanner has already checked that [first, last) is a valid JSON
|
||||||
|
integer, so this only needs to accumulate the digits and detect overflow. This
|
||||||
|
avoids the locale/errno machinery of std::strtoull, which dominates
|
||||||
|
integer-heavy inputs.
|
||||||
|
|
||||||
|
@param[in] first pointer to the first character (a digit)
|
||||||
|
@param[in] last pointer past the last character
|
||||||
|
@param[out] value the parsed value on success
|
||||||
|
@return true if the value fit into @a NumberUnsignedType; false on overflow, in
|
||||||
|
which case the caller falls back to floating-point parsing (matching the
|
||||||
|
previous std::strtoull behavior)
|
||||||
|
*/
|
||||||
|
template<typename NumberUnsignedType>
|
||||||
|
bool parse_integer_unsigned(const char* first, const char* last, NumberUnsignedType& value) noexcept
|
||||||
|
{
|
||||||
|
// accumulate in the widest unsigned type used by the previous strtoull
|
||||||
|
// path so the overflow behavior is unchanged for custom number types
|
||||||
|
std::uint64_t x = 0;
|
||||||
|
constexpr std::uint64_t cutoff = (std::numeric_limits<std::uint64_t>::max)() / 10u;
|
||||||
|
constexpr std::uint64_t cutlim = (std::numeric_limits<std::uint64_t>::max)() % 10u;
|
||||||
|
for (const char* p = first; p != last; ++p)
|
||||||
|
{
|
||||||
|
const auto digit = static_cast<std::uint64_t>(static_cast<unsigned char>(*p) - static_cast<unsigned char>('0'));
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(x > cutoff || (x == cutoff && digit > cutlim)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
x = (x * 10u) + digit;
|
||||||
|
}
|
||||||
|
value = static_cast<NumberUnsignedType>(x);
|
||||||
|
// reject values that do not round-trip into a narrower NumberUnsignedType
|
||||||
|
return static_cast<std::uint64_t>(value) == x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief fast integer parser for an already-validated negative integer
|
||||||
|
|
||||||
|
@param[in] first pointer to the leading '-'
|
||||||
|
@param[in] last pointer past the last character
|
||||||
|
@param[out] value the parsed (negative) value on success
|
||||||
|
@return true on success; false on overflow (caller falls back to float)
|
||||||
|
*/
|
||||||
|
template<typename NumberIntegerType>
|
||||||
|
bool parse_integer_signed(const char* first, const char* last, NumberIntegerType& value) noexcept
|
||||||
|
{
|
||||||
|
// the state machine only reaches the signed path via a leading '-'
|
||||||
|
JSON_ASSERT(first != last && *first == '-');
|
||||||
|
std::uint64_t magnitude = 0;
|
||||||
|
// |INT64_MIN| == INT64_MAX + 1; this is the largest admissible magnitude
|
||||||
|
constexpr std::uint64_t limit = static_cast<std::uint64_t>((std::numeric_limits<std::int64_t>::max)()) + 1u;
|
||||||
|
for (const char* p = first + 1; p != last; ++p)
|
||||||
|
{
|
||||||
|
const auto digit = static_cast<std::uint64_t>(static_cast<unsigned char>(*p) - static_cast<unsigned char>('0'));
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(magnitude > (limit - digit) / 10u))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
magnitude = (magnitude * 10u) + digit;
|
||||||
|
}
|
||||||
|
const std::int64_t x = (magnitude == limit)
|
||||||
|
? (std::numeric_limits<std::int64_t>::min)()
|
||||||
|
: -static_cast<std::int64_t>(magnitude);
|
||||||
|
value = static_cast<NumberIntegerType>(x);
|
||||||
|
// reject values that do not round-trip into a narrower NumberIntegerType
|
||||||
|
return static_cast<std::int64_t>(value) == x;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief exact fast path for parsing a `double` (Clinger's algorithm)
|
||||||
|
|
||||||
|
For the common case - at most 19 significant digits, a decimal exponent in
|
||||||
|
[-22, 22], and a significand below 2^53 - the value equals significand *
|
||||||
|
10^exp computed in IEEE-754 double arithmetic, which is exact under
|
||||||
|
round-to-nearest because both operands are exactly representable. This is the
|
||||||
|
same fast path used by fast_float/simdjson; the general cases are left to
|
||||||
|
std::strtod. The parser only activates for number_float_t == double; float and
|
||||||
|
long double keep the std::strtof/std::strtold paths (see the templated overload
|
||||||
|
below).
|
||||||
|
|
||||||
|
@param[in] first pointer to the first character of the number
|
||||||
|
@param[in] last pointer past the last character
|
||||||
|
@param[in] decimal_point the (locale-dependent) decimal point character
|
||||||
|
@param[out] out the parsed value on success
|
||||||
|
@return true if the value was parsed exactly; false to fall back to strtod
|
||||||
|
*/
|
||||||
|
template<typename DecimalPointType>
|
||||||
|
bool parse_float_fast(const char* first, const char* last, DecimalPointType decimal_point, double& out) noexcept
|
||||||
|
{
|
||||||
|
#if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD != 0
|
||||||
|
// Clinger's fast path is only exact when double operations are evaluated in
|
||||||
|
// true double precision. On platforms that keep intermediates in extended
|
||||||
|
// precision (e.g. the x87 FPU on 32-bit x86, where FLT_EVAL_METHOD == 2) the
|
||||||
|
// single significand * 10^scale step is double-rounded and can be 1 ULP off,
|
||||||
|
// so decline and let the caller fall back to the correctly-rounded
|
||||||
|
// std::from_chars / std::strtod path.
|
||||||
|
static_cast<void>(first);
|
||||||
|
static_cast<void>(last);
|
||||||
|
static_cast<void>(decimal_point);
|
||||||
|
static_cast<void>(out);
|
||||||
|
return false;
|
||||||
|
#else
|
||||||
|
static const std::array<double, 23> powers_of_ten =
|
||||||
|
{
|
||||||
|
{
|
||||||
|
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11,
|
||||||
|
1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const char* p = first;
|
||||||
|
bool negative = false;
|
||||||
|
if (p != last && (*p == '-' || *p == '+'))
|
||||||
|
{
|
||||||
|
negative = (*p == '-');
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint64_t significand = 0;
|
||||||
|
int num_digits = 0;
|
||||||
|
int fractional_digits = 0;
|
||||||
|
bool seen_dot = false;
|
||||||
|
bool any_digit = false;
|
||||||
|
for (; p != last; ++p)
|
||||||
|
{
|
||||||
|
const char c = *p;
|
||||||
|
if (c >= '0' && c <= '9')
|
||||||
|
{
|
||||||
|
any_digit = true;
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(num_digits >= 19))
|
||||||
|
{
|
||||||
|
return false; // significand may not fit into uint64_t
|
||||||
|
}
|
||||||
|
significand = (significand * 10u) + static_cast<std::uint64_t>(c - '0');
|
||||||
|
++num_digits;
|
||||||
|
fractional_digits += static_cast<int>(seen_dot);
|
||||||
|
}
|
||||||
|
else if (static_cast<DecimalPointType>(c) == decimal_point)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(seen_dot))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
seen_dot = true;
|
||||||
|
}
|
||||||
|
else if (c == 'e' || c == 'E')
|
||||||
|
{
|
||||||
|
++p;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!any_digit))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int exponent = 0;
|
||||||
|
if (p != last) // an exponent part remains
|
||||||
|
{
|
||||||
|
bool exp_negative = false;
|
||||||
|
if (p != last && (*p == '-' || *p == '+'))
|
||||||
|
{
|
||||||
|
exp_negative = (*p == '-');
|
||||||
|
++p;
|
||||||
|
}
|
||||||
|
bool any_exp_digit = false;
|
||||||
|
for (; p != last; ++p)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(*p < '0' || *p > '9'))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
exponent = (exponent * 10) + (*p - '0');
|
||||||
|
any_exp_digit = true;
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(exponent > 9999))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!any_exp_digit))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (exp_negative)
|
||||||
|
{
|
||||||
|
exponent = -exponent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const int scale = exponent - fractional_digits;
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(significand >= (static_cast<std::uint64_t>(1) << 53)))
|
||||||
|
{
|
||||||
|
return false; // significand not exactly representable as double
|
||||||
|
}
|
||||||
|
|
||||||
|
auto result = static_cast<double>(significand);
|
||||||
|
if (scale >= 0)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(scale > 22))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result *= powers_of_ten[static_cast<std::size_t>(scale)];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(-scale > 22))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
result /= powers_of_ten[static_cast<std::size_t>(-scale)];
|
||||||
|
}
|
||||||
|
out = negative ? -result : result;
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
/// fast float path is only exact for `double`; decline for float/long double
|
||||||
|
template<typename DecimalPointType, typename FloatType>
|
||||||
|
bool parse_float_fast(const char* /*first*/, const char* /*last*/, DecimalPointType /*decimal_point*/, FloatType& /*out*/) noexcept
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief parse a float with std::from_chars (Eisel-Lemire) when available
|
||||||
|
|
||||||
|
std::from_chars is locale-independent, correctly rounded, and - via the
|
||||||
|
Eisel-Lemire algorithm in modern standard libraries - much faster than strtod
|
||||||
|
over the whole value range (not just the Clinger subset). It is used only when
|
||||||
|
__cpp_lib_to_chars indicates full floating-point support and only when it
|
||||||
|
consumes the entire token ([first, last)); a partial parse means the buffer
|
||||||
|
uses a non-'.' locale decimal point, in which case the caller falls back to the
|
||||||
|
locale-aware path. An under-/overflow (result_out_of_range) also declines, so
|
||||||
|
the caller's strtod fallback supplies the well-defined ±inf/0 result the parser
|
||||||
|
expects (side-stepping the P4168 divergence between implementations).
|
||||||
|
|
||||||
|
@return true if the value was parsed exactly and fully; false to fall back
|
||||||
|
*/
|
||||||
|
template<typename FloatType>
|
||||||
|
bool parse_float_from_chars(const char* first, const char* last, FloatType& out) noexcept
|
||||||
|
{
|
||||||
|
// JSON_HAS_CPP_17 must gate the use as well as the <charconv> include above:
|
||||||
|
// some standard libraries (e.g. libstdc++ 15) define __cpp_lib_to_chars even
|
||||||
|
// in C++14 mode, where <charconv> is not included.
|
||||||
|
#if defined(JSON_HAS_CPP_17) && defined(__cpp_lib_to_chars)
|
||||||
|
const auto result = std::from_chars(first, last, out);
|
||||||
|
return result.ec == std::errc() && result.ptr == last;
|
||||||
|
#else
|
||||||
|
static_cast<void>(first);
|
||||||
|
static_cast<void>(last);
|
||||||
|
static_cast<void>(out);
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
NLOHMANN_JSON_NAMESPACE_END
|
||||||
@@ -0,0 +1,289 @@
|
|||||||
|
// __ _____ _____ _____
|
||||||
|
// __| | __| | | | JSON for Modern C++
|
||||||
|
// | | |__ | | | | | | version 3.12.0
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstddef> // size_t
|
||||||
|
#include <cstdint> // uint64_t
|
||||||
|
#include <cstring> // memcpy
|
||||||
|
|
||||||
|
#if defined(JSON_USE_SIMDUTF)
|
||||||
|
// Optional SIMD backend for bulk UTF-8 validation. This is an opt-in
|
||||||
|
// external dependency: nlohmann/json itself stays header-only and the C++11
|
||||||
|
// scalar validator below is always available; defining JSON_USE_SIMDUTF
|
||||||
|
// additionally requires the simdutf headers on the include path and linking
|
||||||
|
// the simdutf library. See string_bulk_run().
|
||||||
|
#include <simdutf.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <nlohmann/detail/macro_scope.hpp>
|
||||||
|
|
||||||
|
// This file contains the byte-level string-scanning helpers used by the lexer's
|
||||||
|
// contiguous fast path. They operate purely on raw bytes (no dependency on the
|
||||||
|
// lexer's template parameters) so they are free functions, keeping the lexer
|
||||||
|
// itself focused on the state machine; see lexer::scan_string_bulk().
|
||||||
|
|
||||||
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||||
|
namespace detail
|
||||||
|
{
|
||||||
|
|
||||||
|
// classify a single byte as needing individual string handling: the closing
|
||||||
|
// quote, an escape, a control character, or a non-ASCII (UTF-8)
|
||||||
|
// lead/continuation byte. Ordinary bytes (0x20..0x7F except '"' and '\\') are
|
||||||
|
// copied verbatim, which the bulk scanner does 8 bytes at a time.
|
||||||
|
inline bool is_string_special(unsigned char c) noexcept
|
||||||
|
{
|
||||||
|
return c == '\"' || c == '\\' || c < 0x20u || c >= 0x80u;
|
||||||
|
}
|
||||||
|
|
||||||
|
// SWAR helper: return a word whose high bit is set in every byte of @a v that
|
||||||
|
// is_string_special(); zero if the 8 bytes are all ordinary.
|
||||||
|
inline std::uint64_t swar_string_special(std::uint64_t v) noexcept
|
||||||
|
{
|
||||||
|
constexpr std::uint64_t ones = 0x0101010101010101ull;
|
||||||
|
constexpr std::uint64_t high = 0x8080808080808080ull;
|
||||||
|
const std::uint64_t q = v ^ 0x2222222222222222ull; // '"' (0x22)
|
||||||
|
const std::uint64_t b = v ^ 0x5C5C5C5C5C5C5C5Cull; // '\\' (0x5C)
|
||||||
|
const std::uint64_t has_quote = (q - ones) & ~q & high;
|
||||||
|
const std::uint64_t has_backslash = (b - ones) & ~b & high;
|
||||||
|
const std::uint64_t has_control = (v - 0x2020202020202020ull) & ~v & high; // < 0x20
|
||||||
|
const std::uint64_t has_non_ascii = v & high; // >= 0x80
|
||||||
|
return has_quote | has_backslash | has_control | has_non_ascii;
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the index of the first is_string_special() byte in [data, data+n), or
|
||||||
|
// n if every byte is ordinary; scans 8 bytes at a time
|
||||||
|
inline std::size_t find_string_special(const unsigned char* data, std::size_t n) noexcept
|
||||||
|
{
|
||||||
|
std::size_t i = 0;
|
||||||
|
for (; i + 8 <= n; i += 8)
|
||||||
|
{
|
||||||
|
std::uint64_t word = 0;
|
||||||
|
std::memcpy(&word, data + i, sizeof(word));
|
||||||
|
if (swar_string_special(word) != 0)
|
||||||
|
{
|
||||||
|
// a special byte is in this word; locate it (endian-agnostic)
|
||||||
|
for (std::size_t j = 0; j < 8; ++j)
|
||||||
|
{
|
||||||
|
if (is_string_special(data[i + j]))
|
||||||
|
{
|
||||||
|
return i + j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (; i < n; ++i)
|
||||||
|
{
|
||||||
|
if (is_string_special(data[i]))
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// classify a byte as one the serializer must NOT copy verbatim when
|
||||||
|
// ensure_ascii is requested: the closing quote, an escape, a control character
|
||||||
|
// (< 0x20), DEL (0x7F), or any non-ASCII byte (>= 0x80). Everything else -
|
||||||
|
// 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
|
||||||
|
{
|
||||||
|
return c >= 0x20u && c < 0x7Fu && c != '\"' && c != '\\';
|
||||||
|
}
|
||||||
|
|
||||||
|
// return the index of the first byte in [data, data+n) that is NOT
|
||||||
|
// is_ascii_copyable(), or n if every byte can be copied verbatim; scans 8 bytes
|
||||||
|
// at a time. Used by the serializer's ensure_ascii fast path.
|
||||||
|
inline std::size_t find_ascii_copyable_run(const unsigned char* data, std::size_t n) noexcept
|
||||||
|
{
|
||||||
|
constexpr std::uint64_t ones = 0x0101010101010101ull;
|
||||||
|
constexpr std::uint64_t high = 0x8080808080808080ull;
|
||||||
|
std::size_t i = 0;
|
||||||
|
for (; i + 8 <= n; i += 8)
|
||||||
|
{
|
||||||
|
std::uint64_t v = 0;
|
||||||
|
std::memcpy(&v, data + i, sizeof(v));
|
||||||
|
const std::uint64_t q = v ^ 0x2222222222222222ull; // '"' (0x22)
|
||||||
|
const std::uint64_t b = v ^ 0x5C5C5C5C5C5C5C5Cull; // '\\' (0x5C)
|
||||||
|
const std::uint64_t d = v ^ 0x7F7F7F7F7F7F7F7Full; // DEL (0x7F)
|
||||||
|
const std::uint64_t stop = ((q - ones) & ~q & high) // == '"'
|
||||||
|
| ((b - ones) & ~b & high) // == '\\'
|
||||||
|
| ((d - ones) & ~d & high) // == 0x7F
|
||||||
|
| ((v - 0x2020202020202020ull) & ~v & high) // < 0x20
|
||||||
|
| (v & high); // >= 0x80
|
||||||
|
if (stop != 0)
|
||||||
|
{
|
||||||
|
for (std::size_t j = 0; j < 8; ++j)
|
||||||
|
{
|
||||||
|
if (!is_ascii_copyable(data[i + j]))
|
||||||
|
{
|
||||||
|
return i + j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (; i < n; ++i)
|
||||||
|
{
|
||||||
|
if (!is_ascii_copyable(data[i]))
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate one UTF-8 sequence at the front of [data, data+avail). Returns its
|
||||||
|
// length (2..4) only when the bytes form a *well-formed* sequence using exactly
|
||||||
|
// the same ranges as scan_string()'s per-byte switch, so the bulk path accepts
|
||||||
|
// precisely what the byte path accepts. Returns 0 for anything that is invalid,
|
||||||
|
// incomplete, or that the byte path must diagnose (the caller then defers to
|
||||||
|
// that path, keeping error messages unchanged). Lead bytes < 0x80 are handled
|
||||||
|
// by the caller and never passed here.
|
||||||
|
inline std::size_t validate_one_utf8(const unsigned char* data, std::size_t avail) noexcept
|
||||||
|
{
|
||||||
|
const unsigned char c0 = data[0];
|
||||||
|
if (c0 >= 0xC2 && c0 <= 0xDF) // U+0080..U+07FF
|
||||||
|
{
|
||||||
|
if (avail >= 2 && data[1] >= 0x80 && data[1] <= 0xBF)
|
||||||
|
{
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c0 == 0xE0) // U+0800..U+0FFF
|
||||||
|
{
|
||||||
|
if (avail >= 3 && data[1] >= 0xA0 && data[1] <= 0xBF && data[2] >= 0x80 && data[2] <= 0xBF)
|
||||||
|
{
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if ((c0 >= 0xE1 && c0 <= 0xEC) || c0 == 0xEE || c0 == 0xEF) // U+1000..U+CFFF, U+E000..U+FFFF
|
||||||
|
{
|
||||||
|
if (avail >= 3 && data[1] >= 0x80 && data[1] <= 0xBF && data[2] >= 0x80 && data[2] <= 0xBF)
|
||||||
|
{
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c0 == 0xED) // U+D000..U+D7FF (excludes surrogates)
|
||||||
|
{
|
||||||
|
if (avail >= 3 && data[1] >= 0x80 && data[1] <= 0x9F && data[2] >= 0x80 && data[2] <= 0xBF)
|
||||||
|
{
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c0 == 0xF0) // U+10000..U+3FFFF
|
||||||
|
{
|
||||||
|
if (avail >= 4 && data[1] >= 0x90 && data[1] <= 0xBF && data[2] >= 0x80 && data[2] <= 0xBF && data[3] >= 0x80 && data[3] <= 0xBF)
|
||||||
|
{
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c0 >= 0xF1 && c0 <= 0xF3) // U+40000..U+FFFFF
|
||||||
|
{
|
||||||
|
if (avail >= 4 && data[1] >= 0x80 && data[1] <= 0xBF && data[2] >= 0x80 && data[2] <= 0xBF && data[3] >= 0x80 && data[3] <= 0xBF)
|
||||||
|
{
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (c0 == 0xF4) // U+100000..U+10FFFF
|
||||||
|
{
|
||||||
|
if (avail >= 4 && data[1] >= 0x80 && data[1] <= 0x8F && data[2] >= 0x80 && data[2] <= 0xBF && data[3] >= 0x80 && data[3] <= 0xBF)
|
||||||
|
{
|
||||||
|
return 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0; // invalid, incomplete, or must be diagnosed by the byte path
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scalar (C++11) computation of the bulk run length: the number of leading
|
||||||
|
// bytes in [data, data+n) that are ordinary ASCII or complete well-formed UTF-8
|
||||||
|
// sequences, stopping before the first byte that needs individual handling (the
|
||||||
|
// closing quote, an escape, a control character, or an ill-formed/truncated
|
||||||
|
// sequence). ASCII is skipped 8 bytes at a time.
|
||||||
|
inline std::size_t scalar_string_bulk_run(const unsigned char* data, std::size_t n) noexcept
|
||||||
|
{
|
||||||
|
std::size_t pos = 0;
|
||||||
|
while (pos < n)
|
||||||
|
{
|
||||||
|
pos += find_string_special(data + pos, n - pos);
|
||||||
|
if (pos >= n || data[pos] < 0x80u)
|
||||||
|
{
|
||||||
|
break; // end of buffer, or a quote/escape/control byte
|
||||||
|
}
|
||||||
|
const std::size_t seq = validate_one_utf8(data + pos, n - pos);
|
||||||
|
if (seq == 0)
|
||||||
|
{
|
||||||
|
break; // ill-formed or truncated: let the byte path diagnose it
|
||||||
|
}
|
||||||
|
pos += seq;
|
||||||
|
}
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(JSON_USE_SIMDUTF)
|
||||||
|
// Index of the first quote/escape/control byte in [data, data+n) (non-ASCII
|
||||||
|
// bytes are *not* stops here - the whole run is handed to simdutf), or n.
|
||||||
|
inline std::size_t find_string_delimiter(const unsigned char* data, std::size_t n) noexcept
|
||||||
|
{
|
||||||
|
constexpr std::uint64_t ones = 0x0101010101010101ull;
|
||||||
|
constexpr std::uint64_t high = 0x8080808080808080ull;
|
||||||
|
std::size_t i = 0;
|
||||||
|
for (; i + 8 <= n; i += 8)
|
||||||
|
{
|
||||||
|
std::uint64_t v = 0;
|
||||||
|
std::memcpy(&v, data + i, sizeof(v));
|
||||||
|
const std::uint64_t q = v ^ 0x2222222222222222ull;
|
||||||
|
const std::uint64_t b = v ^ 0x5C5C5C5C5C5C5C5Cull;
|
||||||
|
const std::uint64_t hit = ((q - ones) & ~q & high)
|
||||||
|
| ((b - ones) & ~b & high)
|
||||||
|
| ((v - 0x2020202020202020ull) & ~v & high);
|
||||||
|
if (hit != 0)
|
||||||
|
{
|
||||||
|
for (std::size_t j = 0; j < 8; ++j)
|
||||||
|
{
|
||||||
|
const unsigned char c = data[i + j];
|
||||||
|
if (c == '\"' || c == '\\' || c < 0x20u)
|
||||||
|
{
|
||||||
|
return i + j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (; i < n; ++i)
|
||||||
|
{
|
||||||
|
const unsigned char c = data[i];
|
||||||
|
if (c == '\"' || c == '\\' || c < 0x20u)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Backend-dispatched bulk run length. With JSON_USE_SIMDUTF the run up to the
|
||||||
|
// next delimiter is validated in one shot by simdutf; on the rare failure the
|
||||||
|
// scalar helper recomputes the exact valid prefix so the byte path still
|
||||||
|
// produces the precise diagnostic. Without it, the pure scalar path is used.
|
||||||
|
inline std::size_t string_bulk_run(const unsigned char* data, std::size_t n) noexcept
|
||||||
|
{
|
||||||
|
#if defined(JSON_USE_SIMDUTF)
|
||||||
|
const std::size_t run = find_string_delimiter(data, n);
|
||||||
|
if (run != 0 && simdutf::validate_utf8(reinterpret_cast<const char*>(data), run))
|
||||||
|
{
|
||||||
|
return run;
|
||||||
|
}
|
||||||
|
return scalar_string_bulk_run(data, n);
|
||||||
|
#else
|
||||||
|
return scalar_string_bulk_run(data, n);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
NLOHMANN_JSON_NAMESPACE_END
|
||||||
@@ -231,7 +231,9 @@ struct char_traits<signed char> : std::char_traits<char>
|
|||||||
// Redefine to_int_type function
|
// Redefine to_int_type function
|
||||||
static int_type to_int_type(char_type c) noexcept
|
static int_type to_int_type(char_type c) noexcept
|
||||||
{
|
{
|
||||||
return static_cast<int_type>(c);
|
// cast via unsigned char: sign-extending a negative char_type would make
|
||||||
|
// byte 0xFF indistinguishable from eof()
|
||||||
|
return static_cast<int_type>(static_cast<unsigned char>(c));
|
||||||
}
|
}
|
||||||
|
|
||||||
static char_type to_char_type(int_type i) noexcept
|
static char_type to_char_type(int_type i) noexcept
|
||||||
@@ -699,21 +701,35 @@ struct is_json_pointer_of<A, ::nlohmann::json_pointer<A>> : std::true_type {};
|
|||||||
template <typename A>
|
template <typename A>
|
||||||
struct is_json_pointer_of<A, ::nlohmann::json_pointer<A>&> : std::true_type {};
|
struct is_json_pointer_of<A, ::nlohmann::json_pointer<A>&> : std::true_type {};
|
||||||
|
|
||||||
// checks if A and B are comparable using Compare functor
|
// checks if A and B are comparable using Compare functor, assuming that
|
||||||
|
// neither A nor B is a json_pointer type (that case is handled by
|
||||||
|
// is_comparable below, which never instantiates this helper otherwise)
|
||||||
template<typename Compare, typename A, typename B, typename = void>
|
template<typename Compare, typename A, typename B, typename = void>
|
||||||
struct is_comparable : std::false_type {};
|
struct is_comparable_no_json_pointer : std::false_type {};
|
||||||
|
|
||||||
// We exclude json_pointer here, because the checks using Compare(A, B) will
|
|
||||||
// use json_pointer::operator string_t() which triggers a deprecation warning
|
|
||||||
// for GCC. See https://github.com/nlohmann/json/issues/4621. The call to
|
|
||||||
// is_json_pointer_of can be removed once the deprecated function has been
|
|
||||||
// removed.
|
|
||||||
template<typename Compare, typename A, typename B>
|
template<typename Compare, typename A, typename B>
|
||||||
struct is_comparable < Compare, A, B, enable_if_t < !is_json_pointer_of<A, B>::value
|
struct is_comparable_no_json_pointer < Compare, A, B, enable_if_t <
|
||||||
&& std::is_constructible <decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>()))>::value
|
std::is_constructible <decltype(std::declval<Compare>()(std::declval<A>(), std::declval<B>()))>::value
|
||||||
&& std::is_constructible <decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))>::value
|
&& std::is_constructible <decltype(std::declval<Compare>()(std::declval<B>(), std::declval<A>()))>::value
|
||||||
>> : std::true_type {};
|
>> : std::true_type {};
|
||||||
|
|
||||||
|
// checks if A and B are comparable using Compare functor
|
||||||
|
// We dispatch on is_json_pointer_of as a plain bool (rather than folding it
|
||||||
|
// into a single enable_if_t condition together with the checks below) so
|
||||||
|
// that the Compare(A, B) checks are only ever written - and thus only ever
|
||||||
|
// instantiated - when A/B are not a json_pointer/string pair. Those checks
|
||||||
|
// use json_pointer::operator string_t() (GCC, see #4621) resp. the
|
||||||
|
// deprecated json_pointer/string operator== (Clang, see #5288), and merely
|
||||||
|
// naming them as later operands of a plain && chain is not sufficient to
|
||||||
|
// avoid their instantiation on all compilers, even when the first operand
|
||||||
|
// is false. The dispatch on is_json_pointer_of can be removed once the
|
||||||
|
// deprecated json_pointer comparison operators have been removed.
|
||||||
|
template<typename Compare, typename A, typename B, bool = is_json_pointer_of<A, B>::value>
|
||||||
|
struct is_comparable : std::false_type {};
|
||||||
|
|
||||||
|
template<typename Compare, typename A, typename B>
|
||||||
|
struct is_comparable<Compare, A, B, false> : is_comparable_no_json_pointer<Compare, A, B> {};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
using detect_is_transparent = typename T::is_transparent;
|
using detect_is_transparent = typename T::is_transparent;
|
||||||
|
|
||||||
|
|||||||
@@ -1662,7 +1662,15 @@ class binary_writer
|
|||||||
std::size_t len = (value.at(key).empty() ? 0 : 1);
|
std::size_t len = (value.at(key).empty() ? 0 : 1);
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
len *= static_cast<std::size_t>(el.m_data.m_value.number_unsigned);
|
// a dimension is read as an unsigned value below, so anything that
|
||||||
|
// is not a non-negative integer is rejected: a non-integer entry
|
||||||
|
// would pun unrelated bytes as the dimension, and a negative one
|
||||||
|
// would wrap into a nonsensical length
|
||||||
|
if (!el.is_number_integer() || (!el.is_number_unsigned() && el.template get<std::int64_t>() < 0))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
len *= static_cast<std::size_t>(el.template get<std::uint64_t>());
|
||||||
}
|
}
|
||||||
|
|
||||||
key = "_ArrayData_";
|
key = "_ArrayData_";
|
||||||
@@ -1671,6 +1679,24 @@ class binary_writer
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// every element is written below as the number kind dtype names, so it
|
||||||
|
// has to actually be a number of that category: an element of any other
|
||||||
|
// type would reinterpret unrelated bytes, e.g. a string's heap pointer,
|
||||||
|
// as that number. Such an object falls back to a plain object encoding.
|
||||||
|
// dtype names the wire type, not the storage type: whether an integer
|
||||||
|
// is held as number_integer or number_unsigned depends on how the value
|
||||||
|
// was built (parsing stores non-negative integers as unsigned, the C++
|
||||||
|
// API stores int literals as signed), so both are accepted here and the
|
||||||
|
// writes below go through get<>, which reads the member that is active.
|
||||||
|
const bool ndarray_is_float = (dtype == 'd' || dtype == 'D');
|
||||||
|
for (const auto& el : value.at(key))
|
||||||
|
{
|
||||||
|
if (ndarray_is_float ? !el.is_number_float() : !el.is_number_integer())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
oa->write_character('[');
|
oa->write_character('[');
|
||||||
oa->write_character('$');
|
oa->write_character('$');
|
||||||
oa->write_character(dtype);
|
oa->write_character(dtype);
|
||||||
@@ -1684,70 +1710,70 @@ class binary_writer
|
|||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<std::uint8_t>(el.m_data.m_value.number_unsigned), true);
|
write_number(static_cast<std::uint8_t>(el.template get<std::uint64_t>()), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'i')
|
else if (dtype == 'i')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<std::int8_t>(el.m_data.m_value.number_integer), true);
|
write_number(static_cast<std::int8_t>(el.template get<std::int64_t>()), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'u')
|
else if (dtype == 'u')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<std::uint16_t>(el.m_data.m_value.number_unsigned), true);
|
write_number(static_cast<std::uint16_t>(el.template get<std::uint64_t>()), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'I')
|
else if (dtype == 'I')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<std::int16_t>(el.m_data.m_value.number_integer), true);
|
write_number(static_cast<std::int16_t>(el.template get<std::int64_t>()), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'm')
|
else if (dtype == 'm')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<std::uint32_t>(el.m_data.m_value.number_unsigned), true);
|
write_number(static_cast<std::uint32_t>(el.template get<std::uint64_t>()), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'l')
|
else if (dtype == 'l')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<std::int32_t>(el.m_data.m_value.number_integer), true);
|
write_number(static_cast<std::int32_t>(el.template get<std::int64_t>()), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'M')
|
else if (dtype == 'M')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<std::uint64_t>(el.m_data.m_value.number_unsigned), true);
|
write_number(el.template get<std::uint64_t>(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'L')
|
else if (dtype == 'L')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<std::int64_t>(el.m_data.m_value.number_integer), true);
|
write_number(el.template get<std::int64_t>(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'd')
|
else if (dtype == 'd')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<float>(el.m_data.m_value.number_float), true);
|
write_number(static_cast<float>(el.template get<double>()), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (dtype == 'D')
|
else if (dtype == 'D')
|
||||||
{
|
{
|
||||||
for (const auto& el : value.at(key))
|
for (const auto& el : value.at(key))
|
||||||
{
|
{
|
||||||
write_number(static_cast<double>(el.m_data.m_value.number_float), true);
|
write_number(el.template get<double>(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include <cstddef> // size_t, ptrdiff_t
|
#include <cstddef> // size_t, ptrdiff_t
|
||||||
#include <cstdint> // uint8_t
|
#include <cstdint> // uint8_t
|
||||||
#include <cstdio> // snprintf
|
#include <cstdio> // snprintf
|
||||||
|
#include <cstring> // memcpy
|
||||||
#include <limits> // numeric_limits
|
#include <limits> // numeric_limits
|
||||||
#include <string> // string, char_traits
|
#include <string> // string, char_traits
|
||||||
#include <iomanip> // setfill, setw
|
#include <iomanip> // setfill, setw
|
||||||
@@ -24,6 +25,7 @@
|
|||||||
|
|
||||||
#include <nlohmann/detail/conversions/to_chars.hpp>
|
#include <nlohmann/detail/conversions/to_chars.hpp>
|
||||||
#include <nlohmann/detail/exceptions.hpp>
|
#include <nlohmann/detail/exceptions.hpp>
|
||||||
|
#include <nlohmann/detail/input/string_scan.hpp>
|
||||||
#include <nlohmann/detail/macro_scope.hpp>
|
#include <nlohmann/detail/macro_scope.hpp>
|
||||||
#include <nlohmann/detail/meta/cpp_future.hpp>
|
#include <nlohmann/detail/meta/cpp_future.hpp>
|
||||||
#include <nlohmann/detail/output/binary_writer.hpp>
|
#include <nlohmann/detail/output/binary_writer.hpp>
|
||||||
@@ -109,6 +111,25 @@ class serializer
|
|||||||
const bool ensure_ascii,
|
const bool ensure_ascii,
|
||||||
const unsigned int indent_step,
|
const unsigned int indent_step,
|
||||||
const unsigned int current_indent = 0)
|
const unsigned int current_indent = 0)
|
||||||
|
{
|
||||||
|
dump_internal(val, pretty_print, ensure_ascii, indent_step, current_indent);
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
JSON_PRIVATE_UNLESS_TESTED:
|
||||||
|
/*!
|
||||||
|
@brief recursive worker for @ref dump
|
||||||
|
|
||||||
|
Identical in behavior to the historical @ref dump, but writes into the
|
||||||
|
serializer's internal @ref write_buffer instead of issuing a virtual call
|
||||||
|
per token. The public @ref dump wraps this and flushes the buffer once the
|
||||||
|
top-level value has been serialized.
|
||||||
|
*/
|
||||||
|
void dump_internal(const BasicJsonType& val,
|
||||||
|
const bool pretty_print,
|
||||||
|
const bool ensure_ascii,
|
||||||
|
const unsigned int indent_step,
|
||||||
|
const unsigned int current_indent = 0)
|
||||||
{
|
{
|
||||||
switch (val.m_data.m_type)
|
switch (val.m_data.m_type)
|
||||||
{
|
{
|
||||||
@@ -116,13 +137,13 @@ class serializer
|
|||||||
{
|
{
|
||||||
if (val.m_data.m_value.object->empty())
|
if (val.m_data.m_value.object->empty())
|
||||||
{
|
{
|
||||||
o->write_characters("{}", 2);
|
put_chars("{}", 2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pretty_print)
|
if (pretty_print)
|
||||||
{
|
{
|
||||||
o->write_characters("{\n", 2);
|
put_chars("{\n", 2);
|
||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
@@ -135,51 +156,51 @@ class serializer
|
|||||||
auto i = val.m_data.m_value.object->cbegin();
|
auto i = val.m_data.m_value.object->cbegin();
|
||||||
for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
|
for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
|
||||||
{
|
{
|
||||||
o->write_characters(indent_string.c_str(), new_indent);
|
put_chars(indent_string.c_str(), new_indent);
|
||||||
o->write_character('\"');
|
put_char('\"');
|
||||||
dump_escaped(i->first, ensure_ascii);
|
dump_escaped(i->first, ensure_ascii);
|
||||||
o->write_characters("\": ", 3);
|
put_chars("\": ", 3);
|
||||||
dump(i->second, true, ensure_ascii, indent_step, new_indent);
|
dump_internal(i->second, true, ensure_ascii, indent_step, new_indent);
|
||||||
o->write_characters(",\n", 2);
|
put_chars(",\n", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// last element
|
// last element
|
||||||
JSON_ASSERT(i != val.m_data.m_value.object->cend());
|
JSON_ASSERT(i != val.m_data.m_value.object->cend());
|
||||||
JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
|
JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
|
||||||
o->write_characters(indent_string.c_str(), new_indent);
|
put_chars(indent_string.c_str(), new_indent);
|
||||||
o->write_character('\"');
|
put_char('\"');
|
||||||
dump_escaped(i->first, ensure_ascii);
|
dump_escaped(i->first, ensure_ascii);
|
||||||
o->write_characters("\": ", 3);
|
put_chars("\": ", 3);
|
||||||
dump(i->second, true, ensure_ascii, indent_step, new_indent);
|
dump_internal(i->second, true, ensure_ascii, indent_step, new_indent);
|
||||||
|
|
||||||
o->write_character('\n');
|
put_char('\n');
|
||||||
o->write_characters(indent_string.c_str(), current_indent);
|
put_chars(indent_string.c_str(), current_indent);
|
||||||
o->write_character('}');
|
put_char('}');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
o->write_character('{');
|
put_char('{');
|
||||||
|
|
||||||
// first n-1 elements
|
// first n-1 elements
|
||||||
auto i = val.m_data.m_value.object->cbegin();
|
auto i = val.m_data.m_value.object->cbegin();
|
||||||
for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
|
for (std::size_t cnt = 0; cnt < val.m_data.m_value.object->size() - 1; ++cnt, ++i)
|
||||||
{
|
{
|
||||||
o->write_character('\"');
|
put_char('\"');
|
||||||
dump_escaped(i->first, ensure_ascii);
|
dump_escaped(i->first, ensure_ascii);
|
||||||
o->write_characters("\":", 2);
|
put_chars("\":", 2);
|
||||||
dump(i->second, false, ensure_ascii, indent_step, current_indent);
|
dump_internal(i->second, false, ensure_ascii, indent_step, current_indent);
|
||||||
o->write_character(',');
|
put_char(',');
|
||||||
}
|
}
|
||||||
|
|
||||||
// last element
|
// last element
|
||||||
JSON_ASSERT(i != val.m_data.m_value.object->cend());
|
JSON_ASSERT(i != val.m_data.m_value.object->cend());
|
||||||
JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
|
JSON_ASSERT(std::next(i) == val.m_data.m_value.object->cend());
|
||||||
o->write_character('\"');
|
put_char('\"');
|
||||||
dump_escaped(i->first, ensure_ascii);
|
dump_escaped(i->first, ensure_ascii);
|
||||||
o->write_characters("\":", 2);
|
put_chars("\":", 2);
|
||||||
dump(i->second, false, ensure_ascii, indent_step, current_indent);
|
dump_internal(i->second, false, ensure_ascii, indent_step, current_indent);
|
||||||
|
|
||||||
o->write_character('}');
|
put_char('}');
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -189,13 +210,13 @@ class serializer
|
|||||||
{
|
{
|
||||||
if (val.m_data.m_value.array->empty())
|
if (val.m_data.m_value.array->empty())
|
||||||
{
|
{
|
||||||
o->write_characters("[]", 2);
|
put_chars("[]", 2);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pretty_print)
|
if (pretty_print)
|
||||||
{
|
{
|
||||||
o->write_characters("[\n", 2);
|
put_chars("[\n", 2);
|
||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
@@ -208,37 +229,37 @@ class serializer
|
|||||||
for (auto i = val.m_data.m_value.array->cbegin();
|
for (auto i = val.m_data.m_value.array->cbegin();
|
||||||
i != val.m_data.m_value.array->cend() - 1; ++i)
|
i != val.m_data.m_value.array->cend() - 1; ++i)
|
||||||
{
|
{
|
||||||
o->write_characters(indent_string.c_str(), new_indent);
|
put_chars(indent_string.c_str(), new_indent);
|
||||||
dump(*i, true, ensure_ascii, indent_step, new_indent);
|
dump_internal(*i, true, ensure_ascii, indent_step, new_indent);
|
||||||
o->write_characters(",\n", 2);
|
put_chars(",\n", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// last element
|
// last element
|
||||||
JSON_ASSERT(!val.m_data.m_value.array->empty());
|
JSON_ASSERT(!val.m_data.m_value.array->empty());
|
||||||
o->write_characters(indent_string.c_str(), new_indent);
|
put_chars(indent_string.c_str(), new_indent);
|
||||||
dump(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
|
dump_internal(val.m_data.m_value.array->back(), true, ensure_ascii, indent_step, new_indent);
|
||||||
|
|
||||||
o->write_character('\n');
|
put_char('\n');
|
||||||
o->write_characters(indent_string.c_str(), current_indent);
|
put_chars(indent_string.c_str(), current_indent);
|
||||||
o->write_character(']');
|
put_char(']');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
o->write_character('[');
|
put_char('[');
|
||||||
|
|
||||||
// first n-1 elements
|
// first n-1 elements
|
||||||
for (auto i = val.m_data.m_value.array->cbegin();
|
for (auto i = val.m_data.m_value.array->cbegin();
|
||||||
i != val.m_data.m_value.array->cend() - 1; ++i)
|
i != val.m_data.m_value.array->cend() - 1; ++i)
|
||||||
{
|
{
|
||||||
dump(*i, false, ensure_ascii, indent_step, current_indent);
|
dump_internal(*i, false, ensure_ascii, indent_step, current_indent);
|
||||||
o->write_character(',');
|
put_char(',');
|
||||||
}
|
}
|
||||||
|
|
||||||
// last element
|
// last element
|
||||||
JSON_ASSERT(!val.m_data.m_value.array->empty());
|
JSON_ASSERT(!val.m_data.m_value.array->empty());
|
||||||
dump(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
|
dump_internal(val.m_data.m_value.array->back(), false, ensure_ascii, indent_step, current_indent);
|
||||||
|
|
||||||
o->write_character(']');
|
put_char(']');
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@@ -246,9 +267,9 @@ class serializer
|
|||||||
|
|
||||||
case value_t::string:
|
case value_t::string:
|
||||||
{
|
{
|
||||||
o->write_character('\"');
|
put_char('\"');
|
||||||
dump_escaped(*val.m_data.m_value.string, ensure_ascii);
|
dump_escaped(*val.m_data.m_value.string, ensure_ascii);
|
||||||
o->write_character('\"');
|
put_char('\"');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,7 +277,7 @@ class serializer
|
|||||||
{
|
{
|
||||||
if (pretty_print)
|
if (pretty_print)
|
||||||
{
|
{
|
||||||
o->write_characters("{\n", 2);
|
put_chars("{\n", 2);
|
||||||
|
|
||||||
// variable to hold indentation for recursive calls
|
// variable to hold indentation for recursive calls
|
||||||
const auto new_indent = current_indent + indent_step;
|
const auto new_indent = current_indent + indent_step;
|
||||||
@@ -265,9 +286,9 @@ class serializer
|
|||||||
indent_string.resize(indent_string.size() * 2, ' ');
|
indent_string.resize(indent_string.size() * 2, ' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
o->write_characters(indent_string.c_str(), new_indent);
|
put_chars(indent_string.c_str(), new_indent);
|
||||||
|
|
||||||
o->write_characters("\"bytes\": [", 10);
|
put_chars("\"bytes\": [", 10);
|
||||||
|
|
||||||
if (!val.m_data.m_value.binary->empty())
|
if (!val.m_data.m_value.binary->empty())
|
||||||
{
|
{
|
||||||
@@ -275,30 +296,30 @@ class serializer
|
|||||||
i != val.m_data.m_value.binary->cend() - 1; ++i)
|
i != val.m_data.m_value.binary->cend() - 1; ++i)
|
||||||
{
|
{
|
||||||
dump_integer(*i);
|
dump_integer(*i);
|
||||||
o->write_characters(", ", 2);
|
put_chars(", ", 2);
|
||||||
}
|
}
|
||||||
dump_integer(val.m_data.m_value.binary->back());
|
dump_integer(val.m_data.m_value.binary->back());
|
||||||
}
|
}
|
||||||
|
|
||||||
o->write_characters("],\n", 3);
|
put_chars("],\n", 3);
|
||||||
o->write_characters(indent_string.c_str(), new_indent);
|
put_chars(indent_string.c_str(), new_indent);
|
||||||
|
|
||||||
o->write_characters("\"subtype\": ", 11);
|
put_chars("\"subtype\": ", 11);
|
||||||
if (val.m_data.m_value.binary->has_subtype())
|
if (val.m_data.m_value.binary->has_subtype())
|
||||||
{
|
{
|
||||||
dump_integer(val.m_data.m_value.binary->subtype());
|
dump_integer(val.m_data.m_value.binary->subtype());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
o->write_characters("null", 4);
|
put_chars("null", 4);
|
||||||
}
|
}
|
||||||
o->write_character('\n');
|
put_char('\n');
|
||||||
o->write_characters(indent_string.c_str(), current_indent);
|
put_chars(indent_string.c_str(), current_indent);
|
||||||
o->write_character('}');
|
put_char('}');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
o->write_characters("{\"bytes\":[", 10);
|
put_chars("{\"bytes\":[", 10);
|
||||||
|
|
||||||
if (!val.m_data.m_value.binary->empty())
|
if (!val.m_data.m_value.binary->empty())
|
||||||
{
|
{
|
||||||
@@ -306,20 +327,20 @@ class serializer
|
|||||||
i != val.m_data.m_value.binary->cend() - 1; ++i)
|
i != val.m_data.m_value.binary->cend() - 1; ++i)
|
||||||
{
|
{
|
||||||
dump_integer(*i);
|
dump_integer(*i);
|
||||||
o->write_character(',');
|
put_char(',');
|
||||||
}
|
}
|
||||||
dump_integer(val.m_data.m_value.binary->back());
|
dump_integer(val.m_data.m_value.binary->back());
|
||||||
}
|
}
|
||||||
|
|
||||||
o->write_characters("],\"subtype\":", 12);
|
put_chars("],\"subtype\":", 12);
|
||||||
if (val.m_data.m_value.binary->has_subtype())
|
if (val.m_data.m_value.binary->has_subtype())
|
||||||
{
|
{
|
||||||
dump_integer(val.m_data.m_value.binary->subtype());
|
dump_integer(val.m_data.m_value.binary->subtype());
|
||||||
o->write_character('}');
|
put_char('}');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
o->write_characters("null}", 5);
|
put_chars("null}", 5);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -329,11 +350,11 @@ class serializer
|
|||||||
{
|
{
|
||||||
if (val.m_data.m_value.boolean)
|
if (val.m_data.m_value.boolean)
|
||||||
{
|
{
|
||||||
o->write_characters("true", 4);
|
put_chars("true", 4);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
o->write_characters("false", 5);
|
put_chars("false", 5);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -358,13 +379,13 @@ class serializer
|
|||||||
|
|
||||||
case value_t::discarded:
|
case value_t::discarded:
|
||||||
{
|
{
|
||||||
o->write_characters("<discarded>", 11);
|
put_chars("<discarded>", 11);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
case value_t::null:
|
case value_t::null:
|
||||||
{
|
{
|
||||||
o->write_characters("null", 4);
|
put_chars("null", 4);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -400,6 +421,45 @@ class serializer
|
|||||||
|
|
||||||
for (std::size_t i = 0; i < s.size(); ++i)
|
for (std::size_t i = 0; i < s.size(); ++i)
|
||||||
{
|
{
|
||||||
|
// Fast path: at a character boundary (state == UTF8_ACCEPT),
|
||||||
|
// bulk-copy the longest run of bytes that need no escaping using a
|
||||||
|
// SWAR scanner shared with the lexer's contiguous path. The scanner
|
||||||
|
// stops exactly at the first byte dump_escaped would handle
|
||||||
|
// individually, so that byte is left to the byte-at-a-time path
|
||||||
|
// below, keeping escaping output and error diagnostics unchanged.
|
||||||
|
//
|
||||||
|
// - ensure_ascii == false: string_bulk_run() copies ordinary bytes
|
||||||
|
// and complete well-formed UTF-8, stopping at a quote, backslash,
|
||||||
|
// control character (< 0x20), or ill-formed/truncated sequence.
|
||||||
|
// - ensure_ascii == true: only printable ASCII may be copied
|
||||||
|
// verbatim; find_ascii_copyable_run() additionally stops at 0x7F
|
||||||
|
// and every non-ASCII byte (>= 0x80), which must be \u-escaped.
|
||||||
|
if (state == UTF8_ACCEPT)
|
||||||
|
{
|
||||||
|
const auto* const data = reinterpret_cast<const unsigned char*>(s.data());
|
||||||
|
const std::size_t run = ensure_ascii
|
||||||
|
? find_ascii_copyable_run(data + i, s.size() - i)
|
||||||
|
: string_bulk_run(data + i, s.size() - i);
|
||||||
|
if (run != 0)
|
||||||
|
{
|
||||||
|
// emit any bytes still pending in string_buffer first to
|
||||||
|
// preserve output order, then write the run directly
|
||||||
|
if (bytes != 0)
|
||||||
|
{
|
||||||
|
put_chars(string_buffer.data(), bytes);
|
||||||
|
bytes = 0;
|
||||||
|
}
|
||||||
|
put_chars(s.data() + i, run);
|
||||||
|
bytes_after_last_accept = 0;
|
||||||
|
undumped_chars = 0;
|
||||||
|
i += run;
|
||||||
|
if (i >= s.size())
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const auto byte = static_cast<std::uint8_t>(s[i]);
|
const auto byte = static_cast<std::uint8_t>(s[i]);
|
||||||
|
|
||||||
switch (decode(state, codepoint, byte))
|
switch (decode(state, codepoint, byte))
|
||||||
@@ -488,7 +548,7 @@ class serializer
|
|||||||
// written ("\uxxxx\uxxxx\0") for one code point
|
// written ("\uxxxx\uxxxx\0") for one code point
|
||||||
if (string_buffer.size() - bytes < 13)
|
if (string_buffer.size() - bytes < 13)
|
||||||
{
|
{
|
||||||
o->write_characters(string_buffer.data(), bytes);
|
put_chars(string_buffer.data(), bytes);
|
||||||
bytes = 0;
|
bytes = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -547,7 +607,7 @@ class serializer
|
|||||||
// written ("\uxxxx\uxxxx\0") for one code point
|
// written ("\uxxxx\uxxxx\0") for one code point
|
||||||
if (string_buffer.size() - bytes < 13)
|
if (string_buffer.size() - bytes < 13)
|
||||||
{
|
{
|
||||||
o->write_characters(string_buffer.data(), bytes);
|
put_chars(string_buffer.data(), bytes);
|
||||||
bytes = 0;
|
bytes = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -586,7 +646,7 @@ class serializer
|
|||||||
// write buffer
|
// write buffer
|
||||||
if (bytes > 0)
|
if (bytes > 0)
|
||||||
{
|
{
|
||||||
o->write_characters(string_buffer.data(), bytes);
|
put_chars(string_buffer.data(), bytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -602,22 +662,22 @@ class serializer
|
|||||||
case error_handler_t::ignore:
|
case error_handler_t::ignore:
|
||||||
{
|
{
|
||||||
// write all accepted bytes
|
// write all accepted bytes
|
||||||
o->write_characters(string_buffer.data(), bytes_after_last_accept);
|
put_chars(string_buffer.data(), bytes_after_last_accept);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case error_handler_t::replace:
|
case error_handler_t::replace:
|
||||||
{
|
{
|
||||||
// write all accepted bytes
|
// write all accepted bytes
|
||||||
o->write_characters(string_buffer.data(), bytes_after_last_accept);
|
put_chars(string_buffer.data(), bytes_after_last_accept);
|
||||||
// add a replacement character
|
// add a replacement character
|
||||||
if (ensure_ascii)
|
if (ensure_ascii)
|
||||||
{
|
{
|
||||||
o->write_characters("\\ufffd", 6);
|
put_chars("\\ufffd", 6);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
o->write_characters("\xEF\xBF\xBD", 3);
|
put_chars("\xEF\xBF\xBD", 3);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -628,6 +688,66 @@ class serializer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/*!
|
||||||
|
@brief append a single character to the write buffer
|
||||||
|
|
||||||
|
Structural characters ('{', '"', ',', ...) previously went straight to the
|
||||||
|
output adapter, one virtual call each. Buffering them and flushing in bulk
|
||||||
|
turns those many indirect calls into a single memcpy plus an occasional
|
||||||
|
flush, which dominates the cost of serializing object/array-heavy values.
|
||||||
|
*/
|
||||||
|
void put_char(char c)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(write_buffer_pos == write_buffer.size()))
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
write_buffer[write_buffer_pos++] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief append @a length characters to the write buffer
|
||||||
|
|
||||||
|
Runs that do not fit the buffer are written straight through the output
|
||||||
|
adapter (after flushing what is pending), so large string/number payloads
|
||||||
|
are not copied an extra time.
|
||||||
|
*/
|
||||||
|
JSON_HEDLEY_NON_NULL(2)
|
||||||
|
void put_chars(const char* s, std::size_t length)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(length >= write_buffer.size()))
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
o->write_characters(s, length);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(write_buffer_pos + length > write_buffer.size()))
|
||||||
|
{
|
||||||
|
flush();
|
||||||
|
}
|
||||||
|
std::memcpy(write_buffer.data() + write_buffer_pos, s, length);
|
||||||
|
write_buffer_pos += length;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSON_PRIVATE_UNLESS_TESTED:
|
||||||
|
/*!
|
||||||
|
@brief flush the write buffer to the output adapter
|
||||||
|
|
||||||
|
Writing zero characters is a well-defined no-op for every output adapter, so
|
||||||
|
the buffered length is passed through unconditionally (no empty-guard branch
|
||||||
|
to leave uncovered).
|
||||||
|
|
||||||
|
@note dump_escaped() and dump_integer()/dump_float() write into the internal
|
||||||
|
write buffer; callers that invoke them directly (rather than through the
|
||||||
|
public dump()) must call flush() before inspecting the output.
|
||||||
|
*/
|
||||||
|
void flush()
|
||||||
|
{
|
||||||
|
o->write_characters(write_buffer.data(), write_buffer_pos);
|
||||||
|
write_buffer_pos = 0;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/*!
|
/*!
|
||||||
@brief count digits
|
@brief count digits
|
||||||
@@ -752,7 +872,7 @@ class serializer
|
|||||||
// special case for "0"
|
// special case for "0"
|
||||||
if (x == 0)
|
if (x == 0)
|
||||||
{
|
{
|
||||||
o->write_character('0');
|
put_char('0');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -805,7 +925,7 @@ class serializer
|
|||||||
*(--buffer_ptr) = static_cast<char>('0' + abs_value);
|
*(--buffer_ptr) = static_cast<char>('0' + abs_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
o->write_characters(number_buffer.data(), n_chars);
|
put_chars(number_buffer.data(), n_chars);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@@ -821,7 +941,7 @@ class serializer
|
|||||||
// NaN / inf
|
// NaN / inf
|
||||||
if (!std::isfinite(x))
|
if (!std::isfinite(x))
|
||||||
{
|
{
|
||||||
o->write_characters("null", 4);
|
put_chars("null", 4);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -842,7 +962,7 @@ class serializer
|
|||||||
auto* begin = number_buffer.data();
|
auto* begin = number_buffer.data();
|
||||||
auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
|
auto* end = ::nlohmann::detail::to_chars(begin, begin + number_buffer.size(), x);
|
||||||
|
|
||||||
o->write_characters(begin, static_cast<size_t>(end - begin));
|
put_chars(begin, static_cast<size_t>(end - begin));
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON_HEDLEY_NON_NULL(1)
|
JSON_HEDLEY_NON_NULL(1)
|
||||||
@@ -893,7 +1013,7 @@ class serializer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
o->write_characters(number_buffer.data(), static_cast<std::size_t>(len));
|
put_chars(number_buffer.data(), static_cast<std::size_t>(len));
|
||||||
|
|
||||||
// determine if we need to append ".0"
|
// determine if we need to append ".0"
|
||||||
const bool value_is_int_like =
|
const bool value_is_int_like =
|
||||||
@@ -905,7 +1025,7 @@ class serializer
|
|||||||
|
|
||||||
if (value_is_int_like)
|
if (value_is_int_like)
|
||||||
{
|
{
|
||||||
o->write_characters(".0", 2);
|
put_chars(".0", 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1015,6 +1135,12 @@ class serializer
|
|||||||
|
|
||||||
/// error_handler how to react on decoding errors
|
/// error_handler how to react on decoding errors
|
||||||
const error_handler_t error_handler;
|
const error_handler_t error_handler;
|
||||||
|
|
||||||
|
/// buffer collecting output before it is flushed to the output adapter, so
|
||||||
|
/// that the many small structural writes become few bulk writes
|
||||||
|
std::array<char, 1024> write_buffer{{}};
|
||||||
|
/// number of valid bytes currently held in @ref write_buffer
|
||||||
|
std::size_t write_buffer_pos = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|||||||
+1270
-147
File diff suppressed because it is too large
Load Diff
@@ -132,3 +132,37 @@ TEST_CASE("BJData")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("CBOR")
|
||||||
|
{
|
||||||
|
SECTION("parse errors")
|
||||||
|
{
|
||||||
|
SECTION("array/map size larger than std::size_t")
|
||||||
|
{
|
||||||
|
// declared lengths do not fit in a 32-bit std::size_t and must not be truncated
|
||||||
|
std::vector<uint8_t> const varr = {0x9B, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05};
|
||||||
|
std::vector<uint8_t> const vmap = {0xBB, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05};
|
||||||
|
|
||||||
|
json _;
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(varr), "[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive array size", json::out_of_range&);
|
||||||
|
CHECK(json::from_cbor(varr, true, false).is_discarded());
|
||||||
|
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vmap), "[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive map size", json::out_of_range&);
|
||||||
|
CHECK(json::from_cbor(vmap, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("array/map size equal to the indefinite-length sentinel")
|
||||||
|
{
|
||||||
|
// on 32-bit platforms a four-byte length of 0xFFFFFFFF aliases unknown_size()
|
||||||
|
std::vector<uint8_t> const varr = {0x9A, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||||
|
std::vector<uint8_t> const vmap = {0xBA, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||||
|
|
||||||
|
json _;
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(varr), "[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive array size", json::out_of_range&);
|
||||||
|
CHECK(json::from_cbor(varr, true, false).is_discarded());
|
||||||
|
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(vmap), "[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive map size", json::out_of_range&);
|
||||||
|
CHECK(json::from_cbor(vmap, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1347,6 +1347,8 @@ TEST_CASE("BJData")
|
|||||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec2), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1A", json::parse_error);
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec2), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1A", json::parse_error);
|
||||||
std::vector<uint8_t> const vec3 = {'H', 'i', 2, '1', '.'};
|
std::vector<uint8_t> const vec3 = {'H', 'i', 2, '1', '.'};
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1.", json::parse_error);
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing BJData high-precision number: invalid number text: 1.", json::parse_error);
|
||||||
|
std::vector<uint8_t> const vec_overflow = {'H', 'i', 5, '1', 'e', '4', '0', '0'};
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec_overflow), "[json.exception.out_of_range.406] number overflow parsing '1e400'", json::out_of_range);
|
||||||
std::vector<uint8_t> const vec4 = {'H', 2, '1', '0'};
|
std::vector<uint8_t> const vec4 = {'H', 2, '1', '0'};
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x02", json::parse_error);
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x02", json::parse_error);
|
||||||
}
|
}
|
||||||
@@ -2587,6 +2589,69 @@ TEST_CASE("BJData")
|
|||||||
CHECK(json::to_bjdata(json::from_bjdata(v_B), true, true) == v_B);
|
CHECK(json::to_bjdata(json::from_bjdata(v_B), true, true) == v_B);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("ndarray with data not matching _ArrayType_ is written as an object")
|
||||||
|
{
|
||||||
|
// A JData-annotated object is only serialized as an ndarray when
|
||||||
|
// its _ArrayData_ elements are actually stored as the number kind
|
||||||
|
// named by _ArrayType_. Otherwise the writer would read the wrong
|
||||||
|
// union member (e.g. a std::string's heap pointer as a uint64) and
|
||||||
|
// emit it, so such an object falls back to a plain object encoding
|
||||||
|
// that still round-trips.
|
||||||
|
|
||||||
|
// string data declared as a uint64 array
|
||||||
|
json const j_str = json({{"_ArrayType_", "uint64"}, {"_ArraySize_", {1}}, {"_ArrayData_", {"pointer"}}});
|
||||||
|
const auto out_str = json::to_bjdata(j_str);
|
||||||
|
CHECK(out_str.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_str) == j_str);
|
||||||
|
|
||||||
|
// integer data declared as a double array
|
||||||
|
json const j_float = json({{"_ArrayType_", "double"}, {"_ArraySize_", {2}}, {"_ArrayData_", {1, 2}}});
|
||||||
|
const auto out_float = json::to_bjdata(j_float);
|
||||||
|
CHECK(out_float.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_float) == j_float);
|
||||||
|
|
||||||
|
// a non-integer shape entry is likewise not treated as an ndarray
|
||||||
|
json const j_size = json({{"_ArrayType_", "uint8"}, {"_ArraySize_", {"x"}}, {"_ArrayData_", {1}}});
|
||||||
|
const auto out_size = json::to_bjdata(j_size);
|
||||||
|
CHECK(out_size.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_size) == j_size);
|
||||||
|
|
||||||
|
// a negative shape entry is not a usable dimension either
|
||||||
|
json const j_neg = json::parse(R"({"_ArrayType_":"uint8","_ArraySize_":[-1],"_ArrayData_":[1]})");
|
||||||
|
const auto out_neg = json::to_bjdata(j_neg);
|
||||||
|
CHECK(out_neg.at(0) == '{');
|
||||||
|
CHECK(json::from_bjdata(out_neg) == j_neg);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("ndarray parsed from text is written as a typed array")
|
||||||
|
{
|
||||||
|
// json::parse stores a non-negative integer as number_unsigned while
|
||||||
|
// the C++ API stores an int literal as number_integer, so _ArrayType_
|
||||||
|
// names the wire type rather than the storage. Both storages have to
|
||||||
|
// produce the same typed array for every type.
|
||||||
|
for (const char* type :
|
||||||
|
{"uint8", "int8", "uint16", "int16", "uint32", "int32", "uint64", "int64", "char", "byte"
|
||||||
|
})
|
||||||
|
{
|
||||||
|
CAPTURE(type);
|
||||||
|
const std::string text = std::string(R"({"_ArrayType_":")") + type +
|
||||||
|
R"(","_ArraySize_":[2,3],"_ArrayData_":[1,2,3,4,5,6]})";
|
||||||
|
const auto from_text = json::to_bjdata(json::parse(text));
|
||||||
|
CHECK(from_text.at(0) == '[');
|
||||||
|
CHECK(from_text == json::to_bjdata(json({{"_ArrayType_", type}, {"_ArraySize_", {2, 3}}, {"_ArrayData_", {1, 2, 3, 4, 5, 6}}})));
|
||||||
|
}
|
||||||
|
|
||||||
|
// negative values under a signed type behave the same way
|
||||||
|
const auto from_neg = json::to_bjdata(json::parse(R"({"_ArrayType_":"int32","_ArraySize_":[2],"_ArrayData_":[-5,7]})"));
|
||||||
|
CHECK(from_neg.at(0) == '[');
|
||||||
|
CHECK(from_neg == json::to_bjdata(json({{"_ArrayType_", "int32"}, {"_ArraySize_", {2}}, {"_ArrayData_", {-5, 7}}})));
|
||||||
|
|
||||||
|
// and so do the floating point types
|
||||||
|
const auto from_float = json::to_bjdata(json::parse(R"({"_ArrayType_":"double","_ArraySize_":[2],"_ArrayData_":[1.5,2.5]})"));
|
||||||
|
CHECK(from_float.at(0) == '[');
|
||||||
|
CHECK(from_float == json::to_bjdata(json({{"_ArrayType_", "double"}, {"_ArraySize_", {2}}, {"_ArrayData_", {1.5, 2.5}}})));
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("optimized ndarray (type and vector-size as 1D array)")
|
SECTION("optimized ndarray (type and vector-size as 1D array)")
|
||||||
{
|
{
|
||||||
// create vector with two elements of the same type
|
// create vector with two elements of the same type
|
||||||
|
|||||||
@@ -529,6 +529,41 @@ TEST_CASE("BSON")
|
|||||||
CHECK(json::from_bson(result, true, false) == j);
|
CHECK(json::from_bson(result, true, false) == j);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("non-empty object with binary member without subtype")
|
||||||
|
{
|
||||||
|
const size_t N = 10;
|
||||||
|
const auto s = std::vector<std::uint8_t>(N, 'x');
|
||||||
|
json const j =
|
||||||
|
{
|
||||||
|
{ "entry", json::binary(s) }
|
||||||
|
};
|
||||||
|
|
||||||
|
CHECK(!j.at("entry").get_binary().has_subtype());
|
||||||
|
|
||||||
|
std::vector<std::uint8_t> const expected =
|
||||||
|
{
|
||||||
|
0x1B, 0x00, 0x00, 0x00, // size (little endian)
|
||||||
|
0x05, // entry: binary
|
||||||
|
'e', 'n', 't', 'r', 'y', '\x00',
|
||||||
|
|
||||||
|
0x0A, 0x00, 0x00, 0x00, // size of binary (little endian)
|
||||||
|
0x00, // Generic binary subtype
|
||||||
|
0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78, 0x78,
|
||||||
|
|
||||||
|
0x00 // end marker
|
||||||
|
};
|
||||||
|
|
||||||
|
const auto result = json::to_bson(j);
|
||||||
|
CHECK(result == expected);
|
||||||
|
|
||||||
|
// roundtrip adds the generic binary subtype
|
||||||
|
const auto roundtrip = json::from_bson(result);
|
||||||
|
CHECK(roundtrip != j);
|
||||||
|
CHECK(roundtrip.at("entry").get_binary().has_subtype());
|
||||||
|
CHECK(roundtrip.at("entry").get_binary().subtype() == 0);
|
||||||
|
CHECK(json::from_bson(result, true, false) == roundtrip);
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("non-empty object with binary member with subtype")
|
SECTION("non-empty object with binary member with subtype")
|
||||||
{
|
{
|
||||||
// an MD5 hash
|
// an MD5 hash
|
||||||
|
|||||||
@@ -1999,6 +1999,42 @@ TEST_CASE("CBOR regressions")
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
TEST_CASE("CBOR definite length equal to the indefinite-length sentinel")
|
||||||
|
{
|
||||||
|
// A definite-length array or map whose declared element count equals the
|
||||||
|
// reserved unknown_size() sentinel (SIZE_MAX) must be rejected. Otherwise
|
||||||
|
// it is read as an indefinite-length container and the following bytes are
|
||||||
|
// silently accepted instead of the (impossible) count being reported.
|
||||||
|
json _;
|
||||||
|
|
||||||
|
SECTION("array")
|
||||||
|
{
|
||||||
|
// 0x9B: array with eight-byte length; length = 0xFFFFFFFFFFFFFFFF
|
||||||
|
const std::vector<uint8_t> input = {0x9B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x02, 0xFF};
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive array size", json::out_of_range&);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("map")
|
||||||
|
{
|
||||||
|
// 0xBB: map with eight-byte length; length = 0xFFFFFFFFFFFFFFFF
|
||||||
|
const std::vector<uint8_t> input = {0xBB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x61, 0x61, 0x01, 0xFF};
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive map size", json::out_of_range&);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("indefinite-length containers are unaffected")
|
||||||
|
{
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x9F, 0x01, 0x02, 0xFF})) == json({1, 2}));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xBF, 0x61, 0x61, 0x01, 0xFF})) == json({{"a", 1}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("ordinary four-byte length containers are unaffected")
|
||||||
|
{
|
||||||
|
// 0x9A/0xBA carry a four-byte length; a normal count still parses
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x9A, 0x00, 0x00, 0x00, 0x02, 0x01, 0x02})) == json({1, 2}));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xBA, 0x00, 0x00, 0x00, 0x01, 0x61, 0x61, 0x01})) == json({{"a", 1}}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("CBOR roundtrips" * doctest::skip())
|
TEST_CASE("CBOR roundtrips" * doctest::skip())
|
||||||
{
|
{
|
||||||
SECTION("input from flynn")
|
SECTION("input from flynn")
|
||||||
|
|||||||
@@ -12,6 +12,10 @@
|
|||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
using nlohmann::json;
|
using nlohmann::json;
|
||||||
|
|
||||||
|
#include <sstream> // stringstream
|
||||||
|
#include <string> // string
|
||||||
|
#include <vector> // vector
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
// shortcut to scan a string literal
|
// shortcut to scan a string literal
|
||||||
@@ -224,3 +228,75 @@ TEST_CASE("lexer class")
|
|||||||
CHECK((scan_string("/**//**//**/", true) == json::lexer::token_type::end_of_input));
|
CHECK((scan_string("/**//**//**/", true) == json::lexer::token_type::end_of_input));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("lexer number fast path")
|
||||||
|
{
|
||||||
|
// The contiguous fast path (used for pointer/string input) must agree with
|
||||||
|
// the streaming byte path (used for std::istream) on token type, numeric
|
||||||
|
// value, and round-trip text for every well-formed number, and reject the
|
||||||
|
// same malformed numbers with the same message.
|
||||||
|
SECTION("contiguous vs streaming parity")
|
||||||
|
{
|
||||||
|
const std::vector<std::string> numbers =
|
||||||
|
{
|
||||||
|
"0", "-0", "1", "-1", "42", "-42", "10", "100", "1234567890",
|
||||||
|
"0.0", "-0.0", "3.14", "-3.14", "0.5", "-0.001", "123.456789",
|
||||||
|
"1e0", "1E0", "1e10", "1e-10", "1e+10", "1.5e3", "-2.5E-4",
|
||||||
|
"9223372036854775807", // INT64_MAX -> unsigned
|
||||||
|
"9223372036854775808", // INT64_MAX + 1 -> unsigned
|
||||||
|
"18446744073709551615", // UINT64_MAX -> unsigned
|
||||||
|
"18446744073709551616", // UINT64_MAX + 1 -> float
|
||||||
|
"-9223372036854775808", // INT64_MIN -> integer
|
||||||
|
"-9223372036854775809", // INT64_MIN - 1 -> float
|
||||||
|
"123456789012345678901234567890", // huge -> float
|
||||||
|
"0.30000000000000004", "2.2250738585072014e-308", "1e308",
|
||||||
|
// high-precision / wide-exponent values that exercise the
|
||||||
|
// std::from_chars (Eisel-Lemire) path beyond the Clinger subset
|
||||||
|
"1.7976931348623157e308", "1.2345678901234567e-250",
|
||||||
|
"9007199254740993", "5e-324", "1e-320"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& n : numbers)
|
||||||
|
{
|
||||||
|
const std::string doc = "[" + n + "]";
|
||||||
|
|
||||||
|
// contiguous fast path
|
||||||
|
const json a = json::parse(doc);
|
||||||
|
// streaming byte path
|
||||||
|
std::stringstream ss(doc);
|
||||||
|
const json b = json::parse(ss);
|
||||||
|
|
||||||
|
CAPTURE(n);
|
||||||
|
CHECK(a == b);
|
||||||
|
CHECK(a.dump() == b.dump());
|
||||||
|
CHECK(a[0].type() == b[0].type());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("token type classification")
|
||||||
|
{
|
||||||
|
CHECK((scan_string("0") == json::lexer::token_type::value_unsigned));
|
||||||
|
CHECK((scan_string("-1") == json::lexer::token_type::value_integer));
|
||||||
|
CHECK((scan_string("1.5") == json::lexer::token_type::value_float));
|
||||||
|
CHECK((scan_string("1e5") == json::lexer::token_type::value_float));
|
||||||
|
CHECK((scan_string("18446744073709551615") == json::lexer::token_type::value_unsigned));
|
||||||
|
CHECK((scan_string("18446744073709551616") == json::lexer::token_type::value_float));
|
||||||
|
CHECK((scan_string("-9223372036854775808") == json::lexer::token_type::value_integer));
|
||||||
|
CHECK((scan_string("-9223372036854775809") == json::lexer::token_type::value_float));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("malformed numbers are rejected identically")
|
||||||
|
{
|
||||||
|
for (const char* bad :
|
||||||
|
{"-", "1.", "1e", "1e+", "1.2e", "01", "-01", "1..2", "1.2.3"
|
||||||
|
})
|
||||||
|
{
|
||||||
|
CAPTURE(bad);
|
||||||
|
// the contiguous fast path must decline and let the byte path report
|
||||||
|
const std::string doc = std::string("[") + bad + "]";
|
||||||
|
CHECK_FALSE(json::accept(doc));
|
||||||
|
std::stringstream ss(doc);
|
||||||
|
CHECK_FALSE(json::accept(ss));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1440,6 +1440,13 @@ TEST_CASE("parser class")
|
|||||||
]
|
]
|
||||||
)";
|
)";
|
||||||
|
|
||||||
|
const auto* structured_object = R"(
|
||||||
|
{
|
||||||
|
"foo": [1, 2],
|
||||||
|
"bar": 3
|
||||||
|
}
|
||||||
|
)";
|
||||||
|
|
||||||
SECTION("filter nothing")
|
SECTION("filter nothing")
|
||||||
{
|
{
|
||||||
const json j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept
|
const json j_object = json::parse(s_object, [](int /*unused*/, json::parse_event_t /*unused*/, const json& /*unused*/) noexcept
|
||||||
@@ -1515,6 +1522,48 @@ TEST_CASE("parser class")
|
|||||||
CHECK (j_filtered2 == json({1}));
|
CHECK (j_filtered2 == json({1}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("filter array in object")
|
||||||
|
{
|
||||||
|
// the array is discarded once it is already stored under its key
|
||||||
|
const json j_filtered1 = json::parse(structured_object, [](int /*unused*/, json::parse_event_t e, const json& /*parsed*/) noexcept
|
||||||
|
{
|
||||||
|
return e != json::parse_event_t::array_end;
|
||||||
|
});
|
||||||
|
|
||||||
|
CHECK (j_filtered1 == json({{"bar", 3}}));
|
||||||
|
|
||||||
|
// the array is discarded before it is stored, leaving the
|
||||||
|
// placeholder the key event wrote
|
||||||
|
const json j_filtered2 = json::parse(structured_object, [](int /*unused*/, json::parse_event_t e, const json& /*parsed*/) noexcept
|
||||||
|
{
|
||||||
|
return e != json::parse_event_t::array_start;
|
||||||
|
});
|
||||||
|
|
||||||
|
CHECK (j_filtered2 == json({{"bar", 3}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("filter value in object")
|
||||||
|
{
|
||||||
|
// the value is discarded after its key was kept, leaving the
|
||||||
|
// placeholder the key event wrote
|
||||||
|
const json j_filtered1 = json::parse(structured_object, [](int /*unused*/, json::parse_event_t e, const json & parsed) noexcept
|
||||||
|
{
|
||||||
|
return !(e == json::parse_event_t::value && parsed == json(3));
|
||||||
|
});
|
||||||
|
|
||||||
|
CHECK (j_filtered1 == json({{"foo", {1, 2}}}));
|
||||||
|
|
||||||
|
// the same value is discarded together with its key, so no
|
||||||
|
// placeholder was stored for it
|
||||||
|
const json j_filtered2 = json::parse(structured_object, [](int /*unused*/, json::parse_event_t e, const json & parsed) noexcept
|
||||||
|
{
|
||||||
|
return !((e == json::parse_event_t::key && parsed == json("bar")) ||
|
||||||
|
(e == json::parse_event_t::value && parsed == json(3)));
|
||||||
|
});
|
||||||
|
|
||||||
|
CHECK (j_filtered2 == json({{"foo", {1, 2}}}));
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("filter specific events")
|
SECTION("filter specific events")
|
||||||
{
|
{
|
||||||
SECTION("first closing event")
|
SECTION("first closing event")
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ void check_escaped(const char* original, const char* escaped, const bool ensure_
|
|||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
json::serializer s(nlohmann::detail::output_adapter<char>(ss), ' ');
|
json::serializer s(nlohmann::detail::output_adapter<char>(ss), ' ');
|
||||||
s.dump_escaped(original, ensure_ascii);
|
s.dump_escaped(original, ensure_ascii);
|
||||||
|
s.flush(); // dump_escaped writes into the serializer's internal buffer
|
||||||
CHECK(ss.str() == escaped);
|
CHECK(ss.str() == escaped);
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -427,6 +427,30 @@ TEST_CASE("deserialization")
|
|||||||
CHECK(l.events == std::vector<std::string>({"boolean(true)"}));
|
CHECK(l.events == std::vector<std::string>({"boolean(true)"}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("from std::vector<signed char>")
|
||||||
|
{
|
||||||
|
std::vector<signed char> const v = {'t', 'r', 'u', 'e'};
|
||||||
|
CHECK(json::parse(v) == json(true));
|
||||||
|
CHECK(json::accept(v));
|
||||||
|
|
||||||
|
SaxEventLogger l;
|
||||||
|
CHECK(json::sax_parse(v, &l));
|
||||||
|
CHECK(l.events.size() == 1);
|
||||||
|
CHECK(l.events == std::vector<std::string>({"boolean(true)"}));
|
||||||
|
|
||||||
|
// bytes outside ASCII are negative here and must not be sign-extended;
|
||||||
|
// 0xC3 and 0xA9 do not fit in signed char (MSVC C4309), so spell them as negative values
|
||||||
|
std::vector<signed char> const umlaut = {'"', static_cast<signed char>(0xC3 - 0x100), static_cast<signed char>(0xA9 - 0x100), '"'};
|
||||||
|
CHECK(json::parse(umlaut) == json("\xC3\xA9"));
|
||||||
|
CHECK(json::accept(umlaut));
|
||||||
|
|
||||||
|
// 0xFF (spelled as -1 to stay in range) must not be reported as end of input
|
||||||
|
std::vector<signed char> const trailing = {'t', 'r', 'u', 'e', static_cast<signed char>(0xFF - 0x100)};
|
||||||
|
json _;
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::parse(trailing), "[json.exception.parse_error.101] parse error at line 1, column 5: syntax error while parsing value - invalid literal; last read: 'true\xFF'; expected end of input", json::parse_error&);
|
||||||
|
CHECK(!json::accept(trailing));
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("from std::array")
|
SECTION("from std::array")
|
||||||
{
|
{
|
||||||
std::array<uint8_t, 5> const v { {'t', 'r', 'u', 'e'} };
|
std::array<uint8_t, 5> const v { {'t', 'r', 'u', 'e'} };
|
||||||
|
|||||||
@@ -1530,4 +1530,29 @@ TEST_CASE("issue #4320 - custom base class must not leak nlohmann::detail into A
|
|||||||
CHECK(j == json({{"x", 1.0}, {"y", 2.0}, {"z", 3.0}}));
|
CHECK(j == json({{"x", 1.0}, {"y", 2.0}, {"z", 3.0}}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("issue #5338 - truncated CBOR tagged binary subtype is rejected")
|
||||||
|
{
|
||||||
|
const std::vector<std::vector<std::uint8_t>> truncated_tags =
|
||||||
|
{
|
||||||
|
{0xD8},
|
||||||
|
{0xD9, 0x00},
|
||||||
|
{0xDA, 0x00, 0x00, 0x00},
|
||||||
|
{0xDB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& data : truncated_tags)
|
||||||
|
{
|
||||||
|
CAPTURE(data);
|
||||||
|
for (const auto tag_handler :
|
||||||
|
{
|
||||||
|
json::cbor_tag_handler_t::ignore, json::cbor_tag_handler_t::store
|
||||||
|
})
|
||||||
|
{
|
||||||
|
CAPTURE(tag_handler);
|
||||||
|
const auto result = json::from_cbor(data, true, false, tag_handler);
|
||||||
|
CHECK(result.is_discarded());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING_POP
|
DOCTEST_CLANG_SUPPRESS_WARNING_POP
|
||||||
|
|||||||
@@ -382,3 +382,91 @@ TEST_CASE("dump for basic_json with long double number_float_t")
|
|||||||
check_same(100.0L, 100.0);
|
check_same(100.0L, 100.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("serialization of strings (bulk fast path)")
|
||||||
|
{
|
||||||
|
// These cases exercise the SWAR bulk-copy fast path in dump_escaped and the
|
||||||
|
// internal write buffer: long runs, escapes interrupting runs, 0x7F/DEL,
|
||||||
|
// multibyte UTF-8 under both ensure_ascii settings, and payloads larger than
|
||||||
|
// the write buffer.
|
||||||
|
|
||||||
|
SECTION("long unescaped ASCII exceeds the write buffer")
|
||||||
|
{
|
||||||
|
const std::string big(3000, 'a');
|
||||||
|
const json j = big;
|
||||||
|
CHECK(j.dump() == '"' + big + '"');
|
||||||
|
CHECK(j.dump(-1, ' ', true) == '"' + big + '"');
|
||||||
|
// round-trips
|
||||||
|
CHECK(json::parse(j.dump()) == j);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("runs interrupted by escapes")
|
||||||
|
{
|
||||||
|
const json j = std::string(500, 'x') + "\n\"\\" + std::string(500, 'y');
|
||||||
|
const std::string out = j.dump();
|
||||||
|
CHECK(out == '"' + std::string(500, 'x') + "\\n\\\"\\\\" + std::string(500, 'y') + '"');
|
||||||
|
CHECK(json::parse(out) == j);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("DEL (0x7F) depends on ensure_ascii")
|
||||||
|
{
|
||||||
|
const json j = std::string("a\x7f" "b");
|
||||||
|
CHECK(j.dump(-1, ' ', false) == "\"a\x7f" "b\""); // copied verbatim
|
||||||
|
CHECK(j.dump(-1, ' ', true) == "\"a\\u007fb\""); // escaped
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("multibyte UTF-8 under both ensure_ascii settings")
|
||||||
|
{
|
||||||
|
const json j = std::string("A\xc3\xa9\xe4\xbd\xa0\xf0\x9f\x98\x80Z"); // A é 你 😀 Z
|
||||||
|
// not escaping non-ASCII: bytes are copied through the bulk validator
|
||||||
|
CHECK(j.dump(-1, ' ', false) == "\"A\xc3\xa9\xe4\xbd\xa0\xf0\x9f\x98\x80Z\"");
|
||||||
|
// ensure_ascii: escaped (with a surrogate pair for the emoji)
|
||||||
|
CHECK(j.dump(-1, ' ', true) == "\"A\\u00e9\\u4f60\\ud83d\\ude00Z\"");
|
||||||
|
CHECK(json::parse(j.dump(-1, ' ', true)) == j);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("many small structural writes exceed the write buffer")
|
||||||
|
{
|
||||||
|
json arr = json::array();
|
||||||
|
for (int i = 0; i < 2000; ++i)
|
||||||
|
{
|
||||||
|
arr.push_back(i);
|
||||||
|
}
|
||||||
|
const std::string out = arr.dump();
|
||||||
|
CHECK(out.front() == '[');
|
||||||
|
CHECK(out.back() == ']');
|
||||||
|
CHECK(json::parse(out) == arr);
|
||||||
|
|
||||||
|
json obj = json::object();
|
||||||
|
for (int i = 0; i < 500; ++i)
|
||||||
|
{
|
||||||
|
obj["key" + std::to_string(i)] = i;
|
||||||
|
}
|
||||||
|
CHECK(json::parse(obj.dump()) == obj);
|
||||||
|
CHECK(json::parse(obj.dump(2)) == obj);
|
||||||
|
|
||||||
|
// an array of many empty strings emits a long run of single-character
|
||||||
|
// writes ('"', '"', ',') at shallow nesting depth, so the write buffer
|
||||||
|
// fills and flushes mid-run without the deep recursion that would
|
||||||
|
// overflow the stack on some debug builds
|
||||||
|
json many_empty = json::array();
|
||||||
|
for (int i = 0; i < 500; ++i)
|
||||||
|
{
|
||||||
|
many_empty.push_back("");
|
||||||
|
}
|
||||||
|
const std::string out2 = many_empty.dump();
|
||||||
|
CHECK(out2.size() > 1024); // spans multiple write-buffer flushes
|
||||||
|
CHECK(out2.front() == '[');
|
||||||
|
CHECK(out2.back() == ']');
|
||||||
|
CHECK(json::parse(out2) == many_empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("invalid UTF-8 handling is unaffected by the fast path")
|
||||||
|
{
|
||||||
|
const json j = std::string("valid\xff" "more");
|
||||||
|
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 5: 0xFF", json::type_error&);
|
||||||
|
CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"valid\xef\xbf\xbd" "more\"");
|
||||||
|
CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"valid\\ufffdmore\"");
|
||||||
|
CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"validmore\"");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,4 +53,34 @@ TEST_CASE("type traits")
|
|||||||
// NOLINTEND(hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
|
// NOLINTEND(hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("char_traits")
|
||||||
|
{
|
||||||
|
SECTION("to_int_type does not sign-extend")
|
||||||
|
{
|
||||||
|
using unsigned_traits = nlohmann::detail::char_traits<unsigned char>;
|
||||||
|
using signed_traits = nlohmann::detail::char_traits<signed char>;
|
||||||
|
|
||||||
|
CHECK(unsigned_traits::to_int_type(static_cast<unsigned char>(0x7F)) == 0x7F);
|
||||||
|
CHECK(unsigned_traits::to_int_type(static_cast<unsigned char>(0x80)) == 0x80);
|
||||||
|
CHECK(unsigned_traits::to_int_type(static_cast<unsigned char>(0xFF)) == 0xFF);
|
||||||
|
|
||||||
|
CHECK(signed_traits::to_int_type(static_cast<signed char>(0x7F)) == 0x7F);
|
||||||
|
// 0x80 and 0xFF do not fit in signed char (MSVC C4309), so spell them as negative values
|
||||||
|
CHECK(signed_traits::to_int_type(static_cast<signed char>(0x80 - 0x100)) == 0x80);
|
||||||
|
CHECK(signed_traits::to_int_type(static_cast<signed char>(0xFF - 0x100)) == 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("no byte value collides with eof")
|
||||||
|
{
|
||||||
|
using unsigned_traits = nlohmann::detail::char_traits<unsigned char>;
|
||||||
|
using signed_traits = nlohmann::detail::char_traits<signed char>;
|
||||||
|
|
||||||
|
for (int i = 0; i < 256; ++i)
|
||||||
|
{
|
||||||
|
CHECK(unsigned_traits::to_int_type(static_cast<unsigned char>(i)) != unsigned_traits::eof());
|
||||||
|
CHECK(signed_traits::to_int_type(static_cast<signed char>(i)) != signed_traits::eof());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -819,6 +819,8 @@ TEST_CASE("UBJSON")
|
|||||||
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec2), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A", json::parse_error);
|
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec2), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1A", json::parse_error);
|
||||||
std::vector<uint8_t> const vec3 = {'H', 'i', 2, '1', '.'};
|
std::vector<uint8_t> const vec3 = {'H', 'i', 2, '1', '.'};
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1.", json::parse_error);
|
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec3), "[json.exception.parse_error.115] parse error at byte 5: syntax error while parsing UBJSON high-precision number: invalid number text: 1.", json::parse_error);
|
||||||
|
std::vector<uint8_t> const vec_overflow = {'H', 'i', 5, '1', 'e', '4', '0', '0'};
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec_overflow), "[json.exception.out_of_range.406] number overflow parsing '1e400'", json::out_of_range&);
|
||||||
std::vector<uint8_t> const vec4 = {'H', 2, '1', '0'};
|
std::vector<uint8_t> const vec4 = {'H', 2, '1', '0'};
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON size: expected length type specification (U, i, I, l, L) after '#'; last byte: 0x02", json::parse_error);
|
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(vec4), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing UBJSON size: expected length type specification (U, i, I, l, L) after '#'; last byte: 0x02", json::parse_error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user