Compare commits
34
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d59b7e2728 | ||
|
|
91a03caa72 | ||
|
|
322063822f | ||
|
|
ccbbe8d0f0 | ||
|
|
745efa2673 | ||
|
|
90987b2a63 | ||
|
|
d957a10f55 | ||
|
|
e20f6dc35d | ||
|
|
7a67ccf6aa | ||
|
|
098eebd412 | ||
|
|
555ef48fb6 | ||
|
|
d732d616ba | ||
|
|
b927100ca1 | ||
|
|
ccb253baf1 | ||
|
|
8c3335d41f | ||
|
|
0ac14610d6 | ||
|
|
2951790d40 | ||
|
|
77611d412f | ||
|
|
f6eb5e4fb1 | ||
|
|
ac0d25f51c | ||
|
|
f91e900875 | ||
|
|
6ac411e14c | ||
|
|
1f7250f77f | ||
|
|
03ce377876 | ||
|
|
a1284028b2 | ||
|
|
abbc29963a | ||
|
|
7127a82fa1 | ||
|
|
99adff001d | ||
|
|
253e4c3926 | ||
|
|
73ec296320 | ||
|
|
79f6513cec | ||
|
|
81888ff9f0 | ||
|
|
38cb5d0573 | ||
|
|
72966ae73d |
No files matched your search
+1
-1
@@ -66,7 +66,7 @@ labels:
|
|||||||
- "tests/src/fuzzer-(parse_json_view|json_view_image)\\.cpp"
|
- "tests/src/fuzzer-(parse_json_view|json_view_image)\\.cpp"
|
||||||
- "tests/benchmarks/json_view/.*"
|
- "tests/benchmarks/json_view/.*"
|
||||||
- "tools/amalgamate/config_json_view\\.json"
|
- "tools/amalgamate/config_json_view\\.json"
|
||||||
- "docs/mkdocs/docs/features/json_view\\.md"
|
- "docs/mkdocs/docs/features/json_view/.*"
|
||||||
- "docs/mkdocs/docs/api/basic_json_(document|view)/.*"
|
- "docs/mkdocs/docs/api/basic_json_(document|view)/.*"
|
||||||
- "docs/mkdocs/docs/api/(ordered_)?json_(editable_)?(document|view)\\.md"
|
- "docs/mkdocs/docs/api/(ordered_)?json_(editable_)?(document|view)\\.md"
|
||||||
- "docs/mkdocs/docs/examples/(basic_json_(document|view)__|(ordered_)?json_(editable_)?(document|view)).*"
|
- "docs/mkdocs/docs/examples/(basic_json_(document|view)__|(ordered_)?json_(editable_)?(document|view)).*"
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
name: Big-endian
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- develop
|
||||||
|
- master
|
||||||
|
- release/*
|
||||||
|
pull_request:
|
||||||
|
schedule:
|
||||||
|
- cron: '17 4 * * 1'
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref || github.run_id }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
ci_test_big_endian:
|
||||||
|
# Allow manual fork validation, but avoid weekly emulation runs in forks.
|
||||||
|
if: github.event_name != 'schedule' || github.repository == 'nlohmann/json'
|
||||||
|
runs-on: ubuntu-26.04
|
||||||
|
container: debian:trixie
|
||||||
|
timeout-minutes: 90
|
||||||
|
steps:
|
||||||
|
- name: Install the s390x cross compiler and qemu-user
|
||||||
|
run: |
|
||||||
|
apt-get update
|
||||||
|
# get-cmake uses unzip to extract Ninja's zip; debian:trixie does not include it.
|
||||||
|
apt-get install -y --no-install-recommends g++ g++-s390x-linux-gnu qemu-user git ca-certificates unzip
|
||||||
|
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||||
|
with:
|
||||||
|
persist-credentials: false
|
||||||
|
- name: Get latest CMake and ninja
|
||||||
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
|
- name: Check that the target is big-endian
|
||||||
|
run: echo | s390x-linux-gnu-g++ -dM -E - | grep -q '__BYTE_ORDER__ __ORDER_BIG_ENDIAN__'
|
||||||
|
- name: Run CMake
|
||||||
|
run: cmake -S . -B build -G Ninja -DJSON_CI=On
|
||||||
|
- name: Build and test
|
||||||
|
env:
|
||||||
|
TEST_SCOPE: ${{ github.event_name == 'pull_request' && 'focused' || 'full' }}
|
||||||
|
run: cmake --build build --target "ci_test_big_endian_${TEST_SCOPE}"
|
||||||
@@ -28,7 +28,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ jobs:
|
|||||||
runs-on: ubuntu-26.04
|
runs-on: ubuntu-26.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -115,9 +115,7 @@ jobs:
|
|||||||
|
|
||||||
python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json.json -s .
|
python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json.json -s .
|
||||||
python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json_fwd.json -s .
|
python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json_fwd.json -s .
|
||||||
# the configuration of json_view.hpp comes with the pull request until
|
python3 $TOOL_DIR/amalgamate.py -c $TOOL_DIR/config_json_view.json -s .
|
||||||
# it is on develop; the tool itself is still develop's
|
|
||||||
python3 $TOOL_DIR/amalgamate.py -c $MAIN_DIR/tools/amalgamate/config_json_view.json -s .
|
|
||||||
|
|
||||||
# the header list of the Bazel "json" target must match the files in include/
|
# the header list of the Bazel "json" target must match the files in include/
|
||||||
cmake -P cmake/scripts/gen_bazel_build_file.cmake
|
cmake -P cmake/scripts/gen_bazel_build_file.cmake
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ jobs:
|
|||||||
timeout-minutes: 30
|
timeout-minutes: 30
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ jobs:
|
|||||||
runs-on: ubuntu-22.04
|
runs-on: ubuntu-22.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ jobs:
|
|||||||
pull-requests: write
|
pull-requests: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ jobs:
|
|||||||
runs-on: ubuntu-26.04
|
runs-on: ubuntu-26.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ jobs:
|
|||||||
security-events: write
|
security-events: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ jobs:
|
|||||||
timeout-minutes: 60
|
timeout-minutes: 60
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ jobs:
|
|||||||
runs-on: ubuntu-26.04
|
runs-on: ubuntu-26.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ jobs:
|
|||||||
runs-on: ubuntu-26.04
|
runs-on: ubuntu-26.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -80,7 +80,7 @@ jobs:
|
|||||||
runs-on: ubuntu-26.04
|
runs-on: ubuntu-26.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -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
|
||||||
@@ -112,7 +112,7 @@ jobs:
|
|||||||
target: [ci_test_single_header, ci_cppcheck, ci_cpplint, ci_reproducible_tests, ci_non_git_tests, ci_offline_testdata, ci_reuse_compliance, ci_test_valgrind]
|
target: [ci_test_single_header, ci_cppcheck, ci_cpplint, ci_reproducible_tests, ci_non_git_tests, ci_offline_testdata, ci_reuse_compliance, ci_test_valgrind]
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -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
|
||||||
@@ -178,7 +178,7 @@ jobs:
|
|||||||
runs-on: ubuntu-26.04
|
runs-on: ubuntu-26.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -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,16 @@ 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
|
# get-cmake extracts the Ninja zip with the system unzip, which the Clang
|
||||||
# import-std support. Its opt-in token is CMake-version-specific, so pin
|
# image lacks; other Clang jobs get away with it by hitting the cache.
|
||||||
# CMake to the version whose token is set in tests/module_cpp20/CMakeLists.txt.
|
- name: Install unzip (Clang)
|
||||||
- name: Get pinned CMake and ninja
|
if: matrix.container == 'silkeh/clang:latest'
|
||||||
uses: lukka/get-cmake@f86dd60acae0d132056913e248bdabf0e0580b7a # v4.4.5
|
run: apt-get update ; apt-get install -y unzip
|
||||||
|
# The module test uses `import std;`, which needs CMake 4.5 or newer.
|
||||||
|
- name: Get CMake 4.5+ and ninja
|
||||||
|
uses: lukka/get-cmake@16e8153a8b4918849d92aad131befe67a3705daa # v4.5.0
|
||||||
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 +395,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 +412,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 +426,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
|
||||||
@@ -433,7 +436,7 @@ jobs:
|
|||||||
runs-on: ubuntu-26.04
|
runs-on: ubuntu-26.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -443,7 +446,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
|
||||||
@@ -456,7 +459,7 @@ jobs:
|
|||||||
target: [ci_test_examples, ci_test_build_documentation, ci_test_documentation_mermaid]
|
target: [ci_test_examples, ci_test_build_documentation, ci_test_documentation_mermaid]
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@46b06cea07f9e64621fdf066ba77ac6915163d43 # v2.22.3
|
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -484,7 +487,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)
|
||||||
|
|||||||
+2
-3
@@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
# fuzzers
|
# fuzzers
|
||||||
/tests/corpus_*
|
/tests/corpus_*
|
||||||
/tests/parse_*_fuzzer
|
/tests/*_fuzzer
|
||||||
|
|
||||||
# documentation
|
# documentation
|
||||||
/docs/docset/JSON_for_Modern_C++.docset/
|
/docs/docset/JSON_for_Modern_C++.docset/
|
||||||
@@ -47,11 +47,10 @@ nlohmann_json.spdx
|
|||||||
# Bazel-related
|
# Bazel-related
|
||||||
MODULE.bazel.lock
|
MODULE.bazel.lock
|
||||||
|
|
||||||
# backups left by `make check-amalgamation` or the ci_test_amalgamation target if they are interrupted
|
# backups left by `make check-amalgamation` if it is interrupted
|
||||||
/BUILD.bazel~
|
/BUILD.bazel~
|
||||||
/nlohmann_json.natvis~
|
/nlohmann_json.natvis~
|
||||||
/single_include/nlohmann/*.hpp~
|
/single_include/nlohmann/*.hpp~
|
||||||
/venv_astyle/
|
|
||||||
|
|
||||||
# release outputs of `make release`
|
# release outputs of `make release`
|
||||||
/include.zip
|
/include.zip
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ The [GitHub Actions](https://docs.github.com/en/actions) workflows that build, t
|
|||||||
- `publish_documentation.yml` — build and publish the documentation on pushes to the `develop` branch that touch the documentation sources (or the files it embeds).
|
- `publish_documentation.yml` — build and publish the documentation on pushes to the `develop` branch that touch the documentation sources (or the files it embeds).
|
||||||
- `check_docs_links.yml` — check the links of the documentation weekly, as external links can break without any change in this repository.
|
- `check_docs_links.yml` — check the links of the documentation weekly, as external links can break without any change in this repository.
|
||||||
- `cancel_closed_pr_runs.yml` — cancel unfinished workflow runs of a pull request once it is closed or merged.
|
- `cancel_closed_pr_runs.yml` — cancel unfinished workflow runs of a pull request once it is closed or merged.
|
||||||
|
- `json_view_benchmarks.yml` — compare the performance of `json_view` with other JSON libraries on GitHub-hosted x86-64 and AArch64 Linux runners, on demand or when a pull request gets the label "benchmark".
|
||||||
|
|
||||||
Further documentation:
|
Further documentation:
|
||||||
|
|
||||||
|
|||||||
@@ -215,7 +215,7 @@ release: include.zip json.tar.xz
|
|||||||
|
|
||||||
# clean up
|
# clean up
|
||||||
clean:
|
clean:
|
||||||
rm -fr *.dSYM tests/*.dSYM tests/parse_*_fuzzer
|
rm -fr *.dSYM tests/*.dSYM tests/*_fuzzer
|
||||||
rm -fr cmake-build-benchmarks cmake-build-pvs-studio release_files
|
rm -fr cmake-build-benchmarks cmake-build-pvs-studio release_files
|
||||||
$(MAKE) clean -Cdocs
|
$(MAKE) clean -Cdocs
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -1382,6 +1383,7 @@ Use your favorite [**package manager**](https://json.nlohmann.me/integration/pac
|
|||||||
- [**build2**](https://json.nlohmann.me/integration/package_managers/#build2) `nlohmann-json`
|
- [**build2**](https://json.nlohmann.me/integration/package_managers/#build2) `nlohmann-json`
|
||||||
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/CPM.png" height="20"> [**cpm.cmake**](https://json.nlohmann.me/integration/package_managers/#cpmcmake) `gh:nlohmann/json`
|
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/CPM.png" height="20"> [**cpm.cmake**](https://json.nlohmann.me/integration/package_managers/#cpmcmake) `gh:nlohmann/json`
|
||||||
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/xmake.svg" height="20"> [**xmake**](https://json.nlohmann.me/integration/package_managers/#xmake) `nlohmann_json`
|
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/xmake.svg" height="20"> [**xmake**](https://json.nlohmann.me/integration/package_managers/#xmake) `nlohmann_json`
|
||||||
|
- <img src="https://raw.githubusercontent.com/nlohmann/json/refs/heads/develop/docs/mkdocs/docs/images/package_managers/nix.svg" height="20"> [**Nix**](https://json.nlohmann.me/integration/package_managers/#nix) `nlohmann_json`
|
||||||
|
|
||||||
The library is part of many package managers. See the [**documentation**](https://json.nlohmann.me/integration/package_managers/) for detailed descriptions and examples.
|
The library is part of many package managers. See the [**documentation**](https://json.nlohmann.me/integration/package_managers/) for detailed descriptions and examples.
|
||||||
|
|
||||||
|
|||||||
+34
-77
@@ -100,6 +100,40 @@ add_custom_target(ci_test_clang
|
|||||||
COMMENT "Compile and test with Clang using maximal warning flags"
|
COMMENT "Compile and test with Clang using maximal warning flags"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
# Big-endian tests (cross-compiled for s390x, executed under qemu-user).
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
foreach(S390X_TEST_SCOPE IN ITEMS focused full)
|
||||||
|
set(S390X_TEST_FILTER)
|
||||||
|
if(S390X_TEST_SCOPE STREQUAL "focused")
|
||||||
|
# Keep this filter in sync with new or renamed endianness-sensitive tests.
|
||||||
|
# "serialization" intentionally includes "deserialization"; the develop
|
||||||
|
# and weekly full runs cover tests outside this focused runtime subset.
|
||||||
|
set(S390X_TEST_FILTER -R "cbor|msgpack|ubjson|bjdata|bson|bon8|binary|json_view|alt-string|serialization|class_parser|fmt_formatter")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_custom_target(ci_test_big_endian_${S390X_TEST_SCOPE}
|
||||||
|
COMMAND ${CMAKE_COMMAND}
|
||||||
|
-DCMAKE_BUILD_TYPE=Debug -GNinja
|
||||||
|
-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=s390x
|
||||||
|
-DCMAKE_CXX_COMPILER=s390x-linux-gnu-g++
|
||||||
|
"-DCMAKE_CROSSCOMPILING_EMULATOR=qemu-s390x;-L;/usr/s390x-linux-gnu"
|
||||||
|
-DJSON_BuildTests=ON -DJSON_FastTests=ON
|
||||||
|
-S${PROJECT_SOURCE_DIR} -B${PROJECT_BINARY_DIR}/build_big_endian
|
||||||
|
# Build every test binary, even when executing only the focused subset.
|
||||||
|
# Limit parallelism for memory-intensive compilation and emulated tests.
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E echo "s390x build timing:"
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E time ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_big_endian --parallel 2
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E echo "s390x test timing:"
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E time ${CMAKE_CTEST_COMMAND} --test-dir ${PROJECT_BINARY_DIR}/build_big_endian
|
||||||
|
--parallel 2 --output-on-failure --no-tests=error ${S390X_TEST_FILTER}
|
||||||
|
COMMENT "Cross-compile all tests for s390x and run the ${S390X_TEST_SCOPE} suite under qemu-user"
|
||||||
|
VERBATIM
|
||||||
|
USES_TERMINAL
|
||||||
|
)
|
||||||
|
endforeach()
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Different C++ Standards.
|
# Different C++ Standards.
|
||||||
###############################################################################
|
###############################################################################
|
||||||
@@ -406,61 +440,6 @@ add_custom_target(ci_test_clang_sanitizer
|
|||||||
COMMENT "Compile and test with sanitizers"
|
COMMENT "Compile and test with sanitizers"
|
||||||
)
|
)
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
# Check if header is amalgamated and sources are properly indented.
|
|
||||||
###############################################################################
|
|
||||||
|
|
||||||
# Same file set as .github/workflows/check_amalgamation.yml, so this CMake target (which can be run locally) is held
|
|
||||||
# to the same standard as the workflow (which runs for pull requests and for pushes to develop/master/release/*).
|
|
||||||
file(GLOB_RECURSE INDENT_FILES
|
|
||||||
${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.hpp
|
|
||||||
${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.cpp
|
|
||||||
${PROJECT_SOURCE_DIR}/docs/mkdocs/docs/examples/*.cu
|
|
||||||
${PROJECT_SOURCE_DIR}/include/*.hpp
|
|
||||||
${PROJECT_SOURCE_DIR}/include/*.cpp
|
|
||||||
${PROJECT_SOURCE_DIR}/include/*.cu
|
|
||||||
${PROJECT_SOURCE_DIR}/tests/*.hpp
|
|
||||||
${PROJECT_SOURCE_DIR}/tests/*.cpp
|
|
||||||
${PROJECT_SOURCE_DIR}/tests/*.cu
|
|
||||||
)
|
|
||||||
list(FILTER INDENT_FILES EXCLUDE REGEX "/tests/thirdparty/|/tests/abi/include/nlohmann/")
|
|
||||||
|
|
||||||
set(include_dir ${PROJECT_SOURCE_DIR}/single_include/nlohmann)
|
|
||||||
set(tool_dir ${PROJECT_SOURCE_DIR}/tools/amalgamate)
|
|
||||||
add_custom_target(ci_test_amalgamation
|
|
||||||
COMMAND rm -fr ${include_dir}/json.hpp~ ${include_dir}/json_fwd.hpp~ ${include_dir}/json_literals.hpp~ ${include_dir}/json_view.hpp~
|
|
||||||
COMMAND cp ${include_dir}/json.hpp ${include_dir}/json.hpp~
|
|
||||||
COMMAND cp ${include_dir}/json_fwd.hpp ${include_dir}/json_fwd.hpp~
|
|
||||||
COMMAND cp ${include_dir}/json_literals.hpp ${include_dir}/json_literals.hpp~
|
|
||||||
COMMAND cp ${include_dir}/json_view.hpp ${include_dir}/json_view.hpp~
|
|
||||||
COMMAND cp ${PROJECT_SOURCE_DIR}/BUILD.bazel ${PROJECT_SOURCE_DIR}/BUILD.bazel~
|
|
||||||
|
|
||||||
COMMAND ${Python3_EXECUTABLE} -mvenv venv_astyle
|
|
||||||
COMMAND venv_astyle/bin/pip3 --quiet install -r ${CMAKE_SOURCE_DIR}/tools/astyle/requirements.txt
|
|
||||||
COMMAND venv_astyle/bin/astyle --version
|
|
||||||
|
|
||||||
COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json.json -s .
|
|
||||||
COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json_fwd.json -s .
|
|
||||||
COMMAND cp ${PROJECT_SOURCE_DIR}/include/nlohmann/json_literals.hpp ${include_dir}/json_literals.hpp
|
|
||||||
COMMAND ${Python3_EXECUTABLE} ${tool_dir}/amalgamate.py -c ${tool_dir}/config_json_view.json -s .
|
|
||||||
COMMAND venv_astyle/bin/astyle --project=tools/astyle/.astylerc --suffix=none ${include_dir}/json.hpp ${include_dir}/json_fwd.hpp ${include_dir}/json_literals.hpp ${include_dir}/json_view.hpp
|
|
||||||
COMMAND ${CMAKE_COMMAND} -P ${PROJECT_SOURCE_DIR}/cmake/scripts/gen_bazel_build_file.cmake
|
|
||||||
|
|
||||||
COMMAND diff ${include_dir}/json.hpp~ ${include_dir}/json.hpp
|
|
||||||
COMMAND diff ${include_dir}/json_fwd.hpp~ ${include_dir}/json_fwd.hpp
|
|
||||||
COMMAND diff ${include_dir}/json_literals.hpp~ ${include_dir}/json_literals.hpp
|
|
||||||
COMMAND diff ${include_dir}/json_view.hpp~ ${include_dir}/json_view.hpp
|
|
||||||
COMMAND diff ${PROJECT_SOURCE_DIR}/BUILD.bazel~ ${PROJECT_SOURCE_DIR}/BUILD.bazel
|
|
||||||
|
|
||||||
COMMAND venv_astyle/bin/astyle --project=tools/astyle/.astylerc --suffix=orig ${INDENT_FILES}
|
|
||||||
COMMAND for FILE in `find . -name '*.orig'`\; do false \; done
|
|
||||||
|
|
||||||
COMMAND rm -f ${include_dir}/json.hpp~ ${include_dir}/json_fwd.hpp~ ${include_dir}/json_literals.hpp~ ${include_dir}/json_view.hpp~ ${PROJECT_SOURCE_DIR}/BUILD.bazel~
|
|
||||||
|
|
||||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
|
|
||||||
COMMENT "Check amalgamation, formatting, and BUILD.bazel"
|
|
||||||
)
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# Build and test using the amalgamated header
|
# Build and test using the amalgamated header
|
||||||
###############################################################################
|
###############################################################################
|
||||||
@@ -692,19 +671,6 @@ else()
|
|||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
# Benchmarks
|
|
||||||
###############################################################################
|
|
||||||
|
|
||||||
add_custom_target(ci_benchmarks
|
|
||||||
COMMAND ${CMAKE_COMMAND}
|
|
||||||
-DCMAKE_BUILD_TYPE=Release -GNinja
|
|
||||||
-S${PROJECT_SOURCE_DIR}/tests/benchmarks -B${PROJECT_BINARY_DIR}/build_benchmarks
|
|
||||||
COMMAND ${CMAKE_COMMAND} --build ${PROJECT_BINARY_DIR}/build_benchmarks --target json_benchmarks
|
|
||||||
COMMAND cd ${PROJECT_BINARY_DIR}/build_benchmarks && ./json_benchmarks
|
|
||||||
COMMENT "Run benchmarks"
|
|
||||||
)
|
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
# CMake flags
|
# CMake flags
|
||||||
###############################################################################
|
###############################################################################
|
||||||
@@ -965,12 +931,3 @@ add_custom_target(ci_test_documentation_mermaid
|
|||||||
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/docs/mkdocs
|
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/docs/mkdocs
|
||||||
COMMENT "Check the Mermaid diagrams of the documentation"
|
COMMENT "Check the Mermaid diagrams of the documentation"
|
||||||
)
|
)
|
||||||
|
|
||||||
###############################################################################
|
|
||||||
# Clean up all generated files.
|
|
||||||
###############################################################################
|
|
||||||
|
|
||||||
add_custom_target(ci_clean
|
|
||||||
COMMAND rm -fr ${PROJECT_BINARY_DIR}/build_* ${PROJECT_BINARY_DIR}/cmake-3.5.0 ${PROJECT_BINARY_DIR}/cmake-3.31.6 ${PROJECT_BINARY_DIR}/cmake-4.0.0 ${JSON_CMAKE_FLAG_BUILD_DIRS} ${single_binaries}
|
|
||||||
COMMENT "Clean generated directories"
|
|
||||||
)
|
|
||||||
@@ -1 +1 @@
|
|||||||
semgrep==1.184.0
|
semgrep==1.185.0
|
||||||
@@ -79,7 +79,14 @@ basic_json(basic_json&& other) noexcept;
|
|||||||
(e.g., `json j = std::move(vec);` or `json j(std::vector<std::string>{...});`), the elements are moved into
|
(e.g., `json j = std::move(vec);` or `json j(std::vector<std::string>{...});`), the elements are moved into
|
||||||
the JSON value instead of copied. Afterwards, the elements of the source container are in a valid but
|
the JSON value instead of copied. Afterwards, the elements of the source container are in a valid but
|
||||||
unspecified state (**moved-from**). Keys of associative containers are `const` and therefore still copied.
|
unspecified state (**moved-from**). Keys of associative containers are `const` and therefore still copied.
|
||||||
Lvalues and `const` rvalues are copied, and the source is left unchanged.
|
Lvalues and `const` rvalues are copied, and the source is left unchanged. Only containers that own their
|
||||||
|
elements are moved from, that is, containers whose elements are `const` when accessed through a `const`
|
||||||
|
container. Non-owning ranges such as spans or iterator ranges (e.g., `gsl::span`, `std::span`) are copied, so
|
||||||
|
converting an rvalue span leaves the referenced elements unchanged.
|
||||||
|
A user-defined type whose elements are `const` through a `const` object, but which does not own them alone,
|
||||||
|
is treated as owning: a view, or a handle that shares its storage with other handles (e.g., a wrapper of a
|
||||||
|
`std::shared_ptr<std::vector<T>>`) and does not copy it before giving non-`const` access. Moving from such
|
||||||
|
an rvalue changes the elements seen through the other handles; pass it as an lvalue to copy the elements.
|
||||||
- **numbers**: [`number_integer_t`](number_integer_t.md), [`number_unsigned_t`](number_unsigned_t.md),
|
- **numbers**: [`number_integer_t`](number_integer_t.md), [`number_unsigned_t`](number_unsigned_t.md),
|
||||||
[`number_float_t`](number_float_t.md), and all convertible number types such as `int`, `size_t`, `int64_t`, `float`
|
[`number_float_t`](number_float_t.md), and all convertible number types such as `int`, `size_t`, `int64_t`, `float`
|
||||||
or `double` can be used.
|
or `double` can be used.
|
||||||
@@ -265,7 +272,9 @@ basic_json(basic_json&& other) noexcept;
|
|||||||
4. Depends on the called constructor. For types directly supported by the library (i.e., all types for which no
|
4. Depends on the called constructor. For types directly supported by the library (i.e., all types for which no
|
||||||
`to_json()` function was provided), a strong guarantee holds: if an exception is thrown, there are no changes to any
|
`to_json()` function was provided), a strong guarantee holds: if an exception is thrown, there are no changes to any
|
||||||
JSON value.
|
JSON value.
|
||||||
5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value. Values that are moved into
|
||||||
|
`init` (e.g., `a` in `#!cpp json::object({std::move(a), 5})`) are moved when the initializer list is created,
|
||||||
|
before the constructor runs, and stay moved-from if it throws.
|
||||||
6. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
6. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||||
7. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
7. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||||
8. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
8. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||||
|
|||||||
@@ -58,7 +58,9 @@ is not UTF-8 encoded and `error_handler` is set to `strict`
|
|||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
Linear.
|
Linear in the size of the result. With an `indent`, every line starts with up to `indent` characters per nesting level,
|
||||||
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -26,10 +26,12 @@ void erase(const size_type idx);
|
|||||||
|
|
||||||
If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`.
|
If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`.
|
||||||
|
|
||||||
2. Remove an element range specified by `[first; last)` from a JSON value. The iterator `first` does not need to be
|
2. Remove an element range specified by `[first; last)` from a JSON value. For arrays and objects, the iterator `first`
|
||||||
dereferenceable if `first == last`: erasing an empty range is a no-op.
|
does not need to be dereferenceable if `first == last`: erasing an empty range is a no-op.
|
||||||
|
|
||||||
If called on a primitive type other than `#!json null`, the resulting JSON value will be `#!json null`.
|
If called on a primitive type other than `#!json null`, the range must be `[begin(); end())`, and the resulting JSON
|
||||||
|
value will be `#!json null`. Any other range, including the empty ranges `[begin(); begin())` and
|
||||||
|
`[end(); end())`, throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204).
|
||||||
|
|
||||||
3. Removes an element from a JSON object by key.
|
3. Removes an element from a JSON object by key.
|
||||||
|
|
||||||
@@ -93,8 +95,8 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
- Throws [`invalid_iterator.203`](../../home/exceptions.md#jsonexceptioninvalid_iterator203) if called on iterators
|
- Throws [`invalid_iterator.203`](../../home/exceptions.md#jsonexceptioninvalid_iterator203) if called on iterators
|
||||||
which does not belong to the current JSON value; example: `"iterators do not fit current value"`
|
which does not belong to the current JSON value; example: `"iterators do not fit current value"`
|
||||||
- Throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) if called on a
|
- Throws [`invalid_iterator.204`](../../home/exceptions.md#jsonexceptioninvalid_iterator204) if called on a
|
||||||
primitive type with invalid iterators (i.e., if `first != begin()` and `last != end()`); example: `"iterators out
|
primitive type with invalid iterators (i.e., if `first != begin()` or `last != end()`, which includes the empty
|
||||||
of range"`
|
ranges `[begin(); begin())` and `[end(); end())`); example: `"iterators out of range"`
|
||||||
3. The function can throw the following exceptions:
|
3. The function can throw the following exceptions:
|
||||||
- Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than
|
- Throws [`type_error.307`](../../home/exceptions.md#jsonexceptiontype_error307) when called on a type other than
|
||||||
JSON object; example: `"cannot use erase() with null"`
|
JSON object; example: `"cannot use erase() with null"`
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
Linear in the size of the JSON value.
|
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
|
||||||
|
factor of its nesting depth (see [Parsing untrusted input](../../features/parsing/untrusted_input.md#nesting-depth)).
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
|||||||
@@ -69,8 +69,10 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
|
|
||||||
## Exceptions
|
## Exceptions
|
||||||
|
|
||||||
- Throws [parse_error.110](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely or
|
- Throws [parse_error.110](../../home/exceptions.md#jsonexceptionparse_error110) if the given input ends prematurely,
|
||||||
the end of the file was not reached when `strict` was set to true
|
if the end of the file was not reached when `strict` was set to true, or if a string at the end of the value is not
|
||||||
|
terminated by 0xFF although a byte follows it (also when `strict` is false: the byte that ends the string is
|
||||||
|
taken from the input, so the next value would be lost)
|
||||||
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs, for instance
|
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs, for instance
|
||||||
an invalid byte, a string that is not valid UTF-8, or an object key that is not a string
|
an invalid byte, a string that is not valid UTF-8, or an object key that is not a string
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,9 @@ JSON object value
|
|||||||
|
|
||||||
## Exception safety
|
## Exception safety
|
||||||
|
|
||||||
Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
|
Strong guarantee: if an exception is thrown, there are no changes in the JSON value. Values that are moved into `init`
|
||||||
|
(e.g., `a` in `#!cpp json::object({std::move(a), 5})`) are moved when the initializer list is created, before this
|
||||||
|
function runs, and stay moved-from if it throws.
|
||||||
|
|
||||||
## Exceptions
|
## Exceptions
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,21 @@
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
namespace std {
|
namespace std {
|
||||||
void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2);
|
void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2) noexcept(
|
||||||
|
is_nothrow_move_constructible<nlohmann::basic_json>::value &&
|
||||||
|
is_nothrow_move_assignable<nlohmann::basic_json>::value
|
||||||
|
);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Exchanges the values of two JSON objects.
|
Exchanges the values of two JSON objects.
|
||||||
|
|
||||||
|
This specialization is only provided before C++20, which no longer allows specializing function templates in
|
||||||
|
namespace `std`. With C++20 (and in generic code with any standard), use the idiom `#!cpp using std::swap; swap(j1, j2);`: it finds
|
||||||
|
the [`swap`](swap.md) friend function (overload 2) by argument-dependent lookup. A qualified `#!cpp std::swap(j1, j2)`
|
||||||
|
still compiles with C++20, but uses the generic `std::swap`, which swaps by three moves instead of exchanging the
|
||||||
|
contents.
|
||||||
|
|
||||||
## Parameters
|
## Parameters
|
||||||
|
|
||||||
`j1` (in, out)
|
`j1` (in, out)
|
||||||
@@ -18,7 +27,9 @@ Exchanges the values of two JSON objects.
|
|||||||
|
|
||||||
## Exception safety
|
## Exception safety
|
||||||
|
|
||||||
No-throw guarantee: this function never throws exceptions.
|
No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the
|
||||||
|
[`json_base_class_t`](json_base_class_t.md) can throw. With the default base class, this function never throws
|
||||||
|
exceptions.
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
@@ -57,3 +68,4 @@ void swap(nlohmann::basic_json& j1, nlohmann::basic_json& j2)
|
|||||||
|
|
||||||
- Added in version 1.0.0.
|
- Added in version 1.0.0.
|
||||||
- Extended for arbitrary basic_json types in version 3.10.5.
|
- Extended for arbitrary basic_json types in version 3.10.5.
|
||||||
|
- Not provided with C++20 since version 3.9.0.
|
||||||
@@ -75,8 +75,12 @@ void swap(typename binary_t::container_type& other);
|
|||||||
|
|
||||||
## Exception safety
|
## Exception safety
|
||||||
|
|
||||||
1. No-throw guarantee: this function never throws exceptions.
|
1. No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the
|
||||||
2. No-throw guarantee: this function never throws exceptions.
|
[`json_base_class_t`](json_base_class_t.md) can throw (moving the type tag and the stored value never throws).
|
||||||
|
With the default base class, this function never throws exceptions.
|
||||||
|
2. No-throw guarantee if the `noexcept` condition above holds: it holds unless moving the
|
||||||
|
[`json_base_class_t`](json_base_class_t.md) can throw (moving the type tag and the stored value never throws).
|
||||||
|
With the default base class, this function never throws exceptions.
|
||||||
3. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
3. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||||
4. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
4. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||||
5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
5. Strong guarantee: if an exception is thrown, there are no changes to any JSON value.
|
||||||
@@ -85,8 +89,8 @@ void swap(typename binary_t::container_type& other);
|
|||||||
|
|
||||||
## Exceptions
|
## Exceptions
|
||||||
|
|
||||||
1. No-throw guarantee: this function never throws exceptions.
|
1. Throws only what moving the [`json_base_class_t`](json_base_class_t.md) throws; nothing with the default base class.
|
||||||
2. No-throw guarantee: this function never throws exceptions.
|
2. Throws only what moving the [`json_base_class_t`](json_base_class_t.md) throws; nothing with the default base class.
|
||||||
3. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
|
3. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
|
||||||
arrays; example: `"cannot use swap(array_t&) with boolean"`
|
arrays; example: `"cannot use swap(array_t&) with boolean"`
|
||||||
4. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
|
4. Throws [`type_error.310`](../../home/exceptions.md#jsonexceptiontype_error310) if called on JSON values other than
|
||||||
|
|||||||
@@ -57,7 +57,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
## Exceptions
|
## Exceptions
|
||||||
|
|
||||||
- Throws [`other_error.502`](../../home/exceptions.md#jsonexceptionother_error502) if `use_type` is true and `use_size`
|
- Throws [`other_error.502`](../../home/exceptions.md#jsonexceptionother_error502) if `use_type` is true and `use_size`
|
||||||
is false, and `j` contains a non-empty array, object, or binary value.
|
is false, and `j` contains a non-empty array, object, or binary value. Scalars and empty arrays, objects, or binary
|
||||||
|
values do not throw.
|
||||||
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
- Throws [type_error.316](../../home/exceptions.md#jsonexceptiontype_error316) if a string or object key in `j` is
|
||||||
not valid UTF-8 and `error_handler` is `strict` (the default only if
|
not valid UTF-8 and `error_handler` is `strict` (the default only if
|
||||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled)
|
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled)
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ The function can throw the following exceptions:
|
|||||||
- Throws [`type_error.314`](../../home/exceptions.md#jsonexceptiontype_error314) if value is not an object
|
- Throws [`type_error.314`](../../home/exceptions.md#jsonexceptiontype_error314) if value is not an object
|
||||||
- Throws [`type_error.315`](../../home/exceptions.md#jsonexceptiontype_error315) if object values are not primitive
|
- Throws [`type_error.315`](../../home/exceptions.md#jsonexceptiontype_error315) if object values are not primitive
|
||||||
- Throws [`type_error.313`](../../home/exceptions.md#jsonexceptiontype_error313) if a key (JSON pointer) leads to a
|
- Throws [`type_error.313`](../../home/exceptions.md#jsonexceptiontype_error313) if a key (JSON pointer) leads to a
|
||||||
conflicting nesting; example: `"invalid value to unflatten"`
|
conflicting nesting, for example if a key with a value other than `#!json null` is a prefix of another key (as `/a`
|
||||||
|
and `/a/b`), in whichever order the keys come; example: `"invalid value to unflatten"`
|
||||||
- Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index in a key begins
|
- Throws [`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) if an array index in a key begins
|
||||||
with '0'; example: `"array index '01' must not begin with '0'"`
|
with '0'; example: `"array index '01' must not begin with '0'"`
|
||||||
- Throws [`parse_error.107`](../../home/exceptions.md#jsonexceptionparse_error107) if a key is not empty and does not
|
- Throws [`parse_error.107`](../../home/exceptions.md#jsonexceptionparse_error107) if a key is not empty and does not
|
||||||
@@ -90,3 +91,6 @@ Apart from these three cases, for a JSON value `j`, the following is always true
|
|||||||
- Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of
|
- Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of
|
||||||
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) for a one-character array index that is
|
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) for a one-character array index that is
|
||||||
not a digit (e.g., `/x`) in version 3.13.0, as it already did for longer ones.
|
not a digit (e.g., `/x`) in version 3.13.0, as it already did for longer ones.
|
||||||
|
- Throws [`type_error.313`](../../home/exceptions.md#jsonexceptiontype_error313) for a key that is a prefix of an
|
||||||
|
earlier key in version 3.13.0; before, an object type that keeps the insertion order (such as
|
||||||
|
[`ordered_json`](../ordered_json.md)) silently replaced the nested value.
|
||||||
@@ -66,7 +66,8 @@ has `erase`; calling it on a read-only `basic_json_document` fails to compile (`
|
|||||||
[`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) (a leading `#!cpp '0'`),
|
[`parse_error.106`](../../home/exceptions.md#jsonexceptionparse_error106) (a leading `#!cpp '0'`),
|
||||||
[`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) (not a number),
|
[`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) (not a number),
|
||||||
[`out_of_range.410`](../../home/exceptions.md#jsonexceptionout_of_range410) (too large for `size_type`), or
|
[`out_of_range.410`](../../home/exceptions.md#jsonexceptionout_of_range410) (too large for `size_type`), or
|
||||||
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) (an empty token); otherwise (an
|
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) (an empty token, or the token
|
||||||
|
`#!cpp "-"`, which never names an existing element); otherwise (an
|
||||||
object, or a primitive value the pointer's parent resolves to) throws what 1. throws.
|
object, or a primitive value the pointer's parent resolves to) throws what 1. throws.
|
||||||
|
|
||||||
Overloads 1. and 2. also throw [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202)
|
Overloads 1. and 2. also throw [`invalid_iterator.202`](../../home/exceptions.md#jsonexceptioninvalid_iterator202)
|
||||||
|
|||||||
@@ -100,7 +100,8 @@ points apply to every edit:
|
|||||||
[`root()`](root.md). New values (and the element sequences of an edited array/object) go to storage owned by the
|
[`root()`](root.md). New values (and the element sequences of an edited array/object) go to storage owned by the
|
||||||
document, allocated on demand.
|
document, allocated on demand.
|
||||||
- **A view keeps referring to the same value.** After [`set`](set.md) replaces the value a view refers to, that view
|
- **A view keeps referring to the same value.** After [`set`](set.md) replaces the value a view refers to, that view
|
||||||
sees the new value; a view of a value that a later edit replaces or drops keeps showing what it last held. An edit
|
sees the new value; a view of a value that a later edit replaces or drops keeps showing what it last held. Such a
|
||||||
|
view can still be edited: the edit changes only that detached value, never anything reachable from `root()`. An edit
|
||||||
of an array or object, however, **invalidates the iterators taken over it** (its members may now live in a
|
of an array or object, however, **invalidates the iterators taken over it** (its members may now live in a
|
||||||
different sequence), and a string obtained with [`get_string()`](../basic_json_view/get_string.md) stays valid even
|
different sequence), and a string obtained with [`get_string()`](../basic_json_view/get_string.md) stays valid even
|
||||||
as further edits happen (earlier buffers of edited text are kept alive, not overwritten).
|
as further edits happen (earlier buffers of edited text are kept alive, not overwritten).
|
||||||
|
|||||||
@@ -164,7 +164,9 @@ general.
|
|||||||
The same switch happens, for the same reason, when overload 1. replaces a multi-node array/object value with a
|
The same switch happens, for the same reason, when overload 1. replaces a multi-node array/object value with a
|
||||||
scalar: the *parent's* element sequence is what has to switch to links, not `target` itself, because the parent
|
scalar: the *parent's* element sequence is what has to switch to links, not `target` itself, because the parent
|
||||||
originally stepped over `target`'s whole subtree by its node count, which no longer applies once `target` is a
|
originally stepped over `target`'s whole subtree by its node count, which no longer applies once `target` is a
|
||||||
one-node scalar.
|
one-node scalar. This also holds when `target` is no longer part of the document (an edit erased or replaced it, or
|
||||||
|
a value that contains it): if the container that `target` was in still uses its parsed layout, it switches to links
|
||||||
|
(otherwise nothing steps over `target` any more), and only the dropped values change.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|||||||
@@ -62,10 +62,9 @@ view.
|
|||||||
|
|
||||||
!!! info "Overload 2 never throws"
|
!!! info "Overload 2 never throws"
|
||||||
|
|
||||||
Unlike [`BasicJsonType::contains(const json_pointer&)`](../basic_json/contains.md), which can throw for certain
|
Like [`BasicJsonType::contains(const json_pointer&)`](../basic_json/contains.md), overload 2 never throws: a
|
||||||
malformed pointers (for instance an empty array-index reference token), overload 2 never throws: a missing key,
|
missing key, an out-of-range or malformed array index, a `#!cpp "-"` index, or a reference token used on a
|
||||||
an out-of-range or malformed array index, a `#!cpp "-"` index, or a reference token used on a primitive all
|
primitive all simply make it return `#!cpp false`.
|
||||||
simply make it return `#!cpp false`.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|||||||
@@ -27,9 +27,10 @@ for (const auto [key, value] : v.items())
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Note the `#!cpp const auto` (by value), not `#!cpp const auto&`: unlike `BasicJsonType::items()`, whose elements are
|
Taking `item` by value (`#!cpp const auto`) is recommended: unlike `BasicJsonType::items()`, whose elements are
|
||||||
references into an existing object, a view's `item` is produced on the fly for each step of the iteration, so there
|
references into an existing object, a view's `item` is produced on the fly for each step of the iteration and is
|
||||||
is nothing for a reference to bind to.
|
cheap to copy. A `#!cpp const auto&` (as in the first snippet) also works, as it binds to that temporary, but a
|
||||||
|
non-const `#!cpp auto&` does not compile.
|
||||||
|
|
||||||
## Return value
|
## Return value
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ bool operator==(const basic_json_view& lhs, const basic_json_view& rhs);
|
|||||||
// (2)
|
// (2)
|
||||||
bool operator==(const basic_json_view& lhs, const BasicJsonType& rhs);
|
bool operator==(const basic_json_view& lhs, const BasicJsonType& rhs);
|
||||||
bool operator==(const BasicJsonType& lhs, const basic_json_view& rhs);
|
bool operator==(const BasicJsonType& lhs, const basic_json_view& rhs);
|
||||||
|
|
||||||
|
// (3)
|
||||||
|
template<view_mode M>
|
||||||
|
bool operator==(const basic_json_view& lhs, const basic_json_view<BasicJsonType, M>& rhs);
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Compares two views for equality: whether the values [`BasicJsonType::parse()`](../basic_json/parse.md) would
|
1. Compares two views for equality: whether the values [`BasicJsonType::parse()`](../basic_json/parse.md) would
|
||||||
@@ -14,8 +18,12 @@ bool operator==(const BasicJsonType& lhs, const basic_json_view& rhs);
|
|||||||
2. Compares a view and a `BasicJsonType` value for equality, in either order: whether the value `parse()` would
|
2. Compares a view and a `BasicJsonType` value for equality, in either order: whether the value `parse()` would
|
||||||
produce for the view and the other operand are equal, according to `BasicJsonType`'s
|
produce for the view and the other operand are equal, according to `BasicJsonType`'s
|
||||||
[`operator==`](../basic_json/operator_eq.md).
|
[`operator==`](../basic_json/operator_eq.md).
|
||||||
|
3. Like overload 1, for two views of the same `BasicJsonType` but of different [view modes](../view_mode.md), in
|
||||||
|
either order: a view of a read-only document (such as a [`json_view`](../json_view.md)) and a view of an editable
|
||||||
|
document (such as a [`json_editable_view`](../json_editable_view.md)). This overload only takes part in overload
|
||||||
|
resolution if `M` is not the mode of `lhs`.
|
||||||
|
|
||||||
Neither overload builds a `BasicJsonType` value for a view to do the comparison (see [Notes](#notes) below). Numbers
|
No overload builds a `BasicJsonType` value for a view to do the comparison (see [Notes](#notes) below). Numbers
|
||||||
compare by value across their types (`#!cpp 1 == 1.0`), and an object compares by its members, with duplicate keys
|
compare by value across their types (`#!cpp 1 == 1.0`), and an object compares by its members, with duplicate keys
|
||||||
resolved exactly as `parse()` resolves them — the last value, at the position of the first occurrence of the key.
|
resolved exactly as `parse()` resolves them — the last value, at the position of the first occurrence of the key.
|
||||||
This is not what [`operator[]`](operator[].md) and the other lookups return for a duplicate key: they find the *first*
|
This is not what [`operator[]`](operator[].md) and the other lookups return for a duplicate key: they find the *first*
|
||||||
|
|||||||
@@ -7,11 +7,17 @@ bool operator!=(const basic_json_view& lhs, const basic_json_view& rhs);
|
|||||||
// (2)
|
// (2)
|
||||||
bool operator!=(const basic_json_view& lhs, const BasicJsonType& rhs);
|
bool operator!=(const basic_json_view& lhs, const BasicJsonType& rhs);
|
||||||
bool operator!=(const BasicJsonType& lhs, const basic_json_view& rhs);
|
bool operator!=(const BasicJsonType& lhs, const basic_json_view& rhs);
|
||||||
|
|
||||||
|
// (3)
|
||||||
|
template<view_mode M>
|
||||||
|
bool operator!=(const basic_json_view& lhs, const basic_json_view<BasicJsonType, M>& rhs);
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Compares two views for inequality. Returns `#!cpp !(lhs == rhs)`, see [operator==](operator_eq.md).
|
1. Compares two views for inequality. Returns `#!cpp !(lhs == rhs)`, see [operator==](operator_eq.md).
|
||||||
2. Compares a view and a `BasicJsonType` value for inequality, in either order. Returns `#!cpp !(lhs == rhs)` (or,
|
2. Compares a view and a `BasicJsonType` value for inequality, in either order. Returns `#!cpp !(lhs == rhs)` (or,
|
||||||
for the reversed order, `#!cpp !(rhs == lhs)`), see [operator==](operator_eq.md).
|
for the reversed order, `#!cpp !(rhs == lhs)`), see [operator==](operator_eq.md).
|
||||||
|
3. Compares a read-only and an editable view of the same `BasicJsonType` for inequality, in either order. Returns
|
||||||
|
`#!cpp !(lhs == rhs)`, see [operator==](operator_eq.md).
|
||||||
|
|
||||||
Since `operator!=` is defined as the negation of [`operator==`](operator_eq.md), it follows the same rules for
|
Since `operator!=` is defined as the negation of [`operator==`](operator_eq.md), it follows the same rules for
|
||||||
special cases: for instance, since a [discarded](is_discarded.md) view is never equal to anything by default (see
|
special cases: for instance, since a [discarded](is_discarded.md) view is never equal to anything by default (see
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ materializing anything.
|
|||||||
- `#!cpp static_cast<std::size_t>(-1)` for a [discarded](is_discarded.md) view, and for a string that contains
|
- `#!cpp static_cast<std::size_t>(-1)` for a [discarded](is_discarded.md) view, and for a string that contains
|
||||||
escapes — such a string was decoded once into the document's own buffer, so there is no single byte range in
|
escapes — such a string was decoded once into the document's own buffer, so there is no single byte range in
|
||||||
`source()` left to point at.
|
`source()` left to point at.
|
||||||
|
- `#!cpp static_cast<std::size_t>(-1)` in an [editable document](../json_editable_document.md) for a value written
|
||||||
|
by an edit ([`set`](../basic_json_document/set.md), [`push_back`](../basic_json_document/push_back.md),
|
||||||
|
[`insert`](../basic_json_document/insert.md)), and for an array or object whose elements an edit added or removed.
|
||||||
|
The values an edit did not touch keep their offsets.
|
||||||
|
|
||||||
## Exception safety
|
## Exception safety
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ using json_editable_document = basic_json_document<json, view_mode::editable>;
|
|||||||
|
|
||||||
This type is an **editable** [`basic_json_document`](basic_json_document/index.md) of the default
|
This type is an **editable** [`basic_json_document`](basic_json_document/index.md) of the default
|
||||||
[`json`](json.md) specialization: in addition to everything [`json_document`](json_document.md) offers,
|
[`json`](json.md) specialization: in addition to everything [`json_document`](json_document.md) offers,
|
||||||
[`set`](basic_json_document/set.md) and [`push_back`](basic_json_document/push_back.md) change values after
|
[`set`](basic_json_document/set.md), [`push_back`](basic_json_document/push_back.md),
|
||||||
parsing, without ever rewriting the source text — see [Edits](basic_json_document/index.md#edits) and
|
[`insert`](basic_json_document/insert.md), and [`erase`](basic_json_document/erase.md) change values after parsing,
|
||||||
|
without ever rewriting the source text — see [Edits](basic_json_document/index.md#edits) and
|
||||||
[Editing a document](../features/json_view/editing.md).
|
[Editing a document](../features/json_view/editing.md).
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ using json_editable_view = basic_json_view<json, view_mode::editable>;
|
|||||||
This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a
|
This type is a [`basic_json_view`](basic_json_view/index.md) of a value of a
|
||||||
[`json_editable_document`](json_editable_document.md). It offers the same read-only interface as
|
[`json_editable_document`](json_editable_document.md). It offers the same read-only interface as
|
||||||
[`json_view`](json_view.md); what is different is what it can be a view *of* — a value that
|
[`json_view`](json_view.md); what is different is what it can be a view *of* — a value that
|
||||||
[`set`](basic_json_document/set.md) and [`push_back`](basic_json_document/push_back.md) can change, with every view
|
[`set`](basic_json_document/set.md), [`push_back`](basic_json_document/push_back.md),
|
||||||
|
[`insert`](basic_json_document/insert.md), and [`erase`](basic_json_document/erase.md) can change, with every view
|
||||||
still referring to it seeing the change, see [Edits](basic_json_document/index.md#edits).
|
still referring to it seeing the change, see [Edits](basic_json_document/index.md#edits).
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|||||||
@@ -47,11 +47,9 @@ Therefore, assertions can be switched off by defining `NDEBUG`.
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Output:
|
The assertion `#!cpp it != m_data.m_value.object->end()` fails in `#!cpp operator[]`.
|
||||||
|
The exact diagnostic format, source filename, and line number depend on the platform,
|
||||||
```
|
library version, and whether the single-header or multi-header version is used.
|
||||||
Assertion failed: (m_value.object->find(key) != m_value.object->end()), function operator[], file json.hpp, line 2144.
|
|
||||||
```
|
|
||||||
|
|
||||||
??? example "Example: user-defined behavior"
|
??? example "Example: user-defined behavior"
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ comparison of two [`json_view`](../basic_json_view/index.md) documents has a fix
|
|||||||
and does not depend on it either.
|
and does not depend on it either.
|
||||||
|
|
||||||
The call stack that these operations need grows with the limit: roughly *a few kilobytes plus a cost per level times the
|
The call stack that these operations need grows with the limit: roughly *a few kilobytes plus a cost per level times the
|
||||||
limit*, where the cost per level is up to about 2.3 KiB in an unoptimized and up to about 0.5 KiB in an optimized build
|
limit*, where the cost per level is up to about 1.8 KiB in an unoptimized and up to about 0.5 KiB in an optimized build
|
||||||
(see [Stack needed](#stack-needed)). The default of `128` levels is tuned for threads with ordinary stacks of a few
|
(see [Stack needed](#stack-needed)). The default of `128` levels is tuned for threads with ordinary stacks of a few
|
||||||
hundred kilobytes or more. Set a lower limit if your threads have small stacks (RTOS tasks, fibers, threads of the musl
|
hundred kilobytes or more. Set a lower limit if your threads have small stacks (RTOS tasks, fibers, threads of the musl
|
||||||
C
|
C
|
||||||
@@ -67,7 +67,7 @@ When the macro is not defined, the library defines it to its default value.
|
|||||||
the levels that are descended into are counted, so the count never exceeds the limit, not even when user code (a
|
the levels that are descended into are counted, so the count never exceeds the limit, not even when user code (a
|
||||||
copy constructor of a custom base class, say) copies a value while a copy is going on. A byte holds `255` at
|
copy constructor of a custom base class, say) copies a value while a copy is going on. A byte holds `255` at
|
||||||
most, and the limit stays below that, so that the count cannot wrap around. (More levels would hardly be
|
most, and the limit stays below that, so that the count cannot wrap around. (More levels would hardly be
|
||||||
useful: `254` levels need up to 576 KiB of stack in an unoptimized build already, twice as much as the
|
useful: `254` levels need up to 448 KiB of stack in an unoptimized build already, twice as much as the
|
||||||
default.)
|
default.)
|
||||||
|
|
||||||
### Stack needed
|
### Stack needed
|
||||||
@@ -83,7 +83,7 @@ Unoptimized (`-O0`):
|
|||||||
|
|
||||||
| Operation | limit 16 | limit 32 | limit 64 | limit 128 | limit 254 |
|
| Operation | limit 16 | limit 32 | limit 64 | limit 128 | limit 254 |
|
||||||
|---|---:|---:|---:|---:|---:|
|
|---|---:|---:|---:|---:|---:|
|
||||||
| `dump()` and `dump(indent)`, `operator<<` | 48 / 32 | 80 / 64 | 144 / 128 | 288 / 256 | 576 / 512 |
|
| `dump()` and `dump(indent)`, `operator<<` | 16 / 16 | 16 / 16 | 32 / 32 | 48 / 48 | 96 / 96 |
|
||||||
| copy construction and assignment | 32 / 16 | 48 / 32 | 112 / 80 | 224 / 144 | 432 / 304 |
|
| copy construction and assignment | 32 / 16 | 48 / 32 | 112 / 80 | 224 / 144 | 432 / 304 |
|
||||||
| `==` | 16 / 16 | 16 / 16 | 16 / 16 | 48 / 32 | 96 / 48 |
|
| `==` | 16 / 16 | 16 / 16 | 16 / 16 | 48 / 32 | 96 / 48 |
|
||||||
| `<=>` (C++20) | 16 / 16 | 16 / 16 | 32 / 32 | 64 / 64 | 144 / 128 |
|
| `<=>` (C++20) | 16 / 16 | 16 / 16 | 32 / 32 | 64 / 64 | 144 / 128 |
|
||||||
@@ -123,23 +123,24 @@ The rule of thumb is
|
|||||||
|
|
||||||
> stack needed ≈ a few KiB + cost per level × limit
|
> stack needed ≈ a few KiB + cost per level × limit
|
||||||
|
|
||||||
with a cost per level of up to 2.3 KiB (`dump` at `-O0` with Clang; 2.1 KiB with GCC), 1.7 KiB for copying and merging
|
with a cost per level of up to 1.8 KiB (conversion to another `basic_json` type at `-O0` with Clang; 1.3 KiB with
|
||||||
(1.3 KiB with GCC), and 0.1 to 0.5 KiB for most other operations at `-O0`; and 0.5 KiB or less at `-O2`. The numbers
|
GCC), 1.7 KiB for copying and merging (1.3 KiB with GCC), 0.4 KiB for `dump`, and 0.1 to 0.5 KiB for most other
|
||||||
come from the difference between the limits `128` and `254` in the tables, so they are rough: other compilers,
|
operations at `-O0`; and 0.5 KiB or less at `-O2`. The numbers come from the difference between the limits `128` and
|
||||||
other targets (the frames of x86-64 code are about as large), other standard libraries, `JSON_DIAGNOSTICS`, and
|
`254` in the tables, so they are rough: other compilers, other targets (the frames of x86-64 code are about as large),
|
||||||
sanitizers change them. Measure with your own tool chain (see the example below) rather than relying on the tables.
|
other standard libraries, `JSON_DIAGNOSTICS`, and sanitizers change them. Measure with your own tool chain (see the
|
||||||
The stack that your own code and the operating system need comes on top of these numbers.
|
example below) rather than relying on the tables. The stack that your own code and the operating system need comes on
|
||||||
|
top of these numbers.
|
||||||
|
|
||||||
Which limit to choose, for the stack that you can spare for the library (the largest limit of those measured
|
Which limit to choose, for the stack that you can spare for the library (the largest limit of those measured
|
||||||
for which the slowest operation needs at most that much):
|
for which the slowest operation needs at most that much):
|
||||||
|
|
||||||
| Stack for the library | optimized (`-O2`) | unoptimized (`-O0`) |
|
| Stack for the library | optimized (`-O2`) | unoptimized (`-O0`) |
|
||||||
|---|---:|---:|
|
|---|---:|---:|
|
||||||
| 32 KiB | 64 | 0 |
|
| 32 KiB | 64 | 16 |
|
||||||
| 64 KiB | 128 | 16 |
|
| 64 KiB | 128 | 32 |
|
||||||
| 128 KiB (the default stack of threads on musl) | 254 | 32 |
|
| 128 KiB (the default stack of threads on musl) | 254 | 64 |
|
||||||
| 256 KiB | 254 | 64 |
|
| 256 KiB | 254 | 128 |
|
||||||
| 512 KiB | 254 | 128 |
|
| 512 KiB | 254 | 254 |
|
||||||
| 1 MiB or more | 254 | 254 |
|
| 1 MiB or more | 254 | 254 |
|
||||||
|
|
||||||
### Speed
|
### Speed
|
||||||
|
|||||||
@@ -171,7 +171,7 @@ See the examples below for the concrete generated code.
|
|||||||
|
|
||||||
The macro is equivalent to:
|
The macro is equivalent to:
|
||||||
|
|
||||||
```cpp hl_lines="22 22 23 24 25 26 27 28"
|
```cpp hl_lines="22 23 24 25 26 27 28"
|
||||||
--8<-- "examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp"
|
--8<-- "examples/nlohmann_define_type_intrusive_only_serialize_explicit.cpp"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ For further information please refer to the corresponding macros without `WITH_N
|
|||||||
|
|
||||||
The macro is equivalent to:
|
The macro is equivalent to:
|
||||||
|
|
||||||
```cpp hl_lines="16 17 18 19 20 21 22 23 24 25 26 27 28"
|
```cpp hl_lines="16 17 18 19 20 21 22 23 24 25 26 27 28 29 30"
|
||||||
--8<-- "examples/nlohmann_define_type_non_intrusive_with_names_explicit.cpp"
|
--8<-- "examples/nlohmann_define_type_non_intrusive_with_names_explicit.cpp"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -89,19 +89,21 @@ 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 as well as to
|
**O(n²)** in total. This applies to filling an [`ordered_json`](ordered_json.md) object key by key (with
|
||||||
parsing one, since the parser inserts each key as it is read.
|
`operator[]`, `emplace`, or `insert`) and, for now, to reading one with a binary reader such as
|
||||||
|
[`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 `-O2 -DNDEBUG` for parsing a flat
|
steeply for machine-generated objects with many thousands of keys. Measured with Apple clang 21, `-O2 -DNDEBUG`,
|
||||||
object of `n` keys, relative to `#!cpp nlohmann::json` (which uses `#!cpp std::map`):
|
arm64, for a flat object of `n` keys (best of seven runs):
|
||||||
|
|
||||||
| `n` | `json` | `ordered_json` | factor |
|
| `n` | `json::parse` | `ordered_json::parse` | `ordered_json`, key by key | `ordered_json::from_cbor` |
|
||||||
|--------|--------|----------------|--------|
|
|--------|---------------|-----------------------|----------------------------|---------------------------|
|
||||||
| 2000 | 0.7 ms | 3.6 ms | 5× |
|
| 2000 | 0.3 ms | 0.1 ms | 4.5 ms | 4.4 ms |
|
||||||
| 4000 | 0.8 ms | 14.0 ms | 19× |
|
| 4000 | 0.5 ms | 0.2 ms | 17.1 ms | 16.8 ms |
|
||||||
| 8000 | 1.6 ms | 67.8 ms | 43× |
|
| 8000 | 0.9 ms | 0.3 ms | 67.0 ms | 66.8 ms |
|
||||||
| 16 000 | 3.3 ms | 181.6 ms | 54× |
|
| 16 000 | 2.0 ms | 0.7 ms | 282.4 ms | 282.4 ms |
|
||||||
|
|
||||||
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)
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ For products, applications, and organizations that use the library, see [Custome
|
|||||||
|
|
||||||
## Format converters
|
## Format converters
|
||||||
|
|
||||||
|
For the formats related to JSON that the library reads itself, such as JSONC or JSON Lines, see
|
||||||
|
[JSON variants](../features/parsing/json_variants.md).
|
||||||
|
|
||||||
- [**tojson**](https://github.com/mircodz/tojson), a header-only converter between YAML/XML documents and
|
- [**tojson**](https://github.com/mircodz/tojson), a header-only converter between YAML/XML documents and
|
||||||
`nlohmann::json`
|
`nlohmann::json`
|
||||||
- [**json2xml**](https://github.com/testillano/json2xml), a header-only converter from `nlohmann::json` to XML for
|
- [**json2xml**](https://github.com/testillano/json2xml), a header-only converter from `nlohmann::json` to XML for
|
||||||
|
|||||||
@@ -18,6 +18,10 @@ before including the `json.hpp` header.
|
|||||||
|
|
||||||
## Function with runtime assertions
|
## Function with runtime assertions
|
||||||
|
|
||||||
|
The examples below identify the failing assertion and the function containing it.
|
||||||
|
The exact diagnostic format, source filename, and line number depend on the platform,
|
||||||
|
library version, and whether the single-header or multi-header version is used.
|
||||||
|
|
||||||
### Unchecked access to a const value
|
### Unchecked access to a const value
|
||||||
|
|
||||||
Function [`operator[]`](../api/basic_json/operator%5B%5D.md) implements unchecked access for arrays and objects. Whereas
|
Function [`operator[]`](../api/basic_json/operator%5B%5D.md) implements unchecked access for arrays and objects. Whereas
|
||||||
@@ -46,11 +50,7 @@ See also the documentation on [element access](element_access/index.md).
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Output:
|
The assertion `#!cpp it != m_data.m_value.object->end()` fails in `#!cpp operator[]`.
|
||||||
|
|
||||||
```
|
|
||||||
Assertion failed: (it != m_data.m_value.object->end()), function operator[], file json.hpp, line 28795.
|
|
||||||
```
|
|
||||||
|
|
||||||
??? example "Example 2: Invalid array index in a JSON pointer"
|
??? example "Example 2: Invalid array index in a JSON pointer"
|
||||||
|
|
||||||
@@ -69,11 +69,7 @@ See also the documentation on [element access](element_access/index.md).
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Output:
|
The assertion `#!cpp idx < ptr->m_data.m_value.array->size()` fails in `#!cpp get_unchecked`.
|
||||||
|
|
||||||
```
|
|
||||||
Assertion failed: (idx < m_data.m_value.array->size()), function operator[], file json.hpp, line 28758.
|
|
||||||
```
|
|
||||||
|
|
||||||
### Constructing from an uninitialized iterator range
|
### Constructing from an uninitialized iterator range
|
||||||
|
|
||||||
@@ -96,11 +92,7 @@ uninitialized iterator is undefined behavior and yields a runtime assertion.
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Output:
|
The assertion `#!cpp first.m_object != nullptr` fails in `#!cpp basic_json`.
|
||||||
|
|
||||||
```
|
|
||||||
Assertion failed: (m_object != nullptr), function operator++, file iter_impl.hpp, line 368.
|
|
||||||
```
|
|
||||||
|
|
||||||
### Operations on uninitialized iterators
|
### Operations on uninitialized iterators
|
||||||
|
|
||||||
@@ -123,11 +115,7 @@ behavior and yields a runtime assertion.
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Output:
|
The assertion `#!cpp m_object != nullptr` fails in `#!cpp operator++`.
|
||||||
|
|
||||||
```
|
|
||||||
Assertion failed: (m_object != nullptr), function operator++, file iter_impl.hpp, line 368.
|
|
||||||
```
|
|
||||||
|
|
||||||
## Changes
|
## Changes
|
||||||
|
|
||||||
|
|||||||
@@ -181,7 +181,10 @@ of the message are reported as [parse_error.110](../../home/exceptions.md#jsonex
|
|||||||
arrays and objects with up to four elements that are terminated by 0xFE, unsorted object keys, or a 0xFF after a
|
arrays and objects with up to four elements that are terminated by 0xFE, unsorted object keys, or a 0xFF after a
|
||||||
string that would also end without it, are accepted. A second 0xFF is not a terminator but an empty string.
|
string that would also end without it, are accepted. A second 0xFF is not a terminator but an empty string.
|
||||||
|
|
||||||
Strings must be valid UTF-8, and a string at the very end of a message must be terminated by 0xFF.
|
Strings must be valid UTF-8, and a string at the very end of a message must be terminated by 0xFF. This also
|
||||||
|
holds when `strict` is false and more data follows the value: the byte that would end the string belongs to the
|
||||||
|
next value, and reading it would remove it from the input, so the missing 0xFF is reported as
|
||||||
|
[parse_error.110](../../home/exceptions.md#jsonexceptionparse_error110).
|
||||||
|
|
||||||
Duplicate object keys are accepted; the last value wins (`ordered_json` keeps the position of the first occurrence).
|
Duplicate object keys are accepted; the last value wins (`ordered_json` keeps the position of the first occurrence).
|
||||||
The specification does not say how to treat them; the HikoGUI reference decoder keeps the first value.
|
The specification does not say how to treat them; the HikoGUI reference decoder keeps the first value.
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ This library does not support comments *by default*. It does so for three reason
|
|||||||
|
|
||||||
3. It is dangerous for interoperability if some libraries add comment support while others do not. Please check [The Harmful Consequences of the Robustness Principle](https://tools.ietf.org/html/draft-iab-protocol-maintenance-01) on this.
|
3. It is dangerous for interoperability if some libraries add comment support while others do not. Please check [The Harmful Consequences of the Robustness Principle](https://tools.ietf.org/html/draft-iab-protocol-maintenance-01) on this.
|
||||||
|
|
||||||
However, you can set parameter `ignore_comments` to `#!cpp true` in the [`parse`](../api/basic_json/parse.md) function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace. Combined with [`ignore_trailing_commas`](trailing_commas.md) (also a `parse` parameter), this covers what is commonly referred to as **JSONC** (JSON with Comments, as used e.g. by Visual Studio Code's `.jsonc` files) — comments and trailing commas, nothing more. This is a different, smaller extension than [JSON5](https://json5.org), which additionally allows unquoted keys, single-quoted strings, and other syntax changes that this library does not support.
|
However, you can set parameter `ignore_comments` to `#!cpp true` in the [`parse`](../api/basic_json/parse.md) function to ignore `//` or `/* */` comments. Comments will then be treated as whitespace. Combined with [`ignore_trailing_commas`](trailing_commas.md) (also a `parse` parameter), this covers what is commonly referred to as **JSONC** (JSON with Comments, as used e.g. by Visual Studio Code's `.jsonc` files) — comments and trailing commas, nothing more. This is a different, smaller extension than [JSON5](https://json5.org), which additionally allows unquoted keys, single-quoted strings, and other syntax changes that this library does not support. See [JSON variants](parsing/json_variants.md) for a
|
||||||
|
comparison with these and other formats.
|
||||||
|
|
||||||
For more information, see [JSON With Commas and Comments (JWCC)](https://nigeltao.github.io/blog/2021/json-with-commas-comments.html).
|
For more information, see [JSON With Commas and Comments (JWCC)](https://nigeltao.github.io/blog/2021/json-with-commas-comments.html).
|
||||||
|
|
||||||
@@ -43,5 +44,6 @@ For more information, see [JSON With Commas and Comments (JWCC)](https://nigelta
|
|||||||
|
|
||||||
- [`parse`](../api/basic_json/parse.md) - deserialize from a compatible input
|
- [`parse`](../api/basic_json/parse.md) - deserialize from a compatible input
|
||||||
- [Trailing Commas](trailing_commas.md) - accept trailing commas in arrays and objects
|
- [Trailing Commas](trailing_commas.md) - accept trailing commas in arrays and objects
|
||||||
|
- [JSON variants](parsing/json_variants.md) - JSONC, JWCC, JSON5, and other formats related to JSON
|
||||||
- [Parsing](parsing/index.md) - overview of the parsing functions
|
- [Parsing](parsing/index.md) - overview of the parsing functions
|
||||||
- [Parsing Untrusted Input](parsing/untrusted_input.md) - limits and checks for input you do not control
|
- [Parsing Untrusted Input](parsing/untrusted_input.md) - limits and checks for input you do not control
|
||||||
@@ -18,6 +18,8 @@ C++ types, and finally serialize it again.
|
|||||||
- [Editable documents](json_view/editing.md) — modify a parsed document without building a `json` tree.
|
- [Editable documents](json_view/editing.md) — modify a parsed document without building a `json` tree.
|
||||||
- [Images](json_view/images.md) — save a parsed document and load it again without parsing it.
|
- [Images](json_view/images.md) — save a parsed document and load it again without parsing it.
|
||||||
- [Comments](comments.md) and [trailing commas](trailing_commas.md) — opt-in relaxations of the JSON grammar.
|
- [Comments](comments.md) and [trailing commas](trailing_commas.md) — opt-in relaxations of the JSON grammar.
|
||||||
|
- [JSON variants](parsing/json_variants.md) — which of JSONC, JSON5, JSON Lines, I-JSON, and other formats related to
|
||||||
|
JSON the library supports.
|
||||||
|
|
||||||
## Accessing and modifying values
|
## Accessing and modifying values
|
||||||
|
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,8 @@ that arrives in chunks or contains several values is covered in
|
|||||||
[Large, streamed, and partial input](large_and_partial_input.md).
|
[Large, streamed, and partial input](large_and_partial_input.md).
|
||||||
|
|
||||||
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). See
|
||||||
|
[JSON variants](json_variants.md) for how much of JSONC, JSON5, and other formats related to JSON the library supports.
|
||||||
|
|
||||||
[`basic_json_document::parse`](../../api/basic_json_document/parse.md) accepts the same inputs with the same options,
|
[`basic_json_document::parse`](../../api/basic_json_document/parse.md) accepts the same inputs with the same options,
|
||||||
but keeps the result in a document rather than in a `basic_json` value; see
|
but keeps the result in a document rather than in a `basic_json` value; see
|
||||||
@@ -169,6 +170,7 @@ options.
|
|||||||
- [`accept`](../../api/basic_json/accept.md) - check if the input is valid JSON
|
- [`accept`](../../api/basic_json/accept.md) - check if the input is valid JSON
|
||||||
- [`sax_parse`](../../api/basic_json/sax_parse.md) - generate SAX events
|
- [`sax_parse`](../../api/basic_json/sax_parse.md) - generate SAX events
|
||||||
- [JSON Lines](json_lines.md) - parse newline-delimited JSON
|
- [JSON Lines](json_lines.md) - parse newline-delimited JSON
|
||||||
|
- [JSON variants](json_variants.md) - JSONC, JSON5, JSON text sequences, I-JSON, and other formats related to JSON
|
||||||
- [Large, streamed, and partial input](large_and_partial_input.md) - chunked, concatenated, and very large input
|
- [Large, streamed, and partial input](large_and_partial_input.md) - chunked, concatenated, and very large input
|
||||||
- [parser callbacks](parser_callbacks.md) - influence the parsing by a callback function
|
- [parser callbacks](parser_callbacks.md) - influence the parsing by a callback function
|
||||||
- [SAX interface](sax_interface.md) - implement a custom SAX handler
|
- [SAX interface](sax_interface.md) - implement a custom SAX handler
|
||||||
|
|||||||
@@ -68,5 +68,6 @@ JSON Lines input with more than one value is treated as invalid JSON by the [`pa
|
|||||||
|
|
||||||
- [Parsing](index.md) - overview of the parsing functions
|
- [Parsing](index.md) - overview of the parsing functions
|
||||||
- [Large, Streamed, and Partial Input](large_and_partial_input.md) - chunked, concatenated, and very large input
|
- [Large, Streamed, and Partial Input](large_and_partial_input.md) - chunked, concatenated, and very large input
|
||||||
|
- [JSON variants](json_variants.md) - NDJSON, JSON text sequences, and other formats related to JSON
|
||||||
- [`parse`](../../api/basic_json/parse.md) - deserialize from a compatible input
|
- [`parse`](../../api/basic_json/parse.md) - deserialize from a compatible input
|
||||||
- [`operator>>`](../../api/operator_gtgt.md) - deserialize from stream
|
- [`operator>>`](../../api/operator_gtgt.md) - deserialize from stream
|
||||||
@@ -0,0 +1,185 @@
|
|||||||
|
# JSON Variants
|
||||||
|
|
||||||
|
This library reads and writes JSON as defined by [RFC 8259](https://tools.ietf.org/html/rfc8259) and
|
||||||
|
[ECMA-404](https://ecma-international.org/publications-and-standards/standards/ecma-404/). Many other formats are
|
||||||
|
built around JSON: some relax its syntax (for example, to allow comments), some put several JSON values into one
|
||||||
|
stream, some restrict it further, and some give certain JSON values a special meaning. This page lists the common
|
||||||
|
ones and how much of each the library supports.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
| Variant | Kind | Support |
|
||||||
|
|------------------------------------------------------------------|----------------------------|-----------------------------------------------------------------------------------------------------------|
|
||||||
|
| [JSON](https://tools.ietf.org/html/rfc8259) (RFC 8259, ECMA-404) | the standard | :white_check_mark: default |
|
||||||
|
| [JSONC](https://jsonc.org) | relaxed syntax | :white_check_mark: with `ignore_comments` (and `ignore_trailing_commas` where trailing commas are wanted) |
|
||||||
|
| [JWCC](https://nigeltao.github.io/blog/2021/json-with-commas-comments.html) / [HuJSON](https://github.com/tailscale/hujson) | relaxed syntax | :white_check_mark: with `ignore_comments` and `ignore_trailing_commas` |
|
||||||
|
| [JSON5](https://spec.json5.org) | relaxed syntax | :warning: comments and trailing commas only |
|
||||||
|
| [Hjson](https://hjson.github.io), [HOCON](https://github.com/lightbend/config/blob/main/HOCON.md), [UCL](https://github.com/vstakhov/libucl), SJSON | configuration languages | :x: only input that is JSON plus `//`/`/* */` comments and trailing commas |
|
||||||
|
| [YAML](https://yaml.org), [Amazon Ion](https://amazon-ion.github.io/ion-docs/) | other languages | :x: see [converters](#other-languages-and-converters) |
|
||||||
|
| [JSON Lines](https://jsonlines.org), [NDJSON](https://github.com/ndjson/ndjson-spec) | several values | :white_check_mark: line by line, see [JSON Lines](json_lines.md) |
|
||||||
|
| concatenated JSON | several values | :white_check_mark: with `operator>>`, see [Large, streamed, and partial input](large_and_partial_input.md#several-values-in-one-stream) |
|
||||||
|
| [JSON text sequences](https://www.rfc-editor.org/rfc/rfc7464) (RFC 7464) | several values | :warning: split the input yourself, see [below](#json-text-sequences-rfc-7464) |
|
||||||
|
| [I-JSON](https://www.rfc-editor.org/rfc/rfc7493) (RFC 7493) | restricted profile | :warning: most rules are enforced, see [below](#i-json-rfc-7493) |
|
||||||
|
| [JSON Canonicalization Scheme](https://www.rfc-editor.org/rfc/rfc8785) (RFC 8785) | restricted output | :x: `dump()` output is close, but not canonical |
|
||||||
|
| `NaN`, `Infinity` (JavaScript, Python) | number extension | :x: rejected by the parser, written as `null` |
|
||||||
|
| [JSON Schema](https://json-schema.org), [JSON-LD](https://json-ld.org), [GeoJSON](https://www.rfc-editor.org/rfc/rfc7946), [MongoDB Extended JSON](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/) | conventions on top of JSON | :white_check_mark: parsed as plain JSON; interpreting them is up to you |
|
||||||
|
|
||||||
|
Whatever was parsed, [`dump`](../../api/basic_json/dump.md) always writes standard JSON: no comments, no trailing
|
||||||
|
commas, and no `NaN` or `Infinity`.
|
||||||
|
|
||||||
|
## Standard JSON
|
||||||
|
|
||||||
|
By default, [`parse`](../../api/basic_json/parse.md) accepts exactly the JSON grammar of RFC 8259, including values
|
||||||
|
other than objects and arrays at the top level (such as `#!json 42` or `#!json "text"`). A UTF-8 byte order mark at the
|
||||||
|
very start of the input is skipped; anywhere else it is a parse error.
|
||||||
|
|
||||||
|
## Relaxed syntax
|
||||||
|
|
||||||
|
### JSONC and JWCC
|
||||||
|
|
||||||
|
**JSONC** ("JSON with comments", used, for example, by Visual Studio Code's `settings.json` and `devcontainer.json`)
|
||||||
|
adds `//` and `/* */` comments to JSON. Block comments do not nest. The [JSONC specification](https://jsonc.org) lets
|
||||||
|
parsers decide whether they accept trailing commas. **JWCC** ("JSON with commas and comments") adds the same comments
|
||||||
|
and always allows a single trailing comma in arrays and objects; [HuJSON](https://github.com/tailscale/hujson) is
|
||||||
|
Tailscale's implementation of it.
|
||||||
|
|
||||||
|
Both are fully supported by passing `ignore_comments` and `ignore_trailing_commas` to `parse`:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
json j = json::parse(input,
|
||||||
|
/* callback */ nullptr,
|
||||||
|
/* allow_exceptions */ true,
|
||||||
|
/* ignore_comments */ true,
|
||||||
|
/* ignore_trailing_commas */ true);
|
||||||
|
```
|
||||||
|
|
||||||
|
Comments are treated as whitespace and are not kept, so they are lost when the value is written again. This also holds
|
||||||
|
for a [`json_document`](../../api/basic_json_document/index.md): [`dump()`](../../api/basic_json_view/dump.md) of its
|
||||||
|
root view writes the same text as `dump()` of the parsed `json` value, even though the document keeps the source text
|
||||||
|
with the comments. See
|
||||||
|
[Comments](../comments.md) and [Trailing commas](../trailing_commas.md) for the details. The .NET options
|
||||||
|
`JsonCommentHandling.Skip` and `AllowTrailingCommas` accept the same syntax.
|
||||||
|
|
||||||
|
### JSON5
|
||||||
|
|
||||||
|
[JSON5](https://spec.json5.org) is a superset of JSON that follows the syntax of ECMAScript 5 object literals. Besides
|
||||||
|
comments and trailing commas, it allows unquoted keys, single-quoted strings, line continuations in strings, more
|
||||||
|
escape sequences (`\v`, `\0`, `\xHH`, ...), hexadecimal numbers, a leading `+`, a leading or trailing decimal point, and
|
||||||
|
`Infinity` and `NaN`. With the two options above, the library accepts JSON5 documents that only use comments and
|
||||||
|
trailing commas; using any of the other extensions is a parse error.
|
||||||
|
|
||||||
|
### Configuration languages
|
||||||
|
|
||||||
|
[Hjson](https://hjson.github.io), [HOCON](https://github.com/lightbend/config/blob/main/HOCON.md),
|
||||||
|
[UCL](https://github.com/vstakhov/libucl), and SJSON (from the Bitsquid/Stingray game engine) are configuration
|
||||||
|
languages that accept JSON but go much further: `#` comments, unquoted keys and values, `=` instead of `:`, optional
|
||||||
|
commas and braces, multiline strings, and, in HOCON and UCL, includes and variable substitutions. The library does not
|
||||||
|
support these extensions. A `#` comment in particular is always a parse error, even with `ignore_comments`.
|
||||||
|
|
||||||
|
### Other languages and converters
|
||||||
|
|
||||||
|
[YAML](https://yaml.org) and the text format of [Amazon Ion](https://amazon-ion.github.io/ion-docs/) are separate
|
||||||
|
languages with their own data models, and the library does not read them. The [Ecosystem](../../community/ecosystem.md)
|
||||||
|
page lists projects that convert YAML or XML to and from `nlohmann::json`.
|
||||||
|
|
||||||
|
## Several values in one input
|
||||||
|
|
||||||
|
`parse` reads exactly one value and reports anything after it as an error. Several common formats combine many values
|
||||||
|
into one input:
|
||||||
|
|
||||||
|
- **[JSON Lines](https://jsonlines.org)** and **[NDJSON](https://github.com/ndjson/ndjson-spec)** put one value on each
|
||||||
|
line. Read them line by line, as shown on the [JSON Lines](json_lines.md) page. The two formats differ in one detail:
|
||||||
|
JSON Lines does not allow empty lines, while NDJSON parsers may skip them. Passing an empty line to `parse` is an
|
||||||
|
error, so skip empty lines yourself if you read NDJSON.
|
||||||
|
- **Concatenated JSON** puts values directly after each other, optionally separated by whitespace. Read them with
|
||||||
|
[`operator>>`](../../api/operator_gtgt.md), as shown in
|
||||||
|
[Large, streamed, and partial input](large_and_partial_input.md#several-values-in-one-stream).
|
||||||
|
|
||||||
|
### JSON text sequences (RFC 7464)
|
||||||
|
|
||||||
|
A [JSON text sequence](https://www.rfc-editor.org/rfc/rfc7464) (media type `application/json-seq`) starts every value
|
||||||
|
with the ASCII record separator (RS, `0x1E`) and ends it with a line feed. The library does not read the record
|
||||||
|
separator, but splitting the input at it is enough. The RFC asks parsers to skip a truncated or invalid value and
|
||||||
|
continue with the next one, which parsing without exceptions does:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
std::string record;
|
||||||
|
while (std::getline(input, record, '\x1e'))
|
||||||
|
{
|
||||||
|
// the input starts with RS, and several RS in a row are allowed
|
||||||
|
if (record.find_first_not_of(" \t\r\n") == std::string::npos)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
json j = json::parse(record, nullptr, /* allow_exceptions */ false);
|
||||||
|
if (j.is_discarded())
|
||||||
|
{
|
||||||
|
continue; // truncated or invalid value
|
||||||
|
}
|
||||||
|
|
||||||
|
// use j
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
To write a sequence, write `'\x1e'`, then `j.dump()`, then `'\n'` for every value.
|
||||||
|
|
||||||
|
## Restricted profiles
|
||||||
|
|
||||||
|
### I-JSON (RFC 7493)
|
||||||
|
|
||||||
|
[I-JSON](https://www.rfc-editor.org/rfc/rfc7493) ("Internet JSON") is a subset of JSON meant for interoperable
|
||||||
|
protocols. The table shows how the library handles each of its rules:
|
||||||
|
|
||||||
|
| I-JSON rule | Library behavior |
|
||||||
|
|-----------------------------------------------------------------|-----------------------------------------------------------------------------------------------|
|
||||||
|
| text must be UTF-8 | enforced: invalid UTF-8 is a parse error |
|
||||||
|
| no surrogate code points in strings, also not as `\u` escapes | enforced: a lone surrogate such as `"\ud800"` is a parse error |
|
||||||
|
| no noncharacters (such as U+FFFF) in strings | not enforced: they are accepted |
|
||||||
|
| no duplicate object keys | not enforced: they are accepted; `parse` keeps the last value, while lookups in a [`json_view`](../json_view/differences.md#duplicate-keys) return the first; see the [recipe for rejecting duplicate keys](parser_callbacks.md#recipe-rejecting-duplicate-object-keys) |
|
||||||
|
| numbers should fit into an IEEE 754 double | numbers that overflow a double, such as `1E400`, throw [`out_of_range.406`](../../home/exceptions.md#jsonexceptionout_of_range406); integers are kept exactly up to 64 bits, larger ones are stored as doubles, see [number handling](../types/number_handling.md) |
|
||||||
|
| top-level values should be objects or arrays | not enforced: any value is accepted at the top level |
|
||||||
|
|
||||||
|
### JSON Canonicalization Scheme (RFC 8785)
|
||||||
|
|
||||||
|
The [JSON Canonicalization Scheme](https://www.rfc-editor.org/rfc/rfc8785) (JCS) defines one exact text for each value,
|
||||||
|
so that it can be hashed or signed. `dump()` without indentation comes close, as it writes no whitespace and, with the
|
||||||
|
default [`std::map`](../types/index.md#objects), sorts the object keys. It is not canonical, though:
|
||||||
|
|
||||||
|
- JCS formats numbers like ECMAScript, so `#!json 1.0` is written as `1` and `#!json 0.000001` as `0.000001`, while
|
||||||
|
`dump()` writes `1.0` and `1e-06`.
|
||||||
|
- JCS sorts keys by their UTF-16 code units, while `std::map` sorts them by their UTF-8 bytes. The two orders differ
|
||||||
|
for keys that contain characters above U+FFFF.
|
||||||
|
|
||||||
|
## Non-finite numbers
|
||||||
|
|
||||||
|
`NaN`, `Infinity`, and `-Infinity` are not part of JSON, but some libraries write them, among them Python's `json`
|
||||||
|
module, which does so by default (use `allow_nan=False` there to get standard JSON). This library rejects them as
|
||||||
|
parse errors, and it writes the non-finite values that a `basic_json` value can hold as `#!json null`. See
|
||||||
|
[NaN handling](../types/number_handling.md#nan-handling).
|
||||||
|
|
||||||
|
## Conventions on top of JSON
|
||||||
|
|
||||||
|
Formats such as [JSON Schema](https://json-schema.org), [JSON-LD](https://json-ld.org),
|
||||||
|
[GeoJSON](https://www.rfc-editor.org/rfc/rfc7946), and
|
||||||
|
[MongoDB Extended JSON](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/) are standard JSON with
|
||||||
|
agreed meanings for certain keys or values, such as `{"$numberLong": "42"}` or a `"$comment"` key. The library parses
|
||||||
|
and writes them like any other JSON; what the values mean is up to your code. For schema validation, see the
|
||||||
|
[Ecosystem](../../community/ecosystem.md#schema-validation) page.
|
||||||
|
|
||||||
|
A few other standards built on JSON are supported directly: [JSON Pointer](../json_pointer.md),
|
||||||
|
[JSON Patch](../json_patch.md), and [JSON Merge Patch](../merge_patch.md).
|
||||||
|
|
||||||
|
For compact binary encodings of the JSON data model, such as CBOR or MessagePack, see
|
||||||
|
[Binary formats](../binary_formats/index.md).
|
||||||
|
|
||||||
|
## See also
|
||||||
|
|
||||||
|
- [Parsing](index.md) - overview of the parsing functions
|
||||||
|
- [Comments](../comments.md) - ignore `//` and `/* */` comments while parsing
|
||||||
|
- [Trailing Commas](../trailing_commas.md) - accept trailing commas in arrays and objects
|
||||||
|
- [JSON Lines](json_lines.md) - parse newline-delimited JSON
|
||||||
|
- [Large, Streamed, and Partial Input](large_and_partial_input.md) - chunked, concatenated, and very large input
|
||||||
|
- [Number handling](../types/number_handling.md) - how numbers are parsed, stored, and written
|
||||||
|
- [Binary formats](../binary_formats/index.md) - binary encodings of the JSON data model
|
||||||
|
- [Ecosystem](../../community/ecosystem.md) - projects that build on this library, including format converters
|
||||||
@@ -64,13 +64,13 @@ stack for the nesting.)
|
|||||||
|
|
||||||
The call stack still needs room for the levels that are recursed into, and the default limit assumes a stack of ordinary
|
The call stack still needs room for the levels that are recursed into, and the default limit assumes a stack of ordinary
|
||||||
size. The stack that an operation needs is roughly proportional to the limit; with the default of 128 levels, the most
|
size. The stack that an operation needs is roughly proportional to the limit; with the default of 128 levels, the most
|
||||||
demanding operation (`dump` in an unoptimized build) needed up to 288 KiB, and an optimized build needed 48 KiB or less,
|
demanding operations (copying a value and converting it to another `basic_json` type, in an unoptimized build) needed
|
||||||
in measurements on macOS with arm64. The most demanding operation needed this much stack at the limits `0`, `32`,
|
up to 224 KiB, and an optimized build needed 48 KiB or less, in measurements on macOS with arm64. The most demanding
|
||||||
`128`, and `254`:
|
operation needed this much stack at the limits `0`, `32`, `128`, and `254`:
|
||||||
|
|
||||||
| Stack needed, in KiB | limit 0 | limit 32 | limit 128 (default) | limit 254 |
|
| Stack needed, in KiB | limit 0 | limit 32 | limit 128 (default) | limit 254 |
|
||||||
|---|---:|---:|---:|---:|
|
|---|---:|---:|---:|---:|
|
||||||
| unoptimized build (`-O0`) | 16 or less | 80 | 288 | 576 |
|
| unoptimized build (`-O0`) | 16 or less | 64 | 224 | 448 |
|
||||||
| optimized build (`-O2`) | 16 or less | 16 or less | 48 | 112 |
|
| optimized build (`-O2`) | 16 or less | 16 or less | 48 | 112 |
|
||||||
|
|
||||||
A thread with a smaller stack (a task of a real-time operating system, a fiber, or a thread of the musl C library, whose
|
A thread with a smaller stack (a task of a real-time operating system, a fiber, or a thread of the musl C library, whose
|
||||||
@@ -81,6 +81,20 @@ 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
|
||||||
@@ -168,6 +182,20 @@ 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,9 +227,11 @@ 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, enforce your own limit with a
|
- If the input's nesting depth matters to you (for example because you pretty-print, log, or flatten it), enforce your
|
||||||
[parser callback](parser_callbacks.md#recipe-max-nesting-depth-via-a-callback) or a
|
own limit with 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
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ serialization fails by default. The fourth argument of `dump` selects an
|
|||||||
|
|
||||||
## Using `std::format`, `std::print`, and `fmt`
|
## Using `std::format`, `std::print`, and `fmt`
|
||||||
|
|
||||||
Since version 3.12.0, JSON values can be formatted directly with C++20's
|
Since version 3.13.0, JSON values can be formatted directly with C++20's
|
||||||
[`std::format`](https://en.cppreference.com/w/cpp/utility/format/format) whenever the standard library provides the
|
[`std::format`](https://en.cppreference.com/w/cpp/utility/format/format) whenever the standard library provides the
|
||||||
`<format>` header (controlled by [`JSON_HAS_STD_FORMAT`](../api/macros/json_has_std_format.md)). This is enabled by the
|
`<format>` header (controlled by [`JSON_HAS_STD_FORMAT`](../api/macros/json_has_std_format.md)). This is enabled by the
|
||||||
[`std::formatter<basic_json>`](../api/basic_json/std_formatter.md) specialization, which also makes JSON values work with
|
[`std::formatter<basic_json>`](../api/basic_json/std_formatter.md) specialization, which also makes JSON values work with
|
||||||
|
|||||||
@@ -42,5 +42,6 @@ For more information, see [JSON With Commas and Comments (JWCC)](https://nigelta
|
|||||||
|
|
||||||
- [`parse`](../api/basic_json/parse.md) - deserialize from a compatible input
|
- [`parse`](../api/basic_json/parse.md) - deserialize from a compatible input
|
||||||
- [Comments](comments.md) - ignore `//` and `/* */` comments while parsing
|
- [Comments](comments.md) - ignore `//` and `/* */` comments while parsing
|
||||||
|
- [JSON variants](parsing/json_variants.md) - JSONC, JWCC, JSON5, and other formats related to JSON
|
||||||
- [Parsing](parsing/index.md) - overview of the parsing functions
|
- [Parsing](parsing/index.md) - overview of the parsing functions
|
||||||
- [Parsing Untrusted Input](parsing/untrusted_input.md) - limits and checks for input you do not control
|
- [Parsing Untrusted Input](parsing/untrusted_input.md) - limits and checks for input you do not control
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
# Recipes for Common Types
|
# Recipes for Common Types
|
||||||
|
|
||||||
The library converts the standard containers, strings, numbers, tuples, `std::optional`, and `std::filesystem::path`
|
The library converts the standard containers, strings, numbers, tuples, `std::optional` (since version 3.12.0), and
|
||||||
(the last two since version 3.12.0, C++17) out of the box, and your own types once you write conversion functions or use
|
`std::filesystem::path` (since version 3.10.5), the last two with C++17, out of the box, and your own types once you write conversion functions or use
|
||||||
the macros (see [Arbitrary type conversions](arbitrary_types.md)). It deliberately does **not** convert smart pointers,
|
the macros (see [Arbitrary type conversions](arbitrary_types.md)). It deliberately does **not** convert smart pointers,
|
||||||
`std::variant`, `std::chrono` types, or class hierarchies, because there is more than one reasonable JSON
|
`std::variant`, `std::chrono` types, or class hierarchies, because there is more than one reasonable JSON
|
||||||
representation for each of them. This page shows a representation for each that you can copy and adapt.
|
representation for each of them. This page shows a representation for each that you can copy and adapt.
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ the result of an internet search. If you know further customers of the library,
|
|||||||
- [**Audinate**](https://www.audinate.com/legal/software-licensing/dante-av-h-open-source-licenses/), a provider of networked audio solutions specializing in Dante technology, which facilitates high-quality digital audio transport over IP networks
|
- [**Audinate**](https://www.audinate.com/legal/software-licensing/dante-av-h-open-source-licenses/), a provider of networked audio solutions specializing in Dante technology, which facilitates high-quality digital audio transport over IP networks
|
||||||
- [**Canon CanoScan LIDE**](https://carolburo.com/wp-content/uploads/2024/06/LiDE400_OnlineManual_Win_FR_V02.pdf), a series of flatbed scanners offering high-resolution image scanning for home and office use
|
- [**Canon CanoScan LIDE**](https://carolburo.com/wp-content/uploads/2024/06/LiDE400_OnlineManual_Win_FR_V02.pdf), a series of flatbed scanners offering high-resolution image scanning for home and office use
|
||||||
- [**Canon PIXMA Printers**](https://www.mediaexpert.pl/products/files/73/7338196/Instrukcja-obslugi-CANON-Pixma-TS7450i.pdf), a line of all-in-one inkjet printers known for high-quality printing and wireless connectivity
|
- [**Canon PIXMA Printers**](https://www.mediaexpert.pl/products/files/73/7338196/Instrukcja-obslugi-CANON-Pixma-TS7450i.pdf), a line of all-in-one inkjet printers known for high-quality printing and wireless connectivity
|
||||||
- [**Cisco Webex Desk Camera**](https://www.cisco.com/c/dam/en_us/about/doing_business/open_source/docs/CiscoWebexDeskCamera-23-1622100417.pdf), a video camera designed for professional-quality video conferencing and remote collaboration
|
- [**Cisco Webex Desk Camera**](https://www.cisco.com/c/en/us/products/collaboration-endpoints/webex-desk-camera/index.html), a video camera designed for professional-quality video conferencing and remote collaboration
|
||||||
- [**DJI Edge SDK**](https://github.com/dji-sdk/Edge-SDK-V2-Demo), the reference applications for DJI's Edge SDK, used to build edge computing services on DJI drone docks
|
- [**DJI Edge SDK**](https://github.com/dji-sdk/Edge-SDK-V2-Demo), the reference applications for DJI's Edge SDK, used to build edge computing services on DJI drone docks
|
||||||
- [**Elgato Stream Deck**](https://github.com/elgatosf/streamdeck-obs-plugin2), a family of programmable control surfaces for content creators and their plugin ecosystem
|
- [**Elgato Stream Deck**](https://github.com/elgatosf/streamdeck-obs-plugin2), a family of programmable control surfaces for content creators and their plugin ecosystem
|
||||||
- [**Instagrid**](https://instagrid.co/intellectual-property/foss), a manufacturer of portable, high-performance battery systems for professional mobile power supply
|
- [**Instagrid**](https://instagrid.co/intellectual-property/foss), a manufacturer of portable, high-performance battery systems for professional mobile power supply
|
||||||
@@ -102,7 +102,7 @@ the result of an internet search. If you know further customers of the library,
|
|||||||
|
|
||||||
- [**Apple iOS and macOS**](https://www.apple.com/macos), a family of operating systems developed by Apple, including iOS for mobile devices and macOS for desktop computers
|
- [**Apple iOS and macOS**](https://www.apple.com/macos), a family of operating systems developed by Apple, including iOS for mobile devices and macOS for desktop computers
|
||||||
- [**Chromium**](https://chromium.googlesource.com/chromium/src/+/main/third_party/nlohmann_json/), the open-source browser project that Google Chrome, Microsoft Edge, and many other browsers are built on, where the library is used as data container for on-device model execution
|
- [**Chromium**](https://chromium.googlesource.com/chromium/src/+/main/third_party/nlohmann_json/), the open-source browser project that Google Chrome, Microsoft Edge, and many other browsers are built on, where the library is used as data container for on-device model execution
|
||||||
- [**Google Fuchsia**](https://fuchsia.googlesource.com/third_party/json/), an open-source operating system developed by Google, designed to be secure, updatable, and adaptable across various devices
|
- [**Google Fuchsia**](https://fuchsia.googlesource.com/fuchsia/+/refs/heads/main/scripts/prebuilts/swiftshader/build_config), an open-source operating system developed by Google, designed to be secure, updatable, and adaptable across various devices, where the library is used indirectly through bundled dependencies such as SwiftShader's debugger support and cppdap
|
||||||
- [**LG webOS**](https://github.com/webosose/com.webos.service.camera), a Linux-based operating system used in LG smart TVs, signage, and embedded devices
|
- [**LG webOS**](https://github.com/webosose/com.webos.service.camera), a Linux-based operating system used in LG smart TVs, signage, and embedded devices
|
||||||
- [**Microsoft Azure Linux**](https://github.com/microsoft/azurelinux), a Linux distribution developed by Microsoft for Azure infrastructure and edge workloads
|
- [**Microsoft Azure Linux**](https://github.com/microsoft/azurelinux), a Linux distribution developed by Microsoft for Azure infrastructure and edge workloads
|
||||||
- [**OpenHarmony**](https://github.com/openharmony/third_party_json), an open-source operating system for smart devices and the foundation of HarmonyOS
|
- [**OpenHarmony**](https://github.com/openharmony/third_party_json), an open-source operating system for smart devices and the foundation of HarmonyOS
|
||||||
@@ -174,7 +174,7 @@ the result of an internet search. If you know further customers of the library,
|
|||||||
- [**NVIDIA Instant NGP**](https://github.com/NVlabs/instant-ngp/blob/master/src/nerf_loader.cu), an implementation of instant neural graphics primitives for rapid scene reconstruction
|
- [**NVIDIA Instant NGP**](https://github.com/NVlabs/instant-ngp/blob/master/src/nerf_loader.cu), an implementation of instant neural graphics primitives for rapid scene reconstruction
|
||||||
- [**NVIDIA TensorRT**](https://github.com/NVIDIA/TensorRT), an SDK for high-performance deep learning inference, including its TensorRT-LLM extension for large language models
|
- [**NVIDIA TensorRT**](https://github.com/NVIDIA/TensorRT), an SDK for high-performance deep learning inference, including its TensorRT-LLM extension for large language models
|
||||||
- [**NVIDIA TensorRT-LLM**](https://github.com/NVIDIA/TensorRT-LLM/blob/main/cpp/tensorrt_llm/common/safetensors.cpp), a toolkit for optimizing and serving large language model inference on GPUs
|
- [**NVIDIA TensorRT-LLM**](https://github.com/NVIDIA/TensorRT-LLM/blob/main/cpp/tensorrt_llm/common/safetensors.cpp), a toolkit for optimizing and serving large language model inference on GPUs
|
||||||
- [**ONNX Runtime**](https://github.com/microsoft/onnxruntime), a cross-platform inference and training accelerator for machine learning models
|
- [**ONNX Runtime**](https://onnxruntime.ai), a cross-platform inference and training accelerator for machine learning models
|
||||||
- [**OpenVINO**](https://github.com/openvinotoolkit/openvino), Intel's toolkit for optimizing and deploying deep learning inference across CPUs, GPUs, and NPUs
|
- [**OpenVINO**](https://github.com/openvinotoolkit/openvino), Intel's toolkit for optimizing and deploying deep learning inference across CPUs, GPUs, and NPUs
|
||||||
- [**PaddleOCR**](https://github.com/PaddlePaddle/PaddleOCR/blob/main/deploy/cpp_infer/src/modules/text_detection/result.cc), an optical character recognition toolkit that turns documents and images into structured data
|
- [**PaddleOCR**](https://github.com/PaddlePaddle/PaddleOCR/blob/main/deploy/cpp_infer/src/modules/text_detection/result.cc), an optical character recognition toolkit that turns documents and images into structured data
|
||||||
- [**PaddlePaddle**](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/ap/src/axpr/anf_expr.cc), a deep learning framework for distributed training and inference
|
- [**PaddlePaddle**](https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/ap/src/axpr/anf_expr.cc), a deep learning framework for distributed training and inference
|
||||||
@@ -216,7 +216,7 @@ the result of an internet search. If you know further customers of the library,
|
|||||||
- [**Check Point**](https://www.checkpoint.com/about-us/copyright-and-trademarks/), a cybersecurity company specializing in threat prevention and network security solutions, offering a range of products designed to protect enterprises from cyber threats and ensure data integrity
|
- [**Check Point**](https://www.checkpoint.com/about-us/copyright-and-trademarks/), a cybersecurity company specializing in threat prevention and network security solutions, offering a range of products designed to protect enterprises from cyber threats and ensure data integrity
|
||||||
- [**EasyEffects**](https://github.com/wwmm/easyeffects/blob/master/src/presets_manager.hpp), an audio effects processor for PipeWire offering limiting, compression and equalization
|
- [**EasyEffects**](https://github.com/wwmm/easyeffects/blob/master/src/presets_manager.hpp), an audio effects processor for PipeWire offering limiting, compression and equalization
|
||||||
- [**espanso**](https://github.com/espanso/espanso/blob/dev/espanso-ui/src/win32/native.cpp), a cross-platform text expander
|
- [**espanso**](https://github.com/espanso/espanso/blob/dev/espanso-ui/src/win32/native.cpp), a cross-platform text expander
|
||||||
- [**Karabiner-Elements**](https://github.com/pqrs-org/Karabiner-Elements/blob/main/src/share/app_icon.hpp), a keyboard customizer for macOS
|
- [**Karabiner-Elements**](https://github.com/pqrs-org/Karabiner-Elements/blob/main/src/share/json_utility.hpp), a keyboard customizer for macOS
|
||||||
- [**MacType**](https://github.com/snowie2000/mactype/blob/directwrite/settings.h), a font rendering engine for Windows
|
- [**MacType**](https://github.com/snowie2000/mactype/blob/directwrite/settings.h), a font rendering engine for Windows
|
||||||
- [**magicplan**](https://help.magicplan.app/acknowledgments), a mobile application for creating floor plans and interior designs using augmented reality
|
- [**magicplan**](https://help.magicplan.app/acknowledgments), a mobile application for creating floor plans and interior designs using augmented reality
|
||||||
- [**Microsoft Office for Mac**](https://officecdnmac.microsoft.com/pr/legal/mac/OfficeforMacAttributions.html), a suite of productivity applications developed by Microsoft for macOS, including tools for word processing, spreadsheets, and presentations
|
- [**Microsoft Office for Mac**](https://officecdnmac.microsoft.com/pr/legal/mac/OfficeforMacAttributions.html), a suite of productivity applications developed by Microsoft for macOS, including tools for word processing, spreadsheets, and presentations
|
||||||
@@ -280,8 +280,8 @@ the result of an internet search. If you know further customers of the library,
|
|||||||
- [**Bitdefender Home Scanner**](https://www.bitdefender.com/site/Main/view/home-scanner-open-source.html), a tool from Bitdefender that scans devices for malware and security threats, providing a safeguard against potential online dangers
|
- [**Bitdefender Home Scanner**](https://www.bitdefender.com/site/Main/view/home-scanner-open-source.html), a tool from Bitdefender that scans devices for malware and security threats, providing a safeguard against potential online dangers
|
||||||
- [**Cisco MLS++**](https://github.com/cisco/mlspp), an implementation of the Messaging Layer Security protocol for end-to-end encrypted group messaging
|
- [**Cisco MLS++**](https://github.com/cisco/mlspp), an implementation of the Messaging Layer Security protocol for end-to-end encrypted group messaging
|
||||||
- [**Citrix Provisioning**](https://docs.citrix.com/en-us/provisioning/2203-ltsr/downloads/pvs-third-party-notices-2203.pdf), a solution that streamlines the delivery of virtual desktops and applications by allowing administrators to manage and provision resources efficiently across multiple environments
|
- [**Citrix Provisioning**](https://docs.citrix.com/en-us/provisioning/2203-ltsr/downloads/pvs-third-party-notices-2203.pdf), a solution that streamlines the delivery of virtual desktops and applications by allowing administrators to manage and provision resources efficiently across multiple environments
|
||||||
- [**Citrix Virtual Apps and Desktops**](https://docs.citrix.com/en-us/citrix-virtual-apps-desktops/2305/downloads/third-party-notices-apps-and-desktops.pdf), a solution from Citrix that delivers virtual apps and desktops
|
- [**Citrix Virtual Apps and Desktops**](https://docs.citrix.com/en-us/citrix-virtual-apps-desktops/), a solution from Citrix that delivers virtual apps and desktops
|
||||||
- [**CyberArk**](https://docs.cyberark.com/Downloads/Legal/Privileged%20Session%20Manager%20for%20SSH%20Third-Party%20Notices.pdf), a security solution that specializes in privileged access management, enabling organizations to control and monitor access to critical systems and data, thereby enhancing overall cybersecurity posture
|
- [**CyberArk**](https://www.cyberark.com), a security solution that specializes in privileged access management, enabling organizations to control and monitor access to critical systems and data, thereby enhancing overall cybersecurity posture
|
||||||
- [**Deutsche Telekom sysrepo-plugins**](https://github.com/telekom/sysrepo-plugins), a collection of YANG datastore plugins used to manage network devices
|
- [**Deutsche Telekom sysrepo-plugins**](https://github.com/telekom/sysrepo-plugins), a collection of YANG datastore plugins used to manage network devices
|
||||||
- [**Egnyte Desktop**](https://helpdesk.egnyte.com/hc/en-us/articles/360007071732-Third-Party-Software-Acknowledgements), a secure cloud storage solution designed for businesses, enabling file sharing, collaboration, and data management across teams while ensuring compliance and data protection
|
- [**Egnyte Desktop**](https://helpdesk.egnyte.com/hc/en-us/articles/360007071732-Third-Party-Software-Acknowledgements), a secure cloud storage solution designed for businesses, enabling file sharing, collaboration, and data management across teams while ensuring compliance and data protection
|
||||||
- [**Elster**](https://www.secunet.com/en/about-us/press/article/elstersecure-bietet-komfortablen-login-ohne-passwort-dank-secunet-protect4use), a digital platform developed by German tax authorities for secure and efficient electronic tax filing and management using secunet protect4use
|
- [**Elster**](https://www.secunet.com/en/about-us/press/article/elstersecure-bietet-komfortablen-login-ohne-passwort-dank-secunet-protect4use), a digital platform developed by German tax authorities for secure and efficient electronic tax filing and management using secunet protect4use
|
||||||
|
|||||||
@@ -140,6 +140,8 @@ Exceptions have ids 1xx.
|
|||||||
|
|
||||||
This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member `byte` indicates the error position.
|
This error indicates a syntax error while deserializing a JSON text. The error message describes that an unexpected token (character) was encountered, and the member `byte` indicates the error position.
|
||||||
|
|
||||||
|
The message ends with the last read token (`last read: '...'`), with control characters written as `<U+XXXX>`. A token of more than 8192 bytes is shown by its first and its last 4096 bytes, separated by `...`; each part is shortened by up to three bytes so that it does not end or begin within a UTF-8 character.
|
||||||
|
|
||||||
!!! failure "Example message"
|
!!! failure "Example message"
|
||||||
|
|
||||||
Input ended prematurely:
|
Input ended prematurely:
|
||||||
@@ -985,7 +987,9 @@ contains other characters (e.g., `1a`), or `-` where it cannot be used.
|
|||||||
|
|
||||||
The JSON Patch operations 'remove' and 'move' cannot be applied to the root element of the JSON value. The exception is
|
The JSON Patch operations 'remove' and 'move' cannot be applied to the root element of the JSON value. The exception is
|
||||||
also thrown when the parent or a reference token of an empty JSON Pointer is requested, for instance by
|
also thrown when the parent or a reference token of an empty JSON Pointer is requested, for instance by
|
||||||
[`back`](../api/json_pointer/back.md) or [`pop_back`](../api/json_pointer/pop_back.md).
|
[`front`](../api/json_pointer/front.md), [`back`](../api/json_pointer/back.md),
|
||||||
|
[`pop_front`](../api/json_pointer/pop_front.md), or [`pop_back`](../api/json_pointer/pop_back.md), and when
|
||||||
|
[`basic_json_document::erase`](../api/basic_json_document/erase.md) is called with an empty JSON Pointer.
|
||||||
|
|
||||||
!!! failure "Example message"
|
!!! failure "Example message"
|
||||||
|
|
||||||
@@ -1261,9 +1265,9 @@ A JSON Patch operation 'test' failed. The unsuccessful operation is also printed
|
|||||||
### json.exception.other_error.502
|
### json.exception.other_error.502
|
||||||
|
|
||||||
[`to_ubjson`](../api/basic_json/to_ubjson.md) and [`to_bjdata`](../api/basic_json/to_bjdata.md) were called with
|
[`to_ubjson`](../api/basic_json/to_ubjson.md) and [`to_bjdata`](../api/basic_json/to_bjdata.md) were called with
|
||||||
`use_type = true` but `use_size = false`, and the value contains a non-empty array or object (or, with BJData Draft 3,
|
`use_type = true` but `use_size = false`, and the value contains a non-empty array, object, or binary value (with
|
||||||
any binary value). UBJSON requires a size marker (`#`) after a type marker (`$`); scalars and empty arrays or objects
|
BJData Draft 3, any binary value, including an empty one). UBJSON requires a size marker (`#`) after a type marker
|
||||||
do not throw.
|
(`$`); scalars and empty arrays or objects do not throw.
|
||||||
|
|
||||||
!!! failure "Example message"
|
!!! failure "Example message"
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ These changes are part of the `develop` branch, but not of a release yet.
|
|||||||
2.3 ms). Two consequences: the source container is left in a valid but unspecified (moved-from) state, and if an
|
2.3 ms). Two consequences: the source container is left in a valid but unspecified (moved-from) state, and if an
|
||||||
exception is thrown during the conversion, the source may be partially moved-from, so the strong exception guarantee
|
exception is thrown during the conversion, the source may be partially moved-from, so the strong exception guarantee
|
||||||
no longer holds for the source of an rvalue conversion (the JSON value itself is unaffected). Lvalues and `const`
|
no longer holds for the source of an rvalue conversion (the JSON value itself is unaffected). Lvalues and `const`
|
||||||
objects are still copied. See [`basic_json`](../../api/basic_json/basic_json.md).
|
objects are still copied, and so are non-owning ranges such as spans (a range whose elements stay mutable through a
|
||||||
|
`const` range): converting an rvalue span leaves the referenced elements unchanged.
|
||||||
|
See [`basic_json`](../../api/basic_json/basic_json.md).
|
||||||
- In C++20 (library support for `char8_t`, `__cpp_lib_char8_t`), a `#!cpp std::u8string` is a JSON **string**:
|
- In C++20 (library support for `char8_t`, `__cpp_lib_char8_t`), a `#!cpp std::u8string` is a JSON **string**:
|
||||||
`json j = std::u8string(u8"abc");` stores `"abc"`, and `#!cpp j.get<std::u8string>()` reads it back. Version 3.12.0
|
`json j = std::u8string(u8"abc");` stores `"abc"`, and `#!cpp j.get<std::u8string>()` reads it back. Version 3.12.0
|
||||||
stored a `#!cpp std::u8string` as an array of code units (`[97,98,99]`); on the 3.13.0 development branch it was
|
stored a `#!cpp std::u8string` as an array of code units (`[97,98,99]`); on the 3.13.0 development branch it was
|
||||||
|
|||||||
@@ -0,0 +1,187 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<!-- Created with Inkscape (http://www.inkscape.org/) -->
|
||||||
|
|
||||||
|
<svg
|
||||||
|
width="535"
|
||||||
|
height="535"
|
||||||
|
viewBox="0 0 501.56251 501.56249"
|
||||||
|
id="svg2"
|
||||||
|
version="1.1"
|
||||||
|
inkscape:version="1.3.2 (091e20ef0f, 2023-11-25)"
|
||||||
|
sodipodi:docname="nix-snowflake-colours.svg"
|
||||||
|
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
|
||||||
|
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
|
||||||
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
xmlns:svg="http://www.w3.org/2000/svg"
|
||||||
|
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
||||||
|
xmlns:cc="http://creativecommons.org/ns#"
|
||||||
|
xmlns:dc="http://purl.org/dc/elements/1.1/">
|
||||||
|
<defs
|
||||||
|
id="defs4">
|
||||||
|
<linearGradient
|
||||||
|
inkscape:collect="always"
|
||||||
|
id="linearGradient5562">
|
||||||
|
<stop
|
||||||
|
style="stop-color:#699ad7;stop-opacity:1"
|
||||||
|
offset="0"
|
||||||
|
id="stop5564" />
|
||||||
|
<stop
|
||||||
|
id="stop5566"
|
||||||
|
offset="0.24345198"
|
||||||
|
style="stop-color:#7eb1dd;stop-opacity:1" />
|
||||||
|
<stop
|
||||||
|
style="stop-color:#7ebae4;stop-opacity:1"
|
||||||
|
offset="1"
|
||||||
|
id="stop5568" />
|
||||||
|
</linearGradient>
|
||||||
|
<linearGradient
|
||||||
|
inkscape:collect="always"
|
||||||
|
id="linearGradient5053">
|
||||||
|
<stop
|
||||||
|
style="stop-color:#415e9a;stop-opacity:1"
|
||||||
|
offset="0"
|
||||||
|
id="stop5055" />
|
||||||
|
<stop
|
||||||
|
id="stop5057"
|
||||||
|
offset="0.23168644"
|
||||||
|
style="stop-color:#4a6baf;stop-opacity:1" />
|
||||||
|
<stop
|
||||||
|
style="stop-color:#5277c3;stop-opacity:1"
|
||||||
|
offset="1"
|
||||||
|
id="stop5059" />
|
||||||
|
</linearGradient>
|
||||||
|
<linearGradient
|
||||||
|
inkscape:collect="always"
|
||||||
|
xlink:href="#linearGradient5562"
|
||||||
|
id="linearGradient4328"
|
||||||
|
gradientUnits="userSpaceOnUse"
|
||||||
|
gradientTransform="translate(70.650339,-1055.1511)"
|
||||||
|
x1="200.59668"
|
||||||
|
y1="351.41116"
|
||||||
|
x2="290.08701"
|
||||||
|
y2="506.18814" />
|
||||||
|
<linearGradient
|
||||||
|
inkscape:collect="always"
|
||||||
|
xlink:href="#linearGradient5053"
|
||||||
|
id="linearGradient4330"
|
||||||
|
gradientUnits="userSpaceOnUse"
|
||||||
|
gradientTransform="translate(864.69589,-1491.3405)"
|
||||||
|
x1="-584.19934"
|
||||||
|
y1="782.33563"
|
||||||
|
x2="-496.29703"
|
||||||
|
y2="937.71399" />
|
||||||
|
</defs>
|
||||||
|
<sodipodi:namedview
|
||||||
|
id="base"
|
||||||
|
pagecolor="#ffffff"
|
||||||
|
bordercolor="#666666"
|
||||||
|
borderopacity="1.0"
|
||||||
|
inkscape:pageopacity="0.0"
|
||||||
|
inkscape:pageshadow="2"
|
||||||
|
inkscape:zoom="0.70904368"
|
||||||
|
inkscape:cx="99.429699"
|
||||||
|
inkscape:cy="195.33352"
|
||||||
|
inkscape:document-units="px"
|
||||||
|
inkscape:current-layer="layer3"
|
||||||
|
showgrid="false"
|
||||||
|
inkscape:window-width="1920"
|
||||||
|
inkscape:window-height="1050"
|
||||||
|
inkscape:window-x="1920"
|
||||||
|
inkscape:window-y="30"
|
||||||
|
inkscape:window-maximized="1"
|
||||||
|
inkscape:snap-global="true"
|
||||||
|
fit-margin-top="0"
|
||||||
|
fit-margin-left="0"
|
||||||
|
fit-margin-right="0"
|
||||||
|
fit-margin-bottom="0"
|
||||||
|
inkscape:showpageshadow="2"
|
||||||
|
inkscape:pagecheckerboard="0"
|
||||||
|
inkscape:deskcolor="#d1d1d1" />
|
||||||
|
<metadata
|
||||||
|
id="metadata7">
|
||||||
|
<rdf:RDF>
|
||||||
|
<cc:Work
|
||||||
|
rdf:about="">
|
||||||
|
<dc:format>image/svg+xml</dc:format>
|
||||||
|
<dc:type
|
||||||
|
rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
|
||||||
|
</cc:Work>
|
||||||
|
</rdf:RDF>
|
||||||
|
</metadata>
|
||||||
|
<g
|
||||||
|
inkscape:groupmode="layer"
|
||||||
|
id="layer3"
|
||||||
|
inkscape:label="gradient-logo"
|
||||||
|
style="display:inline;opacity:1"
|
||||||
|
transform="translate(-156.41121,933.30685)">
|
||||||
|
<g
|
||||||
|
id="g2"
|
||||||
|
transform="matrix(0.99994059,0,0,0.99994059,-0.06321798,33.188377)"
|
||||||
|
style="stroke-width:1.00006">
|
||||||
|
<path
|
||||||
|
sodipodi:nodetypes="cccccccccc"
|
||||||
|
inkscape:connector-curvature="0"
|
||||||
|
id="path3336-6"
|
||||||
|
d="m 309.54892,-710.38827 122.19683,211.67512 -56.15706,0.5268 -32.6236,-56.8692 -32.85645,56.5653 -27.90237,-0.011 -14.29086,-24.6896 46.81047,-80.4901 -33.22946,-57.8257 z"
|
||||||
|
style="opacity:1;fill:url(#linearGradient4328);fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:3.00018;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1" />
|
||||||
|
<use
|
||||||
|
height="100%"
|
||||||
|
width="100%"
|
||||||
|
transform="rotate(60,407.11155,-715.78724)"
|
||||||
|
id="use3439-6"
|
||||||
|
inkscape:transform-center-y="151.59082"
|
||||||
|
inkscape:transform-center-x="124.43045"
|
||||||
|
xlink:href="#path3336-6"
|
||||||
|
y="0"
|
||||||
|
x="0"
|
||||||
|
style="stroke-width:1.00006" />
|
||||||
|
<use
|
||||||
|
height="100%"
|
||||||
|
width="100%"
|
||||||
|
transform="rotate(-60,407.31177,-715.70016)"
|
||||||
|
id="use3445-0"
|
||||||
|
inkscape:transform-center-y="75.573958"
|
||||||
|
inkscape:transform-center-x="-168.20651"
|
||||||
|
xlink:href="#path3336-6"
|
||||||
|
y="0"
|
||||||
|
x="0"
|
||||||
|
style="stroke-width:1.00006" />
|
||||||
|
<use
|
||||||
|
height="100%"
|
||||||
|
width="100%"
|
||||||
|
transform="rotate(180,407.41868,-715.7565)"
|
||||||
|
id="use3449-5"
|
||||||
|
inkscape:transform-center-y="-139.94592"
|
||||||
|
inkscape:transform-center-x="59.669705"
|
||||||
|
xlink:href="#path3336-6"
|
||||||
|
y="0"
|
||||||
|
x="0"
|
||||||
|
style="stroke-width:1.00006" />
|
||||||
|
<path
|
||||||
|
style="color:#000000;clip-rule:nonzero;display:inline;overflow:visible;visibility:visible;opacity:1;isolation:auto;mix-blend-mode:normal;color-interpolation:sRGB;color-interpolation-filters:linearRGB;solid-color:#000000;solid-opacity:1;fill:url(#linearGradient4330);fill-opacity:1;fill-rule:evenodd;stroke:none;stroke-width:3.00018;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1;color-rendering:auto;image-rendering:auto;shape-rendering:auto;text-rendering:auto;enable-background:accumulate"
|
||||||
|
d="m 309.54892,-710.38827 122.19683,211.67512 -56.15706,0.5268 -32.6236,-56.8692 -32.85645,56.5653 -27.90237,-0.011 -14.29086,-24.6896 46.81047,-80.4901 -33.22946,-57.8256 z"
|
||||||
|
id="path4260-0"
|
||||||
|
inkscape:connector-curvature="0"
|
||||||
|
sodipodi:nodetypes="cccccccccc" />
|
||||||
|
<use
|
||||||
|
height="100%"
|
||||||
|
width="100%"
|
||||||
|
transform="rotate(120,407.33916,-716.08356)"
|
||||||
|
id="use4354-5"
|
||||||
|
xlink:href="#path4260-0"
|
||||||
|
y="0"
|
||||||
|
x="0"
|
||||||
|
style="display:inline;stroke-width:1.00006" />
|
||||||
|
<use
|
||||||
|
height="100%"
|
||||||
|
width="100%"
|
||||||
|
transform="rotate(-120,407.28823,-715.86995)"
|
||||||
|
id="use4362-2"
|
||||||
|
xlink:href="#path4260-0"
|
||||||
|
y="0"
|
||||||
|
x="0"
|
||||||
|
style="display:inline;stroke-width:1.00006" />
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 6.4 KiB |
@@ -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
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.15)
|
||||||
|
project(json_example)
|
||||||
|
|
||||||
|
find_package(nlohmann_json CONFIG REQUIRED)
|
||||||
|
|
||||||
|
add_executable(json_example example.cpp)
|
||||||
|
target_link_libraries(json_example PRIVATE nlohmann_json::nlohmann_json)
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout << std::setw(4) << json::meta() << std::endl;
|
||||||
|
}
|
||||||
@@ -14,7 +14,8 @@
|
|||||||
{: style="height:1em"} [**Conda**](#conda) `nlohmann_json`  
|
{: style="height:1em"} [**Conda**](#conda) `nlohmann_json`  
|
||||||
{: style="height:1em"} [**MacPorts**](#macports) `nlohmann-json`<br>
|
{: style="height:1em"} [**MacPorts**](#macports) `nlohmann-json`<br>
|
||||||
{: style="height:1em"} [**CPM.cmake**](#cpmcmake) `gh:nlohmann/json`  
|
{: style="height:1em"} [**CPM.cmake**](#cpmcmake) `gh:nlohmann/json`  
|
||||||
{: style="height:1em"} [**xmake**](#xmake) `nlohmann_json`
|
{: style="height:1em"} [**xmake**](#xmake) `nlohmann_json`  
|
||||||
|
{: style="height:1em"} [**Nix**](#nix) `nlohmann_json`
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
## Running example
|
## Running example
|
||||||
@@ -906,6 +907,56 @@ CPMAddPackage("gh:nlohmann/json@3.12.0")
|
|||||||
|
|
||||||
* * *
|
* * *
|
||||||
|
|
||||||
|
## Nix
|
||||||
|
|
||||||
|
!!! abstract "Summary"
|
||||||
|
|
||||||
|
package: [**`nlohmann_json`**](https://search.nixos.org/packages?channel=unstable&show=nlohmann_json&query=nlohmann_json)
|
||||||
|
|
||||||
|
- :octicons-tag-24: Available versions: current version of each Nixpkgs channel (may lag behind the latest release)
|
||||||
|
- :octicons-rocket-24: The package is maintained by the Nixpkgs community and updated after releases.
|
||||||
|
- :octicons-file-24: File issues at the [Nixpkgs issue tracker](https://github.com/NixOS/nixpkgs/issues)
|
||||||
|
- :octicons-question-24: [Nix website](https://nixos.org)
|
||||||
|
|
||||||
|
If you are using [Nix](https://nixos.org) or [NixOS](https://nixos.org/manual/nixos/stable/), the library is available
|
||||||
|
as the [nlohmann_json](https://search.nixos.org/packages?channel=unstable&show=nlohmann_json&query=nlohmann_json)
|
||||||
|
package in [Nixpkgs](https://github.com/NixOS/nixpkgs). To try it in a temporary shell, execute
|
||||||
|
|
||||||
|
```shell
|
||||||
|
nix-shell -p nlohmann_json
|
||||||
|
```
|
||||||
|
|
||||||
|
On NixOS, the package can be installed system-wide by adding it to `configuration.nix`:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
environment.systemPackages = [ pkgs.nlohmann_json ];
|
||||||
|
```
|
||||||
|
|
||||||
|
??? example "Example: CMake"
|
||||||
|
|
||||||
|
1. Create the following files:
|
||||||
|
|
||||||
|
```cpp title="example.cpp"
|
||||||
|
--8<-- "integration/nix/example.cpp"
|
||||||
|
```
|
||||||
|
|
||||||
|
```cmake title="CMakeLists.txt"
|
||||||
|
--8<-- "integration/nix/CMakeLists.txt"
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Enter a shell that provides the package, CMake, and a C++ compiler:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
nix-shell -p nlohmann_json cmake
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Compile the code:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cmake -S . -B build
|
||||||
|
cmake --build build
|
||||||
|
```
|
||||||
|
|
||||||
## Other package managers
|
## Other package managers
|
||||||
|
|
||||||
The library is also contained in many other package repositories; [Repology](https://repology.org/project/nlohmann-json/versions) tracks the packaged
|
The library is also contained in many other package repositories; [Repology](https://repology.org/project/nlohmann-json/versions) tracks the packaged
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ nav:
|
|||||||
- Parsing:
|
- Parsing:
|
||||||
- features/parsing/index.md
|
- features/parsing/index.md
|
||||||
- features/parsing/json_lines.md
|
- features/parsing/json_lines.md
|
||||||
|
- features/parsing/json_variants.md
|
||||||
- features/parsing/large_and_partial_input.md
|
- features/parsing/large_and_partial_input.md
|
||||||
- features/parsing/parse_exceptions.md
|
- features/parsing/parse_exceptions.md
|
||||||
- features/parsing/parser_callbacks.md
|
- features/parsing/parser_callbacks.md
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
wheel==0.48.0
|
wheel==0.48.0
|
||||||
|
|
||||||
mkdocs==1.6.1 # documentation framework
|
mkdocs==1.6.1 # documentation framework
|
||||||
mkdocs-git-revision-date-localized-plugin==1.6.3 # plugin "git-revision-date-localized"
|
mkdocs-git-revision-date-localized-plugin==1.7.0 # plugin "git-revision-date-localized"
|
||||||
mkdocs-material==9.8.0 # theme for mkdocs
|
mkdocs-material==9.8.1 # theme for mkdocs
|
||||||
mkdocs-material-extensions==1.3.1 # extensions
|
mkdocs-material-extensions==1.3.1 # extensions
|
||||||
mkdocs-minify-plugin==0.8.0 # plugin "minify"
|
mkdocs-minify-plugin==0.8.0 # plugin "minify"
|
||||||
mkdocs-redirects==1.2.3 # plugin "redirects"
|
mkdocs-redirects==1.2.3 # plugin "redirects"
|
||||||
|
|||||||
+4
-4
@@ -6,7 +6,7 @@
|
|||||||
"": {
|
"": {
|
||||||
"name": "check-mermaid",
|
"name": "check-mermaid",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"jsdom": "31.0.0",
|
"jsdom": "31.0.1",
|
||||||
"mermaid": "12.3.0"
|
"mermaid": "12.3.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -1109,9 +1109,9 @@
|
|||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/jsdom": {
|
"node_modules/jsdom": {
|
||||||
"version": "31.0.0",
|
"version": "31.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/jsdom/-/jsdom-31.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/jsdom/-/jsdom-31.0.1.tgz",
|
||||||
"integrity": "sha512-xAkhD9z1eyFC5+QUi9K24CaZJgdCQ2GOGLW1GdVDoQvkp/AxEH9TQ0pAplPOlIiV92hfcgnFOADZypbjgbBT6Q==",
|
"integrity": "sha512-CbhJL8ZQg3eKswxSZth4QO9tW0jPIhmR0vtSXQSbKSfWbwIT6hoRc17wdHIVTQ43KI0mdV5iTJr3281IDgVZ5g==",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@exodus/bytes": "^1.16.0",
|
"@exodus/bytes": "^1.16.0",
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
"description": "Validate the Mermaid diagrams of the documentation (see check_mermaid.mjs)",
|
"description": "Validate the Mermaid diagrams of the documentation (see check_mermaid.mjs)",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"jsdom": "31.0.0",
|
"jsdom": "31.0.1",
|
||||||
"mermaid": "12.3.0"
|
"mermaid": "12.3.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -48,10 +48,11 @@ namespace detail
|
|||||||
{
|
{
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief implements the Grisu2 algorithm for binary to decimal floating-point
|
@brief implements binary to decimal floating-point conversion: the shortest
|
||||||
conversion.
|
digits of Zmij (see zmij.hpp) for IEEE 754 binary64 numbers, and the Grisu2
|
||||||
|
algorithm for all other formats.
|
||||||
|
|
||||||
This implementation is a slightly modified version of the reference
|
The Grisu2 implementation is a slightly modified version of the reference
|
||||||
implementation which may be obtained from
|
implementation which may be obtained from
|
||||||
http://florian.loitsch.com/publications (bench.tar.gz).
|
http://florian.loitsch.com/publications (bench.tar.gz).
|
||||||
|
|
||||||
|
|||||||
@@ -48,30 +48,47 @@ namespace detail
|
|||||||
* leave j with the type of the new value, but the pointer to the destroyed old one.
|
* leave j with the type of the new value, but the pointer to the destroyed old one.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// a move iterator over the elements of an rvalue container, unless they are
|
// reference type of the elements of a const T
|
||||||
// const (e.g., std::set): libstdc++ before GCC 5 defines the reference of a
|
template<typename T>
|
||||||
// std::move_iterator as value_type&&, which cannot bind to a const element
|
using const_range_reference_t = decltype(*std::declval<iterator_t<const T>&>());
|
||||||
template<typename Iterator>
|
|
||||||
std::move_iterator<Iterator> make_move_iterator_if_mutable_impl(const Iterator& it, std::true_type /*mutable*/)
|
|
||||||
{
|
|
||||||
return std::make_move_iterator(it);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename Iterator>
|
// a container that owns its elements propagates constness to them (deep
|
||||||
Iterator make_move_iterator_if_mutable_impl(const Iterator& it, std::false_type /*mutable*/)
|
// constness): the elements of a const std::vector<std::string> are const. A
|
||||||
{
|
// non-owning range (a span, an iterator range) has shallow constness: a const
|
||||||
return it;
|
// span still gives mutable access to the elements it refers to. Moving from an
|
||||||
}
|
// rvalue of such a range would empty the storage it refers to, so the elements
|
||||||
|
// of an rvalue range are only moved if it has deep constness.
|
||||||
|
template<typename T>
|
||||||
|
using owns_its_elements = std::is_const<typename std::remove_reference<detected_t<const_range_reference_t, T>>::type>;
|
||||||
|
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
using is_mutable_iterator = std::integral_constant < bool,
|
using is_mutable_iterator = std::integral_constant < bool,
|
||||||
!std::is_const<typename std::remove_reference<decltype(*std::declval<Iterator&>())>::type>::value >;
|
!std::is_const<typename std::remove_reference<decltype(*std::declval<Iterator&>())>::type>::value >;
|
||||||
|
|
||||||
|
// a move iterator over the elements of an rvalue container that owns them,
|
||||||
|
// unless they are const (e.g., std::set): libstdc++ before GCC 5 defines the
|
||||||
|
// reference of a std::move_iterator as value_type&&, which cannot bind to a
|
||||||
|
// const element
|
||||||
template<typename Iterator>
|
template<typename Iterator>
|
||||||
auto make_move_iterator_if_mutable(const Iterator& it)
|
std::move_iterator<Iterator> make_move_iterator_if_impl(const Iterator& it, std::true_type /*move*/)
|
||||||
-> decltype(make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {}))
|
|
||||||
{
|
{
|
||||||
return make_move_iterator_if_mutable_impl(it, is_mutable_iterator<Iterator> {});
|
return std::make_move_iterator(it);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Iterator>
|
||||||
|
Iterator make_move_iterator_if_impl(const Iterator& it, std::false_type /*move*/)
|
||||||
|
{
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Container, typename Iterator>
|
||||||
|
using moves_elements = conjunction<owns_its_elements<Container>, is_mutable_iterator<Iterator>>;
|
||||||
|
|
||||||
|
template<typename Container, typename Iterator>
|
||||||
|
auto make_move_iterator_if_owned(const Iterator& it)
|
||||||
|
-> decltype(make_move_iterator_if_impl(it, moves_elements<Container, Iterator> {}))
|
||||||
|
{
|
||||||
|
return make_move_iterator_if_impl(it, moves_elements<Container, Iterator> {});
|
||||||
}
|
}
|
||||||
|
|
||||||
template<value_t> struct external_constructor;
|
template<value_t> struct external_constructor;
|
||||||
@@ -193,14 +210,14 @@ struct external_constructor<value_t::array>
|
|||||||
j.replace_value(value_t::array, value);
|
j.replace_value(value_t::array, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// an rvalue container: move its elements
|
/// an rvalue container: move its elements if it owns them
|
||||||
template<typename BasicJsonType, typename CompatibleArrayType>
|
template<typename BasicJsonType, typename CompatibleArrayType>
|
||||||
static void construct_moving(BasicJsonType& j, CompatibleArrayType& arr)
|
static void construct_moving(BasicJsonType& j, CompatibleArrayType& arr)
|
||||||
{
|
{
|
||||||
using std::begin;
|
using std::begin;
|
||||||
using std::end;
|
using std::end;
|
||||||
|
|
||||||
const typename BasicJsonType::json_value value(j.template create<typename BasicJsonType::array_t>(make_move_iterator_if_mutable(begin(arr)), make_move_iterator_if_mutable(end(arr))));
|
const typename BasicJsonType::json_value value(j.template create<typename BasicJsonType::array_t>(make_move_iterator_if_owned<CompatibleArrayType>(begin(arr)), make_move_iterator_if_owned<CompatibleArrayType>(end(arr))));
|
||||||
j.replace_value(value_t::array, value);
|
j.replace_value(value_t::array, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -254,14 +271,14 @@ struct external_constructor<value_t::object>
|
|||||||
j.replace_value(value_t::object, value);
|
j.replace_value(value_t::object, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// an rvalue container: move its values
|
/// an rvalue container: move its values if it owns them
|
||||||
template<typename BasicJsonType, typename CompatibleObjectType>
|
template<typename BasicJsonType, typename CompatibleObjectType>
|
||||||
static void construct_moving(BasicJsonType& j, CompatibleObjectType& obj)
|
static void construct_moving(BasicJsonType& j, CompatibleObjectType& obj)
|
||||||
{
|
{
|
||||||
using std::begin;
|
using std::begin;
|
||||||
using std::end;
|
using std::end;
|
||||||
|
|
||||||
const typename BasicJsonType::json_value value(j.template create<typename BasicJsonType::object_t>(make_move_iterator_if_mutable(begin(obj)), make_move_iterator_if_mutable(end(obj))));
|
const typename BasicJsonType::json_value value(j.template create<typename BasicJsonType::object_t>(make_move_iterator_if_owned<CompatibleObjectType>(begin(obj)), make_move_iterator_if_owned<CompatibleObjectType>(end(obj))));
|
||||||
j.replace_value(value_t::object, value);
|
j.replace_value(value_t::object, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -456,7 +473,8 @@ using is_rvalue_array_to_json = conjunction<is_movable_container_rvalue<T>,
|
|||||||
is_compatible_string_type<BasicJsonType, T>,
|
is_compatible_string_type<BasicJsonType, T>,
|
||||||
is_compatible_binary_type<BasicJsonType, T>>>>;
|
is_compatible_binary_type<BasicJsonType, T>>>>;
|
||||||
|
|
||||||
// rvalue containers: their elements are moved instead of copied
|
// rvalue containers: their elements are moved instead of copied if the
|
||||||
|
// container owns them (see owns_its_elements)
|
||||||
template<typename BasicJsonType, typename CompatibleArrayType,
|
template<typename BasicJsonType, typename CompatibleArrayType,
|
||||||
enable_if_t<is_rvalue_array_to_json<BasicJsonType, CompatibleArrayType>::value, int> = 0>
|
enable_if_t<is_rvalue_array_to_json<BasicJsonType, CompatibleArrayType>::value, int> = 0>
|
||||||
inline void to_json(BasicJsonType& j, CompatibleArrayType && arr) // NOLINT(cppcoreguidelines-missing-std-forward)
|
inline void to_json(BasicJsonType& j, CompatibleArrayType && arr) // NOLINT(cppcoreguidelines-missing-std-forward)
|
||||||
|
|||||||
@@ -88,8 +88,9 @@ JSON_INLINE_VARIABLE constexpr std::size_t max_valueless_container_size = 1 << 2
|
|||||||
|
|
||||||
The limit @ref max_valueless_container_size applies to each container on its
|
The limit @ref max_valueless_container_size applies to each container on its
|
||||||
own, so on its own it lets 9 bytes announce 1048576 values, and every further
|
own, so on its own it lets 9 bytes announce 1048576 values, and every further
|
||||||
8 bytes (a row of an optimized array of arrays) another 1048576. The elements of all containers of a valueless type
|
8 bytes (a row of an optimized array of arrays) another 1048576. The elements
|
||||||
in one input are therefore added up, and the sum must stay below this base
|
of all containers of a valueless type in one input are therefore added up, and
|
||||||
|
the sum must stay below this base
|
||||||
plus @ref valueless_elements_per_byte for every byte read so far. The base is
|
plus @ref valueless_elements_per_byte for every byte read so far. The base is
|
||||||
the per-container limit, so a container that was accepted on its own before
|
the per-container limit, so a container that was accepted on its own before
|
||||||
the sum was introduced is still accepted at the start of an input.
|
the sum was introduced is still accepted at the start of an input.
|
||||||
@@ -299,6 +300,17 @@ class binary_reader
|
|||||||
JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
|
JSON_ASSERT(false); // NOLINT(cert-dcl03-c,hicpp-static-assert,misc-static-assert) LCOV_EXCL_LINE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BON8: a string ends at the first byte that cannot continue it. If
|
||||||
|
// that byte follows the value, the reader has already taken it from
|
||||||
|
// the input and holds it in bon8_pushback. Without the end-of-input
|
||||||
|
// check below it would be dropped, and the next read would start
|
||||||
|
// behind it, so report the missing 0xFF instead.
|
||||||
|
if (result && !strict && input_format == input_format_t::bon8 && bon8_pushback_size != 0)
|
||||||
|
{
|
||||||
|
get_bon8();
|
||||||
|
return last_byte_error(exception_id::unexpected_end_of_input, "string at the end of the value is not terminated by 0xFF", "value", "");
|
||||||
|
}
|
||||||
|
|
||||||
// strict mode: next byte must be EOF
|
// strict mode: next byte must be EOF
|
||||||
if (result && strict)
|
if (result && strict)
|
||||||
{
|
{
|
||||||
@@ -1915,6 +1927,21 @@ class binary_reader
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief move past a value of @a size bytes, starting with @a head, that a
|
||||||
|
bulk run reads, as get() and get_number() would
|
||||||
|
|
||||||
|
Called before the value is passed on, so that an error reported while
|
||||||
|
passing it on (see @ref emit_float) has the position and the last byte
|
||||||
|
that reading the value byte by byte gives.
|
||||||
|
*/
|
||||||
|
void bulk_advance(const std::size_t size, const unsigned char head)
|
||||||
|
{
|
||||||
|
ia.bulk_skip(size);
|
||||||
|
chars_read += size;
|
||||||
|
current = head;
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief read the integers, floats, booleans, and nulls that follow in a
|
@brief read the integers, floats, booleans, and nulls that follow in a
|
||||||
definite-length array, without the per-element bookkeeping of
|
definite-length array, without the per-element bookkeeping of
|
||||||
@@ -1940,71 +1967,70 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
const unsigned char head = data[0];
|
const unsigned char head = data[0];
|
||||||
std::size_t size = 0;
|
|
||||||
bool accepted = false;
|
bool accepted = false;
|
||||||
switch (head)
|
switch (head)
|
||||||
{
|
{
|
||||||
case 0x18:
|
case 0x18:
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_unsigned(data[1]);
|
accepted = emit_unsigned(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0x19:
|
case 0x19:
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x1A:
|
case 0x1A:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x1B:
|
case 0x1B:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x38:
|
case 0x38:
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_cbor_negative_integer(data[1]);
|
accepted = emit_cbor_negative_integer(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0x39:
|
case 0x39:
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x3A:
|
case 0x3A:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0x3B:
|
case 0x3B:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_cbor_negative_integer(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xF4:
|
case 0xF4:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(false);
|
accepted = sax->boolean(false);
|
||||||
break;
|
break;
|
||||||
case 0xF5:
|
case 0xF5:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(true);
|
accepted = sax->boolean(true);
|
||||||
break;
|
break;
|
||||||
case 0xF6:
|
case 0xF6:
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->null();
|
accepted = sax->null();
|
||||||
break;
|
break;
|
||||||
case 0xFA:
|
case 0xFA:
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xFB:
|
case 0xFB:
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (head <= 0x17)
|
if (head <= 0x17)
|
||||||
{
|
{
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||||
}
|
}
|
||||||
else if (head >= 0x20 && head <= 0x37)
|
else if (head >= 0x20 && head <= 0x37)
|
||||||
{
|
{
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
accepted = sax->number_integer(static_cast<std::int8_t>(0x20 - 1 - head));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -2014,10 +2040,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// as if read by get() and get_number(): current is the head byte
|
|
||||||
ia.bulk_skip(size);
|
|
||||||
chars_read += size;
|
|
||||||
current = head;
|
|
||||||
--remaining;
|
--remaining;
|
||||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||||
{
|
{
|
||||||
@@ -2855,73 +2877,72 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
const unsigned char head = data[0];
|
const unsigned char head = data[0];
|
||||||
std::size_t size = 0;
|
|
||||||
bool accepted = false;
|
bool accepted = false;
|
||||||
switch (head)
|
switch (head)
|
||||||
{
|
{
|
||||||
case 0xC0: // nil
|
case 0xC0: // nil
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->null();
|
accepted = sax->null();
|
||||||
break;
|
break;
|
||||||
case 0xC2: // false
|
case 0xC2: // false
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(false);
|
accepted = sax->boolean(false);
|
||||||
break;
|
break;
|
||||||
case 0xC3: // true
|
case 0xC3: // true
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->boolean(true);
|
accepted = sax->boolean(true);
|
||||||
break;
|
break;
|
||||||
case 0xCA: // float 32
|
case 0xCA: // float 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_float(load_big_endian<float>(data + 1));
|
accepted = emit_float(load_big_endian<float>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCB: // float 64
|
case 0xCB: // float 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_float(load_big_endian<double>(data + 1));
|
accepted = emit_float(load_big_endian<double>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCC: // uint 8
|
case 0xCC: // uint 8
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_unsigned(data[1]);
|
accepted = emit_unsigned(data[1]);
|
||||||
break;
|
break;
|
||||||
case 0xCD: // uint 16
|
case 0xCD: // uint 16
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCE: // uint 32
|
case 0xCE: // uint 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xCF: // uint 64
|
case 0xCF: // uint 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
accepted = emit_unsigned(load_big_endian<std::uint64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD0: // int 8
|
case 0xD0: // int 8
|
||||||
size = 2;
|
bulk_advance(2, head);
|
||||||
accepted = emit_signed(static_cast<std::int8_t>(data[1]));
|
accepted = emit_signed(static_cast<std::int8_t>(data[1]));
|
||||||
break;
|
break;
|
||||||
case 0xD1: // int 16
|
case 0xD1: // int 16
|
||||||
size = 3;
|
bulk_advance(3, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int16_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int16_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD2: // int 32
|
case 0xD2: // int 32
|
||||||
size = 5;
|
bulk_advance(5, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int32_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int32_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
case 0xD3: // int 64
|
case 0xD3: // int 64
|
||||||
size = 9;
|
bulk_advance(9, head);
|
||||||
accepted = emit_signed(load_big_endian<std::int64_t>(data + 1));
|
accepted = emit_signed(load_big_endian<std::int64_t>(data + 1));
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (head <= 0x7F)
|
if (head <= 0x7F)
|
||||||
{
|
{
|
||||||
// positive fixint
|
// positive fixint
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
accepted = sax->number_unsigned(static_cast<number_unsigned_t>(head));
|
||||||
}
|
}
|
||||||
else if (head >= 0xE0)
|
else if (head >= 0xE0)
|
||||||
{
|
{
|
||||||
// negative fixint
|
// negative fixint
|
||||||
size = 1;
|
bulk_advance(1, head);
|
||||||
accepted = sax->number_integer(static_cast<std::int8_t>(head));
|
accepted = sax->number_integer(static_cast<std::int8_t>(head));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -2931,10 +2952,6 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// as if read by get() and get_number(): current is the last byte
|
|
||||||
ia.bulk_skip(size);
|
|
||||||
chars_read += size;
|
|
||||||
current = data[size - 1];
|
|
||||||
--remaining;
|
--remaining;
|
||||||
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
if (JSON_HEDLEY_UNLIKELY(!accepted))
|
||||||
{
|
{
|
||||||
@@ -3838,6 +3855,7 @@ class binary_reader
|
|||||||
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
const auto* const data = reinterpret_cast<const unsigned char*>(ia.bulk_data());
|
||||||
// UBJSON stores numbers big-endian, BJData little-endian
|
// UBJSON stores numbers big-endian, BJData little-endian
|
||||||
const bool swap = is_little_endian != (input_format == input_format_t::bjdata);
|
const bool swap = is_little_endian != (input_format == input_format_t::bjdata);
|
||||||
|
const std::size_t start = chars_read;
|
||||||
std::size_t read = 0;
|
std::size_t read = 0;
|
||||||
bool accepted = true;
|
bool accepted = true;
|
||||||
while (read < count && accepted)
|
while (read < count && accepted)
|
||||||
@@ -3849,16 +3867,39 @@ class binary_reader
|
|||||||
byte_swap(number);
|
byte_swap(number);
|
||||||
}
|
}
|
||||||
++read;
|
++read;
|
||||||
|
// where passing the value on can report an error (see
|
||||||
|
// emit_float), the position is that after the value, as if it
|
||||||
|
// was read by get_number()
|
||||||
|
set_chars_read(start + (read * size), std::integral_constant<bool, may_overflow_number_float<NumberType>()> {});
|
||||||
accepted = emit_ubjson_number(number);
|
accepted = emit_ubjson_number(number);
|
||||||
}
|
}
|
||||||
// as if read by get_number(): current is the last byte read
|
// get_number() does not change current
|
||||||
ia.bulk_skip(read * size);
|
ia.bulk_skip(read * size);
|
||||||
chars_read += read * size;
|
chars_read = start + (read * size);
|
||||||
current = data[(read * size) - 1];
|
|
||||||
remaining -= read;
|
remaining -= read;
|
||||||
return accepted;
|
return accepted;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief whether a value of type @a NumberType can be finite and still
|
||||||
|
overflow number_float_t, which @ref emit_float reports as an error
|
||||||
|
|
||||||
|
False for the default number types, so that the loop in @ref
|
||||||
|
parse_ubjson_typed_run need not track the position per value.
|
||||||
|
*/
|
||||||
|
template<typename NumberType>
|
||||||
|
static constexpr bool may_overflow_number_float() noexcept
|
||||||
|
{
|
||||||
|
return static_cast<long double>((std::numeric_limits<number_float_t>::max)()) < static_cast<long double>((std::numeric_limits<NumberType>::max)());
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_chars_read(const std::size_t position, std::true_type /*track*/) noexcept
|
||||||
|
{
|
||||||
|
chars_read = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_chars_read(const std::size_t /*position*/, std::false_type /*track*/) const noexcept {}
|
||||||
|
|
||||||
template<typename NumberType, enable_if_t<std::is_floating_point<NumberType>::value, int> = 0>
|
template<typename NumberType, enable_if_t<std::is_floating_point<NumberType>::value, int> = 0>
|
||||||
bool emit_ubjson_number(const NumberType number)
|
bool emit_ubjson_number(const NumberType number)
|
||||||
{
|
{
|
||||||
@@ -4084,6 +4125,10 @@ class binary_reader
|
|||||||
auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
|
auto number_lexer = detail::lexer<BasicJsonType, ia_type>(detail::input_adapter(number_vector), false);
|
||||||
const auto result_number = number_lexer.scan();
|
const auto result_number = number_lexer.scan();
|
||||||
const auto number_string = number_lexer.get_token_string();
|
const auto number_string = number_lexer.get_token_string();
|
||||||
|
// the number as the lexer read it, for SAX number_float(): unlike
|
||||||
|
// get_token_string(), which is meant for error messages and shortens
|
||||||
|
// long tokens, it is complete; the next scan() replaces it
|
||||||
|
const string_t number_text = number_lexer.get_string();
|
||||||
const auto result_remainder = number_lexer.scan();
|
const auto result_remainder = number_lexer.scan();
|
||||||
|
|
||||||
using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
|
using token_type = typename detail::lexer_base<BasicJsonType>::token_type;
|
||||||
@@ -4110,10 +4155,7 @@ class binary_reader
|
|||||||
number_string,
|
number_string,
|
||||||
out_of_range::create(exception_id::number_overflow, concat("number overflow parsing '", number_string, '\''), nullptr));
|
out_of_range::create(exception_id::number_overflow, concat("number overflow parsing '", number_string, '\''), nullptr));
|
||||||
}
|
}
|
||||||
// number_string is a std::string, while the SAX interface takes a
|
return sax->number_float(parsed_float, number_text);
|
||||||
// string_t; convert explicitly, as the two are only implicitly
|
|
||||||
// convertible for some string types
|
|
||||||
return sax->number_float(parsed_float, string_t(number_string.data(), number_string.size()));
|
|
||||||
}
|
}
|
||||||
case token_type::uninitialized:
|
case token_type::uninitialized:
|
||||||
case token_type::literal_true:
|
case token_type::literal_true:
|
||||||
|
|||||||
@@ -110,87 +110,164 @@ inline const char* empty_input_message() noexcept
|
|||||||
return "attempting to parse an empty input; check that your input string or stream contains the expected JSON";
|
return "attempting to parse an empty input; check that your input string or stream contains the expected JSON";
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////
|
|
||||||
// input adapters //
|
|
||||||
////////////////////
|
|
||||||
|
|
||||||
#ifndef JSON_NO_IO
|
|
||||||
/*!
|
/*!
|
||||||
|
@brief the characters of the last read token, kept for error messages
|
||||||
|
|
||||||
A growable array of characters for the part of a token that an input adapter
|
A growable array of characters for the part of a token that an input adapter
|
||||||
copied out of its window: unlike std::vector<char>::push_back(), appending a
|
copied out of its window, or that the lexer copies while it scans input it
|
||||||
character takes no reference to a local, and is a few instructions when there
|
cannot read again: unlike std::vector::push_back(), appending a character
|
||||||
is room.
|
takes no reference to a local, and is a few instructions when there is room.
|
||||||
|
|
||||||
|
The token is only needed for error messages, which show its first head()
|
||||||
|
characters and its last head() characters (see lexer::get_token_string). The
|
||||||
|
buffer therefore keeps the first head() and at least the last tail()
|
||||||
|
characters: when it is full and holds more than limit() characters, it drops
|
||||||
|
the characters in between instead of growing. That happens where it would
|
||||||
|
otherwise reallocate, so appending stays as cheap as before, and a long string
|
||||||
|
or a long run of whitespace read from a stream is not kept a second time.
|
||||||
*/
|
*/
|
||||||
class token_carry_buffer
|
template<typename CharType>
|
||||||
|
class token_text_buffer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
token_carry_buffer() = default;
|
static constexpr std::size_t head() noexcept
|
||||||
token_carry_buffer(const token_carry_buffer&) = delete;
|
{
|
||||||
token_carry_buffer& operator=(const token_carry_buffer&) = delete;
|
return 4096;
|
||||||
token_carry_buffer(token_carry_buffer&& rhs) noexcept
|
}
|
||||||
|
/// a little more than head(), as the last character may be handed back
|
||||||
|
static constexpr std::size_t tail() noexcept
|
||||||
|
{
|
||||||
|
return head() + 16;
|
||||||
|
}
|
||||||
|
static constexpr std::size_t limit() noexcept
|
||||||
|
{
|
||||||
|
return head() + (2 * tail());
|
||||||
|
}
|
||||||
|
|
||||||
|
token_text_buffer() = default;
|
||||||
|
token_text_buffer(const token_text_buffer&) = delete;
|
||||||
|
token_text_buffer& operator=(const token_text_buffer&) = delete;
|
||||||
|
token_text_buffer(token_text_buffer&& rhs) noexcept
|
||||||
: m_data(std::move(rhs.m_data)), m_end(rhs.m_end), m_capacity_end(rhs.m_capacity_end)
|
: m_data(std::move(rhs.m_data)), m_end(rhs.m_end), m_capacity_end(rhs.m_capacity_end)
|
||||||
{
|
{
|
||||||
rhs.m_end = rhs.m_capacity_end = nullptr;
|
rhs.m_end = rhs.m_capacity_end = nullptr;
|
||||||
}
|
}
|
||||||
token_carry_buffer& operator=(token_carry_buffer&&) = delete;
|
token_text_buffer& operator=(token_text_buffer&& rhs) noexcept
|
||||||
~token_carry_buffer() = default;
|
{
|
||||||
|
m_data = std::move(rhs.m_data);
|
||||||
|
m_end = rhs.m_end;
|
||||||
|
m_capacity_end = rhs.m_capacity_end;
|
||||||
|
rhs.m_end = rhs.m_capacity_end = nullptr;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
~token_text_buffer() = default;
|
||||||
|
|
||||||
void clear() noexcept
|
void clear() noexcept
|
||||||
{
|
{
|
||||||
m_end = m_data.get();
|
m_end = m_data.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_back(char c)
|
bool empty() const noexcept
|
||||||
|
{
|
||||||
|
return m_end == m_data.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
void push_back(CharType c)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(m_end == m_capacity_end))
|
if (JSON_HEDLEY_UNLIKELY(m_end == m_capacity_end))
|
||||||
{
|
{
|
||||||
grow(1);
|
make_room(1);
|
||||||
}
|
}
|
||||||
*m_end++ = c;
|
*m_end++ = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
void append(const char* first, const char* last)
|
/// drop the last character
|
||||||
|
void pop_back() noexcept
|
||||||
|
{
|
||||||
|
JSON_ASSERT(!empty());
|
||||||
|
--m_end;
|
||||||
|
}
|
||||||
|
|
||||||
|
void append(const CharType* first, const CharType* last)
|
||||||
{
|
{
|
||||||
const auto n = static_cast<std::size_t>(last - first);
|
const auto n = static_cast<std::size_t>(last - first);
|
||||||
const auto room = static_cast<std::size_t>(m_capacity_end - m_end);
|
const auto room = static_cast<std::size_t>(m_capacity_end - m_end);
|
||||||
if (n > room)
|
if (JSON_HEDLEY_UNLIKELY(n > room))
|
||||||
{
|
{
|
||||||
grow(n);
|
append_long(first, n);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (n != 0)
|
if (n != 0)
|
||||||
{
|
{
|
||||||
std::memcpy(m_end, first, n);
|
std::memcpy(m_end, first, n * sizeof(CharType));
|
||||||
}
|
}
|
||||||
m_end += n;
|
m_end += n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// replace the contents of @a out
|
/// replace the contents of @a out
|
||||||
void copy_to(std::vector<char>& out) const
|
void copy_to(std::vector<CharType>& out) const
|
||||||
{
|
{
|
||||||
out.assign(m_data.get(), m_end);
|
out.assign(m_data.get(), m_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// make room for @a extra more characters
|
/// append @a n characters that do not fit, in pieces of at most tail()
|
||||||
JSON_HEDLEY_NEVER_INLINE void grow(std::size_t extra)
|
/// characters, so that dropping the middle keeps the last tail() of them
|
||||||
|
JSON_HEDLEY_NEVER_INLINE void append_long(const CharType* first, std::size_t n)
|
||||||
{
|
{
|
||||||
const auto size = static_cast<std::size_t>(m_end - m_data.get());
|
while (n != 0)
|
||||||
const auto capacity = static_cast<std::size_t>(m_capacity_end - m_data.get());
|
{
|
||||||
const std::size_t new_capacity = (std::max)(capacity < 32 ? static_cast<std::size_t>(32) : 2 * capacity, size + extra);
|
const std::size_t piece = n < tail() ? n : tail();
|
||||||
std::unique_ptr<char[]> data(new char[new_capacity]); // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
make_room(piece);
|
||||||
|
std::memcpy(m_end, first, piece * sizeof(CharType));
|
||||||
|
m_end += piece;
|
||||||
|
first += piece;
|
||||||
|
n -= piece;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// make room for @a extra (at most tail()) more characters: drop the
|
||||||
|
/// middle of a long token instead of growing beyond limit()
|
||||||
|
JSON_HEDLEY_NEVER_INLINE void make_room(std::size_t extra)
|
||||||
|
{
|
||||||
|
// nothing is allocated before the first character
|
||||||
|
std::size_t size = m_data ? static_cast<std::size_t>(m_end - m_data.get()) : 0;
|
||||||
|
if (size + extra > limit() && size > head() + tail())
|
||||||
|
{
|
||||||
|
std::memmove(m_data.get() + head(), m_end - tail(), tail() * sizeof(CharType));
|
||||||
|
m_end = m_data.get() + head() + tail();
|
||||||
|
size = head() + tail();
|
||||||
|
}
|
||||||
|
const std::size_t capacity = m_data ? static_cast<std::size_t>(m_capacity_end - m_data.get()) : 0;
|
||||||
|
if (size + extra > capacity)
|
||||||
|
{
|
||||||
|
// never more than limit(): from there on, the middle is dropped
|
||||||
|
const std::size_t doubled = capacity < 32 ? static_cast<std::size_t>(32) : 2 * capacity;
|
||||||
|
const std::size_t new_capacity = (std::max)(doubled < limit() ? doubled : limit(), size + extra);
|
||||||
|
std::unique_ptr<CharType[]> data(new CharType[new_capacity]); // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
||||||
if (size != 0)
|
if (size != 0)
|
||||||
{
|
{
|
||||||
std::memcpy(data.get(), m_data.get(), size);
|
std::memcpy(data.get(), m_data.get(), size * sizeof(CharType));
|
||||||
}
|
}
|
||||||
m_data = std::move(data);
|
m_data = std::move(data);
|
||||||
m_end = m_data.get() + size;
|
m_end = m_data.get() + size;
|
||||||
m_capacity_end = m_data.get() + new_capacity;
|
m_capacity_end = m_data.get() + new_capacity;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<char[]> m_data{}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays,readability-redundant-member-init)
|
std::unique_ptr<CharType[]> m_data{}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays,readability-redundant-member-init)
|
||||||
char* m_end = nullptr;
|
CharType* m_end = nullptr;
|
||||||
char* m_capacity_end = nullptr;
|
CharType* m_capacity_end = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// the part of a token that an input adapter copied out of its window
|
||||||
|
using token_carry_buffer = token_text_buffer<char>;
|
||||||
|
|
||||||
|
////////////////////
|
||||||
|
// input adapters //
|
||||||
|
////////////////////
|
||||||
|
|
||||||
|
#ifndef JSON_NO_IO
|
||||||
/*!
|
/*!
|
||||||
Input adapter for stdio file access. The file is locked for the lifetime of the
|
Input adapter for stdio file access. The file is locked for the lifetime of the
|
||||||
adapter and read with the unlocked stdio functions (see
|
adapter and read with the unlocked stdio functions (see
|
||||||
|
|||||||
@@ -215,6 +215,19 @@ template<typename ArrayType>
|
|||||||
inline void reserve_array(ArrayType& /*arr*/, std::size_t /*len*/, priority_tag<0> /*unused*/)
|
inline void reserve_array(ArrayType& /*arr*/, std::size_t /*len*/, priority_tag<0> /*unused*/)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
/// reserve capacity for @a count elements in array @a arr, where @a count is
|
||||||
|
/// at most reserve_cap() already (see json_sax_dom_error_state::reserve_count)
|
||||||
|
template<typename ArrayType>
|
||||||
|
auto reserve_array_capped(ArrayType& arr, std::size_t count, priority_tag<1> /*unused*/)
|
||||||
|
-> decltype(arr.reserve(count), void())
|
||||||
|
{
|
||||||
|
arr.reserve(count);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ArrayType>
|
||||||
|
inline void reserve_array_capped(ArrayType& /*arr*/, std::size_t /*count*/, priority_tag<0> /*unused*/)
|
||||||
|
{}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reports an object or array whose announced size exceeds max_size()
|
@brief reports an object or array whose announced size exceeds max_size()
|
||||||
|
|
||||||
@@ -457,32 +470,35 @@ class json_sax_dom_error_state
|
|||||||
announces more than what is left of that is given what is left, and
|
announces more than what is left of that is given what is left, and
|
||||||
grows as its elements arrive. Without the position of the input (the
|
grows as its elements arrive. Without the position of the input (the
|
||||||
parser is not driven by a binary_reader that is running) nothing is
|
parser is not driven by a binary_reader that is running) nothing is
|
||||||
bounded but the cap of reserve_array().
|
bounded but reserve_cap() for each array.
|
||||||
|
|
||||||
The common case, an array that announces no more than the credit left,
|
The common case, an array whose reservation is no more than the credit
|
||||||
costs a comparison and a subtraction. It is charged what it announces, which
|
left, costs a comparison and a subtraction. An array is charged what is
|
||||||
for an array of more than reserve_cap() elements is more than reserve_array()
|
reserved for it, at most reserve_cap() elements, not what it announces, so
|
||||||
reserves for it, which its own elements refill as they are read.
|
an array of more elements does not take credit from the arrays nested in
|
||||||
|
it.
|
||||||
*/
|
*/
|
||||||
std::size_t reserve_count(const std::size_t len) noexcept
|
std::size_t reserve_count(const std::size_t len) noexcept
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_LIKELY(len <= reserve_credit))
|
const std::size_t count = (std::min)(len, reserve_cap());
|
||||||
|
if (JSON_HEDLEY_LIKELY(count <= reserve_credit))
|
||||||
{
|
{
|
||||||
reserve_credit -= len;
|
reserve_credit -= count;
|
||||||
return len;
|
return count;
|
||||||
}
|
}
|
||||||
return reserve_count_slowly(len);
|
return reserve_count_slowly(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
/// @a count is at most reserve_cap()
|
||||||
JSON_HEDLEY_NEVER_INLINE
|
JSON_HEDLEY_NEVER_INLINE
|
||||||
std::size_t reserve_count_slowly(const std::size_t len) noexcept
|
std::size_t reserve_count_slowly(std::size_t count) noexcept
|
||||||
{
|
{
|
||||||
constexpr std::size_t max_size = (std::numeric_limits<std::size_t>::max)();
|
constexpr std::size_t max_size = (std::numeric_limits<std::size_t>::max)();
|
||||||
if (input_position == nullptr)
|
if (input_position == nullptr)
|
||||||
{
|
{
|
||||||
reserve_credit = max_size;
|
reserve_credit = max_size;
|
||||||
return len;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
// refill the credit with 'reserved_elements_per_byte' for every
|
// refill the credit with 'reserved_elements_per_byte' for every
|
||||||
@@ -500,7 +516,6 @@ class json_sax_dom_error_state
|
|||||||
credit_position = position;
|
credit_position = position;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::size_t count = (std::min)(len, reserve_cap());
|
|
||||||
count = (std::min)(count, reserve_credit);
|
count = (std::min)(count, reserve_credit);
|
||||||
reserve_credit -= count;
|
reserve_credit -= count;
|
||||||
return count;
|
return count;
|
||||||
@@ -1346,7 +1361,7 @@ class json_sax_dom_parser : public json_sax_dom_error_state
|
|||||||
|
|
||||||
if (len != detail::unknown_size())
|
if (len != detail::unknown_size())
|
||||||
{
|
{
|
||||||
reserve_array(*ref->m_data.m_value.array, reserve_count(len), priority_tag<1> {});
|
reserve_array_capped(*ref->m_data.m_value.array, reserve_count(len), priority_tag<1> {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1694,7 +1709,7 @@ class json_sax_dom_binary_parser : public json_sax_dom_error_state
|
|||||||
|
|
||||||
if (len != detail::unknown_size())
|
if (len != detail::unknown_size())
|
||||||
{
|
{
|
||||||
reserve_array(*ref_stack.back()->m_data.m_value.array, reserve_count(len), priority_tag<1> {});
|
reserve_array_capped(*ref_stack.back()->m_data.m_value.array, reserve_count(len), priority_tag<1> {});
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -1986,7 +2001,7 @@ class json_sax_dom_callback_parser : public json_sax_dom_error_state
|
|||||||
|
|
||||||
if (len != detail::unknown_size())
|
if (len != detail::unknown_size())
|
||||||
{
|
{
|
||||||
reserve_array(*ref_stack.back()->m_data.m_value.array, reserve_count(len), priority_tag<1> {});
|
reserve_array_capped(*ref_stack.back()->m_data.m_value.array, reserve_count(len), priority_tag<1> {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -494,7 +494,10 @@ class lexer : public lexer_base<BasicJsonType>
|
|||||||
/// streaming adapter: capture the characters
|
/// streaming adapter: capture the characters
|
||||||
void bulk_track_token(const char* data, std::size_t n, std::false_type /*lazy*/)
|
void bulk_track_token(const char* data, std::size_t n, std::false_type /*lazy*/)
|
||||||
{
|
{
|
||||||
token_string.insert(token_string.end(), data, data + n);
|
for (std::size_t i = 0; i < n; ++i)
|
||||||
|
{
|
||||||
|
token_string.push_back(static_cast<char_type>(data[i]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// streaming input: no bulk fast path
|
/// streaming input: no bulk fast path
|
||||||
@@ -2236,16 +2239,29 @@ scan_number_done:
|
|||||||
const bool pending_real_unget = next_unget && current != char_traits<char_type>::eof();
|
const bool pending_real_unget = next_unget && current != char_traits<char_type>::eof();
|
||||||
const std::size_t stop = ia.get_consumed_count() - (pending_real_unget ? 1u : 0u);
|
const std::size_t stop = ia.get_consumed_count() - (pending_real_unget ? 1u : 0u);
|
||||||
if (JSON_HEDLEY_LIKELY(stop >= token_string_start))
|
if (JSON_HEDLEY_LIKELY(stop >= token_string_start))
|
||||||
|
{
|
||||||
|
// only the beginning and the end of a long token are shown (see
|
||||||
|
// get_token_string), so copy only these
|
||||||
|
constexpr std::size_t head = token_text_buffer<char_type>::head();
|
||||||
|
constexpr std::size_t tail = token_text_buffer<char_type>::tail();
|
||||||
|
if (stop - token_string_start > head + tail)
|
||||||
|
{
|
||||||
|
ia.copy_consumed_range(token_string_start, token_string_start + head, out);
|
||||||
|
ia.copy_consumed_range(stop - tail, stop, out);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
ia.copy_consumed_range(token_string_start, stop, out);
|
ia.copy_consumed_range(token_string_start, stop, out);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// streaming adapter: the token was copied eagerly while scanning
|
/// streaming adapter: the token was copied eagerly while scanning
|
||||||
const std::vector<char_type>& collect_token_chars(std::vector<char_type>& /*out*/, std::false_type /*lazy*/) const
|
const std::vector<char_type>& collect_token_chars(std::vector<char_type>& out, std::false_type /*lazy*/) const
|
||||||
{
|
{
|
||||||
return token_string;
|
token_string.copy_to(out);
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// return the last read token (for errors only). Will never contain EOF
|
/// return the last read token (for errors only). Will never contain EOF
|
||||||
@@ -2256,10 +2272,86 @@ scan_number_done:
|
|||||||
std::vector<char_type> reconstructed;
|
std::vector<char_type> reconstructed;
|
||||||
const std::vector<char_type>& chars = collect_token_chars(reconstructed, std::integral_constant<bool, lazy_token_string> {});
|
const std::vector<char_type>& chars = collect_token_chars(reconstructed, std::integral_constant<bool, lazy_token_string> {});
|
||||||
|
|
||||||
// escape control characters
|
// a long token is shown by its first and its last head() characters;
|
||||||
|
// the buffers that keep it while it is read keep no more than these
|
||||||
|
// and a few more (see token_text_buffer)
|
||||||
|
constexpr std::size_t shown = token_text_buffer<char_type>::head();
|
||||||
std::string result;
|
std::string result;
|
||||||
for (const auto c : chars)
|
if (chars.size() > 2 * shown)
|
||||||
{
|
{
|
||||||
|
// the lexer reads bytes (wide characters arrive as UTF-8), so
|
||||||
|
// move both cuts to character boundaries to keep the message
|
||||||
|
// valid UTF-8; this only shortens the two parts
|
||||||
|
const auto head_end = chars.begin() + static_cast<std::ptrdiff_t>(length_without_partial_utf8_at_end(chars.data(), shown));
|
||||||
|
const auto tail_begin = chars.end() - static_cast<std::ptrdiff_t>(shown);
|
||||||
|
append_escaped(result, chars.begin(), head_end);
|
||||||
|
result += "...";
|
||||||
|
append_escaped(result, tail_begin + static_cast<std::ptrdiff_t>(partial_utf8_length_at_start(&*tail_begin)), chars.end());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
append_escaped(result, chars.begin(), chars.end());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/// whether @a c is a UTF-8 continuation byte
|
||||||
|
static bool is_utf8_continuation(const char_type c) noexcept
|
||||||
|
{
|
||||||
|
return (static_cast<unsigned char>(c) & 0xC0u) == 0x80u;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief the length of the first @a n characters at @a data without a UTF-8
|
||||||
|
character that begins there but does not end there
|
||||||
|
|
||||||
|
Looks only at the last 4 of these characters: in valid UTF-8, at most 3
|
||||||
|
bytes of a character can be before the cut.
|
||||||
|
*/
|
||||||
|
static std::size_t length_without_partial_utf8_at_end(const char_type* data, const std::size_t n) noexcept
|
||||||
|
{
|
||||||
|
std::size_t lead = n - 1;
|
||||||
|
while (lead > n - 4 && is_utf8_continuation(data[lead]))
|
||||||
|
{
|
||||||
|
--lead;
|
||||||
|
}
|
||||||
|
const auto c = static_cast<unsigned char>(data[lead]);
|
||||||
|
std::size_t length = 1;
|
||||||
|
if (c >= 0xF0u)
|
||||||
|
{
|
||||||
|
length = 4;
|
||||||
|
}
|
||||||
|
else if (c >= 0xE0u)
|
||||||
|
{
|
||||||
|
length = 3;
|
||||||
|
}
|
||||||
|
else if (c >= 0xC0u)
|
||||||
|
{
|
||||||
|
length = 2;
|
||||||
|
}
|
||||||
|
return lead + length > n ? lead : n;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// the number of UTF-8 continuation bytes (at most 3) at the start of @a data, which
|
||||||
|
/// belong to a character that begins before it
|
||||||
|
static std::size_t partial_utf8_length_at_start(const char_type* data) noexcept
|
||||||
|
{
|
||||||
|
std::size_t skip = 0;
|
||||||
|
while (skip < 3 && is_utf8_continuation(data[skip]))
|
||||||
|
{
|
||||||
|
++skip;
|
||||||
|
}
|
||||||
|
return skip;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// append the characters [first, last) to @a result, escaping control characters
|
||||||
|
template<typename Iterator>
|
||||||
|
static void append_escaped(std::string& result, Iterator first, Iterator last)
|
||||||
|
{
|
||||||
|
for (; first != last; ++first)
|
||||||
|
{
|
||||||
|
const auto c = *first;
|
||||||
if (static_cast<unsigned char>(c) <= '\x1F')
|
if (static_cast<unsigned char>(c) <= '\x1F')
|
||||||
{
|
{
|
||||||
// escape control characters
|
// escape control characters
|
||||||
@@ -2273,10 +2365,10 @@ scan_number_done:
|
|||||||
result.push_back(static_cast<std::string::value_type>(c));
|
result.push_back(static_cast<std::string::value_type>(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
/// return syntax error message
|
/// return syntax error message
|
||||||
JSON_HEDLEY_RETURNS_NON_NULL
|
JSON_HEDLEY_RETURNS_NON_NULL
|
||||||
constexpr const char* get_error_message() const noexcept
|
constexpr const char* get_error_message() const noexcept
|
||||||
@@ -2525,7 +2617,7 @@ scan_number_done:
|
|||||||
|
|
||||||
/// raw input token string for error messages; only populated for streaming
|
/// raw input token string for error messages; only populated for streaming
|
||||||
/// adapters (seekable adapters reconstruct it lazily via token_string_start)
|
/// adapters (seekable adapters reconstruct it lazily via token_string_start)
|
||||||
std::vector<char_type> token_string {};
|
token_text_buffer<char_type> token_string {};
|
||||||
|
|
||||||
/// start offset of the current token within the input, used to reconstruct
|
/// start offset of the current token within the input, used to reconstruct
|
||||||
/// the last read token on error for seekable adapters (see collect_token_chars)
|
/// the last read token on error for seekable adapters (see collect_token_chars)
|
||||||
|
|||||||
@@ -257,7 +257,7 @@ struct ieee_binary_format<53> // binary64
|
|||||||
|
|
||||||
These formats (float, double, and long double where it is binary64, e.g.
|
These formats (float, double, and long double where it is binary64, e.g.
|
||||||
with MSVC or on Apple arm64) are converted by parse_float_native(). The
|
with MSVC or on Apple arm64) are converted by parse_float_native(). The
|
||||||
predicate is the one the serializer uses to choose Grisu2.
|
predicate is the one the serializer uses to choose to_chars() over snprintf.
|
||||||
*/
|
*/
|
||||||
template<typename FloatType>
|
template<typename FloatType>
|
||||||
struct has_native_float_format
|
struct has_native_float_format
|
||||||
|
|||||||
@@ -108,7 +108,9 @@ class json_reverse_iterator : public std::reverse_iterator<Base>
|
|||||||
/// access to successor
|
/// access to successor
|
||||||
reference operator[](difference_type n) const
|
reference operator[](difference_type n) const
|
||||||
{
|
{
|
||||||
return *(this->operator+(n));
|
// the element n positions after *this is the one n + 1 positions before base();
|
||||||
|
// Base::operator[] then reports invalid_iterator.208 for object iterators
|
||||||
|
return this->base()[-n - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
/// return the key of an object iterator
|
/// return the key of an object iterator
|
||||||
|
|||||||
@@ -852,139 +852,6 @@ class json_pointer
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/*!
|
|
||||||
@brief the pointer prefixes of a flattened object that denote arrays
|
|
||||||
|
|
||||||
@ref unflatten creates arrays exactly below the prefixes that have a
|
|
||||||
reference token 0 among their children, and objects everywhere else.
|
|
||||||
Deciding this up front keeps the result independent of the order in which
|
|
||||||
the flattened object is iterated, which is unspecified for some object
|
|
||||||
types.
|
|
||||||
|
|
||||||
A prefix is identified by the text of the key it is the beginning of:
|
|
||||||
escaping is unambiguous, so equal prefixes of valid keys are equal texts.
|
|
||||||
The set refers to the keys, which must outlive it.
|
|
||||||
*/
|
|
||||||
class array_prefix_set
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
void insert(const char* data, std::size_t size)
|
|
||||||
{
|
|
||||||
// flattened objects are usually sorted, so most keys that have a
|
|
||||||
// 0 yield the prefix of the previous one
|
|
||||||
if (last_data != nullptr && last_size == size && std::memcmp(last_data, data, size) == 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
last_data = data;
|
|
||||||
last_size = size;
|
|
||||||
|
|
||||||
if (table.empty())
|
|
||||||
{
|
|
||||||
table.resize(16);
|
|
||||||
}
|
|
||||||
const std::size_t hash = hash_of(data, size);
|
|
||||||
const std::size_t mask = table.size() - 1;
|
|
||||||
std::size_t i = hash & mask;
|
|
||||||
while (table[i].data != nullptr)
|
|
||||||
{
|
|
||||||
if (table[i].hash == hash && table[i].size == size && std::memcmp(table[i].data, data, size) == 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
i = (i + 1) & mask;
|
|
||||||
}
|
|
||||||
table[i] = slot(hash, data, size);
|
|
||||||
|
|
||||||
// keep the load factor below 1/2
|
|
||||||
if (++used * 2 > table.size())
|
|
||||||
{
|
|
||||||
grow();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool empty() const noexcept
|
|
||||||
{
|
|
||||||
return used == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool contains(const char* data, std::size_t size) const noexcept
|
|
||||||
{
|
|
||||||
if (used == 0)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const std::size_t hash = hash_of(data, size);
|
|
||||||
const std::size_t mask = table.size() - 1;
|
|
||||||
for (std::size_t i = hash & mask; table[i].data != nullptr; i = (i + 1) & mask)
|
|
||||||
{
|
|
||||||
if (table[i].hash == hash && table[i].size == size && std::memcmp(table[i].data, data, size) == 0)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
struct slot
|
|
||||||
{
|
|
||||||
slot() noexcept = default;
|
|
||||||
|
|
||||||
slot(std::size_t h, const char* d, std::size_t s) noexcept
|
|
||||||
: hash(h), data(d), size(s)
|
|
||||||
{}
|
|
||||||
|
|
||||||
std::size_t hash = 0;
|
|
||||||
const char* data = nullptr; // nullptr: the slot is empty
|
|
||||||
std::size_t size = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
static std::size_t hash_of(const char* data, std::size_t size) noexcept
|
|
||||||
{
|
|
||||||
std::uint64_t h = 0x9E3779B97F4A7C15ULL ^ size;
|
|
||||||
std::size_t i = 0;
|
|
||||||
for (; i + 8 <= size; i += 8)
|
|
||||||
{
|
|
||||||
std::uint64_t w = 0;
|
|
||||||
std::memcpy(&w, data + i, 8);
|
|
||||||
h = (h ^ w) * 0x100000001B3ULL;
|
|
||||||
h ^= h >> 32u;
|
|
||||||
}
|
|
||||||
std::uint64_t w = 0;
|
|
||||||
if (i < size)
|
|
||||||
{
|
|
||||||
std::memcpy(&w, data + i, size - i);
|
|
||||||
}
|
|
||||||
h = (h ^ w) * 0x9E3779B97F4A7C15ULL;
|
|
||||||
return detail::conditional_static_cast<std::size_t>(h ^ (h >> 29u));
|
|
||||||
}
|
|
||||||
|
|
||||||
void grow()
|
|
||||||
{
|
|
||||||
std::vector<slot> old(table.size() * 2);
|
|
||||||
old.swap(table);
|
|
||||||
const std::size_t mask = table.size() - 1;
|
|
||||||
for (const auto& s : old)
|
|
||||||
{
|
|
||||||
if (s.data != nullptr)
|
|
||||||
{
|
|
||||||
std::size_t i = s.hash & mask;
|
|
||||||
while (table[i].data != nullptr)
|
|
||||||
{
|
|
||||||
i = (i + 1) & mask;
|
|
||||||
}
|
|
||||||
table[i] = s;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<slot> table{};
|
|
||||||
std::size_t used = 0;
|
|
||||||
const char* last_data = nullptr;
|
|
||||||
std::size_t last_size = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
/// @brief the length of the longest common prefix of the first @a limit bytes of @a a and @a b
|
/// @brief the length of the longest common prefix of the first @a limit bytes of @a a and @a b
|
||||||
static std::size_t common_prefix_length(const char* a, const char* b, const std::size_t limit) noexcept
|
static std::size_t common_prefix_length(const char* a, const char* b, const std::size_t limit) noexcept
|
||||||
{
|
{
|
||||||
@@ -1007,6 +874,355 @@ class json_pointer
|
|||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief the prefixes of a flattened object that are marked as arrays, for
|
||||||
|
prefixes that are not short
|
||||||
|
|
||||||
|
A prefix is identified by the tokens it consists of: a node of a tree
|
||||||
|
stands for the prefix of its parent followed by a token, and is looked up
|
||||||
|
by its parent and the text of the token. The node of a prefix is found by
|
||||||
|
walking the tokens of the key from the root, so identifying all prefixes
|
||||||
|
of a key together costs one pass over the key, however long the prefixes
|
||||||
|
are, and no prefix is ever compared byte by byte as a whole. The nodes
|
||||||
|
along the key that was walked last are kept, and the walk of the next key
|
||||||
|
starts below the beginning it has in common with that key. The tree refers
|
||||||
|
to the keys, which must outlive it.
|
||||||
|
*/
|
||||||
|
class prefix_tree
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// @brief set the seed of the hash function, before anything is added
|
||||||
|
void reseed(const std::uint64_t seed_value) noexcept
|
||||||
|
{
|
||||||
|
seed = seed_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief mark the first @a prefix_size bytes of the key of @a key_size bytes at @a data
|
||||||
|
void mark(const char* data, std::size_t key_size, std::size_t prefix_size)
|
||||||
|
{
|
||||||
|
nodes[walk(data, key_size, prefix_size)].marked = true;
|
||||||
|
any_marked = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_marked(const char* data, std::size_t key_size, std::size_t prefix_size)
|
||||||
|
{
|
||||||
|
return any_marked && nodes[walk(data, key_size, prefix_size)].marked;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty() const noexcept
|
||||||
|
{
|
||||||
|
return !any_marked;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief the hash of the first @a size bytes at @a data, for the seed @a seed_value
|
||||||
|
static std::size_t hash_bytes(const char* data, std::size_t size, const std::uint64_t seed_value) noexcept
|
||||||
|
{
|
||||||
|
std::uint64_t h = seed_value ^ size;
|
||||||
|
std::size_t i = 0;
|
||||||
|
for (; i + 8 <= size; i += 8)
|
||||||
|
{
|
||||||
|
std::uint64_t w = 0;
|
||||||
|
std::memcpy(&w, data + i, 8);
|
||||||
|
h = (h ^ w) * 0x100000001B3ULL;
|
||||||
|
h ^= h >> 32u;
|
||||||
|
}
|
||||||
|
std::uint64_t w = 0;
|
||||||
|
if (i < size)
|
||||||
|
{
|
||||||
|
std::memcpy(&w, data + i, size - i);
|
||||||
|
}
|
||||||
|
h = (h ^ w) * 0x9E3779B97F4A7C15ULL;
|
||||||
|
return detail::conditional_static_cast<std::size_t>(h ^ (h >> 29u));
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct node
|
||||||
|
{
|
||||||
|
std::size_t parent = 0;
|
||||||
|
std::size_t hash = 0;
|
||||||
|
const char* token = nullptr;
|
||||||
|
std::size_t token_size = 0;
|
||||||
|
bool marked = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief a slot of the hash table; the index of a node, 0 if the slot is empty
|
||||||
|
struct bucket
|
||||||
|
{
|
||||||
|
std::size_t index = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief the node after a token of the key that was walked last, and where the token ends
|
||||||
|
struct step
|
||||||
|
{
|
||||||
|
std::size_t node = 0;
|
||||||
|
std::size_t end = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// @brief the index of the node of the prefix that ends at the slash (or the end) at @a prefix_size
|
||||||
|
std::size_t walk(const char* data, std::size_t key_size, std::size_t prefix_size)
|
||||||
|
{
|
||||||
|
if (nodes.empty())
|
||||||
|
{
|
||||||
|
nodes.emplace_back();
|
||||||
|
table.resize(16);
|
||||||
|
chain.emplace_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data != last_key)
|
||||||
|
{
|
||||||
|
// keep the nodes within the beginning that this key has in common
|
||||||
|
// with the key they were found for; a prefix that ends inside a
|
||||||
|
// token of this key is not one of its prefixes
|
||||||
|
const std::size_t same = common_prefix_length(data, last_key, (std::min)(key_size, last_key_size));
|
||||||
|
std::size_t keep = chain.size();
|
||||||
|
while (keep > 1 && (chain[keep - 1].end > same || (chain[keep - 1].end == same && same < key_size && data[same] != '/')))
|
||||||
|
{
|
||||||
|
--keep;
|
||||||
|
}
|
||||||
|
chain.resize(keep);
|
||||||
|
last_key = data;
|
||||||
|
last_key_size = key_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// walk the tokens that are not walked yet
|
||||||
|
while (chain.back().end < prefix_size && chain.back().end < key_size)
|
||||||
|
{
|
||||||
|
const std::size_t start = chain.back().end + 1;
|
||||||
|
const void* const slash = std::memchr(data + start, '/', key_size - start);
|
||||||
|
const std::size_t end = slash == nullptr ? key_size : static_cast<std::size_t>(static_cast<const char*>(slash) - data);
|
||||||
|
step next;
|
||||||
|
next.node = child(chain.back().node, data + start, end - start);
|
||||||
|
next.end = end;
|
||||||
|
chain.push_back(next);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chain.back().end == prefix_size)
|
||||||
|
{
|
||||||
|
return chain.back().node;
|
||||||
|
}
|
||||||
|
// an earlier call walked further
|
||||||
|
const auto it = std::lower_bound(chain.begin(), chain.end(), prefix_size, [](const step & s, std::size_t size)
|
||||||
|
{
|
||||||
|
return s.end < size;
|
||||||
|
});
|
||||||
|
return it->node;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t slot_of(std::size_t parent, std::size_t hash) const noexcept
|
||||||
|
{
|
||||||
|
std::uint64_t x = static_cast<std::uint64_t>(hash) ^ (static_cast<std::uint64_t>(parent) * 0x9E3779B97F4A7C15ULL);
|
||||||
|
x ^= x >> 32u;
|
||||||
|
x *= 0xD6E8FEB86659FD93ULL;
|
||||||
|
x ^= x >> 32u;
|
||||||
|
return static_cast<std::size_t>(x) & (table.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief the node of the prefix of the node @a parent followed by the token, which is added if it is not there
|
||||||
|
std::size_t child(std::size_t parent, const char* token, std::size_t token_size)
|
||||||
|
{
|
||||||
|
const std::size_t hash = hash_bytes(token, token_size, seed);
|
||||||
|
std::size_t i = slot_of(parent, hash);
|
||||||
|
while (table[i].index != 0)
|
||||||
|
{
|
||||||
|
const node& n = nodes[table[i].index];
|
||||||
|
if (n.parent == parent && n.hash == hash && n.token_size == token_size && std::memcmp(n.token, token, token_size) == 0)
|
||||||
|
{
|
||||||
|
return table[i].index;
|
||||||
|
}
|
||||||
|
i = (i + 1) & (table.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
node created;
|
||||||
|
created.parent = parent;
|
||||||
|
created.hash = hash;
|
||||||
|
created.token = token;
|
||||||
|
created.token_size = token_size;
|
||||||
|
nodes.push_back(created);
|
||||||
|
table[i].index = nodes.size() - 1;
|
||||||
|
|
||||||
|
// keep the load factor below 1/2
|
||||||
|
if (nodes.size() * 2 > table.size())
|
||||||
|
{
|
||||||
|
std::vector<bucket> old(table.size() * 2);
|
||||||
|
old.swap(table);
|
||||||
|
for (std::size_t k = 1; k < nodes.size(); ++k)
|
||||||
|
{
|
||||||
|
std::size_t j = slot_of(nodes[k].parent, nodes[k].hash);
|
||||||
|
while (table[j].index != 0)
|
||||||
|
{
|
||||||
|
j = (j + 1) & (table.size() - 1);
|
||||||
|
}
|
||||||
|
table[j].index = k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nodes.size() - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint64_t seed = 0;
|
||||||
|
/// @brief the nodes; the first is the root, the empty prefix
|
||||||
|
std::vector<node> nodes{};
|
||||||
|
/// @brief the nodes (not the root) in a hash table
|
||||||
|
std::vector<bucket> table{};
|
||||||
|
/// @brief the root, and the node after each token of the key that was walked last
|
||||||
|
std::vector<step> chain{};
|
||||||
|
const char* last_key = nullptr;
|
||||||
|
std::size_t last_key_size = 0;
|
||||||
|
bool any_marked = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief the pointer prefixes of a flattened object that denote arrays
|
||||||
|
|
||||||
|
@ref unflatten creates arrays exactly below the prefixes that have a
|
||||||
|
reference token 0 among their children, and objects everywhere else.
|
||||||
|
Deciding this up front keeps the result independent of the order in which
|
||||||
|
the flattened object is iterated, which is unspecified for some object
|
||||||
|
types.
|
||||||
|
|
||||||
|
A prefix is identified by the text of the key it is the beginning of:
|
||||||
|
escaping is unambiguous, so equal prefixes of valid keys are equal texts.
|
||||||
|
The set refers to the keys, which must outlive it. Short prefixes are
|
||||||
|
kept in a hash table and compared as texts, which costs at most the length
|
||||||
|
of the bound. Longer prefixes are kept in a @ref prefix_tree, because
|
||||||
|
hashing and comparing every one of the prefixes of a key with many tokens
|
||||||
|
from its beginning would take quadratic time.
|
||||||
|
|
||||||
|
The hash function depends on a seed that is derived from the address of a
|
||||||
|
local variable, which address space layout randomization changes between
|
||||||
|
processes, so keys cannot be crafted to collide in advance; the result
|
||||||
|
does not depend on the seed. Where the addresses are not randomized, or
|
||||||
|
when the seed is known, keys can collide: k short prefixes then cost
|
||||||
|
k^2 / 2 comparisons of fewer than 64 bytes each. The long prefixes are not
|
||||||
|
hashed as a whole.
|
||||||
|
*/
|
||||||
|
class array_prefix_set
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/// @brief choose the seed of the hash function, before anything is added: it is derived from the address that is passed
|
||||||
|
void reseed(const void* address) noexcept
|
||||||
|
{
|
||||||
|
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,hicpp-use-auto,modernize-use-auto): the address is only a source of entropy
|
||||||
|
std::uint64_t x = reinterpret_cast<std::uintptr_t>(address);
|
||||||
|
x ^= 0x9E3779B97F4A7C15ULL;
|
||||||
|
x *= 0xBF58476D1CE4E5B9ULL;
|
||||||
|
seed = x ^ (x >> 31u);
|
||||||
|
tree.reseed(seed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief add the first @a prefix_size bytes of the key of @a key_size bytes at @a data
|
||||||
|
void insert(const char* data, std::size_t key_size, std::size_t prefix_size)
|
||||||
|
{
|
||||||
|
if (prefix_size >= short_limit)
|
||||||
|
{
|
||||||
|
tree.mark(data, key_size, prefix_size);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// flattened objects are usually sorted, so most keys that have a
|
||||||
|
// 0 yield the prefix of the previous one
|
||||||
|
if (last_data != nullptr && last_size == prefix_size && std::memcmp(last_data, data, prefix_size) == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
last_data = data;
|
||||||
|
last_size = prefix_size;
|
||||||
|
|
||||||
|
const std::size_t hash = prefix_tree::hash_bytes(data, prefix_size, seed);
|
||||||
|
if (table.empty())
|
||||||
|
{
|
||||||
|
table.resize(16);
|
||||||
|
}
|
||||||
|
const std::size_t mask = table.size() - 1;
|
||||||
|
std::size_t i = hash & mask;
|
||||||
|
while (table[i].data != nullptr)
|
||||||
|
{
|
||||||
|
if (table[i].hash == hash && table[i].size == prefix_size && std::memcmp(table[i].data, data, prefix_size) == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
i = (i + 1) & mask;
|
||||||
|
}
|
||||||
|
table[i] = slot(hash, data, prefix_size);
|
||||||
|
|
||||||
|
// keep the load factor below 1/2
|
||||||
|
if (++used * 2 > table.size())
|
||||||
|
{
|
||||||
|
grow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool empty() const noexcept
|
||||||
|
{
|
||||||
|
return used == 0 && tree.empty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @brief whether the first @a prefix_size bytes of the key of @a key_size bytes at @a data were added
|
||||||
|
bool contains(const char* data, std::size_t key_size, std::size_t prefix_size)
|
||||||
|
{
|
||||||
|
if (prefix_size >= short_limit)
|
||||||
|
{
|
||||||
|
return tree.is_marked(data, key_size, prefix_size);
|
||||||
|
}
|
||||||
|
if (used == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const std::size_t hash = prefix_tree::hash_bytes(data, prefix_size, seed);
|
||||||
|
const std::size_t mask = table.size() - 1;
|
||||||
|
for (std::size_t i = hash & mask; table[i].data != nullptr; i = (i + 1) & mask)
|
||||||
|
{
|
||||||
|
if (table[i].hash == hash && table[i].size == prefix_size && std::memcmp(table[i].data, data, prefix_size) == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
/// @brief prefixes of fewer bytes are kept in the hash table
|
||||||
|
static constexpr std::size_t short_limit = 64;
|
||||||
|
|
||||||
|
struct slot
|
||||||
|
{
|
||||||
|
slot() noexcept = default;
|
||||||
|
|
||||||
|
slot(std::size_t h, const char* d, std::size_t s) noexcept
|
||||||
|
: hash(h), data(d), size(s)
|
||||||
|
{}
|
||||||
|
|
||||||
|
std::size_t hash = 0;
|
||||||
|
const char* data = nullptr; // nullptr: the slot is empty
|
||||||
|
std::size_t size = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
void grow()
|
||||||
|
{
|
||||||
|
std::vector<slot> old(table.size() * 2);
|
||||||
|
old.swap(table);
|
||||||
|
const std::size_t mask = table.size() - 1;
|
||||||
|
for (const auto& s : old)
|
||||||
|
{
|
||||||
|
if (s.data != nullptr)
|
||||||
|
{
|
||||||
|
std::size_t i = s.hash & mask;
|
||||||
|
while (table[i].data != nullptr)
|
||||||
|
{
|
||||||
|
i = (i + 1) & mask;
|
||||||
|
}
|
||||||
|
table[i] = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint64_t seed = 0;
|
||||||
|
prefix_tree tree{};
|
||||||
|
std::vector<slot> table{};
|
||||||
|
std::size_t used = 0;
|
||||||
|
const char* last_data = nullptr;
|
||||||
|
std::size_t last_size = 0;
|
||||||
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief check that @a key is a valid JSON pointer, and add the prefixes of
|
@brief check that @a key is a valid JSON pointer, and add the prefixes of
|
||||||
it that denote arrays to @a arrays
|
it that denote arrays to @a arrays
|
||||||
@@ -1052,7 +1268,7 @@ class json_pointer
|
|||||||
// a token 0 makes the pointer before it an array
|
// a token 0 makes the pointer before it an array
|
||||||
if (i - begin == 1 && data[begin] == '0')
|
if (i - begin == 1 && data[begin] == '0')
|
||||||
{
|
{
|
||||||
arrays.insert(data, begin - 1);
|
arrays.insert(data, size, begin - 1);
|
||||||
}
|
}
|
||||||
begin = i + 1;
|
begin = i + 1;
|
||||||
}
|
}
|
||||||
@@ -2095,6 +2311,7 @@ class json_pointer
|
|||||||
// token 0 among their children; the values below them are arrays,
|
// token 0 among their children; the values below them are arrays,
|
||||||
// all others are objects (see array_prefix_set)
|
// all others are objects (see array_prefix_set)
|
||||||
array_prefix_set arrays;
|
array_prefix_set arrays;
|
||||||
|
arrays.reseed(&arrays);
|
||||||
const string_t* previous_key = nullptr;
|
const string_t* previous_key = nullptr;
|
||||||
for (const auto& element : *value.m_data.m_value.object)
|
for (const auto& element : *value.m_data.m_value.object)
|
||||||
{
|
{
|
||||||
@@ -2186,7 +2403,7 @@ class json_pointer
|
|||||||
case detail::value_t::null:
|
case detail::value_t::null:
|
||||||
{
|
{
|
||||||
// a first child 0 makes the value an array without a look-up
|
// a first child 0 makes the value an array without a look-up
|
||||||
if ((token_size == 1 && data[pos + 1] == '0') || arrays.contains(data, pos))
|
if ((token_size == 1 && data[pos + 1] == '0') || arrays.contains(data, key.size(), pos))
|
||||||
{
|
{
|
||||||
// some reference token below this position is 0, so the
|
// some reference token below this position is 0, so the
|
||||||
// value is an array
|
// value is an array
|
||||||
@@ -2238,6 +2455,19 @@ class json_pointer
|
|||||||
pos = end;
|
pos = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!current->is_null()))
|
||||||
|
{
|
||||||
|
// the key is a prefix of an earlier key (only possible if the
|
||||||
|
// object type does not sort its keys), so the value is the
|
||||||
|
// array or object built for that key: a null leaves it as it
|
||||||
|
// is (as when the shorter key comes first), any other value
|
||||||
|
// conflicts with it
|
||||||
|
if (element.second.is_null())
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
JSON_THROW(detail::type_error::create(detail::exception_id::unflatten_invalid_value, "invalid value to unflatten", &result));
|
||||||
|
}
|
||||||
*current = element.second;
|
*current = element.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -230,6 +230,16 @@
|
|||||||
// detail/recursion_depth_limit.hpp. Like JSON_NO_THREAD_LOCAL it is neither part
|
// detail/recursion_depth_limit.hpp. Like JSON_NO_THREAD_LOCAL it is neither part
|
||||||
// of the ABI tag nor undefined at the end of json.hpp.
|
// of the ABI tag nor undefined at the end of json.hpp.
|
||||||
|
|
||||||
|
// forced inlining of the serializer's helpers only in optimized builds: at -O0,
|
||||||
|
// GCC and Clang give every inlined copy its own stack slots, which made the
|
||||||
|
// recursive frame of serializer::dump_container() about 2.3 KB (MSVC does not
|
||||||
|
// expand __forceinline at /Ob0, so it keeps it)
|
||||||
|
#if defined(__OPTIMIZE__) || defined(_MSC_VER)
|
||||||
|
#define JSON_INTERNAL_SERIALIZER_INLINE JSON_HEDLEY_ALWAYS_INLINE
|
||||||
|
#else
|
||||||
|
#define JSON_INTERNAL_SERIALIZER_INLINE inline
|
||||||
|
#endif
|
||||||
|
|
||||||
// allow disabling exceptions
|
// allow disabling exceptions
|
||||||
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
|
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(JSON_NOEXCEPTION)
|
||||||
#define JSON_THROW(exception) throw exception
|
#define JSON_THROW(exception) throw exception
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
// clean up
|
// clean up
|
||||||
#undef JSON_ASSERT
|
#undef JSON_ASSERT
|
||||||
#undef JSON_INTERNAL_CATCH
|
#undef JSON_INTERNAL_CATCH
|
||||||
|
#undef JSON_INTERNAL_SERIALIZER_INLINE
|
||||||
#undef JSON_THROW
|
#undef JSON_THROW
|
||||||
#undef JSON_PRIVATE_UNLESS_TESTED
|
#undef JSON_PRIVATE_UNLESS_TESTED
|
||||||
#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
|
#undef NLOHMANN_BASIC_JSON_TPL_DECLARATION
|
||||||
|
|||||||
@@ -915,7 +915,7 @@ class binary_writer
|
|||||||
JSON_THROW(out_of_range::create(exception_id::subtype_out_of_range, concat("subtype ", std::to_string(subtype), " is too large for ", what, " (max 255)"), &j));
|
JSON_THROW(out_of_range::create(exception_id::subtype_out_of_range, concat("subtype ", std::to_string(subtype), " is too large for ", what, " (max 255)"), &j));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @throw other_error.407 always
|
/// @throw other_error.502 always
|
||||||
JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE
|
JSON_HEDLEY_NO_RETURN JSON_HEDLEY_NEVER_INLINE
|
||||||
static void throw_size_marker_required(const BasicJsonType& j)
|
static void throw_size_marker_required(const BasicJsonType& j)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -265,7 +265,7 @@ class serializer
|
|||||||
for a container; writing them from here spares each the entry and exit of
|
for a container; writing them from here spares each the entry and exit of
|
||||||
that function.
|
that function.
|
||||||
*/
|
*/
|
||||||
JSON_HEDLEY_ALWAYS_INLINE void dump_element(const BasicJsonType& val,
|
JSON_INTERNAL_SERIALIZER_INLINE void dump_element(const BasicJsonType& val,
|
||||||
const std::size_t current_indent,
|
const std::size_t current_indent,
|
||||||
const std::size_t depth)
|
const std::size_t depth)
|
||||||
{
|
{
|
||||||
@@ -636,7 +636,7 @@ class serializer
|
|||||||
put_literal("\"subtype\": ");
|
put_literal("\"subtype\": ");
|
||||||
if (binary.has_subtype())
|
if (binary.has_subtype())
|
||||||
{
|
{
|
||||||
dump_integer(binary.subtype());
|
dump_subtype(binary.subtype());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -662,7 +662,7 @@ class serializer
|
|||||||
put_literal("],\"subtype\":");
|
put_literal("],\"subtype\":");
|
||||||
if (binary.has_subtype())
|
if (binary.has_subtype())
|
||||||
{
|
{
|
||||||
dump_integer(binary.subtype());
|
dump_subtype(binary.subtype());
|
||||||
put_char('}');
|
put_char('}');
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -971,7 +971,7 @@ class serializer
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// write out the bytes pending in @ref string_buffer and reset @a bytes
|
/// write out the bytes pending in @ref string_buffer and reset @a bytes
|
||||||
JSON_HEDLEY_ALWAYS_INLINE void emit_pending(std::size_t& bytes)
|
JSON_INTERNAL_SERIALIZER_INLINE void emit_pending(std::size_t& bytes)
|
||||||
{
|
{
|
||||||
if (bytes != 0)
|
if (bytes != 0)
|
||||||
{
|
{
|
||||||
@@ -983,7 +983,7 @@ class serializer
|
|||||||
/// write buffer and reset index if fewer than 13 bytes are left, as this
|
/// write buffer and reset index if fewer than 13 bytes are left, as this
|
||||||
/// is the maximal number of bytes to be written ("\uxxxx\uxxxx\0") for one
|
/// is the maximal number of bytes to be written ("\uxxxx\uxxxx\0") for one
|
||||||
/// code point
|
/// code point
|
||||||
JSON_HEDLEY_ALWAYS_INLINE void reserve_string_buffer(std::size_t& bytes)
|
JSON_INTERNAL_SERIALIZER_INLINE void reserve_string_buffer(std::size_t& bytes)
|
||||||
{
|
{
|
||||||
if (string_buffer.size() - bytes < 13)
|
if (string_buffer.size() - bytes < 13)
|
||||||
{
|
{
|
||||||
@@ -1344,7 +1344,7 @@ class serializer
|
|||||||
inlining both into the pretty-printing loops once @ref dump_escaped
|
inlining both into the pretty-printing loops once @ref dump_escaped
|
||||||
has grown, which made indented output up to 13% slower
|
has grown, which made indented output up to 13% slower
|
||||||
*/
|
*/
|
||||||
JSON_HEDLEY_ALWAYS_INLINE void put_indent(std::size_t indent)
|
JSON_INTERNAL_SERIALIZER_INLINE void put_indent(std::size_t indent)
|
||||||
{
|
{
|
||||||
// closing braces at the outermost level ask for no indentation at all
|
// closing braces at the outermost level ask for no indentation at all
|
||||||
if (indent == 0)
|
if (indent == 0)
|
||||||
@@ -1398,7 +1398,7 @@ class serializer
|
|||||||
its room and four stores of the indentation, see @ref put_short_literal_indent.
|
its room and four stores of the indentation, see @ref put_short_literal_indent.
|
||||||
*/
|
*/
|
||||||
template<std::size_t N>
|
template<std::size_t N>
|
||||||
JSON_HEDLEY_ALWAYS_INLINE void put_literal_indent(const char (&s)[N], const std::size_t indent) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
JSON_INTERNAL_SERIALIZER_INLINE void put_literal_indent(const char (&s)[N], const std::size_t indent) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_LIKELY(put_short_literal_indent(s, indent)))
|
if (JSON_HEDLEY_LIKELY(put_short_literal_indent(s, indent)))
|
||||||
{
|
{
|
||||||
@@ -1418,7 +1418,7 @@ class serializer
|
|||||||
@a indent is wider or the write buffer lacks the room.
|
@a indent is wider or the write buffer lacks the room.
|
||||||
*/
|
*/
|
||||||
template<std::size_t N>
|
template<std::size_t N>
|
||||||
JSON_HEDLEY_ALWAYS_INLINE bool put_short_literal_indent(const char (&s)[N], const std::size_t indent) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
JSON_INTERNAL_SERIALIZER_INLINE bool put_short_literal_indent(const char (&s)[N], const std::size_t indent) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
||||||
{
|
{
|
||||||
constexpr std::size_t length = N - 1;
|
constexpr std::size_t length = N - 1;
|
||||||
if (indent > 32 || write_buffer_pos + length + 32 > write_buffer.size())
|
if (indent > 32 || write_buffer_pos + length + 32 > write_buffer.size())
|
||||||
@@ -1445,7 +1445,7 @@ class serializer
|
|||||||
write-through path for oversized runs.
|
write-through path for oversized runs.
|
||||||
*/
|
*/
|
||||||
template<std::size_t N>
|
template<std::size_t N>
|
||||||
JSON_HEDLEY_ALWAYS_INLINE void put_literal(const char (&s)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
JSON_INTERNAL_SERIALIZER_INLINE void put_literal(const char (&s)[N]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
||||||
{
|
{
|
||||||
static_assert(N >= 2, "put_literal expects a non-empty string literal");
|
static_assert(N >= 2, "put_literal expects a non-empty string literal");
|
||||||
// the array bound counts the terminating NUL, which is not written
|
// the array bound counts the terminating NUL, which is not written
|
||||||
@@ -1571,7 +1571,7 @@ class serializer
|
|||||||
{
|
{
|
||||||
return n_digits + 3;
|
return n_digits + 3;
|
||||||
}
|
}
|
||||||
x = x / 10000u;
|
x = static_cast<number_magnitude_t>(x / 10000u);
|
||||||
n_digits += 4;
|
n_digits += 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1883,6 +1883,40 @@ class serializer
|
|||||||
write_buffer_pos = pos;
|
write_buffer_pos = pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief dump the subtype of a binary value
|
||||||
|
|
||||||
|
The subtype is a std::uint64_t, whatever the number types are. @ref
|
||||||
|
dump_integer converts its argument to the magnitude type of
|
||||||
|
number_unsigned_t, so it writes the subtype only if that type has at least
|
||||||
|
64 bits; otherwise (it would truncate the subtype) the digits are written
|
||||||
|
one by one.
|
||||||
|
|
||||||
|
@param[in] subtype the subtype to dump
|
||||||
|
*/
|
||||||
|
void dump_subtype(std::uint64_t subtype)
|
||||||
|
{
|
||||||
|
dump_subtype(subtype, std::integral_constant<bool, magnitude_has_at_least_64_bits> {});
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_subtype(std::uint64_t subtype, std::true_type /*magnitude has at least 64 bits*/)
|
||||||
|
{
|
||||||
|
dump_integer(subtype);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_subtype(std::uint64_t subtype, std::false_type /*magnitude has less than 64 bits*/)
|
||||||
|
{
|
||||||
|
std::array<char, 20> digits{{}}; // 2^64 - 1 has 20 digits
|
||||||
|
std::size_t start = digits.size();
|
||||||
|
do
|
||||||
|
{
|
||||||
|
digits[--start] = static_cast<char>('0' + (subtype % 10));
|
||||||
|
subtype /= 10;
|
||||||
|
}
|
||||||
|
while (subtype != 0);
|
||||||
|
put_string(digits, start, digits.size());
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief dump an integer
|
@brief dump an integer
|
||||||
|
|
||||||
@@ -2033,8 +2067,9 @@ class serializer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If number_float_t is an IEEE-754 single or double precision number,
|
// If number_float_t is an IEEE-754 single or double precision number,
|
||||||
// use the Grisu2 algorithm to produce short numbers which are
|
// use to_chars() to produce short numbers which are guaranteed to
|
||||||
// guaranteed to round-trip, using strtof and strtod, resp.
|
// round-trip, using strtof and strtod, resp. (the shortest digits of
|
||||||
|
// Zmij for binary64, the Grisu2 algorithm for binary32).
|
||||||
//
|
//
|
||||||
// NB: The test below works if <long double> == <double>.
|
// NB: The test below works if <long double> == <double>.
|
||||||
static constexpr bool is_ieee_single_or_double
|
static constexpr bool is_ieee_single_or_double
|
||||||
@@ -2136,6 +2171,11 @@ class serializer
|
|||||||
static constexpr bool magnitude_has_at_most_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
|
static constexpr bool magnitude_has_at_most_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
|
||||||
&& std::numeric_limits<number_magnitude_t>::digits <= 64;
|
&& std::numeric_limits<number_magnitude_t>::digits <= 64;
|
||||||
|
|
||||||
|
/// whether every std::uint64_t fits number_magnitude_t (then dump_integer
|
||||||
|
/// can write a binary subtype)
|
||||||
|
static constexpr bool magnitude_has_at_least_64_bits = std::numeric_limits<number_magnitude_t>::is_specialized
|
||||||
|
&& std::numeric_limits<number_magnitude_t>::digits >= 64;
|
||||||
|
|
||||||
/// whether @a x fits 64 bits: always if number_magnitude_t has at most
|
/// whether @a x fits 64 bits: always if number_magnitude_t has at most
|
||||||
/// 64 bits (selected by type, as the comparison would be reported as
|
/// 64 bits (selected by type, as the comparison would be reported as
|
||||||
/// always true), else compared
|
/// always true), else compared
|
||||||
@@ -2161,7 +2201,7 @@ class serializer
|
|||||||
number_magnitude_t remove_sign(number_integer_t x) noexcept
|
number_magnitude_t remove_sign(number_integer_t x) noexcept
|
||||||
{
|
{
|
||||||
JSON_ASSERT(x < 0);
|
JSON_ASSERT(x < 0);
|
||||||
return static_cast<number_magnitude_t>(-(x + 1)) + 1;
|
return static_cast<number_magnitude_t>(static_cast<number_magnitude_t>(-(x + 1)) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -384,11 +384,24 @@ class editor
|
|||||||
// for the container is linear in the size of the document, so
|
// for the container is linear in the size of the document, so
|
||||||
// links (which are marked in the slot) avoid it.
|
// links (which are marked in the slot) avoid it.
|
||||||
node* const p = parent_known ? parent : find_parent(m_doc, slot);
|
node* const p = parent_known ? parent : find_parent(m_doc, slot);
|
||||||
if (p != nullptr && ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0))
|
if (p != nullptr)
|
||||||
|
{
|
||||||
|
if ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0)
|
||||||
{
|
{
|
||||||
block_of(m_doc, p, 0);
|
block_of(m_doc, p, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// the slot is no longer part of the document, but the
|
||||||
|
// (dropped) container it is in may still step over it
|
||||||
|
node* const q = find_layout_parent(m_doc, slot);
|
||||||
|
if (q != nullptr)
|
||||||
|
{
|
||||||
|
block_of(m_doc, q, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
*slot = e.scalar;
|
*slot = e.scalar;
|
||||||
slot->flags = static_cast<std::uint8_t>(slot->flags | linked);
|
slot->flags = static_cast<std::uint8_t>(slot->flags | linked);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -194,7 +194,9 @@ inline node* block_of(document_data& d, node* n, std::size_t extra)
|
|||||||
h->len = n->len;
|
h->len = n->len;
|
||||||
h->next = static_cast<std::uint32_t>(used);
|
h->next = static_cast<std::uint32_t>(used);
|
||||||
node* o = h + 1;
|
node* o = h + 1;
|
||||||
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n); c != e;)
|
// (the bound on o keeps the copy inside the block even if the elements
|
||||||
|
// disagreed with the element count)
|
||||||
|
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n), *const full = h + used; c != e && o != full;)
|
||||||
{
|
{
|
||||||
if (object)
|
if (object)
|
||||||
{
|
{
|
||||||
@@ -276,6 +278,41 @@ inline node* find_parent(const document_data& d, const node* target)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The container whose contiguous layout (in the parsed index, or in a new
|
||||||
|
/// value) steps over `target`, also when an edit dropped the container or a
|
||||||
|
/// value that encloses it; nullptr if there is none, or if the container no
|
||||||
|
/// longer walks that layout (its elements moved). An edit never gives a node a
|
||||||
|
/// larger extent than it was parsed or encoded with (a scalar is one node, an
|
||||||
|
/// assigned array/object keeps the extent of the slot), so the containers whose
|
||||||
|
/// extent covers `target` are its original ancestors: the nearest one before it
|
||||||
|
/// is its original parent. The root of a new value that is the element sequence
|
||||||
|
/// of another container is walked by that container (which find_parent() finds
|
||||||
|
/// while it still uses the sequence), not by itself.
|
||||||
|
inline node* find_layout_parent(const document_data& d, const node* target)
|
||||||
|
{
|
||||||
|
const std::less<const node*> lt{};
|
||||||
|
const node* lo = d.nodes;
|
||||||
|
if (lt(target, lo) || !lt(target, d.nodes + d.node_count))
|
||||||
|
{
|
||||||
|
auto it = d.edits->regions.upper_bound(target);
|
||||||
|
if (it == d.edits->regions.begin())
|
||||||
|
{
|
||||||
|
return nullptr; // LCOV_EXCL_LINE (an array/object with elements is in the index or a new value)
|
||||||
|
}
|
||||||
|
--it;
|
||||||
|
lo = it->second != nullptr ? it->first + 1 : it->first;
|
||||||
|
}
|
||||||
|
for (const node* c = target; c != lo;)
|
||||||
|
{
|
||||||
|
--c;
|
||||||
|
if (is_container(*c) && lt(target, c + c->next))
|
||||||
|
{
|
||||||
|
return (c->flags & node_flags::moved) == 0 ? const_cast<node*>(c) : nullptr; // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to the document
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace view
|
} // namespace view
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
NLOHMANN_JSON_NAMESPACE_END
|
NLOHMANN_JSON_NAMESPACE_END
|
||||||
@@ -849,7 +849,7 @@ class basic_json_view
|
|||||||
return !navigation::editable() || m_doc->edits == nullptr;
|
return !navigation::editable() || m_doc->edits == nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// the member of this object with @a key (the last one if the key occurs more than once), or nullptr
|
/// the member of this object with @a key (the first one if the key occurs more than once), or nullptr
|
||||||
NLOHMANN_VIEW_ALWAYS_INLINE const node* find_key(string_view_t key) const
|
NLOHMANN_VIEW_ALWAYS_INLINE const node* find_key(string_view_t key) const
|
||||||
{
|
{
|
||||||
return find_key(key, std::integral_constant<bool, is_editable> {});
|
return find_key(key, std::integral_constant<bool, is_editable> {});
|
||||||
|
|||||||
+1
-1
@@ -50,6 +50,7 @@
|
|||||||
#undef JSON_HEDLEY_EMSCRIPTEN_VERSION
|
#undef JSON_HEDLEY_EMSCRIPTEN_VERSION
|
||||||
#undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
|
#undef JSON_HEDLEY_EMSCRIPTEN_VERSION_CHECK
|
||||||
#undef JSON_HEDLEY_END_C_DECLS
|
#undef JSON_HEDLEY_END_C_DECLS
|
||||||
|
#undef JSON_HEDLEY_FALL_THROUGH
|
||||||
#undef JSON_HEDLEY_FLAGS
|
#undef JSON_HEDLEY_FLAGS
|
||||||
#undef JSON_HEDLEY_FLAGS_CAST
|
#undef JSON_HEDLEY_FLAGS_CAST
|
||||||
#undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
|
#undef JSON_HEDLEY_GCC_HAS_ATTRIBUTE
|
||||||
@@ -158,4 +159,3 @@
|
|||||||
#undef JSON_HEDLEY_WARNING
|
#undef JSON_HEDLEY_WARNING
|
||||||
#undef JSON_HEDLEY_WARN_UNUSED_RESULT
|
#undef JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
#undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
|
#undef JSON_HEDLEY_WARN_UNUSED_RESULT_MSG
|
||||||
#undef JSON_HEDLEY_FALL_THROUGH
|
|
||||||
Generated
+808
-280
File diff suppressed because it is too large.
Load diff
Generated
+53
-3
@@ -5564,7 +5564,9 @@ inline node* block_of(document_data& d, node* n, std::size_t extra)
|
|||||||
h->len = n->len;
|
h->len = n->len;
|
||||||
h->next = static_cast<std::uint32_t>(used);
|
h->next = static_cast<std::uint32_t>(used);
|
||||||
node* o = h + 1;
|
node* o = h + 1;
|
||||||
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n); c != e;)
|
// (the bound on o keeps the copy inside the block even if the elements
|
||||||
|
// disagreed with the element count)
|
||||||
|
for (const node* c = d.first_child_edited(n), *e = d.child_end_edited(n), *const full = h + used; c != e && o != full;)
|
||||||
{
|
{
|
||||||
if (object)
|
if (object)
|
||||||
{
|
{
|
||||||
@@ -5646,6 +5648,41 @@ inline node* find_parent(const document_data& d, const node* target)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The container whose contiguous layout (in the parsed index, or in a new
|
||||||
|
/// value) steps over `target`, also when an edit dropped the container or a
|
||||||
|
/// value that encloses it; nullptr if there is none, or if the container no
|
||||||
|
/// longer walks that layout (its elements moved). An edit never gives a node a
|
||||||
|
/// larger extent than it was parsed or encoded with (a scalar is one node, an
|
||||||
|
/// assigned array/object keeps the extent of the slot), so the containers whose
|
||||||
|
/// extent covers `target` are its original ancestors: the nearest one before it
|
||||||
|
/// is its original parent. The root of a new value that is the element sequence
|
||||||
|
/// of another container is walked by that container (which find_parent() finds
|
||||||
|
/// while it still uses the sequence), not by itself.
|
||||||
|
inline node* find_layout_parent(const document_data& d, const node* target)
|
||||||
|
{
|
||||||
|
const std::less<const node*> lt{};
|
||||||
|
const node* lo = d.nodes;
|
||||||
|
if (lt(target, lo) || !lt(target, d.nodes + d.node_count))
|
||||||
|
{
|
||||||
|
auto it = d.edits->regions.upper_bound(target);
|
||||||
|
if (it == d.edits->regions.begin())
|
||||||
|
{
|
||||||
|
return nullptr; // LCOV_EXCL_LINE (an array/object with elements is in the index or a new value)
|
||||||
|
}
|
||||||
|
--it;
|
||||||
|
lo = it->second != nullptr ? it->first + 1 : it->first;
|
||||||
|
}
|
||||||
|
for (const node* c = target; c != lo;)
|
||||||
|
{
|
||||||
|
--c;
|
||||||
|
if (is_container(*c) && lt(target, c + c->next))
|
||||||
|
{
|
||||||
|
return (c->flags & node_flags::moved) == 0 ? const_cast<node*>(c) : nullptr; // NOLINT(cppcoreguidelines-pro-type-const-cast): the nodes belong to the document
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace view
|
} // namespace view
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
NLOHMANN_JSON_NAMESPACE_END
|
NLOHMANN_JSON_NAMESPACE_END
|
||||||
@@ -6522,11 +6559,24 @@ class editor
|
|||||||
// for the container is linear in the size of the document, so
|
// for the container is linear in the size of the document, so
|
||||||
// links (which are marked in the slot) avoid it.
|
// links (which are marked in the slot) avoid it.
|
||||||
node* const p = parent_known ? parent : find_parent(m_doc, slot);
|
node* const p = parent_known ? parent : find_parent(m_doc, slot);
|
||||||
if (p != nullptr && ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0))
|
if (p != nullptr)
|
||||||
|
{
|
||||||
|
if ((p->flags & node_flags::moved) == 0 || moved_capacity(m_doc, p) == 0)
|
||||||
{
|
{
|
||||||
block_of(m_doc, p, 0);
|
block_of(m_doc, p, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// the slot is no longer part of the document, but the
|
||||||
|
// (dropped) container it is in may still step over it
|
||||||
|
node* const q = find_layout_parent(m_doc, slot);
|
||||||
|
if (q != nullptr)
|
||||||
|
{
|
||||||
|
block_of(m_doc, q, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
*slot = e.scalar;
|
*slot = e.scalar;
|
||||||
slot->flags = static_cast<std::uint8_t>(slot->flags | linked);
|
slot->flags = static_cast<std::uint8_t>(slot->flags | linked);
|
||||||
return;
|
return;
|
||||||
@@ -11016,7 +11066,7 @@ class basic_json_view
|
|||||||
return !navigation::editable() || m_doc->edits == nullptr;
|
return !navigation::editable() || m_doc->edits == nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// the member of this object with @a key (the last one if the key occurs more than once), or nullptr
|
/// the member of this object with @a key (the first one if the key occurs more than once), or nullptr
|
||||||
NLOHMANN_VIEW_ALWAYS_INLINE const node* find_key(string_view_t key) const
|
NLOHMANN_VIEW_ALWAYS_INLINE const node* find_key(string_view_t key) const
|
||||||
{
|
{
|
||||||
return find_key(key, std::integral_constant<bool, is_editable> {});
|
return find_key(key, std::integral_constant<bool, is_editable> {});
|
||||||
|
|||||||
@@ -1,5 +1,12 @@
|
|||||||
cmake_minimum_required(VERSION 3.13...4.0)
|
cmake_minimum_required(VERSION 3.13...4.0)
|
||||||
|
|
||||||
|
# The unit tests use no C++ modules. Without this, CMake 4.5 scans the C++20 and
|
||||||
|
# newer test targets and builds a `std` module for them, compiling the standard
|
||||||
|
# library's module sources with the test warning flags (which fails with
|
||||||
|
# -Werror) or failing to configure when the libc++ module sources are missing.
|
||||||
|
# The module test in tests/module_cpp20 is a separate project and unaffected.
|
||||||
|
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
|
||||||
|
|
||||||
option(JSON_Valgrind "Execute test suite with Valgrind." OFF)
|
option(JSON_Valgrind "Execute test suite with Valgrind." OFF)
|
||||||
option(JSON_FastTests "Skip expensive/slow tests." OFF)
|
option(JSON_FastTests "Skip expensive/slow tests." OFF)
|
||||||
option(JSON_TestSimdutf "Build the unit tests against the simdutf UTF-8 validation backend." OFF)
|
option(JSON_TestSimdutf "Build the unit tests against the simdutf UTF-8 validation backend." OFF)
|
||||||
@@ -438,8 +445,7 @@ json_test_add_test_for(src/unit-stream-bulk-scan.cpp
|
|||||||
# with other values of JSON_UBJSON_VALUELESS_ELEMENTS_PER_BYTE: 1 (the smallest),
|
# with other values of JSON_UBJSON_VALUELESS_ELEMENTS_PER_BYTE: 1 (the smallest),
|
||||||
# 1000000 (so large that the budget cannot be reached), and the largest value of
|
# 1000000 (so large that the budget cannot be reached), and the largest value of
|
||||||
# a 64-bit integer (which saturates the limit already at the first byte, and is
|
# a 64-bit integer (which saturates the limit already at the first byte, and is
|
||||||
# clamped to the largest std::size_t where that has 32 bits). The file does not
|
# clamped to the largest std::size_t where that has 32 bits)
|
||||||
# mention JSON_HAS_CPP_<n>, so only C++11 is built unless the standards are forced
|
|
||||||
json_test_set_test_options(test-ubjson_valueless_rate_1
|
json_test_set_test_options(test-ubjson_valueless_rate_1
|
||||||
COMPILE_DEFINITIONS JSON_UBJSON_VALUELESS_ELEMENTS_PER_BYTE=1
|
COMPILE_DEFINITIONS JSON_UBJSON_VALUELESS_ELEMENTS_PER_BYTE=1
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,10 +2,21 @@
|
|||||||
# properties on its (always-configured) INTERFACE_LIBRARY "fmt-header-only" target;
|
# properties on its (always-configured) INTERFACE_LIBRARY "fmt-header-only" target;
|
||||||
# CMake rejected those properties on INTERFACE_LIBRARY targets before CMake 3.19
|
# CMake rejected those properties on INTERFACE_LIBRARY targets before CMake 3.19
|
||||||
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.19.0")
|
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.19.0")
|
||||||
|
# The recipe runs its executable after linking, so nested cross-builds need
|
||||||
|
# the parent system and emulator settings as well as the compiler.
|
||||||
|
set(FMT_CROSSCOMPILING_ARGS)
|
||||||
|
if(CMAKE_CROSSCOMPILING)
|
||||||
|
string(REPLACE ";" "\\;" FMT_CROSSCOMPILING_EMULATOR "${CMAKE_CROSSCOMPILING_EMULATOR}")
|
||||||
|
list(APPEND FMT_CROSSCOMPILING_ARGS
|
||||||
|
"-DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}"
|
||||||
|
"-DCMAKE_CROSSCOMPILING_EMULATOR=${FMT_CROSSCOMPILING_EMULATOR}"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
add_test(NAME fmt_formatter_configure
|
add_test(NAME fmt_formatter_configure
|
||||||
COMMAND ${CMAKE_COMMAND}
|
COMMAND ${CMAKE_COMMAND}
|
||||||
-G "${CMAKE_GENERATOR}"
|
-G "${CMAKE_GENERATOR}"
|
||||||
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
|
-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}
|
||||||
|
${FMT_CROSSCOMPILING_ARGS}
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/project
|
${CMAKE_CURRENT_SOURCE_DIR}/project
|
||||||
)
|
)
|
||||||
add_test(NAME fmt_formatter_build
|
add_test(NAME fmt_formatter_build
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -6,6 +6,13 @@
|
|||||||
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// cmake/test.cmake selects the C++ standard versions with which to build a
|
||||||
|
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
|
||||||
|
// When using macros that are only defined for particular versions of the standard
|
||||||
|
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
|
||||||
|
// version macro in a comment close by, like this:
|
||||||
|
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
|
||||||
|
|
||||||
#include "doctest_compatibility.h"
|
#include "doctest_compatibility.h"
|
||||||
|
|
||||||
#define JSON_TESTS_PRIVATE
|
#define JSON_TESTS_PRIVATE
|
||||||
@@ -15,6 +22,7 @@ using nlohmann::json;
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <valarray>
|
#include <valarray>
|
||||||
|
// JSON_HAS_CPP_20 (do not remove; see note at top of file)
|
||||||
#if JSON_HAS_RANGES
|
#if JSON_HAS_RANGES
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ using nlohmann::json;
|
|||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -367,3 +368,125 @@ TEST_CASE("Binary formats with narrow number types")
|
|||||||
CHECK(format.decode(format.encode(json(1e300)), false).is_discarded());
|
CHECK(format.decode(format.encode(json(1e300)), false).is_discarded());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
// records the SAX events of narrow_json, including the arguments of parse_error
|
||||||
|
struct narrow_event_recorder
|
||||||
|
{
|
||||||
|
using number_integer_t = narrow_json::number_integer_t;
|
||||||
|
using number_unsigned_t = narrow_json::number_unsigned_t;
|
||||||
|
using number_float_t = narrow_json::number_float_t;
|
||||||
|
using string_t = narrow_json::string_t;
|
||||||
|
using binary_t = narrow_json::binary_t;
|
||||||
|
|
||||||
|
bool null()
|
||||||
|
{
|
||||||
|
events.emplace_back("null");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool boolean(bool val)
|
||||||
|
{
|
||||||
|
events.emplace_back(val ? "true" : "false");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool number_integer(number_integer_t val)
|
||||||
|
{
|
||||||
|
events.push_back(std::to_string(val));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool number_unsigned(number_unsigned_t val)
|
||||||
|
{
|
||||||
|
events.push_back(std::to_string(val));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool number_float(number_float_t val, const string_t& /*unused*/)
|
||||||
|
{
|
||||||
|
events.push_back(std::to_string(val));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool string(string_t& val)
|
||||||
|
{
|
||||||
|
events.push_back(val);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool binary(binary_t& /*unused*/)
|
||||||
|
{
|
||||||
|
events.emplace_back("binary");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool start_object(std::size_t /*unused*/)
|
||||||
|
{
|
||||||
|
events.emplace_back("{");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool key(string_t& val)
|
||||||
|
{
|
||||||
|
events.push_back(val);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool end_object()
|
||||||
|
{
|
||||||
|
events.emplace_back("}");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool start_array(std::size_t /*unused*/)
|
||||||
|
{
|
||||||
|
events.emplace_back("[");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool end_array()
|
||||||
|
{
|
||||||
|
events.emplace_back("]");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
bool parse_error(std::size_t position, const std::string& last_token, const nlohmann::detail::exception& ex)
|
||||||
|
{
|
||||||
|
events.push_back("parse_error(" + std::to_string(position) + ", " + last_token + ", " + ex.what() + ")");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::string> events {}; // NOLINT(readability-redundant-member-init)
|
||||||
|
};
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST_CASE("Binary formats with narrow number types from contiguous and non-contiguous input")
|
||||||
|
{
|
||||||
|
// contiguous input reads runs of numbers in one loop; a number that
|
||||||
|
// overflows number_float_t must be reported with the same position and
|
||||||
|
// last byte as when the input is read byte by byte
|
||||||
|
const json values = {1, 70000, -70000, 1.5, 2.5, 1e300, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5};
|
||||||
|
const json doubles = {1.5, 2.5, 3.5, 1e300, 4.5, 5.5, 6.5, 7.5, 8.5};
|
||||||
|
|
||||||
|
struct binary_input
|
||||||
|
{
|
||||||
|
const char* name;
|
||||||
|
json::input_format_t format;
|
||||||
|
bytes data;
|
||||||
|
};
|
||||||
|
const std::vector<binary_input> inputs =
|
||||||
|
{
|
||||||
|
{"CBOR", json::input_format_t::cbor, json::to_cbor(values)},
|
||||||
|
{"CBOR doubles", json::input_format_t::cbor, json::to_cbor(doubles)},
|
||||||
|
{"MessagePack", json::input_format_t::msgpack, json::to_msgpack(values)},
|
||||||
|
{"MessagePack doubles", json::input_format_t::msgpack, json::to_msgpack(doubles)},
|
||||||
|
{"UBJSON", json::input_format_t::ubjson, json::to_ubjson(values, true)},
|
||||||
|
{"UBJSON typed doubles", json::input_format_t::ubjson, json::to_ubjson(doubles, true, true)},
|
||||||
|
{"BJData", json::input_format_t::bjdata, json::to_bjdata(values, true)},
|
||||||
|
{"BJData typed doubles", json::input_format_t::bjdata, json::to_bjdata(doubles, true, true)},
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& input : inputs)
|
||||||
|
{
|
||||||
|
const std::string name = input.name;
|
||||||
|
CAPTURE(name)
|
||||||
|
const std::list<std::uint8_t> bytewise(input.data.begin(), input.data.end());
|
||||||
|
narrow_event_recorder from_contiguous;
|
||||||
|
narrow_event_recorder from_bytewise;
|
||||||
|
CHECK_FALSE(narrow_json::sax_parse(input.data, &from_contiguous, input.format));
|
||||||
|
CHECK_FALSE(narrow_json::sax_parse(bytewise.begin(), bytewise.end(), &from_bytewise, input.format));
|
||||||
|
CHECK(from_contiguous.events == from_bytewise.events);
|
||||||
|
REQUIRE(!from_contiguous.events.empty());
|
||||||
|
CHECK(from_contiguous.events.back().find("out_of_range.406") != std::string::npos);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1278,6 +1278,25 @@ TEST_CASE("BJData")
|
|||||||
CHECK(j.dump() == "3.141592653589793");
|
CHECK(j.dump() == "3.141592653589793");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("long floating-point number")
|
||||||
|
{
|
||||||
|
// the SAX parser receives the number text as it is in the
|
||||||
|
// input, also when it is longer than the part of a token
|
||||||
|
// that error messages show
|
||||||
|
const std::vector<std::size_t> lengths = {8192, 8193, 10000};
|
||||||
|
for (const auto length : lengths)
|
||||||
|
{
|
||||||
|
CAPTURE(length)
|
||||||
|
const std::string number = "1." + std::string(length - 2, '3');
|
||||||
|
std::vector<std::uint8_t> vec = {'H', 'I', static_cast<std::uint8_t>(length & 0xFF), static_cast<std::uint8_t>(length >> 8)};
|
||||||
|
vec.insert(vec.end(), number.begin(), number.end());
|
||||||
|
SaxEventLogger logger;
|
||||||
|
CHECK(json::sax_parse(vec, &logger, json::input_format_t::bjdata));
|
||||||
|
CHECK(logger.events == std::vector<std::string>({"number_float(" + number + ")"}));
|
||||||
|
CHECK(json::from_bjdata(vec).is_number_float());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("errors")
|
SECTION("errors")
|
||||||
{
|
{
|
||||||
// error while parsing length
|
// error while parsing length
|
||||||
|
|||||||
Loaded 100 of 115 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user