mirror of
https://github.com/nlohmann/json.git
synced 2026-11-13 15:35:23 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f2f64eb67c | ||
|
|
3004829134 |
No files matched your search
@@ -25,7 +25,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -38,7 +38,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Check that Meson and pkg-config offer the CMake options
|
- name: Check that Meson and pkg-config offer the CMake options
|
||||||
run: make check_build_options
|
run: make check_build_options
|
||||||
- name: Install Meson
|
- name: Install Meson
|
||||||
@@ -92,7 +92,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -123,7 +123,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -142,7 +142,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -168,7 +168,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -244,7 +244,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: CXX=g++-${{ matrix.compiler }} cmake -S . -B build -DJSON_CI=On
|
run: CXX=g++-${{ matrix.compiler }} cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -262,7 +262,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -279,7 +279,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Disable filesystem support (std::filesystem is unusable) for clang 7 / 8 / 9 / 10
|
- name: Disable filesystem support (std::filesystem is unusable) for clang 7 / 8 / 9 / 10
|
||||||
run: echo "JSON_FORCED_GLOBAL_COMPILE_OPTIONS=-DJSON_HAS_FILESYSTEM=0;-DJSON_HAS_EXPERIMENTAL_FILESYSTEM=0" >> "$GITHUB_ENV"
|
run: echo "JSON_FORCED_GLOBAL_COMPILE_OPTIONS=-DJSON_HAS_FILESYSTEM=0;-DJSON_HAS_EXPERIMENTAL_FILESYSTEM=0" >> "$GITHUB_ENV"
|
||||||
if: ${{ matrix.compiler == '7' || matrix.compiler == '8' || matrix.compiler == '9' || matrix.compiler == '10' }}
|
if: ${{ matrix.compiler == '7' || matrix.compiler == '8' || matrix.compiler == '9' || matrix.compiler == '10' }}
|
||||||
@@ -299,7 +299,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -319,7 +319,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build with libc++
|
- name: Build with libc++
|
||||||
@@ -346,7 +346,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -362,13 +362,11 @@ jobs:
|
|||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
# The module test uses `import std;`, which needs CMake's experimental
|
# The module test uses `import std;`, which needs CMake 4.5 or newer.
|
||||||
# import-std support. Its opt-in token is CMake-version-specific, so pin
|
- name: Get CMake 4.5+ and ninja
|
||||||
# CMake to the version whose token is set in tests/module_cpp20/CMakeLists.txt.
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Get pinned CMake and ninja
|
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
|
||||||
with:
|
with:
|
||||||
cmakeVersion: 4.3.4
|
cmakeVersion: '^4.5.0'
|
||||||
# Clang: the std library module is provided by libc++ (the image's libstdc++
|
# Clang: the std library module is provided by libc++ (the image's libstdc++
|
||||||
# ships none), and the image's libc++ module manifest has a broken relative
|
# ships none), and the image's libc++ module manifest has a broken relative
|
||||||
# path — repoint it at the real module sources.
|
# path — repoint it at the real module sources.
|
||||||
@@ -392,7 +390,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -409,7 +407,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -423,7 +421,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DJSON_CI=On
|
run: cmake -S . -B build -DJSON_CI=On
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -443,7 +441,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
run: cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -GNinja
|
run: cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -GNinja
|
||||||
- name: Build
|
- name: Build
|
||||||
@@ -484,7 +482,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Check that the job runs natively on arm64
|
- name: Check that the job runs natively on arm64
|
||||||
run: uname -m && test "$(uname -m)" = aarch64
|
run: uname -m && test "$(uname -m)" = aarch64
|
||||||
- name: Run CMake
|
- name: Run CMake
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
- name: Get latest CMake and ninja
|
- name: Get latest CMake and ninja
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Set extra CXX_FLAGS for latest std_version
|
- name: Set extra CXX_FLAGS for latest std_version
|
||||||
# /wd5285 silences C5285 emitted by the bundled third-party doctest.h, which
|
# /wd5285 silences C5285 emitted by the bundled third-party doctest.h, which
|
||||||
# specializes std::tuple (newly diagnosed by the VS2026 v145 toolset)
|
# specializes std::tuple (newly diagnosed by the VS2026 v145 toolset)
|
||||||
@@ -212,13 +212,11 @@ jobs:
|
|||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
with:
|
with:
|
||||||
persist-credentials: false
|
persist-credentials: false
|
||||||
# The module test uses `import std;`, which needs CMake's experimental
|
# The module test uses `import std;`, which needs CMake 4.5 or newer.
|
||||||
# import-std support. Its opt-in token is CMake-version-specific, so pin
|
- name: Get CMake 4.5+ and ninja
|
||||||
# CMake to the version whose token is set in tests/module_cpp20/CMakeLists.txt.
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
- name: Get pinned CMake and ninja
|
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
|
||||||
with:
|
with:
|
||||||
cmakeVersion: 4.3.4
|
cmakeVersion: '^4.5.0'
|
||||||
# CMAKE_CXX_FLAGS replaces CMake's MSVC defaults (which include /EHsc), and
|
# CMAKE_CXX_FLAGS replaces CMake's MSVC defaults (which include /EHsc), and
|
||||||
# ci_module_cpp20 forwards it to the module build, so list /EHsc explicitly
|
# ci_module_cpp20 forwards it to the module build, so list /EHsc explicitly
|
||||||
- name: Run CMake (Debug)
|
- name: Run CMake (Debug)
|
||||||
|
|||||||
@@ -165,7 +165,8 @@ std::ifstream f("example.json");
|
|||||||
json data = json::parse(f);
|
json data = json::parse(f);
|
||||||
```
|
```
|
||||||
|
|
||||||
If using modules (enabled with `NLOHMANN_JSON_BUILD_MODULES`), this example becomes:
|
If using modules (enabled with `NLOHMANN_JSON_BUILD_MODULES`; `import std;` needs CMake 4.5 or newer, see the
|
||||||
|
[modules documentation](https://json.nlohmann.me/features/modules/)), this example becomes:
|
||||||
```cpp
|
```cpp
|
||||||
import std;
|
import std;
|
||||||
import nlohmann.json;
|
import nlohmann.json;
|
||||||
|
|||||||
@@ -58,9 +58,7 @@ is not UTF-8 encoded and `error_handler` is set to `strict`
|
|||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
Linear in the size of the result. With an `indent`, every line starts with up to `indent` characters per nesting level,
|
Linear.
|
||||||
so the result can be larger than the value by a factor of its nesting depth (see
|
|
||||||
[Parsing untrusted input](../../features/parsing/untrusted_input.md#nesting-depth)).
|
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,7 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
Linear in the size of the result. Every key repeats the path of its value, so the keys can be larger than the value by a
|
Linear in the size of the JSON value.
|
||||||
factor of its nesting depth (see [Parsing untrusted input](../../features/parsing/untrusted_input.md#nesting-depth)).
|
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
|||||||
@@ -89,21 +89,19 @@ This differs from `#!cpp std::map`, where the same operations are O(log n).
|
|||||||
!!! warning "Quadratic cost of building large objects"
|
!!! warning "Quadratic cost of building large objects"
|
||||||
|
|
||||||
Because every insertion scans all elements inserted so far, building an object of `n` distinct keys costs
|
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 (with
|
**O(n²)** in total. This applies to filling an [`ordered_json`](ordered_json.md) object key by key as well as to
|
||||||
`operator[]`, `emplace`, or `insert`) and, for now, to reading one with a binary reader such as
|
parsing one, since the parser inserts each key as it is read.
|
||||||
[`from_cbor`](basic_json/from_cbor.md) ([#6104](https://github.com/nlohmann/json/issues/6104)). Parsing JSON text
|
|
||||||
is not affected: the parser finds the keys of an object with many members in a hash table and takes linear time.
|
|
||||||
|
|
||||||
The cost is negligible for the object sizes typically found in configuration files or API payloads, but it grows
|
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 Apple clang 21, `-O2 -DNDEBUG`,
|
steeply for machine-generated objects with many thousands of keys. Measured with `-O2 -DNDEBUG` for parsing a flat
|
||||||
arm64, for a flat object of `n` keys (best of seven runs):
|
object of `n` keys, relative to `#!cpp nlohmann::json` (which uses `#!cpp std::map`):
|
||||||
|
|
||||||
| `n` | `json::parse` | `ordered_json::parse` | `ordered_json`, key by key | `ordered_json::from_cbor` |
|
| `n` | `json` | `ordered_json` | factor |
|
||||||
|--------|---------------|-----------------------|----------------------------|---------------------------|
|
|--------|--------|----------------|--------|
|
||||||
| 2000 | 0.3 ms | 0.1 ms | 4.5 ms | 4.4 ms |
|
| 2000 | 0.7 ms | 3.6 ms | 5× |
|
||||||
| 4000 | 0.5 ms | 0.2 ms | 17.1 ms | 16.8 ms |
|
| 4000 | 0.8 ms | 14.0 ms | 19× |
|
||||||
| 8000 | 0.9 ms | 0.3 ms | 67.0 ms | 66.8 ms |
|
| 8000 | 1.6 ms | 67.8 ms | 43× |
|
||||||
| 16 000 | 2.0 ms | 0.7 ms | 282.4 ms | 282.4 ms |
|
| 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
|
If key order matters for objects of that size, consider a container with a lookup index, such as
|
||||||
[`nlohmann::fifo_map`](https://github.com/nlohmann/fifo_map)
|
[`nlohmann::fifo_map`](https://github.com/nlohmann/fifo_map)
|
||||||
|
|||||||
@@ -9,7 +9,19 @@ Importing the module instead of including the header reduces the compile time of
|
|||||||
## Requirements
|
## Requirements
|
||||||
The `nlohmann.json` module requires that the build system is configured to build and resolve modules when imported. Obviously, as modules were introduced in C++20, this feature can only be used in C++20 and subsequent versions.
|
The `nlohmann.json` module requires that the build system is configured to build and resolve modules when imported. Obviously, as modules were introduced in C++20, this feature can only be used in C++20 and subsequent versions.
|
||||||
|
|
||||||
To enable building the `nlohmann.json` module (which is not done by default), the macro `NLOHMANN_JSON_BUILD_MODULES` must be passed to the build system.
|
To enable building the `nlohmann.json` module (which is not done by default), set the CMake option
|
||||||
|
[`NLOHMANN_JSON_BUILD_MODULES`](../integration/cmake.md#nlohmann_json_build_modules) and link the
|
||||||
|
`nlohmann_json_modules` target.
|
||||||
|
|
||||||
|
With CMake, building the module needs:
|
||||||
|
|
||||||
|
- CMake 3.28 or newer, and
|
||||||
|
- a generator that scans for modules: Ninja (1.11 or newer), Ninja Multi-Config, or Visual Studio 2022 or newer.
|
||||||
|
|
||||||
|
The example below also uses `import std;`. CMake supports it without experimental opt-in since version 4.5, and then
|
||||||
|
only with the Ninja generators and these toolchains: Clang 18.1.2 or newer (with libc++ or libstdc++), MSVC toolset 14.36
|
||||||
|
(Visual Studio 17.6) or newer, and GCC 15 or newer (GCC 16 on macOS). With older CMake versions, `import std;` needs
|
||||||
|
CMake's version-specific experimental opt-in.
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
When using modules rather than headers, the previous example for creating a `json` object through a JSON file, would instead be:
|
When using modules rather than headers, the previous example for creating a `json` object through a JSON file, would instead be:
|
||||||
@@ -67,7 +79,7 @@ and should not be used directly:
|
|||||||
|
|
||||||
## Known issues
|
## Known issues
|
||||||
|
|
||||||
C++20 modules support is exercised in CI against current GCC and Clang on Ubuntu, and the default MSVC toolset on Windows Server 2022 — there is no documented minimum compiler version, unlike feature-test-macro-gated features such as [`JSON_HAS_RANGES`](../api/macros/json_has_ranges.md).
|
C++20 modules support is exercised in CI with CMake 4.5 and `import std;` against current GCC and Clang on Ubuntu, and the default MSVC toolset on Windows Server 2022 — there is no documented minimum compiler version, unlike feature-test-macro-gated features such as [`JSON_HAS_RANGES`](../api/macros/json_has_ranges.md).
|
||||||
|
|
||||||
!!! info "Known compiler issues"
|
!!! info "Known compiler issues"
|
||||||
|
|
||||||
|
|||||||
@@ -81,20 +81,6 @@ result. See the [documentation of the macro](../../api/macros/json_recursion_dep
|
|||||||
the speed, and the rules for choosing a value. Destroying a value (its destructor) recurses over the same number of
|
the speed, and the rules for choosing a value. Destroying a value (its destructor) recurses over the same number of
|
||||||
levels and then continues without the call stack, and needs 32 KiB or less at every limit.
|
levels and then continues without the call stack, and needs 32 KiB or less at every limit.
|
||||||
|
|
||||||
!!! warning "Indented output and flattened keys grow with the depth"
|
|
||||||
|
|
||||||
Some results are larger than the value by a factor of its nesting depth.
|
|
||||||
[`dump`](../../api/basic_json/dump.md) with an `indent` of `n` (and likewise `operator<<` with `std::setw(n)` and
|
|
||||||
[`basic_json_view::dump`](../../api/basic_json_view/dump.md)) writes up to `n` characters per nesting level in
|
|
||||||
front of every line, and [`flatten`](../../api/basic_json/flatten.md) repeats the path of every value in its key.
|
|
||||||
For the 80,001-byte array `[1,[1,...[1,1]...]]` with 20,000 levels, `dump(1)` returns 600 MB, `dump(4)` 2.4 GB,
|
|
||||||
and the keys of `flatten()` hold 400 MB. A compact `dump()` without indentation stays the size of the value. If you
|
|
||||||
pretty-print, log, or flatten untrusted input, limit its nesting depth while parsing, for example with a
|
|
||||||
[parser callback](parser_callbacks.md#recipe-max-nesting-depth-via-a-callback): with a limit of `d`, every line has
|
|
||||||
at most `n * d` indentation characters, and every flattened key has at most `d` reference tokens.
|
|
||||||
[`JSON_RECURSION_DEPTH_LIMIT`](../../api/macros/json_recursion_depth_limit.md) does not help here: it changes only
|
|
||||||
how much of the call stack a walk uses, never its result, and it does not limit the depth that `parse()` accepts.
|
|
||||||
|
|
||||||
## Input size
|
## Input size
|
||||||
|
|
||||||
The library does not limit the overall size of a JSON text; a value nested or wide enough will use memory
|
The library does not limit the overall size of a JSON text; a value nested or wide enough will use memory
|
||||||
@@ -154,20 +140,6 @@ values for a repeated key ends up in the parsed object. If your application must
|
|||||||
silently resolving them one way or another, see the
|
silently resolving them one way or another, see the
|
||||||
[parser callback recipe for rejecting duplicate keys](parser_callbacks.md#recipe-rejecting-duplicate-object-keys).
|
[parser callback recipe for rejecting duplicate keys](parser_callbacks.md#recipe-rejecting-duplicate-object-keys).
|
||||||
|
|
||||||
## Objects with many keys in `ordered_json`
|
|
||||||
|
|
||||||
[`ordered_json`](../../api/ordered_json.md) keeps the members of an object in a vector in insertion order
|
|
||||||
([`ordered_map`](../../api/ordered_map.md)) and finds a key by searching all members. Parsing JSON text into an
|
|
||||||
`ordered_json` avoids that search for objects with many keys and takes linear time. The binary readers
|
|
||||||
([`from_cbor`](../../api/basic_json/from_cbor.md), [`from_msgpack`](../../api/basic_json/from_msgpack.md),
|
|
||||||
[`from_ubjson`](../../api/basic_json/from_ubjson.md), [`from_bjdata`](../../api/basic_json/from_bjdata.md),
|
|
||||||
[`from_bson`](../../api/basic_json/from_bson.md), and [`from_bon8`](../../api/basic_json/from_bon8.md)) do not yet:
|
|
||||||
reading an object of `n` keys into an `ordered_json` costs time proportional to `n²`. A flat object of 40,000 keys
|
|
||||||
(about 300 KB of CBOR) takes about 1.3 s, against a few milliseconds as JSON text or into a `json`
|
|
||||||
([#6104](https://github.com/nlohmann/json/issues/6104)). `json` is not affected. If you read untrusted binary input
|
|
||||||
into `ordered_json`, bound the input size more tightly, read it into a `json`, or reject objects with many keys in a
|
|
||||||
[SAX handler](sax_interface.md).
|
|
||||||
|
|
||||||
## Numbers
|
## Numbers
|
||||||
|
|
||||||
A number whose value cannot be represented — for instance `1E1000`, which overflows `double` — is rejected while
|
A number whose value cannot be represented — for instance `1E1000`, which overflows `double` — is rejected while
|
||||||
@@ -199,11 +171,9 @@ indices (or the pointer as a whole) before using them, or use [`at`](../../api/b
|
|||||||
|
|
||||||
- Wrap parsing in a `#!cpp try`/`#!cpp catch` block, or use `allow_exceptions=false`/`accept()` if your environment
|
- Wrap parsing in a `#!cpp try`/`#!cpp catch` block, or use `allow_exceptions=false`/`accept()` if your environment
|
||||||
cannot use exceptions; never let `JSON_NOEXCEPTION`'s `abort()` be the first time you think about error handling.
|
cannot use exceptions; never let `JSON_NOEXCEPTION`'s `abort()` be the first time you think about error handling.
|
||||||
- If the input's nesting depth matters to you (for example because you pretty-print, log, or flatten it), enforce your
|
- If the input's nesting depth matters to you, enforce your own limit with a
|
||||||
own limit with a [parser callback](parser_callbacks.md#recipe-max-nesting-depth-via-a-callback) or a
|
[parser callback](parser_callbacks.md#recipe-max-nesting-depth-via-a-callback) or a
|
||||||
[SAX handler](sax_interface.md); the library bounds the call stack but not memory use.
|
[SAX handler](sax_interface.md); the library bounds the call stack but not memory use.
|
||||||
- If you read untrusted binary input into `ordered_json`, mind that objects with many keys are
|
|
||||||
[slow to read](#objects-with-many-keys-in-ordered_json).
|
|
||||||
- Bound the size of the input itself before parsing, independent of the library's own bounded allocations for binary
|
- Bound the size of the input itself before parsing, independent of the library's own bounded allocations for binary
|
||||||
format lengths.
|
format lengths.
|
||||||
- Decide up front how you want a string with invalid UTF-8 — from any source, not only parsing — to be serialized
|
- Decide up front how you want a string with invalid UTF-8 — from any source, not only parsing — to be serialized
|
||||||
|
|||||||
@@ -269,6 +269,9 @@ target_link_libraries(myproject PRIVATE nlohmann_json_modules)
|
|||||||
target_compile_definitions(myproject PRIVATE NLOHMANN_JSON_BUILD_MODULES)
|
target_compile_definitions(myproject PRIVATE NLOHMANN_JSON_BUILD_MODULES)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If `main.cpp` also uses `import std;`, the project needs CMake 4.5 or newer and a Ninja generator; see
|
||||||
|
[Modules](../features/modules.md#requirements) for the supported toolchains.
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
|
|
||||||
- [Macros](../api/macros/index.md) - reference of all macros
|
- [Macros](../api/macros/index.md) - reference of all macros
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
cmake_minimum_required(VERSION 3.30)
|
# `import std;` (used by main.cpp) is supported without experimental opt-in
|
||||||
|
# since CMake 4.5: it is available to every target that scans for C++ modules.
|
||||||
# `import std;` (used by main.cpp) needs CMake's experimental C++ import-std
|
cmake_minimum_required(VERSION 4.5)
|
||||||
# support. The accepted token is CMake-version-specific; this value matches the
|
|
||||||
# CMake version pinned in the ci_module_cpp20 CI jobs (4.3.x). If that pin is
|
|
||||||
# bumped, this token must be updated to match (configuration fails loudly
|
|
||||||
# otherwise).
|
|
||||||
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "451f2fe2-a8a2-47c3-bc32-94786d8fc91b")
|
|
||||||
|
|
||||||
project(json_test CXX)
|
project(json_test CXX)
|
||||||
|
|
||||||
@@ -13,7 +8,6 @@ project(json_test CXX)
|
|||||||
set(CMAKE_CXX_STANDARD 23)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
set(CMAKE_CXX_MODULE_STD ON)
|
|
||||||
|
|
||||||
set(NLOHMANN_JSON_BUILD_MODULES ON CACHE BOOL "Enable nlohmann.json module support")
|
set(NLOHMANN_JSON_BUILD_MODULES ON CACHE BOOL "Enable nlohmann.json module support")
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ using namespace nlohmann::literals;
|
|||||||
// Standard-library facilities are pulled in via `import std;` rather than
|
// Standard-library facilities are pulled in via `import std;` rather than
|
||||||
// textual `#include`s: mixing `import` with textual standard headers does not
|
// textual `#include`s: mixing `import` with textual standard headers does not
|
||||||
// compile under GCC's C++20 modules implementation, whereas `import std;` works
|
// compile under GCC's C++20 modules implementation, whereas `import std;` works
|
||||||
// across GCC, Clang, and MSVC. This requires C++23 and CMake's (experimental)
|
// across GCC, Clang, and MSVC. This requires C++23 and CMake 4.5 or newer;
|
||||||
// import-std support; see CMakeLists.txt.
|
// see CMakeLists.txt.
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// basic_json / json: parsing and value access
|
// basic_json / json: parsing and value access
|
||||||
|
|||||||
Reference in new issue
Block a user