mirror of
https://github.com/nlohmann/json.git
synced 2026-10-27 00:08:55 +00:00
Compare commits
16
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9222763ab9 | ||
|
|
d13c9e7212 | ||
|
|
522e7465b0 | ||
|
|
fe4e78e867 | ||
|
|
9196e92520 | ||
|
|
7d22d865dd | ||
|
|
4c73319d3b | ||
|
|
da5097abcd | ||
|
|
2319f6e6f9 | ||
|
|
9a0d1c0c47 | ||
|
|
1bbb5d400a | ||
|
|
08d18d5a82 | ||
|
|
a27065bd12 | ||
|
|
3940f4b730 | ||
|
|
0663907b68 | ||
|
|
f23b3c63a2 |
No files matched your search
+7
-36
@@ -1,33 +1,3 @@
|
|||||||
# Style or vendor rule sets that do not fit a header-only library:
|
|
||||||
# altera-*, fuchsia-*, llvmlibc-*, android-cloexec-fopen, boost-use-ranges, llvm-header-guard,
|
|
||||||
# llvm-include-order, llvm-prefer-static-over-anonymous-namespace, llvm-use-ranges,
|
|
||||||
# misc-use-anonymous-namespace, google-runtime-float, google-runtime-int, hicpp-signed-bitwise,
|
|
||||||
# bugprone-signed-bitwise, hicpp-explicit-conversions, google-explicit-constructor,
|
|
||||||
# cppcoreguidelines-explicit-constructor, misc-explicit-constructor (implicit conversions are the
|
|
||||||
# API), readability-trailing-comma,
|
|
||||||
# hicpp-avoid-goto / cppcoreguidelines-avoid-goto, hicpp-no-array-decay,
|
|
||||||
# hicpp-uppercase-literal-suffix, readability-uppercase-literal-suffix, modernize-use-trailing-return-type.
|
|
||||||
# Constructs the library needs on purpose (the library supports C++11 and legacy compilers):
|
|
||||||
# cppcoreguidelines-macro-usage, cppcoreguidelines-avoid-do-while (macros),
|
|
||||||
# cppcoreguidelines-pro-bounds-*, cppcoreguidelines-pro-type-reinterpret-cast,
|
|
||||||
# cppcoreguidelines-pro-type-union-access (parsers, serializers, basic_json's value union),
|
|
||||||
# cppcoreguidelines-avoid-const-or-ref-data-members, cppcoreguidelines-rvalue-reference-param-not-moved,
|
|
||||||
# cppcoreguidelines-avoid-non-const-global-variables, misc-non-private-member-variables-in-classes,
|
|
||||||
# misc-no-recursion (value walks are recursive, bounded by detail::recursion_depth_limit()),
|
|
||||||
# modernize-type-traits, modernize-use-constraints, modernize-use-ranges, modernize-use-std-numbers,
|
|
||||||
# modernize-use-designated-initializers, modernize-use-nodiscard, modernize-concat-nested-namespaces,
|
|
||||||
# readability-redundant-typename, readability-redundant-parentheses,
|
|
||||||
# readability-redundant-access-specifiers, readability-simplify-boolean-expr,
|
|
||||||
# readability-use-concise-preprocessor-directives, modernize-use-string-view,
|
|
||||||
# modernize-use-integer-sign-comparison (needs std::cmp_less; the flagged
|
|
||||||
# static_cast<std::size_t>(end - pos) < n comparisons are deliberate)
|
|
||||||
# (all need a newer standard or compiler than C++11, or would change macro-generated or
|
|
||||||
# deliberately explicit code).
|
|
||||||
# Too noisy to be useful (numeric parsing/formatting code, doctest, tests):
|
|
||||||
# bugprone-easily-swappable-parameters, cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers,
|
|
||||||
# readability-identifier-length, readability-function-cognitive-complexity, performance-enum-size,
|
|
||||||
# concurrency-mt-unsafe, misc-confusable-identifiers, misc-include-cleaner (run on its own for
|
|
||||||
# the consumer headers in cmake/ci.cmake).
|
|
||||||
# portability-avoid-pragma-once: kept disabled on purpose. #pragma once is accepted
|
# portability-avoid-pragma-once: kept disabled on purpose. #pragma once is accepted
|
||||||
# by every supported compiler, and tools/amalgamate/amalgamate.py strips it from
|
# by every supported compiler, and tools/amalgamate/amalgamate.py strips it from
|
||||||
# single_include, so there is nothing left to fix here.
|
# single_include, so there is nothing left to fix here.
|
||||||
@@ -40,14 +10,14 @@ Checks: '*,
|
|||||||
-android-cloexec-fopen,
|
-android-cloexec-fopen,
|
||||||
-boost-use-ranges,
|
-boost-use-ranges,
|
||||||
-bugprone-easily-swappable-parameters,
|
-bugprone-easily-swappable-parameters,
|
||||||
-bugprone-signed-bitwise,
|
-cert-err58-cpp,
|
||||||
|
-clang-analyzer-webkit.NoUncountedMemberChecker,
|
||||||
-concurrency-mt-unsafe,
|
-concurrency-mt-unsafe,
|
||||||
-cppcoreguidelines-avoid-const-or-ref-data-members,
|
-cppcoreguidelines-avoid-const-or-ref-data-members,
|
||||||
-cppcoreguidelines-avoid-do-while,
|
-cppcoreguidelines-avoid-do-while,
|
||||||
-cppcoreguidelines-avoid-goto,
|
-cppcoreguidelines-avoid-goto,
|
||||||
-cppcoreguidelines-avoid-magic-numbers,
|
-cppcoreguidelines-avoid-magic-numbers,
|
||||||
-cppcoreguidelines-avoid-non-const-global-variables,
|
-cppcoreguidelines-avoid-non-const-global-variables,
|
||||||
-cppcoreguidelines-explicit-constructor,
|
|
||||||
-cppcoreguidelines-macro-usage,
|
-cppcoreguidelines-macro-usage,
|
||||||
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,
|
-cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,
|
||||||
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
|
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
|
||||||
@@ -56,15 +26,19 @@ Checks: '*,
|
|||||||
-cppcoreguidelines-pro-type-reinterpret-cast,
|
-cppcoreguidelines-pro-type-reinterpret-cast,
|
||||||
-cppcoreguidelines-pro-type-union-access,
|
-cppcoreguidelines-pro-type-union-access,
|
||||||
-cppcoreguidelines-rvalue-reference-param-not-moved,
|
-cppcoreguidelines-rvalue-reference-param-not-moved,
|
||||||
|
-cppcoreguidelines-virtual-class-destructor,
|
||||||
-fuchsia-default-arguments-calls,
|
-fuchsia-default-arguments-calls,
|
||||||
-fuchsia-default-arguments-declarations,
|
-fuchsia-default-arguments-declarations,
|
||||||
-fuchsia-overloaded-operator,
|
-fuchsia-overloaded-operator,
|
||||||
-google-explicit-constructor,
|
-google-explicit-constructor,
|
||||||
|
-google-readability-function-size,
|
||||||
-google-runtime-float,
|
-google-runtime-float,
|
||||||
-google-runtime-int,
|
-google-runtime-int,
|
||||||
-hicpp-avoid-goto,
|
-hicpp-avoid-goto,
|
||||||
-hicpp-explicit-conversions,
|
-hicpp-explicit-conversions,
|
||||||
|
-hicpp-function-size,
|
||||||
-hicpp-no-array-decay,
|
-hicpp-no-array-decay,
|
||||||
|
-hicpp-no-assembler,
|
||||||
-hicpp-signed-bitwise,
|
-hicpp-signed-bitwise,
|
||||||
-hicpp-uppercase-literal-suffix,
|
-hicpp-uppercase-literal-suffix,
|
||||||
-llvm-header-guard,
|
-llvm-header-guard,
|
||||||
@@ -74,7 +48,6 @@ Checks: '*,
|
|||||||
-llvmlibc-*,
|
-llvmlibc-*,
|
||||||
-misc-use-anonymous-namespace,
|
-misc-use-anonymous-namespace,
|
||||||
-misc-confusable-identifiers,
|
-misc-confusable-identifiers,
|
||||||
-misc-explicit-constructor,
|
|
||||||
-misc-include-cleaner,
|
-misc-include-cleaner,
|
||||||
-misc-no-recursion,
|
-misc-no-recursion,
|
||||||
-misc-non-private-member-variables-in-classes,
|
-misc-non-private-member-variables-in-classes,
|
||||||
@@ -82,22 +55,20 @@ Checks: '*,
|
|||||||
-modernize-type-traits,
|
-modernize-type-traits,
|
||||||
-modernize-use-constraints,
|
-modernize-use-constraints,
|
||||||
-modernize-use-designated-initializers,
|
-modernize-use-designated-initializers,
|
||||||
-modernize-use-integer-sign-comparison,
|
|
||||||
-modernize-use-nodiscard,
|
-modernize-use-nodiscard,
|
||||||
-modernize-use-ranges,
|
-modernize-use-ranges,
|
||||||
-modernize-use-string-view,
|
|
||||||
-modernize-use-std-numbers,
|
-modernize-use-std-numbers,
|
||||||
-modernize-use-trailing-return-type,
|
-modernize-use-trailing-return-type,
|
||||||
-performance-enum-size,
|
-performance-enum-size,
|
||||||
-portability-avoid-pragma-once,
|
-portability-avoid-pragma-once,
|
||||||
-readability-function-cognitive-complexity,
|
-readability-function-cognitive-complexity,
|
||||||
|
-readability-function-size,
|
||||||
-readability-identifier-length,
|
-readability-identifier-length,
|
||||||
-readability-magic-numbers,
|
-readability-magic-numbers,
|
||||||
-readability-redundant-access-specifiers,
|
-readability-redundant-access-specifiers,
|
||||||
-readability-redundant-parentheses,
|
-readability-redundant-parentheses,
|
||||||
-readability-redundant-typename,
|
-readability-redundant-typename,
|
||||||
-readability-simplify-boolean-expr,
|
-readability-simplify-boolean-expr,
|
||||||
-readability-trailing-comma,
|
|
||||||
-readability-uppercase-literal-suffix,
|
-readability-uppercase-literal-suffix,
|
||||||
-readability-use-concise-preprocessor-directives'
|
-readability-use-concise-preprocessor-directives'
|
||||||
|
|
||||||
|
|||||||
@@ -18,8 +18,7 @@ environment:
|
|||||||
matrix:
|
matrix:
|
||||||
# The Visual Studio 2017 jobs compile everything with /std:c++17, so they
|
# The Visual Studio 2017 jobs compile everything with /std:c++17, so they
|
||||||
# only build the C++17 variant of each test, split into two jobs each to
|
# only build the C++17 variant of each test, split into two jobs each to
|
||||||
# stay below AppVeyor's 60-minute limit per job. The Visual Studio 2019
|
# stay below AppVeyor's 60-minute limit per job.
|
||||||
# job builds the tests for all standards and is split for the same reason.
|
|
||||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||||
configuration: Debug
|
configuration: Debug
|
||||||
platform: x86
|
platform: x86
|
||||||
@@ -51,13 +50,7 @@ environment:
|
|||||||
configuration: Release
|
configuration: Release
|
||||||
platform: x86
|
platform: x86
|
||||||
CXX_FLAGS: "/W4 /WX"
|
CXX_FLAGS: "/W4 /WX"
|
||||||
CMAKE_OPTIONS: "-DJSON_ImplicitConversions=OFF -DJSON_TestShard=0/2"
|
CMAKE_OPTIONS: "-DJSON_ImplicitConversions=OFF"
|
||||||
GENERATOR: Visual Studio 16 2019
|
|
||||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
|
|
||||||
configuration: Release
|
|
||||||
platform: x86
|
|
||||||
CXX_FLAGS: "/W4 /WX"
|
|
||||||
CMAKE_OPTIONS: "-DJSON_ImplicitConversions=OFF -DJSON_TestShard=1/2"
|
|
||||||
GENERATOR: Visual Studio 16 2019
|
GENERATOR: Visual Studio 16 2019
|
||||||
|
|
||||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
|
||||||
|
|||||||
@@ -64,7 +64,6 @@ labels:
|
|||||||
- "tests/src/unit-json_view.*"
|
- "tests/src/unit-json_view.*"
|
||||||
- "tests/src/json_view_test_helpers\\.hpp"
|
- "tests/src/json_view_test_helpers\\.hpp"
|
||||||
- "tests/src/fuzzer-(parse_json_view|json_view_image)\\.cpp"
|
- "tests/src/fuzzer-(parse_json_view|json_view_image)\\.cpp"
|
||||||
- "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\\.md"
|
||||||
- "docs/mkdocs/docs/api/basic_json_(document|view)/.*"
|
- "docs/mkdocs/docs/api/basic_json_(document|view)/.*"
|
||||||
|
|||||||
@@ -154,28 +154,6 @@ jobs:
|
|||||||
# PR content in a job without credentials.
|
# PR content in a job without credentials.
|
||||||
python3 tools/generate_natvis/generate_natvis.py $MAIN_DIR
|
python3 tools/generate_natvis/generate_natvis.py $MAIN_DIR
|
||||||
|
|
||||||
- name: Check the nlohmann_json.natvis generator options
|
|
||||||
run: |
|
|
||||||
cd $MAIN_DIR
|
|
||||||
# The options for configurations outside the checked-in file write
|
|
||||||
# to a temporary file, so they do not affect the diff below.
|
|
||||||
GENERATOR=tools/generate_natvis/generate_natvis.py
|
|
||||||
python3 $GENERATOR --list-tags
|
|
||||||
TAG_COUNT=$(python3 $GENERATOR --list-tags | wc -l)
|
|
||||||
|
|
||||||
python3 $GENERATOR --abi-tags diag,_dp,_ns --abi-tags _diag --output $RUNNER_TEMP/abi_tags.natvis
|
|
||||||
grep -q '<!-- Namespace nlohmann::json_abi_diag_dp_ns -->' $RUNNER_TEMP/abi_tags.natvis
|
|
||||||
grep -q '<!-- Namespace nlohmann::json_abi_diag_dp_ns_v[0-9_]* -->' $RUNNER_TEMP/abi_tags.natvis
|
|
||||||
test $(grep -c '<!-- Namespace ' $RUNNER_TEMP/abi_tags.natvis) -eq $(( $(grep -c '<!-- Namespace ' nlohmann_json.natvis) + 2 ))
|
|
||||||
|
|
||||||
python3 $GENERATOR --all --output $RUNNER_TEMP/all.natvis
|
|
||||||
test $(grep -c '<!-- Namespace ' $RUNNER_TEMP/all.natvis) -eq $(( (1 << TAG_COUNT) * 2 + 1 ))
|
|
||||||
|
|
||||||
if python3 $GENERATOR --abi-tags _unknown --output $RUNNER_TEMP/unknown.natvis; then
|
|
||||||
echo "generate_natvis.py accepted an unknown ABI tag"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Build patch and check for differences
|
- name: Build patch and check for differences
|
||||||
id: diff
|
id: diff
|
||||||
run: |
|
run: |
|
||||||
|
|||||||
@@ -0,0 +1,81 @@
|
|||||||
|
name: "Check API documentation"
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
check_api_docs:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Harden Runner
|
||||||
|
uses: step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0
|
||||||
|
with:
|
||||||
|
egress-policy: audit
|
||||||
|
|
||||||
|
- name: Checkout pull request
|
||||||
|
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
|
||||||
|
|
||||||
|
- name: Install clang
|
||||||
|
# Used only as a subprocess for `clang++ -E -v` system-include-path discovery in
|
||||||
|
# extract_api.py; it does not need to version-match the pinned libclang pip wheel
|
||||||
|
# below, which does the actual AST parsing. Do not "fix" this to be version-matched.
|
||||||
|
run: sudo apt-get update && sudo apt-get install -y clang
|
||||||
|
|
||||||
|
- name: Install Python dependencies
|
||||||
|
run: pip install -r tools/api_checker/requirements.txt
|
||||||
|
|
||||||
|
- name: Extract API and regenerate the committed surface file
|
||||||
|
run: |
|
||||||
|
python3 tools/api_checker/extract_api.py \
|
||||||
|
--header include/nlohmann/json.hpp \
|
||||||
|
--include include \
|
||||||
|
--output /tmp/api_snapshot.json \
|
||||||
|
--surface-output tools/api_checker/api_surface.json
|
||||||
|
|
||||||
|
- name: "Check API documentation (Phase 1: advisory)"
|
||||||
|
# Surfaces missing/broken @sa links without failing the job while the backlog from the
|
||||||
|
# initial AST-based rollout is burned down. See tools/api_checker/POLICY.md and the PR
|
||||||
|
# that introduced this workflow for the two-phase rollout plan.
|
||||||
|
continue-on-error: true
|
||||||
|
run: |
|
||||||
|
python3 tools/api_checker/check_docs.py \
|
||||||
|
--snapshot /tmp/api_snapshot.json
|
||||||
|
|
||||||
|
- name: Check macro documentation (advisory only)
|
||||||
|
# Cross-checks docs/mkdocs/docs/api/macros/ pages against #define sites. Only checks the
|
||||||
|
# documented-macro-still-exists direction; never blocks CI. See POLICY.md.
|
||||||
|
run: python3 tools/api_checker/check_macros.py
|
||||||
|
|
||||||
|
- name: Check for uncommitted API surface changes
|
||||||
|
id: diff
|
||||||
|
run: |
|
||||||
|
mkdir -p ${{ github.workspace }}/patch
|
||||||
|
git diff --patch --no-color -- tools/api_checker/api_surface.json > ${{ github.workspace }}/patch/api_surface.patch
|
||||||
|
if [ -s ${{ github.workspace }}/patch/api_surface.patch ]; then
|
||||||
|
echo "tools/api_checker/api_surface.json is out of date. Diff:"
|
||||||
|
cat ${{ github.workspace }}/patch/api_surface.patch
|
||||||
|
echo "has_diff=true" >> "$GITHUB_OUTPUT"
|
||||||
|
else
|
||||||
|
echo "has_diff=false" >> "$GITHUB_OUTPUT"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Uploaded so contributors can fix their PR with `git apply api_surface.patch`
|
||||||
|
# instead of installing libclang locally.
|
||||||
|
- name: Upload patch
|
||||||
|
if: steps.diff.outputs.has_diff == 'true'
|
||||||
|
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
|
||||||
|
with:
|
||||||
|
name: api-surface-patch
|
||||||
|
path: patch/api_surface.patch
|
||||||
|
|
||||||
|
- name: Fail if API surface file is not up to date
|
||||||
|
# Unlike the doc-backlog check above, this is purely mechanical regeneration with no
|
||||||
|
# backlog to phase in -- blocking from the start, matching check_amalgamation.yml's
|
||||||
|
# precedent. Contributors who add/remove/rename public API must regenerate and commit
|
||||||
|
# tools/api_checker/api_surface.json as part of their PR.
|
||||||
|
if: steps.diff.outputs.has_diff == 'true'
|
||||||
|
run: exit 1
|
||||||
@@ -38,14 +38,14 @@ jobs:
|
|||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
|
uses: github/codeql-action/init@24c54180a607b1449ed407dd24f251e4e9147c8d # v4.38.3
|
||||||
with:
|
with:
|
||||||
languages: c-cpp
|
languages: c-cpp
|
||||||
|
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
- name: Autobuild
|
- name: Autobuild
|
||||||
uses: github/codeql-action/autobuild@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
|
uses: github/codeql-action/autobuild@24c54180a607b1449ed407dd24f251e4e9147c8d # v4.38.3
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
- name: Perform CodeQL Analysis
|
||||||
uses: github/codeql-action/analyze@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
|
uses: github/codeql-action/analyze@24c54180a607b1449ed407dd24f251e4e9147c8d # v4.38.3
|
||||||
@@ -37,14 +37,6 @@ jobs:
|
|||||||
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
|
var matchArtifact = artifacts.data.artifacts.filter((artifact) => {
|
||||||
return artifact.name == "pr"
|
return artifact.name == "pr"
|
||||||
})[0];
|
})[0];
|
||||||
// a run that failed before its jobs started (e.g. a stale queued
|
|
||||||
// run cancelled long after its push) has no artifact to report on
|
|
||||||
if (!matchArtifact) {
|
|
||||||
core.notice('Run ${{ github.event.workflow_run.id }} has no "pr" artifact; nothing to comment on.');
|
|
||||||
core.setOutput('found', 'false');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
core.setOutput('found', 'true');
|
|
||||||
var download = await github.rest.actions.downloadArtifact({
|
var download = await github.rest.actions.downloadArtifact({
|
||||||
owner: context.repo.owner,
|
owner: context.repo.owner,
|
||||||
repo: context.repo.repo,
|
repo: context.repo.repo,
|
||||||
@@ -59,11 +51,9 @@ jobs:
|
|||||||
# Extract the untrusted PR artifact into a dedicated empty directory and
|
# Extract the untrusted PR artifact into a dedicated empty directory and
|
||||||
# read only the two expected files by fixed path afterwards. This avoids a
|
# read only the two expected files by fixed path afterwards. This avoids a
|
||||||
# malicious archive overwriting workspace files or escaping via ../ paths.
|
# malicious archive overwriting workspace files or escaping via ../ paths.
|
||||||
- if: steps.download.outputs.found == 'true'
|
- run: unzip -o pr.zip -d ./pr_artifact
|
||||||
run: unzip -o pr.zip -d ./pr_artifact
|
|
||||||
|
|
||||||
- name: 'Comment on PR'
|
- name: 'Comment on PR'
|
||||||
if: steps.download.outputs.found == 'true'
|
|
||||||
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
|
||||||
with:
|
with:
|
||||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|||||||
@@ -43,11 +43,10 @@ jobs:
|
|||||||
- name: flawfinder_scan
|
- name: flawfinder_scan
|
||||||
uses: david-a-wheeler/flawfinder@c4216b74cf2639ffa98503768bd6e4299b5440c9 # v2.0.20
|
uses: david-a-wheeler/flawfinder@c4216b74cf2639ffa98503768bd6e4299b5440c9 # v2.0.20
|
||||||
with:
|
with:
|
||||||
# single_include/ is generated from include/ and would report every finding twice
|
arguments: '--sarif ./'
|
||||||
arguments: "--sarif --exclude '*/single_include/*' ./"
|
|
||||||
output: 'flawfinder_results.sarif'
|
output: 'flawfinder_results.sarif'
|
||||||
|
|
||||||
- name: Upload analysis results to GitHub Security tab
|
- name: Upload analysis results to GitHub Security tab
|
||||||
uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
|
uses: github/codeql-action/upload-sarif@24c54180a607b1449ed407dd24f251e4e9147c8d # v4.38.3
|
||||||
with:
|
with:
|
||||||
sarif_file: ${{github.workspace}}/flawfinder_results.sarif
|
sarif_file: ${{github.workspace}}/flawfinder_results.sarif
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
name: "json_view benchmarks"
|
|
||||||
|
|
||||||
# On demand only: runs the comparison of json_view with yyjson, simdjson, and
|
|
||||||
# Boost.JSON (tests/benchmarks/json_view/compare.py) on GitHub-hosted runners,
|
|
||||||
# for numbers from x86-64 and AArch64 Linux. It runs when started by hand, or
|
|
||||||
# when a pull request gets the label "benchmark" (on both architectures, with
|
|
||||||
# GCC and the default settings). Shared runners are noisy: the results show
|
|
||||||
# where json_view stands, but published numbers need a quiet machine (see
|
|
||||||
# tests/benchmarks/json_view/README.md).
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
types: [labeled]
|
|
||||||
workflow_dispatch:
|
|
||||||
inputs:
|
|
||||||
runner:
|
|
||||||
description: "Runner image"
|
|
||||||
type: choice
|
|
||||||
options:
|
|
||||||
- ubuntu-24.04
|
|
||||||
- ubuntu-24.04-arm
|
|
||||||
default: ubuntu-24.04
|
|
||||||
compiler:
|
|
||||||
description: "Compiler"
|
|
||||||
type: choice
|
|
||||||
options:
|
|
||||||
- g++
|
|
||||||
- clang++
|
|
||||||
default: g++
|
|
||||||
native:
|
|
||||||
description: "Compile for the runner's CPU (-march=native)"
|
|
||||||
type: boolean
|
|
||||||
default: false
|
|
||||||
rounds:
|
|
||||||
description: "Rounds of bench_view"
|
|
||||||
type: number
|
|
||||||
default: 30
|
|
||||||
|
|
||||||
permissions:
|
|
||||||
contents: read
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
# (the label is part of the group: adding an unrelated label must not cancel a running benchmark)
|
|
||||||
group: json-view-benchmarks-${{ github.workflow }}-${{ github.ref }}-${{ github.event.label.name }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
compare:
|
|
||||||
if: github.event_name == 'workflow_dispatch' || github.event.label.name == 'benchmark'
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
runner: ${{ fromJSON(github.event_name == 'workflow_dispatch' && format('["{0}"]', inputs.runner) || '["ubuntu-24.04", "ubuntu-24.04-arm"]') }}
|
|
||||||
runs-on: ${{ matrix.runner }}
|
|
||||||
timeout-minutes: 60
|
|
||||||
steps:
|
|
||||||
- name: Harden Runner
|
|
||||||
uses: step-security/harden-runner@ccd8616d44fd3846e67624a50d5aad6d37bf2d25 # v2.22.1
|
|
||||||
with:
|
|
||||||
egress-policy: audit
|
|
||||||
|
|
||||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
||||||
with:
|
|
||||||
persist-credentials: false
|
|
||||||
|
|
||||||
- name: Download test data
|
|
||||||
run: |
|
|
||||||
cmake -S . -B build -DJSON_BuildTests=On
|
|
||||||
cmake --build build --target download_test_data
|
|
||||||
|
|
||||||
- name: Run the comparison
|
|
||||||
env:
|
|
||||||
CXX: ${{ inputs.compiler || 'g++' }}
|
|
||||||
CC: ${{ inputs.compiler == 'clang++' && 'clang' || 'gcc' }}
|
|
||||||
ROUNDS: ${{ inputs.rounds || 30 }}
|
|
||||||
NATIVE: ${{ inputs.native && '--native' || '' }}
|
|
||||||
run: python3 tests/benchmarks/json_view/compare.py --data build/test_files --download --rounds "$ROUNDS" $NATIVE
|
|
||||||
|
|
||||||
- name: Summary
|
|
||||||
run: cat tests/benchmarks/json_view/results/*.md >> "$GITHUB_STEP_SUMMARY"
|
|
||||||
|
|
||||||
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
|
|
||||||
with:
|
|
||||||
name: json_view-benchmarks-${{ matrix.runner }}-${{ inputs.compiler || 'g++' }}
|
|
||||||
path: tests/benchmarks/json_view/results/
|
|
||||||
@@ -80,6 +80,6 @@ jobs:
|
|||||||
|
|
||||||
# Upload the results to GitHub's code scanning dashboard.
|
# Upload the results to GitHub's code scanning dashboard.
|
||||||
- name: "Upload to code-scanning"
|
- name: "Upload to code-scanning"
|
||||||
uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
|
uses: github/codeql-action/upload-sarif@24c54180a607b1449ed407dd24f251e4e9147c8d # v4.38.3
|
||||||
with:
|
with:
|
||||||
sarif_file: results.sarif
|
sarif_file: results.sarif
|
||||||
@@ -65,7 +65,7 @@ jobs:
|
|||||||
|
|
||||||
# Upload SARIF file generated in previous step
|
# Upload SARIF file generated in previous step
|
||||||
- name: Upload SARIF file
|
- name: Upload SARIF file
|
||||||
uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0
|
uses: github/codeql-action/upload-sarif@24c54180a607b1449ed407dd24f251e4e9147c8d # v4.38.3
|
||||||
with:
|
with:
|
||||||
sarif_file: semgrep.sarif
|
sarif_file: semgrep.sarif
|
||||||
if: always()
|
if: always()
|
||||||
@@ -3,6 +3,7 @@
|
|||||||
*.gcno
|
*.gcno
|
||||||
*.gcda
|
*.gcda
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
__pycache__/
|
||||||
|
|
||||||
/.idea
|
/.idea
|
||||||
/cmake-build-*
|
/cmake-build-*
|
||||||
@@ -44,6 +45,10 @@ venv
|
|||||||
|
|
||||||
nlohmann_json.spdx
|
nlohmann_json.spdx
|
||||||
|
|
||||||
|
# api_checker: ephemeral, location/doc-status-sensitive working file (not the committed
|
||||||
|
# release-tracking artifact -- see tools/api_checker/api_surface.json for that)
|
||||||
|
/tools/api_checker/api_snapshot.json
|
||||||
|
|
||||||
# Bazel-related
|
# Bazel-related
|
||||||
MODULE.bazel.lock
|
MODULE.bazel.lock
|
||||||
|
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ cc_library(
|
|||||||
"include/nlohmann/detail/input/parser.hpp",
|
"include/nlohmann/detail/input/parser.hpp",
|
||||||
"include/nlohmann/detail/input/position_t.hpp",
|
"include/nlohmann/detail/input/position_t.hpp",
|
||||||
"include/nlohmann/detail/input/pow5_table.hpp",
|
"include/nlohmann/detail/input/pow5_table.hpp",
|
||||||
"include/nlohmann/detail/input/simd_scan.hpp",
|
|
||||||
"include/nlohmann/detail/input/string_scan.hpp",
|
"include/nlohmann/detail/input/string_scan.hpp",
|
||||||
"include/nlohmann/detail/iterators/internal_iterator.hpp",
|
"include/nlohmann/detail/iterators/internal_iterator.hpp",
|
||||||
"include/nlohmann/detail/iterators/iter_impl.hpp",
|
"include/nlohmann/detail/iterators/iter_impl.hpp",
|
||||||
@@ -90,7 +89,6 @@ cc_library(
|
|||||||
"include/nlohmann/detail/view/simd.hpp",
|
"include/nlohmann/detail/view/simd.hpp",
|
||||||
"include/nlohmann/detail/view/string_ref.hpp",
|
"include/nlohmann/detail/view/string_ref.hpp",
|
||||||
"include/nlohmann/detail/view/value.hpp",
|
"include/nlohmann/detail/view/value.hpp",
|
||||||
"include/nlohmann/detail/view/view_mode.hpp",
|
|
||||||
"include/nlohmann/json.hpp",
|
"include/nlohmann/json.hpp",
|
||||||
"include/nlohmann/json_fwd.hpp",
|
"include/nlohmann/json_fwd.hpp",
|
||||||
"include/nlohmann/json_literals.hpp",
|
"include/nlohmann/json_literals.hpp",
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ This file describes the source for supporting files; that is, files that are not
|
|||||||
- [GitHub](#github)
|
- [GitHub](#github)
|
||||||
- [REUSE](#reuse)
|
- [REUSE](#reuse)
|
||||||
- [Package Managers](#package-managers)
|
- [Package Managers](#package-managers)
|
||||||
- [Debugging](#debugging)
|
|
||||||
|
|
||||||
## Continuous Integration
|
## Continuous Integration
|
||||||
|
|
||||||
@@ -290,29 +289,3 @@ The module definition for [Bazel](https://bazel.build)'s [Bzlmod](https://bazel.
|
|||||||
Further documentation:
|
Further documentation:
|
||||||
|
|
||||||
- [Bazel modules](https://bazel.build/external/module)
|
- [Bazel modules](https://bazel.build/external/module)
|
||||||
|
|
||||||
## Debugging
|
|
||||||
|
|
||||||
### `nlohmann_json.natvis`
|
|
||||||
|
|
||||||
The [Natvis](https://learn.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects) file that
|
|
||||||
gives `json` values a friendly view in the Visual Studio debugger. The file is generated by
|
|
||||||
`tools/generate_natvis/generate_natvis.py` from the template `tools/generate_natvis/nlohmann_json.natvis.j2` and the ABI
|
|
||||||
tags and version in `include/nlohmann/detail/abi_macros.hpp`; change the script or the template rather than editing
|
|
||||||
the file by hand. The file can be updated by calling
|
|
||||||
|
|
||||||
```shell
|
|
||||||
make natvis
|
|
||||||
```
|
|
||||||
|
|
||||||
The "Check amalgamation" workflow fails if the file is out of date.
|
|
||||||
|
|
||||||
The file covers the namespace variants with up to two ABI tags. A Natvis file for other configurations can be
|
|
||||||
generated with the `--abi-tags` or `--all` options, for example
|
|
||||||
`make natvis NATVIS_ARGS="--abi-tags _diag,_dp,_ns --output my_nlohmann_json.natvis"`; see
|
|
||||||
[`tools/generate_natvis`](tools/generate_natvis/README.md). With MSVC, CMake adds the file to the `nlohmann_json`
|
|
||||||
target and installs it.
|
|
||||||
|
|
||||||
Further documentation:
|
|
||||||
|
|
||||||
- [Create custom views of C++ objects in the debugger using the Natvis framework](https://learn.microsoft.com/en-us/visualstudio/debugger/create-custom-views-of-native-objects)
|
|
||||||
@@ -46,7 +46,7 @@ all:
|
|||||||
@echo "clean - remove built files"
|
@echo "clean - remove built files"
|
||||||
@echo "(fuzzers) - see tests/fuzzing.md, or run 'make -C tests fuzzers' to build the AFL fuzzers"
|
@echo "(fuzzers) - see tests/fuzzing.md, or run 'make -C tests fuzzers' to build the AFL fuzzers"
|
||||||
@echo "macro_builder_check - check that macro_scope.hpp matches tools/macro_builder's output"
|
@echo "macro_builder_check - check that macro_scope.hpp matches tools/macro_builder's output"
|
||||||
@echo "natvis - regenerate nlohmann_json.natvis from the current ABI tags and version (pass generator options via NATVIS_ARGS, see tools/generate_natvis/README.md)"
|
@echo "natvis - regenerate nlohmann_json.natvis from the current ABI tags and version"
|
||||||
@echo "pretty - beautify code with Artistic Style"
|
@echo "pretty - beautify code with Artistic Style"
|
||||||
@echo "pvs_studio - run the PVS-Studio static analyzer and open the report"
|
@echo "pvs_studio - run the PVS-Studio static analyzer and open the report"
|
||||||
@echo "release - create the release files (include.zip, json.tar.xz, single headers, signatures, hashes)"
|
@echo "release - create the release files (include.zip, json.tar.xz, single headers, signatures, hashes)"
|
||||||
@@ -120,15 +120,13 @@ $(AMALGAMATED_VIEW_FILE): $(SRCS)
|
|||||||
tools/amalgamate/amalgamate.py -c tools/amalgamate/config_json_view.json -s . --verbose=yes
|
tools/amalgamate/amalgamate.py -c tools/amalgamate/config_json_view.json -s . --verbose=yes
|
||||||
|
|
||||||
NATVIS_VENV=tools/generate_natvis/venv
|
NATVIS_VENV=tools/generate_natvis/venv
|
||||||
# extra options for generate_natvis.py, e.g. NATVIS_ARGS="--abi-tags _diag,_dp,_ns --output my.natvis"
|
|
||||||
NATVIS_ARGS=
|
|
||||||
|
|
||||||
# regenerate nlohmann_json.natvis from the ABI tags and version in include/nlohmann/detail/abi_macros.hpp
|
# regenerate nlohmann_json.natvis from the ABI tags and version in include/nlohmann/detail/abi_macros.hpp
|
||||||
# (the dependencies are installed into a virtual environment first)
|
# (the dependencies are installed into a virtual environment first)
|
||||||
natvis:
|
natvis:
|
||||||
@test -d $(NATVIS_VENV) || python3 -mvenv $(NATVIS_VENV)
|
@test -d $(NATVIS_VENV) || python3 -mvenv $(NATVIS_VENV)
|
||||||
@$(NATVIS_VENV)/bin/python3 -c 'import jinja2' 2>/dev/null || $(NATVIS_VENV)/bin/pip3 install --quiet -r tools/generate_natvis/requirements.txt
|
@$(NATVIS_VENV)/bin/python3 -c 'import jinja2' 2>/dev/null || $(NATVIS_VENV)/bin/pip3 install --quiet -r tools/generate_natvis/requirements.txt
|
||||||
$(NATVIS_VENV)/bin/python3 tools/generate_natvis/generate_natvis.py $(NATVIS_ARGS) .
|
$(NATVIS_VENV)/bin/python3 tools/generate_natvis/generate_natvis.py .
|
||||||
|
|
||||||
# regenerate the two tools/macro_builder blocks of $(MACRO_SCOPE_HPP) (see its README.md) and diff against the
|
# regenerate the two tools/macro_builder blocks of $(MACRO_SCOPE_HPP) (see its README.md) and diff against the
|
||||||
# checked-in header; phony, because it never writes $(MACRO_SCOPE_HPP) itself
|
# checked-in header; phony, because it never writes $(MACRO_SCOPE_HPP) itself
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ Thanks everyone!
|
|||||||
|
|
||||||
:books: If you want to **learn more** about how to use the library, check out the rest of the [**README**](#examples), have a look at [**code examples**](https://github.com/nlohmann/json/tree/develop/docs/mkdocs/docs/examples), or browse through the [**help pages**](https://json.nlohmann.me).
|
:books: If you want to **learn more** about how to use the library, check out the rest of the [**README**](#examples), have a look at [**code examples**](https://github.com/nlohmann/json/tree/develop/docs/mkdocs/docs/examples), or browse through the [**help pages**](https://json.nlohmann.me).
|
||||||
|
|
||||||
:construction: If you want to understand the **API** better, check out the [**API Reference**](https://json.nlohmann.me/api/basic_json/) or have a look at the [quick reference](#quick-reference) below.
|
:construction: If you want to understand the **API** better, check out the [**API Reference**](https://json.nlohmann.me/api/basic_json/) or have a look at the [quick reference](#quick-reference) below. The public API surface is derived mechanically and checked for documentation coverage by the tooling in [`tools/api_checker/`](tools/api_checker/), whose [POLICY.md](tools/api_checker/POLICY.md) defines what counts as public API and what stability is guaranteed.
|
||||||
|
|
||||||
:bug: If you found a **bug**, please check the [**FAQ**](https://json.nlohmann.me/home/faq/) if it is a known issue or the result of a design decision. Please also have a look at the [**issue list**](https://github.com/nlohmann/json/issues) before you [**create a new issue**](https://github.com/nlohmann/json/issues/new/choose). Please provide as much information as possible to help us understand and reproduce your issue.
|
:bug: If you found a **bug**, please check the [**FAQ**](https://json.nlohmann.me/home/faq/) if it is a known issue or the result of a design decision. Please also have a look at the [**issue list**](https://github.com/nlohmann/json/issues) before you [**create a new issue**](https://github.com/nlohmann/json/issues/new/choose). Please provide as much information as possible to help us understand and reproduce your issue.
|
||||||
|
|
||||||
@@ -151,7 +151,7 @@ The `json` class provides an API for manipulating a JSON value. To create a `jso
|
|||||||
```cpp
|
```cpp
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
using nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
@@ -164,7 +164,7 @@ If using modules (enabled with `NLOHMANN_JSON_BUILD_MODULES`), this example beco
|
|||||||
import std;
|
import std;
|
||||||
import nlohmann.json;
|
import nlohmann.json;
|
||||||
|
|
||||||
using nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
@@ -842,7 +842,7 @@ assert(p == p2);
|
|||||||
To make this work with one of your types, you only need to provide two functions:
|
To make this work with one of your types, you only need to provide two functions:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
namespace ns {
|
namespace ns {
|
||||||
void to_json(json& j, const person& p) {
|
void to_json(json& j, const person& p) {
|
||||||
@@ -1257,7 +1257,7 @@ See the page [quality assurance](https://json.nlohmann.me/community/quality_assu
|
|||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
// for convenience
|
// for convenience
|
||||||
using nlohmann::json;
|
using json = nlohmann::json;
|
||||||
```
|
```
|
||||||
|
|
||||||
to the files you want to process JSON and set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang).
|
to the files you want to process JSON and set the necessary switches to enable C++11 (e.g., `-std=c++11` for GCC and Clang).
|
||||||
|
|||||||
@@ -341,14 +341,13 @@ def download(url, docs) -> str:
|
|||||||
raise ValueError(f'not an http(s) URL: {url}')
|
raise ValueError(f'not an http(s) URL: {url}')
|
||||||
req = urllib.request.Request(u.geturl(), headers={'User-Agent': USER_AGENT})
|
req = urllib.request.Request(u.geturl(), headers={'User-Agent': USER_AGENT})
|
||||||
# (the scheme is checked above)
|
# (the scheme is checked above)
|
||||||
# nosemgrep: python.lang.security.audit.dynamic-urllib-use-detected.dynamic-urllib-use-detected
|
|
||||||
with urllib.request.urlopen(req, timeout=20) as r: # nosec B310
|
with urllib.request.urlopen(req, timeout=20) as r: # nosec B310
|
||||||
data = r.read()
|
data = r.read()
|
||||||
ctype = r.headers.get_content_type()
|
ctype = r.headers.get_content_type()
|
||||||
path = urllib.parse.unquote(u.path).lstrip('/')
|
path = urllib.parse.unquote(u.path).lstrip('/')
|
||||||
ext = os.path.splitext(path)[1]
|
ext = os.path.splitext(path)[1]
|
||||||
if u.query or not ext or path.endswith('/'):
|
if u.query or not ext or path.endswith('/'):
|
||||||
digest = hashlib.sha256(url.encode()).hexdigest()[:12]
|
digest = hashlib.sha1(url.encode(), usedforsecurity=False).hexdigest()[:12]
|
||||||
path = os.path.join(os.path.dirname(path), digest + CONTENT_TYPE_EXT.get(ctype, ext or '.bin'))
|
path = os.path.join(os.path.dirname(path), digest + CONTENT_TYPE_EXT.get(ctype, ext or '.bin'))
|
||||||
rel = os.path.normpath(os.path.join('assets', 'external', u.hostname, path))
|
rel = os.path.normpath(os.path.join('assets', 'external', u.hostname, path))
|
||||||
out = os.path.join(docs, rel)
|
out = os.path.join(docs, rel)
|
||||||
|
|||||||
@@ -30,14 +30,6 @@ struct adl_serializer {
|
|||||||
- [**from_json**](from_json.md) - convert a JSON value to any value type
|
- [**from_json**](from_json.md) - convert a JSON value to any value type
|
||||||
- [**to_json**](to_json.md) - convert any value type to a JSON value
|
- [**to_json**](to_json.md) - convert any value type to a JSON value
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
- [Arbitrary type conversions](../../features/arbitrary_types.md) - how to make your types convertible
|
|
||||||
- [**get**](../basic_json/get.md) / [**get_to**](../basic_json/get_to.md) - convert a JSON value to a C++ type
|
|
||||||
- [**basic_json**](../basic_json/basic_json.md) - construct a JSON value from a C++ value
|
|
||||||
- [**NLOHMANN_DEFINE_TYPE_INTRUSIVE**](../macros/nlohmann_define_type_intrusive.md) - generate the conversion functions for a type
|
|
||||||
- [Specializing enum conversion](../../features/enum_conversion.md)
|
|
||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 2.1.0.
|
- Added in version 2.1.0.
|
||||||
@@ -23,9 +23,6 @@ Checks whether the input is valid JSON.
|
|||||||
respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to
|
respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to
|
||||||
the iterator type with `operator!=`.
|
the iterator type with `operator!=`.
|
||||||
|
|
||||||
Ill-formed UTF-16 and UTF-32 sequences are rejected: a lone surrogate, a high surrogate that is not followed by its
|
|
||||||
low surrogate, and a code point above `U+10FFFF` all make this function return `false`.
|
|
||||||
|
|
||||||
Unlike the [`parse()`](parse.md) function, this function neither throws an exception in case of invalid JSON input
|
Unlike the [`parse()`](parse.md) function, this function neither throws an exception in case of invalid JSON input
|
||||||
(i.e., a parse error) nor creates diagnostic information.
|
(i.e., a parse error) nor creates diagnostic information.
|
||||||
|
|
||||||
@@ -38,12 +35,9 @@ Unlike the [`parse()`](parse.md) function, this function neither throws an excep
|
|||||||
- a `#!c FILE` pointer (throws if null)
|
- a `#!c FILE` pointer (throws if null)
|
||||||
- a C-style array of characters
|
- a C-style array of characters
|
||||||
- a pointer to a null-terminated string of single byte characters (throws if null)
|
- a pointer to a null-terminated string of single byte characters (throws if null)
|
||||||
- a `std::string` or a `std::string_view` (C++17)
|
- a `std::string`
|
||||||
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
|
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
|
||||||
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
|
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
|
||||||
- wide characters (`wchar_t`, `char16_t`, `char32_t`), which are converted to UTF-8: a wide string object
|
|
||||||
(`std::wstring`, `std::u16string`, `std::u32string`) or an array (e.g. a wide string literal); a pointer to a
|
|
||||||
null-terminated wide string (e.g. `std::u16string::c_str()`) is not supported
|
|
||||||
|
|
||||||
`IteratorType`
|
`IteratorType`
|
||||||
: a compatible iterator type, for instance.
|
: a compatible iterator type, for instance.
|
||||||
@@ -131,21 +125,6 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by
|
|||||||
--8<-- "examples/accept__iterator_pair.output"
|
--8<-- "examples/accept__iterator_pair.output"
|
||||||
```
|
```
|
||||||
|
|
||||||
??? example "Example: (3) reading from UTF-16 strings"
|
|
||||||
|
|
||||||
The encoding is deduced from the character type of the input. String literals like `u"..."`, `U"..."`, and
|
|
||||||
`L"..."` work as well.
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
--8<-- "examples/accept__wide_string.cpp"
|
|
||||||
```
|
|
||||||
|
|
||||||
Output:
|
|
||||||
|
|
||||||
```json
|
|
||||||
--8<-- "examples/accept__wide_string.output"
|
|
||||||
```
|
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
|
|
||||||
- [parse](parse.md) - deserialize from a compatible input
|
- [parse](parse.md) - deserialize from a compatible input
|
||||||
@@ -172,9 +151,6 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by
|
|||||||
([`JSON_DISABLE_STREAM_BULK_SCAN`](../macros/json_disable_stream_bulk_scan.md)).
|
([`JSON_DISABLE_STREAM_BULK_SCAN`](../macros/json_disable_stream_bulk_scan.md)).
|
||||||
- Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0
|
- Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0
|
||||||
([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md)).
|
([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md)).
|
||||||
- Ill-formed UTF-16 and UTF-32 input (a lone surrogate, a high surrogate that is not followed by its low surrogate, or
|
|
||||||
a code point above `U+10FFFF`) is rejected in version 3.13.0; before that, some such sequences were accepted or
|
|
||||||
ended the input silently.
|
|
||||||
|
|
||||||
!!! warning "Deprecation"
|
!!! warning "Deprecation"
|
||||||
|
|
||||||
|
|||||||
@@ -239,8 +239,9 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
|
|
||||||
1. Added in version 1.0.0.
|
1. Added in version 1.0.0.
|
||||||
2. Added in version 1.0.0.
|
2. Added in version 1.0.0.
|
||||||
3. Added in version 3.11.0. Support for keys convertible to `std::string_view` added in version 3.13.0, consistently
|
3. Added in version 3.11.0. Fixed in version 3.13.0 to consistently accept `std::string_view`-convertible keys, as
|
||||||
across the lookup functions.
|
already supported by [`operator[]`](operator[].md), [`value`](value.md), [`find`](find.md), and other lookup
|
||||||
|
functions.
|
||||||
4. Added in version 2.0.0. Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of
|
4. Added in version 2.0.0. Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of
|
||||||
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) for a one-character array index that is not
|
[`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.
|
a digit (e.g., `/x`) in version 3.13.0, as it already did for longer ones.
|
||||||
@@ -63,4 +63,4 @@ Constant.
|
|||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 1.0.0.
|
- Added in version 1.0.0.
|
||||||
- Binary values are supported since version 3.8.0.
|
- Adjusted code to return reference to binary values in version 3.8.0.
|
||||||
@@ -482,15 +482,17 @@ basic_json(basic_json&& other) noexcept;
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
1. Added in version 1.0.0.
|
1. Since version 1.0.0.
|
||||||
2. Added in version 1.0.0.
|
2. Since version 1.0.0.
|
||||||
3. Added in version 2.1.0.
|
3. Since version 2.1.0.
|
||||||
4. Added in version 3.1.2. Explicit for different string types if `JSON_USE_IMPLICIT_CONVERSIONS` is `0` since
|
4. Since version 3.2.0. Also initializes the position reported by
|
||||||
version 3.13.0.
|
[`start_pos()`](start_pos.md)/[`end_pos()`](end_pos.md) from `val` when
|
||||||
5. Added in version 1.0.0.
|
[`JSON_DIAGNOSTIC_POSITIONS`](../macros/json_diagnostic_positions.md) is enabled, since version 3.12.0. Explicit
|
||||||
6. Added in version 1.0.0.
|
for different string types if `JSON_USE_IMPLICIT_CONVERSIONS` is `0` since version 3.13.0.
|
||||||
7. Added in version 1.0.0. Fixed in version 3.13.0 to also check the iterator range for binary values; before, a range
|
5. Since version 1.0.0.
|
||||||
|
6. Since version 1.0.0.
|
||||||
|
7. Since version 1.0.0. Fixed in version 3.13.0 to also check the iterator range for binary values; before, a range
|
||||||
that did not cover the whole value (such as `(end(), end())`) was accepted and the whole binary value was copied,
|
that did not cover the whole value (such as `(end(), end())`) was accepted and the whole binary value was copied,
|
||||||
unlike the other primitive types.
|
unlike the other primitive types.
|
||||||
8. Added in version 1.0.0.
|
8. Since version 1.0.0.
|
||||||
9. Added in version 1.0.0.
|
9. Since version 1.0.0.
|
||||||
@@ -90,9 +90,8 @@ type `#!cpp binary_t*` must be dereferenced.
|
|||||||
|
|
||||||
- MessagePack
|
- MessagePack
|
||||||
- If a subtype is given and the binary array contains exactly 1, 2, 4, 8, or 16 elements, the fixext family (fixext1,
|
- If a subtype is given and the binary array contains exactly 1, 2, 4, 8, or 16 elements, the fixext family (fixext1,
|
||||||
fixext2, fixext4, fixext8, fixext16) is used. For other sizes, the ext family (ext8, ext16, ext32) is used. The
|
fixext2, fixext4, fixext8) is used. For other sizes, the ext family (ext8, ext16, ext32) is used. The subtype is
|
||||||
subtype is then written as the signed 8-bit ext type: subtypes 128 to 255 map to the negative (reserved) ext
|
then added as a signed 8-bit integer.
|
||||||
types, and on reading the ext type is exposed as an unsigned subtype from 0 to 255.
|
|
||||||
- If no subtype is given, the bin family (bin8, bin16, bin32) is used.
|
- If no subtype is given, the bin family (bin8, bin16, bin32) is used.
|
||||||
|
|
||||||
- BSON
|
- BSON
|
||||||
|
|||||||
@@ -4,29 +4,24 @@
|
|||||||
enum class bjdata_version_t
|
enum class bjdata_version_t
|
||||||
{
|
{
|
||||||
draft2,
|
draft2,
|
||||||
draft3
|
draft3,
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
This enumeration is used in [`to_bjdata`](to_bjdata.md) to choose the version of the BJData specification to write:
|
This enumeration is used in the [`to_bjdata`](to_bjdata.md) function to choose which draft version of
|
||||||
|
the BJData specification to encode ND-array extensions for:
|
||||||
|
|
||||||
draft2
|
draft2
|
||||||
: BJData Draft 2 (the default). Binary values are written as arrays of `U` integers, and ND-arrays with
|
: encode using the BJData Draft 2 ND-array format
|
||||||
`"_ArrayType_": "byte"` are written as regular objects, since the `B` marker is not part of Draft 2.
|
|
||||||
|
|
||||||
draft3
|
draft3
|
||||||
: BJData Draft 3. Binary values are written as `B` elements: as an optimized binary array (`[$B#`) if both `use_size`
|
: encode using the BJData Draft 3 ND-array format
|
||||||
and `use_type` are set, and as an array of `B`-prefixed elements otherwise. ND-arrays with
|
|
||||||
`"_ArrayType_": "byte"` are written as packed arrays (`[$B#[...]`).
|
|
||||||
|
|
||||||
Only the writer is affected by the version. [`from_bjdata`](from_bjdata.md) reads the markers of both drafts.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
??? example
|
??? example
|
||||||
|
|
||||||
The example below shows how the different values of the `bjdata_version_t` influence the serialization of a binary
|
The example shows how `bjdata_version_t` selects the BJData draft used by `to_bjdata`.
|
||||||
value with [`to_bjdata`](to_bjdata.md).
|
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
--8<-- "examples/bjdata_version_t.cpp"
|
--8<-- "examples/bjdata_version_t.cpp"
|
||||||
@@ -34,15 +29,10 @@ Only the writer is affected by the version. [`from_bjdata`](from_bjdata.md) read
|
|||||||
|
|
||||||
Output:
|
Output:
|
||||||
|
|
||||||
```json
|
```
|
||||||
--8<-- "examples/bjdata_version_t.output"
|
--8<-- "examples/bjdata_version_t.output"
|
||||||
```
|
```
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
- [to_bjdata](to_bjdata.md) serializes a JSON value to BJData
|
|
||||||
- [BJData](../../features/binary_formats/bjdata.md) - the article on the BJData format
|
|
||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.12.0.
|
- Added in version 3.12.0.
|
||||||
@@ -15,13 +15,11 @@ error
|
|||||||
: report a parse error in case of a tag (the `from_cbor` overloads throw a `parse_error` exception by default)
|
: report a parse error in case of a tag (the `from_cbor` overloads throw a `parse_error` exception by default)
|
||||||
|
|
||||||
ignore
|
ignore
|
||||||
: ignore tags (also in front of a text string map key)
|
: ignore tags
|
||||||
|
|
||||||
store
|
store
|
||||||
: store tagged byte strings (for bytes 0xd8..0xdb) as binary values with the tag as subtype; other tagged values are
|
: store tagged byte strings (for bytes 0xd8..0xdb) as binary values with the tag as subtype; other tagged values are
|
||||||
read as if the tag were ignored. If several tags precede a byte string, only the tag directly in front of it is
|
read as if the tag were ignored. If several tags precede a byte string, only the innermost one is stored.
|
||||||
stored, and only when it has the form 0xd8..0xdb (a tag 0..23 written as 0xc0..0xd7 is skipped). Tags in front of a
|
|
||||||
text string map key are skipped.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|||||||
@@ -61,18 +61,11 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
1. Logarithmic in the size of the JSON object.
|
Logarithmic in the size of the JSON object.
|
||||||
2. Logarithmic in the size of the JSON object.
|
|
||||||
3. At most one lookup per reference token of `ptr`, each logarithmic in the size of the container at that level:
|
|
||||||
O(*n* log *m*) for *n* reference tokens and containers of size at most *m*.
|
|
||||||
4. Deleted; a call with an integral argument does not compile.
|
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
- Overloads (1) and (2) always return `#!cpp false` when executed on a JSON type that is not an object. Overload (3)
|
- This method always returns `#!cpp false` when executed on a JSON type that is not an object.
|
||||||
also works on arrays and on primitive values: the empty JSON pointer resolves in every JSON value (so
|
|
||||||
`#!cpp json(1).contains(""_json_pointer)` returns `#!cpp true`), and `#!cpp json({1, 2}).contains("/1"_json_pointer)`
|
|
||||||
returns `#!cpp true`.
|
|
||||||
- This method can be executed on any JSON value type.
|
- This method can be executed on any JSON value type.
|
||||||
- Calling this function with an integer argument (for example, `#!cpp contains(0)`) does not compile: such an argument
|
- Calling this function with an integer argument (for example, `#!cpp contains(0)`) does not compile: such an argument
|
||||||
would otherwise implicitly convert to a null `#!cpp const char*` and, from there, cause undefined behavior when
|
would otherwise implicitly convert to a null `#!cpp const char*` and, from there, cause undefined behavior when
|
||||||
@@ -148,8 +141,9 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
1. Added in version 3.11.0.
|
1. Added in version 3.11.0.
|
||||||
2. Added in version 3.6.0. Extended template `KeyType` to support comparable types in version 3.11.0. Support for keys
|
2. Added in version 3.6.0. Extended template `KeyType` to support comparable types in version 3.11.0. Fixed in
|
||||||
convertible to `std::string_view` added in version 3.13.0, consistently across the lookup functions.
|
version 3.13.0 to consistently accept `std::string_view`-convertible keys, as already supported by
|
||||||
|
[`operator[]`](operator[].md), [`at`](at.md), [`value`](value.md), and other lookup functions.
|
||||||
3. Added in version 3.7.0.
|
3. Added in version 3.7.0.
|
||||||
4. Deleted overloads for integral key types added in version 3.13.0 to reject such calls at compile time instead of
|
4. Deleted overloads for integral key types added in version 3.13.0 to reject such calls at compile time instead of
|
||||||
causing undefined behavior at runtime.
|
causing undefined behavior at runtime.
|
||||||
@@ -92,7 +92,8 @@ Logarithmic in the size of the JSON object.
|
|||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
1. Added in version 3.11.0.
|
1. Added in version 3.11.0.
|
||||||
2. Added in version 1.0.0. Changed to support comparable key types in version 3.11.0. Support for keys convertible to
|
2. Added in version 1.0.0. Changed parameter `key` type to `KeyType&&` in version 3.11.0. Fixed in version 3.13.0 to
|
||||||
`std::string_view` added in version 3.13.0, consistently across the lookup functions.
|
consistently accept `std::string_view`-convertible keys, as already supported by [`operator[]`](operator[].md),
|
||||||
|
[`at`](at.md), [`value`](value.md), and other lookup functions.
|
||||||
3. Deleted overload for integral key types added in version 3.13.0 to reject such calls at compile time instead of
|
3. Deleted overload for integral key types added in version 3.13.0 to reject such calls at compile time instead of
|
||||||
causing undefined behavior at runtime.
|
causing undefined behavior at runtime.
|
||||||
@@ -2,27 +2,17 @@
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
static basic_json diff(const basic_json& source,
|
static basic_json diff(const basic_json& source,
|
||||||
const basic_json& target,
|
const basic_json& target);
|
||||||
const string_t& path = "");
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Creates a [JSON Patch](http://jsonpatch.com) so that value `source` can be changed into the value `target` by calling
|
Creates a [JSON Patch](http://jsonpatch.com) so that value `source` can be changed into the value `target` by calling
|
||||||
[`patch`](patch.md) function.
|
[`patch`](patch.md) function.
|
||||||
|
|
||||||
For two JSON values `source` and `target`, the following code yields `#!cpp true`:
|
For two JSON values `source` and `target`, the following code always yields `#!cpp true`:
|
||||||
```cpp
|
```cpp
|
||||||
source.patch(diff(source, target)) == target;
|
source.patch(diff(source, target)) == target;
|
||||||
```
|
```
|
||||||
|
|
||||||
The equality holds up to [`operator==`](operator_eq.md), so it has the following exceptions:
|
|
||||||
|
|
||||||
- A value that is not equal to itself cannot round-trip. This applies to `NaN` (also nested in an array or object) and
|
|
||||||
to [discarded](is_discarded.md) values: `diff` emits a `replace` operation for them. A `NaN` in the patch is
|
|
||||||
serialized as `#!json null`, so a patch that was dumped and parsed again would set `#!json null`.
|
|
||||||
- Numbers that compare equal produce no operation, even if their types or signs differ: `diff(1, 1.0)`,
|
|
||||||
`diff(1u, 1)` (unsigned and signed integer) and `diff(0.0, -0.0)` all return `#!json []`. The patched value is then
|
|
||||||
the `source` and keeps its number type.
|
|
||||||
|
|
||||||
## Parameters
|
## Parameters
|
||||||
|
|
||||||
`source` (in)
|
`source` (in)
|
||||||
@@ -31,12 +21,6 @@ The equality holds up to [`operator==`](operator_eq.md), so it has the following
|
|||||||
`target` (in)
|
`target` (in)
|
||||||
: JSON value to compare against
|
: JSON value to compare against
|
||||||
|
|
||||||
`path` (in)
|
|
||||||
: a prefix that is put verbatim in front of the `path` member of every operation (default: `""`, which addresses the
|
|
||||||
root of the value). The prefix is neither escaped nor validated: `#!cpp diff(1, 2, "abc")` yields an operation with
|
|
||||||
the `path` `abc`, which is not a valid JSON Pointer. Use it with a valid JSON Pointer such as `/root` and escape
|
|
||||||
`~` and `/` in its reference tokens (see [JSON Pointer](../../features/json_pointer.md)).
|
|
||||||
|
|
||||||
## Return value
|
## Return value
|
||||||
|
|
||||||
a JSON patch to convert the `source` to `target`
|
a JSON patch to convert the `source` to `target`
|
||||||
@@ -47,32 +31,11 @@ Strong guarantee: `source` and `target` are never modified.
|
|||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
Linear in the total number of values (including nested ones) in `source` and `target`, times the cost of a key-based
|
Linear in the lengths of `source` and `target`.
|
||||||
object operation (looking up a key in an object): logarithmic for [`json`](../json.md), and linear for
|
|
||||||
[`ordered_json`](../ordered_json.md) (see [`ordered_map` complexity](../ordered_map.md#complexity)). For
|
|
||||||
`ordered_json`, diffing two objects with `n` and `m` members therefore takes O((n+m)²) when their keys do not line up
|
|
||||||
(e.g., a key was added or the keys are in a different order). The size of the patch is proportional to the number of
|
|
||||||
operations times the length of their paths, so a small change deep inside a value yields a long `path`.
|
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
- Currently, only `remove`, `add`, and `replace` operations are generated.
|
Currently, only `remove`, `add`, and `replace` operations are generated.
|
||||||
- The patch is correct, but not guaranteed to be minimal. For instance, `diff([1,2,3], [0,1,2,3])` yields three
|
|
||||||
`replace` operations and one `add` operation instead of a single insertion.
|
|
||||||
- Differences between two arrays are reported index by index. Elements that were added at the end of the array are added
|
|
||||||
with `add` operations on the path `/-` (appended in order), and elements that were removed are removed with `remove`
|
|
||||||
operations from the highest index down to the lowest, so that the remaining indices stay valid.
|
|
||||||
- For objects, the operations for the members of `source` come first (`replace`, `remove`, or the operations for nested
|
|
||||||
values, in the iteration order of `source`), followed by `add` operations for the members that only `target` has.
|
|
||||||
- For [`ordered_json`](../ordered_json.md), objects with the same keys in a different order are different. As a patch
|
|
||||||
cannot reorder members, `diff` removes all keys of the `source` object and adds them again in the order of the
|
|
||||||
`target`.
|
|
||||||
- If `source` and `target` differ as a whole (e.g., different types), the patch consists of a single `replace`
|
|
||||||
operation for the root of the value, i.e., with the `path` `""` (or the given `path` prefix).
|
|
||||||
- The `value` members of the patch hold copies of values of `target`. A [binary](../../features/binary_values.md) value
|
|
||||||
has no JSON representation: when a patch that contains one is serialized, it becomes an object such as
|
|
||||||
`#!json {"bytes":[1,3],"subtype":null}`, so a parsed patch no longer holds a binary value (and would set that object
|
|
||||||
instead).
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|||||||
@@ -69,6 +69,6 @@ Logarithmic in the size of the container, O(log(`size()`)).
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 2.0.8.
|
- Since version 2.0.8.
|
||||||
- Fixed in version 3.13.0: for [`ordered_json`](../ordered_json.md), the value could previously only be passed as an
|
- Fixed in version 3.13.0: for [`ordered_json`](../ordered_json.md), the value could previously only be passed as an
|
||||||
rvalue; it can now also be passed as an lvalue or a `#!cpp const` lvalue, matching the behavior of `json`.
|
rvalue; it can now also be passed as an lvalue or a `#!cpp const` lvalue, matching the behavior of `json`.
|
||||||
@@ -67,5 +67,5 @@ Amortized constant.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 2.0.8.
|
- Since version 2.0.8.
|
||||||
- Returns reference since 3.7.0.
|
- Returns reference since 3.7.0.
|
||||||
@@ -213,6 +213,7 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
1. Added in version 1.0.0. Added support for binary types in version 3.8.0.
|
1. Added in version 1.0.0. Added support for binary types in version 3.8.0.
|
||||||
2. Added in version 1.0.0. Added support for binary types in version 3.8.0.
|
2. Added in version 1.0.0. Added support for binary types in version 3.8.0.
|
||||||
3. Added in version 1.0.0.
|
3. Added in version 1.0.0.
|
||||||
4. Added in version 3.11.0. Support for keys convertible to `std::string_view` added in version 3.13.0, consistently
|
4. Added in version 3.11.0. Fixed in version 3.13.0 to consistently accept `std::string_view`-convertible keys, as
|
||||||
across the lookup functions.
|
already supported by [`operator[]`](operator[].md), [`at`](at.md), [`value`](value.md), and other lookup
|
||||||
|
functions.
|
||||||
5. Added in version 1.0.0.
|
5. Added in version 1.0.0.
|
||||||
@@ -84,4 +84,4 @@ constructor.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.0.0.
|
- Since version 3.0.0.
|
||||||
@@ -99,7 +99,8 @@ Logarithmic in the size of the JSON object.
|
|||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
1. Added in version 3.11.0.
|
1. Added in version 3.11.0.
|
||||||
2. Added in version 1.0.0. Changed to support comparable types in version 3.11.0. Support for keys convertible to
|
2. Added in version 1.0.0. Changed to support comparable types in version 3.11.0. Fixed in version 3.13.0 to
|
||||||
`std::string_view` added in version 3.13.0, consistently across the lookup functions.
|
consistently accept `std::string_view`-convertible keys, as already supported by [`operator[]`](operator[].md),
|
||||||
|
[`at`](at.md), [`value`](value.md), and other lookup functions.
|
||||||
3. Deleted overloads for integral key types added in version 3.13.0 to reject such calls at compile time instead of
|
3. Deleted overloads for integral key types added in version 3.13.0 to reject such calls at compile time instead of
|
||||||
causing undefined behavior at runtime.
|
causing undefined behavior at runtime.
|
||||||
@@ -82,11 +82,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs
|
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs
|
||||||
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
|
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
|
||||||
successfully, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict`
|
successfully, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict`
|
||||||
- Throws [parse_error.115](../../home/exceptions.md#jsonexceptionparse_error115) if the text of a high-precision
|
|
||||||
number (`H`) is not a valid number
|
|
||||||
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the size of an optimized container
|
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the size of an optimized container
|
||||||
cannot be represented by `std::size_t` or exceeds the container's `max_size()`, or if the product of the dimensions of
|
or n-dimensional array cannot be represented by `std::size_t`
|
||||||
an n-dimensional array overflows
|
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
|
|||||||
@@ -74,11 +74,6 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
- 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
|
||||||
|
|
||||||
The UTF-8 validity of strings is always checked, independent of
|
|
||||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md), because the end of a BON8 string is determined from
|
|
||||||
its encoding. Duplicate object keys are accepted; the last value wins (see
|
|
||||||
[BON8](../../features/binary_formats/bon8.md)).
|
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
Linear in the size of the input.
|
Linear in the size of the input.
|
||||||
|
|||||||
@@ -80,8 +80,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
- 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 or
|
||||||
the end of the input was not reached when `strict` was set to true
|
the end of the input was not reached when `strict` was set to true
|
||||||
- Throws [`parse_error.112`](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs (e.g., an
|
- Throws [`parse_error.112`](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs (e.g., an
|
||||||
invalid string or byte array length, an invalid inner length of a binary value with subtype `0x02`, or a document size
|
invalid string or byte array length)
|
||||||
that does not match the content; `strict` does not affect the document size checks)
|
|
||||||
- Throws [`parse_error.114`](../../home/exceptions.md#jsonexceptionparse_error114) if an unsupported BSON record type is
|
- Throws [`parse_error.114`](../../home/exceptions.md#jsonexceptionparse_error114) if an unsupported BSON record type is
|
||||||
encountered
|
encountered
|
||||||
- Throws [`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) if a string value or object key is
|
- Throws [`parse_error.113`](../../home/exceptions.md#jsonexceptionparse_error113) if a string value or object key is
|
||||||
|
|||||||
@@ -32,10 +32,8 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
|||||||
|
|
||||||
- an `std::istream` object
|
- an `std::istream` object
|
||||||
- a `FILE` pointer
|
- a `FILE` pointer
|
||||||
- a C-style array of characters (its terminating NUL is read as trailing data: `parse_error.110` unless `strict` is
|
- a C-style array of characters
|
||||||
`false`)
|
- a pointer to a null-terminated string of single byte characters
|
||||||
- a pointer to a null-terminated string of single byte characters (read up to the first 0x00 byte, which is a valid
|
|
||||||
CBOR byte)
|
|
||||||
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
|
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
|
||||||
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
|
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
|
||||||
|
|
||||||
@@ -91,14 +89,8 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if unsupported features from CBOR were
|
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if unsupported features from CBOR were
|
||||||
used in the given input or if the input is not valid CBOR
|
used in the given input or if the input is not valid CBOR
|
||||||
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a map key is not a string (keys of
|
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a map key is not a string (keys of
|
||||||
other types are not supported, as JSON object keys are always strings), if the head of a string or byte string is
|
other types are not supported, as JSON object keys are always strings), or if a string value or object key is not
|
||||||
malformed (including a chunk of the wrong type or an indefinite-length string nested inside an indefinite-length
|
valid UTF-8 and `error_handler` is `strict`
|
||||||
string), or if a string value or object key (or one chunk of an indefinite-length string) is not valid UTF-8 and
|
|
||||||
`error_handler` is `strict`
|
|
||||||
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if an array or map announces
|
|
||||||
a length that `std::size_t` cannot hold, or that equals `std::size_t(-1)` (for example `9B FF FF FF FF FF FF FF FF`)
|
|
||||||
- Throws [out_of_range.406](../../home/exceptions.md#jsonexceptionout_of_range406) if a floating-point number does not
|
|
||||||
fit into `number_float_t`
|
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
|
|||||||
@@ -79,18 +79,11 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
|
|
||||||
- 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 or
|
||||||
the end of the file was not reached when `strict` was set to true
|
the end of the file was not reached when `strict` was set to true
|
||||||
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if an invalid type marker is read
|
- Throws [parse_error.112](../../home/exceptions.md#jsonexceptionparse_error112) if a parse error occurs
|
||||||
(including a type marker after `$` of an optimized container, even if its count is 0), or if `$` is not followed by `#`
|
|
||||||
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
|
- Throws [parse_error.113](../../home/exceptions.md#jsonexceptionparse_error113) if a string could not be parsed
|
||||||
successfully (negative or non-integer length, `C` followed by a byte above 0x7F), if the count of an optimized
|
successfully, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict`
|
||||||
container is negative, or if a string value or object key is not valid UTF-8 and `error_handler` is `strict`
|
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the size of an optimized container
|
||||||
- Throws [parse_error.115](../../home/exceptions.md#jsonexceptionparse_error115) if the text of a high-precision number
|
or n-dimensional array cannot be represented by `std::size_t`
|
||||||
(`H`) is not a valid JSON number
|
|
||||||
- Throws [out_of_range.406](../../home/exceptions.md#jsonexceptionout_of_range406) if a high-precision number, or a
|
|
||||||
float64 when `number_float_t` is `float`, does not fit into `number_float_t`
|
|
||||||
- Throws [out_of_range.408](../../home/exceptions.md#jsonexceptionout_of_range408) if the count of an optimized
|
|
||||||
container exceeds the capacity of the container (or of `std::size_t`), or if an optimized array of `Z`, `T`, or `F` has
|
|
||||||
more than 1,048,576 elements
|
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
|
|||||||
@@ -57,4 +57,4 @@ Constant.
|
|||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 1.0.0.
|
- Added in version 1.0.0.
|
||||||
- Binary values are supported since version 3.8.0.
|
- Adjusted code to return reference to binary values in version 3.8.0.
|
||||||
@@ -188,6 +188,6 @@ overload (3).
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
1. Added in version 2.1.0.
|
1. Since version 2.1.0.
|
||||||
2. Added in version 2.1.0. Extended to work with other specializations of `basic_json` in version 3.1.2.
|
2. Since version 2.1.0. Extended to work with other specializations of `basic_json` in version 3.2.0.
|
||||||
3. Added in version 1.0.0.
|
3. Since version 1.0.0.
|
||||||
@@ -45,7 +45,7 @@ Constant.
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
using nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -76,5 +76,5 @@ Depends on the `json_serializer<ValueType>::from_json()` implementation.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.3.0.
|
- Since version 3.3.0.
|
||||||
- Added a `static_assert` with a clear message for `const` arguments in version 3.13.0.
|
- Added a `static_assert` with a clear message for `const` arguments in version 3.13.0.
|
||||||
@@ -44,22 +44,6 @@ are collected in [Template Parameter Requirements](../../features/types/template
|
|||||||
- [**json**](../json.md) - default specialization
|
- [**json**](../json.md) - default specialization
|
||||||
- [**ordered_json**](../ordered_json.md) - a specialization that maintains the insertion order of object keys
|
- [**ordered_json**](../ordered_json.md) - a specialization that maintains the insertion order of object keys
|
||||||
|
|
||||||
## Documents and views
|
|
||||||
|
|
||||||
Besides `basic_json`, the library offers a second way to hold a parsed JSON text: a
|
|
||||||
[`basic_json_document`](../basic_json_document/index.md), which owns or borrows the text and holds a flat index of the
|
|
||||||
values in it, and a [`basic_json_view`](../basic_json_view/index.md), a read-only handle to one value of such a document.
|
|
||||||
Both are defined in the header `<nlohmann/json_view.hpp>`; see
|
|
||||||
[Zero-copy JSON views](../../features/json_view.md) for the concept and the trade-offs.
|
|
||||||
|
|
||||||
- [**json_document**](../json_document.md) / [**ordered_json_document**](../ordered_json_document.md) - read-only
|
|
||||||
documents of the `basic_json` specialization they belong to
|
|
||||||
- [**json_editable_document**](../json_editable_document.md) /
|
|
||||||
[**ordered_json_editable_document**](../ordered_json_editable_document.md) - documents that can be modified in place
|
|
||||||
- [**json_view**](../json_view.md) / [**ordered_json_view**](../ordered_json_view.md) - views of a document
|
|
||||||
- [**json_editable_view**](../json_editable_view.md) /
|
|
||||||
[**ordered_json_editable_view**](../ordered_json_editable_view.md) - views of an editable document
|
|
||||||
|
|
||||||
## Iterator invalidation
|
## Iterator invalidation
|
||||||
|
|
||||||
All operations that add values to an **array** ([`push_back`](push_back.md) , [`operator+=`](operator+=.md),
|
All operations that add values to an **array** ([`push_back`](push_back.md) , [`operator+=`](operator+=.md),
|
||||||
@@ -121,7 +105,6 @@ The class satisfies the following concept requirements:
|
|||||||
- [**json_pointer**](../json_pointer/index.md) - JSON Pointer implementation
|
- [**json_pointer**](../json_pointer/index.md) - JSON Pointer implementation
|
||||||
- [**json_serializer**](json_serializer.md) - type of the serializer to for conversions from/to JSON
|
- [**json_serializer**](json_serializer.md) - type of the serializer to for conversions from/to JSON
|
||||||
- [**error_handler_t**](error_handler_t.md) - type to choose behavior on decoding errors
|
- [**error_handler_t**](error_handler_t.md) - type to choose behavior on decoding errors
|
||||||
- [**bjdata_version_t**](bjdata_version_t.md) - type to choose the BJData version to write
|
|
||||||
- [**cbor_tag_handler_t**](cbor_tag_handler_t.md) - type to choose how to handle CBOR tags
|
- [**cbor_tag_handler_t**](cbor_tag_handler_t.md) - type to choose how to handle CBOR tags
|
||||||
- **initializer_list_t** - type for initializer lists of `basic_json` values
|
- **initializer_list_t** - type for initializer lists of `basic_json` values
|
||||||
- [**input_format_t**](input_format_t.md) - type to choose the format to parse
|
- [**input_format_t**](input_format_t.md) - type to choose the format to parse
|
||||||
@@ -360,9 +343,6 @@ Access to the JSON value
|
|||||||
## See also
|
## See also
|
||||||
|
|
||||||
- [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259)
|
- [RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format](https://tools.ietf.org/html/rfc8259)
|
||||||
- [basic_json_document](../basic_json_document/index.md) / [basic_json_view](../basic_json_view/index.md) - parse a JSON
|
|
||||||
text into a flat index and read it through a view
|
|
||||||
- [Zero-copy JSON views](../../features/json_view.md) - when to use a document and a view instead of a `basic_json`
|
|
||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# <small>nlohmann::basic_json::</small>initializer_list_t
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
using initializer_list_t = std::initializer_list<detail::json_ref<basic_json>>;
|
||||||
|
```
|
||||||
|
|
||||||
|
The type used for the initializer-list [constructor](basic_json.md) (overload 5) and for functions
|
||||||
|
such as [`operator=`](operator=.md) that accept a braced-init-list of JSON values. Each element wraps a
|
||||||
|
`basic_json` value or something convertible to one, deferring the decision of whether the list should be
|
||||||
|
parsed as a JSON array or a JSON object to the constructor itself.
|
||||||
|
|
||||||
|
See the [constructor](basic_json.md) documentation for how `initializer_list_t` values are interpreted.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
??? example
|
||||||
|
|
||||||
|
The example shows how an `initializer_list_t` is used to construct a JSON value.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
--8<-- "examples/initializer_list_t.cpp"
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```
|
||||||
|
--8<-- "examples/initializer_list_t.output"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Version history
|
||||||
|
|
||||||
|
- Since version 1.0.0.
|
||||||
@@ -75,4 +75,4 @@ classDiagram
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.0.0.
|
- Since version 3.0.0.
|
||||||
@@ -104,8 +104,8 @@ When iterating over an array, `key()` will return the index of the element as st
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.1.0 as the replacement of the static function `iterator_wrapper`, which was available since
|
- Added `iterator_wrapper` in version 3.0.0.
|
||||||
version 1.0.0 and is deprecated since version 3.1.0.
|
- Added `items` and deprecated `iterator_wrapper` in version 3.1.0.
|
||||||
- Added structured binding support in version 3.5.0.
|
- Added structured binding support in version 3.5.0.
|
||||||
|
|
||||||
!!! warning "Deprecation"
|
!!! warning "Deprecation"
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
# <small>nlohmann::basic_json::</small>json_sax_t
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
using json_sax_t = json_sax<basic_json>;
|
||||||
|
```
|
||||||
|
|
||||||
|
The [`json_sax`](../json_sax/index.md) interface bound to this `basic_json` specialization, i.e. with
|
||||||
|
`BasicJsonType` fixed to `basic_json`. Used as the SAX interface type by [`sax_parse`](sax_parse.md) and
|
||||||
|
other SAX-based parsing functions.
|
||||||
|
|
||||||
|
See [`nlohmann::json_sax`](../json_sax/index.md) for more information.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
??? example
|
||||||
|
|
||||||
|
The example shows the type `json_sax_t`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
--8<-- "examples/json_sax_t.cpp"
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```
|
||||||
|
--8<-- "examples/json_sax_t.output"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Version history
|
||||||
|
|
||||||
|
- Added in version 3.2.0.
|
||||||
@@ -51,4 +51,4 @@ deserializes. See [Template Parameter Requirements](../../features/types/templat
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 2.1.0.
|
- Since version 2.0.0.
|
||||||
@@ -38,7 +38,7 @@ Strong guarantee: `source` and `target` are never modified.
|
|||||||
|
|
||||||
Linear in the total number of values (including nested ones) in `source` and `target`, times the cost of a key-based
|
Linear in the total number of values (including nested ones) in `source` and `target`, times the cost of a key-based
|
||||||
object operation (looking up a key, or adding one to the patch): logarithmic for [`json`](../json.md), and linear for
|
object operation (looking up a key, or adding one to the patch): logarithmic for [`json`](../json.md), and linear for
|
||||||
[`ordered_json`](../ordered_json.md) (see [`ordered_map` complexity](../ordered_map.md#complexity)). For
|
[`ordered_json`](../ordered_json.md) (see [`ordered_map` complexity](../ordered_map/index.md#complexity)). For
|
||||||
`ordered_json`, diffing two objects with `n` and `m` members therefore takes O((n+m)²).
|
`ordered_json`, diffing two objects with `n` and `m` members therefore takes O((n+m)²).
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|||||||
@@ -39,33 +39,13 @@ Basic guarantee: if an exception is thrown during the operation, the JSON value
|
|||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
Linear in the total number of values (including nested ones) in `apply_patch`, times the cost of a key-based object
|
Linear in the lengths of `apply_patch`.
|
||||||
operation (looking up, adding, or erasing a member): logarithmic for [`json`](../json.md), and linear for
|
|
||||||
[`ordered_json`](../ordered_json.md) (see [`ordered_map` complexity](../ordered_map.md#complexity)). For
|
|
||||||
`ordered_json`, patching an object that has `n` members with a patch that has `m` members therefore takes O(n·m). The
|
|
||||||
cost also includes one copy of `apply_patch` (see the notes below).
|
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
- `apply_patch` may be `#!cpp *this` itself or refer to a value contained in `#!cpp *this` (for example, a subobject
|
`apply_patch` may be `#!cpp *this` itself or refer to a value contained in `#!cpp *this` (for example, a subobject
|
||||||
returned by `#!cpp (*this)[key]`); it is read as it was when `merge_patch()` was called, before any modification of
|
returned by `#!cpp (*this)[key]`); it is read as it was when `merge_patch()` was called, before any modification of
|
||||||
`#!cpp *this`. To make this possible, the function copies `apply_patch` before it modifies `#!cpp *this`, so the cost
|
`#!cpp *this`.
|
||||||
includes one copy of the patch.
|
|
||||||
- A merge patch uses `#!json null` to remove a member, so it cannot set a member to `#!json null`: applying
|
|
||||||
`#!json {"a":null}` to `#!json {"a":1}` gives `#!json {}`.
|
|
||||||
- Arrays are not merged: an array in the patch replaces the target value as a whole, and a `#!json null` inside an
|
|
||||||
array is kept as an element. `#!json null` members of the target that the patch does not mention are preserved.
|
|
||||||
- A `#!json null` in the patch only removes a member; it does not prevent the creation of the enclosing objects.
|
|
||||||
Applying `#!json {"a":{"b":null}}` to `#!json {}` gives `#!json {"a":{}}` (see example 15 in Appendix A of the RFC).
|
|
||||||
- If `apply_patch` is not an object, it replaces the target as a whole. In particular, a `#!json null` patch turns the
|
|
||||||
target into `#!json null`. If `apply_patch` is an object and the target is not, the target is first replaced by an
|
|
||||||
empty object, so applying `#!json {}` to `#!json 1` gives `#!json {}`.
|
|
||||||
- A merge patch cannot express every change; for example, it cannot reorder array elements or remove a specific array
|
|
||||||
element. Use [JSON Patch](../../features/json_patch.md) ([RFC 6902](https://tools.ietf.org/html/rfc6902)) for that.
|
|
||||||
[`merge_diff`](merge_diff.md) has a matching limitation, see its notes.
|
|
||||||
- A [discarded](is_discarded.md) value is treated like any other non-object value: a discarded
|
|
||||||
`apply_patch` turns the target into a discarded value, and an object patch replaces a discarded target by an object
|
|
||||||
as described above.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
@@ -91,6 +71,6 @@ cost also includes one copy of `apply_patch` (see the notes below).
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.1.0.
|
- Added in version 3.0.0.
|
||||||
- Fixed use of freed or relocated memory when `apply_patch` is `#!cpp *this` or refers to a value contained in
|
- Fixed use of freed or relocated memory when `apply_patch` is `#!cpp *this` or refers to a value contained in
|
||||||
`#!cpp *this`, in version 3.13.0.
|
`#!cpp *this`, in version 3.13.0.
|
||||||
@@ -128,6 +128,6 @@ interpreted as `object_t::value_type` or `std::initializer_list<basic_json>`, se
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
1. Added in version 1.0.0.
|
1. Since version 1.0.0.
|
||||||
2. Added in version 1.0.0.
|
2. Since version 1.0.0.
|
||||||
3. Added in version 2.0.0.
|
3. Since version 2.0.0.
|
||||||
@@ -51,3 +51,5 @@ Linear.
|
|||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 1.0.0.
|
- Added in version 1.0.0.
|
||||||
|
- The `noexcept` specification was extended to also depend on
|
||||||
|
[`json_base_class_t`](json_base_class_t.md)'s move-assignment in version 3.11.3.
|
||||||
@@ -137,13 +137,10 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
|
|
||||||
When the JSON pointer traverses intermediate levels that don't exist at all yet (not just a missing
|
When the JSON pointer traverses intermediate levels that don't exist at all yet (not just a missing
|
||||||
leaf), each missing level is created as an array or an object depending on whether the corresponding
|
leaf), each missing level is created as an array or an object depending on whether the corresponding
|
||||||
pointer token is a valid array index: the token `0`, a sequence of digits that does not begin with `0`,
|
pointer token parses as a non-negative integer: a numeric token creates an array, a non-numeric token
|
||||||
or the token `-` creates an array, and every other token creates an object. For example, on an
|
creates an object. For example, on an initially `#!json null` value, `/foo/0/0/0` creates nested arrays,
|
||||||
initially `#!json null` value, `/foo/0/0/0` creates nested arrays, while `/foo/one/one/one` creates
|
while `/foo/one/one/one` creates nested objects. This is not specified by the JSON Pointer RFC; it is
|
||||||
nested objects. Tokens such as `01` or the empty token cannot be array indices (cf. RFC 6901, Sect. 4)
|
this library's own, intentional disambiguation rule. See also [JSON Pointer](../../features/json_pointer.md).
|
||||||
and therefore create objects, just as they would if the level already existed as an object. This is not
|
|
||||||
specified by the JSON Pointer RFC; it is this library's own, intentional disambiguation rule. See also
|
|
||||||
[JSON Pointer](../../features/json_pointer.md).
|
|
||||||
|
|
||||||
!!! warning "Deprecation"
|
!!! warning "Deprecation"
|
||||||
|
|
||||||
@@ -287,10 +284,10 @@ Strong exception safety: if an exception occurs, the original value stays intact
|
|||||||
1. Added in version 1.0.0. Fixed in version 3.13.0 to throw `#!cpp std::length_error` instead of emptying the array and
|
1. Added in version 1.0.0. Fixed in version 3.13.0 to throw `#!cpp std::length_error` instead of emptying the array and
|
||||||
accessing it out of bounds when `idx` equals the maximum value of `size_type`. A missing index in the const version
|
accessing it out of bounds when `idx` equals the maximum value of `size_type`. A missing index in the const version
|
||||||
is guarded by a runtime assertion since version 3.13.0.
|
is guarded by a runtime assertion since version 3.13.0.
|
||||||
2. Added in version 1.0.0. Added overloads for `T* key` in version 1.1.0. The `T* key` overloads forward to
|
2. Added in version 1.0.0. Added overloads for `T* key` in version 1.1.0. Removed overloads for `T* key` (replaced by 3)
|
||||||
overload 3 since version 3.11.0.
|
in version 3.11.0.
|
||||||
3. Added in version 3.11.0. Support for keys convertible to `std::string_view` added in version 3.13.0, consistently
|
3. Added in version 3.11.0. Fixed in version 3.13.0 to consistently accept `std::string_view`-convertible keys, as
|
||||||
across the lookup functions.
|
already supported by [`at`](at.md), [`value`](value.md), [`find`](find.md), and other lookup functions.
|
||||||
4. Added in version 2.0.0. A missing array index in the const version is guarded by a runtime assertion since
|
4. Added in version 2.0.0. A missing array index in the const version is guarded by a runtime assertion since
|
||||||
version 3.13.0. Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of
|
version 3.13.0. Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) instead of
|
||||||
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) for a one-character array index that is not
|
[`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) for a one-character array index that is not
|
||||||
|
|||||||
@@ -89,6 +89,11 @@ Linear in the size of the JSON value.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 1.0.0.
|
- Since version 1.0.0.
|
||||||
- Macros `JSON_EXPLICIT`/[`JSON_USE_IMPLICIT_CONVERSIONS`](../macros/json_use_implicit_conversions.md) added
|
- Macros `JSON_EXPLICIT`/[`JSON_USE_IMPLICIT_CONVERSIONS`](../macros/json_use_implicit_conversions.md) added
|
||||||
in version 3.9.0.
|
in version 3.9.0.
|
||||||
|
- The exclusion of `std::any` from this conversion became conditional on
|
||||||
|
[`JSON_HAS_STATIC_RTTI`](../macros/json_has_static_rtti.md) in version 3.11.3.
|
||||||
|
- `std::optional<T>` excluded from this conversion in version 3.13.0; use
|
||||||
|
[`get<std::optional<T>>()`](get.md)/[`get_to()`](get_to.md) instead (see
|
||||||
|
[Converting values](../../features/conversions.md)).
|
||||||
@@ -75,4 +75,4 @@ classDiagram
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.0.0.
|
- Since version 3.0.0.
|
||||||
@@ -76,4 +76,4 @@ classDiagram
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.0.0.
|
- Since version 3.0.0.
|
||||||
@@ -25,10 +25,6 @@ static basic_json parse(IteratorType first, SentinelType last,
|
|||||||
respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to
|
respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to
|
||||||
the iterator type with `operator!=`.
|
the iterator type with `operator!=`.
|
||||||
|
|
||||||
Ill-formed UTF-16 and UTF-32 sequences are rejected: a lone surrogate, a high surrogate that is not followed by its
|
|
||||||
low surrogate, and a code point above `U+10FFFF` all yield a
|
|
||||||
[`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101).
|
|
||||||
|
|
||||||
## Template parameters
|
## Template parameters
|
||||||
|
|
||||||
`InputType`
|
`InputType`
|
||||||
@@ -38,12 +34,9 @@ static basic_json parse(IteratorType first, SentinelType last,
|
|||||||
- a `FILE` pointer (throws if null)
|
- a `FILE` pointer (throws if null)
|
||||||
- a C-style array of characters
|
- a C-style array of characters
|
||||||
- a pointer to a null-terminated string of single byte characters (throws if null)
|
- a pointer to a null-terminated string of single byte characters (throws if null)
|
||||||
- a `std::string` or a `std::string_view` (C++17)
|
- a `std::string`
|
||||||
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
|
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
|
||||||
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
|
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
|
||||||
- wide characters (`wchar_t`, `char16_t`, `char32_t`), which are converted to UTF-8: a wide string object
|
|
||||||
(`std::wstring`, `std::u16string`, `std::u32string`) or an array (e.g. a wide string literal); a pointer to a
|
|
||||||
null-terminated wide string (e.g. `std::u16string::c_str()`) is not supported
|
|
||||||
|
|
||||||
`IteratorType`
|
`IteratorType`
|
||||||
: a compatible iterator type, for instance.
|
: a compatible iterator type, for instance.
|
||||||
@@ -119,13 +112,6 @@ By default, a `'\0'` (NUL) byte anywhere in the input is treated as end of input
|
|||||||
outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-bytes-in-the-input) for details and the
|
outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-bytes-in-the-input) for details and the
|
||||||
[`JSON_STRICT_NUL_HANDLING`](../macros/json_strict_nul_handling.md) macro to opt into rejecting it instead.
|
[`JSON_STRICT_NUL_HANDLING`](../macros/json_strict_nul_handling.md) macro to opt into rejecting it instead.
|
||||||
|
|
||||||
!!! tip "Parsing into a document instead of a value"
|
|
||||||
|
|
||||||
[`basic_json_document::parse`](../basic_json_document/parse.md) accepts the same inputs and the same options, but
|
|
||||||
keeps the result as a flat index of the parsed values instead of a tree of `basic_json` values. It is faster and uses
|
|
||||||
less memory when only parts of the document are read, and it also accepts a pointer to a null-terminated wide string,
|
|
||||||
which `parse()` does not. See [Zero-copy JSON views](../../features/json_view.md).
|
|
||||||
|
|
||||||
!!! tip "Performance"
|
!!! tip "Performance"
|
||||||
|
|
||||||
Contiguous inputs (strings, string views, vectors, arrays, pointer ranges) are the fastest to parse. A
|
Contiguous inputs (strings, string views, vectors, arrays, pointer ranges) are the fastest to parse. A
|
||||||
@@ -264,22 +250,6 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by
|
|||||||
--8<-- "examples/trailing_commas.output"
|
--8<-- "examples/trailing_commas.output"
|
||||||
```
|
```
|
||||||
|
|
||||||
??? example "Example: parse from UTF-16, UTF-32, and wide strings"
|
|
||||||
|
|
||||||
The encoding is deduced from the character type of the input; `std::u16string` and `std::u32string` are interpreted
|
|
||||||
as UTF-16 and UTF-32, and `std::wstring` uses the platform's `wchar_t` width. String literals like `u"..."`,
|
|
||||||
`U"..."`, and `L"..."` work as well, as do iterators over wide characters.
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
--8<-- "examples/parse__wide_string.cpp"
|
|
||||||
```
|
|
||||||
|
|
||||||
Output:
|
|
||||||
|
|
||||||
```json
|
|
||||||
--8<-- "examples/parse__wide_string.output"
|
|
||||||
```
|
|
||||||
|
|
||||||
## See also
|
## See also
|
||||||
|
|
||||||
- [accept](accept.md) - check if the input is valid JSON
|
- [accept](accept.md) - check if the input is valid JSON
|
||||||
@@ -310,9 +280,6 @@ outside of a string, invalid) byte; see the [FAQ entry](../../home/faq.md#nul-by
|
|||||||
time).
|
time).
|
||||||
- Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0
|
- Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0
|
||||||
([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md) restores `std::fgetc`).
|
([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md) restores `std::fgetc`).
|
||||||
- Ill-formed UTF-16 and UTF-32 input (a lone surrogate, a high surrogate that is not followed by its low surrogate, or
|
|
||||||
a code point above `U+10FFFF`) is rejected with `parse_error.101` in version 3.13.0; before that, some such
|
|
||||||
sequences were accepted or ended the input silently.
|
|
||||||
|
|
||||||
!!! warning "Deprecation"
|
!!! warning "Deprecation"
|
||||||
|
|
||||||
|
|||||||
@@ -84,4 +84,4 @@ or the end of file. This also holds true when reading a byte vector for binary f
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.0.0.
|
- Since version 3.0.0.
|
||||||
@@ -40,7 +40,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
where an existing element is required (the "path" of "replace", the "from" of "move" and "copy"); example:
|
where an existing element is required (the "path" of "replace", the "from" of "move" and "copy"); example:
|
||||||
`"array index '-' (3) is out of range"`.
|
`"array index '-' (3) is out of range"`.
|
||||||
- Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if a JSON pointer inside the patch
|
- Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if a JSON pointer inside the patch
|
||||||
could not be resolved successfully in the current JSON value; example: `"key 'baz' not found"`.
|
could not be resolved successfully in the current JSON value; example: `"key baz not found"`.
|
||||||
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if a reference token of a JSON
|
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if a reference token of a JSON
|
||||||
pointer inside the patch cannot be resolved, e.g., `-` in a "remove" operation or `1a` for an array; example:
|
pointer inside the patch cannot be resolved, e.g., `-` in a "remove" operation or `1a` for an array; example:
|
||||||
`"unresolved reference token '-'"`.
|
`"unresolved reference token '-'"`.
|
||||||
@@ -53,10 +53,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
- Throws [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) if a "move" operation's "from"
|
- Throws [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) if a "move" operation's "from"
|
||||||
location is a proper prefix of its "path" location.
|
location is a proper prefix of its "path" location.
|
||||||
- Throws [`other_error.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was
|
- Throws [`other_error.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was
|
||||||
unsuccessful. A "test" operation whose "path" does not exist is unsuccessful, too: the failed lookup is reported as a
|
unsuccessful.
|
||||||
failed test, even if the operation has no "value" member (which would otherwise be
|
|
||||||
[`parse_error.105`](../../home/exceptions.md#jsonexceptionparse_error105)). Which of the two errors is reported for
|
|
||||||
this combination depends on the order in which the operands of a comparison are evaluated, so do not rely on it.
|
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
@@ -68,10 +65,6 @@ the JSON value, the complexity can usually be neglected.
|
|||||||
The application of a patch is atomic: Either all operations succeed and the patched document is returned or an exception
|
The application of a patch is atomic: Either all operations succeed and the patched document is returned or an exception
|
||||||
is thrown. In any case, the original value is not changed: the patch is applied to a copy of the value.
|
is thrown. In any case, the original value is not changed: the patch is applied to a copy of the value.
|
||||||
|
|
||||||
Duplicate member names inside an operation object are resolved by the parser before the patch is applied (the last one
|
|
||||||
is kept, see [`object_t`](object_t.md#behavior)), so `#!json {"op": "add", "op": "remove", "path": "/a"}` is a
|
|
||||||
`remove` operation.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
??? example "Example: apply a JSON patch"
|
??? example "Example: apply a JSON patch"
|
||||||
@@ -115,7 +108,6 @@ is kept, see [`object_t`](object_t.md#behavior)), so `#!json {"op": "add", "op":
|
|||||||
- Added in version 2.0.0.
|
- Added in version 2.0.0.
|
||||||
- Added [`out_of_range.411`](../../home/exceptions.md#jsonexceptionout_of_range411) and stopped relying on an internal assertion when an "add" operation's
|
- Added [`out_of_range.411`](../../home/exceptions.md#jsonexceptionout_of_range411) and stopped relying on an internal assertion when an "add" operation's
|
||||||
target location has a non-object/non-array parent in version 3.13.0.
|
target location has a non-object/non-array parent in version 3.13.0.
|
||||||
This includes a `null` parent, which was previously silently converted to an object, in version 3.13.0.
|
|
||||||
- Added [`out_of_range.413`](../../home/exceptions.md#jsonexceptionout_of_range413) and stopped silently ignoring a "remove" operation whose target
|
- Added [`out_of_range.413`](../../home/exceptions.md#jsonexceptionout_of_range413) and stopped silently ignoring a "remove" operation whose target
|
||||||
location has a non-object/non-array parent in version 3.13.0.
|
location has a non-object/non-array parent in version 3.13.0.
|
||||||
- Added [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) and rejected a "move" operation whose "from" location is a proper
|
- Added [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) and rejected a "move" operation whose "from" location is a proper
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ No guarantees, value may be corrupted by an unsuccessful patch operation.
|
|||||||
where an existing element is required (the "path" of "replace", the "from" of "move" and "copy"); example:
|
where an existing element is required (the "path" of "replace", the "from" of "move" and "copy"); example:
|
||||||
`"array index '-' (3) is out of range"`.
|
`"array index '-' (3) is out of range"`.
|
||||||
- Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if a JSON pointer inside the patch
|
- Throws [`out_of_range.403`](../../home/exceptions.md#jsonexceptionout_of_range403) if a JSON pointer inside the patch
|
||||||
could not be resolved successfully in the current JSON value; example: `"key 'baz' not found"`.
|
could not be resolved successfully in the current JSON value; example: `"key baz not found"`.
|
||||||
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if a reference token of a JSON
|
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if a reference token of a JSON
|
||||||
pointer inside the patch cannot be resolved, e.g., `-` in a "remove" operation or `1a` for an array; example:
|
pointer inside the patch cannot be resolved, e.g., `-` in a "remove" operation or `1a` for an array; example:
|
||||||
`"unresolved reference token '-'"`.
|
`"unresolved reference token '-'"`.
|
||||||
@@ -49,10 +49,7 @@ No guarantees, value may be corrupted by an unsuccessful patch operation.
|
|||||||
- Throws [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) if a "move" operation's "from"
|
- Throws [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) if a "move" operation's "from"
|
||||||
location is a proper prefix of its "path" location.
|
location is a proper prefix of its "path" location.
|
||||||
- Throws [`other_error.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was
|
- Throws [`other_error.501`](../../home/exceptions.md#jsonexceptionother_error501) if "test" operation was
|
||||||
unsuccessful. A "test" operation whose "path" does not exist is unsuccessful, too: the failed lookup is reported as a
|
unsuccessful.
|
||||||
failed test, even if the operation has no "value" member (which would otherwise be
|
|
||||||
[`parse_error.105`](../../home/exceptions.md#jsonexceptionparse_error105)). Which of the two errors is reported for
|
|
||||||
this combination depends on the order in which the operands of a comparison are evaluated, so do not rely on it.
|
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
@@ -65,10 +62,6 @@ Unlike [`patch`](patch.md), `patch_inplace` applies the operation "in place" and
|
|||||||
That makes it faster for large documents by avoiding the copy. However, the JSON value might be corrupted if the
|
That makes it faster for large documents by avoiding the copy. However, the JSON value might be corrupted if the
|
||||||
function throws an exception.
|
function throws an exception.
|
||||||
|
|
||||||
Duplicate member names inside an operation object are resolved by the parser before the patch is applied (the last one
|
|
||||||
is kept, see [`object_t`](object_t.md#behavior)), so `#!json {"op": "add", "op": "remove", "path": "/a"}` is a
|
|
||||||
`remove` operation.
|
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
??? example "Example: apply a JSON patch in place"
|
??? example "Example: apply a JSON patch in place"
|
||||||
@@ -113,7 +106,6 @@ is kept, see [`object_t`](object_t.md#behavior)), so `#!json {"op": "add", "op":
|
|||||||
- Added in version 3.11.0.
|
- Added in version 3.11.0.
|
||||||
- Added [`out_of_range.411`](../../home/exceptions.md#jsonexceptionout_of_range411) and stopped relying on an internal assertion when an "add" operation's
|
- Added [`out_of_range.411`](../../home/exceptions.md#jsonexceptionout_of_range411) and stopped relying on an internal assertion when an "add" operation's
|
||||||
target location has a non-object/non-array parent in version 3.13.0.
|
target location has a non-object/non-array parent in version 3.13.0.
|
||||||
This includes a `null` parent, which was previously silently converted to an object, in version 3.13.0.
|
|
||||||
- Added [`out_of_range.413`](../../home/exceptions.md#jsonexceptionout_of_range413) and stopped silently ignoring a "remove" operation whose target
|
- Added [`out_of_range.413`](../../home/exceptions.md#jsonexceptionout_of_range413) and stopped silently ignoring a "remove" operation whose target
|
||||||
location has a non-object/non-array parent in version 3.13.0.
|
location has a non-object/non-array parent in version 3.13.0.
|
||||||
- Added [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) and rejected a "move" operation whose "from" location is a proper
|
- Added [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) and rejected a "move" operation whose "from" location is a proper
|
||||||
|
|||||||
@@ -125,6 +125,6 @@ throws.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
1. Added in version 1.0.0.
|
1. Since version 1.0.0.
|
||||||
2. Added in version 1.0.0.
|
2. Since version 1.0.0.
|
||||||
3. Added in version 2.0.0.
|
3. Since version 2.0.0.
|
||||||
@@ -31,10 +31,6 @@ Read from input and generate SAX events
|
|||||||
respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to
|
respectively as UTF-8, UTF-16, and UTF-32. If `SentinelType` differs from `IteratorType`, it must be comparable to
|
||||||
the iterator type with `operator!=`.
|
the iterator type with `operator!=`.
|
||||||
|
|
||||||
Ill-formed UTF-16 and UTF-32 sequences are rejected: a lone surrogate, a high surrogate that is not followed by its
|
|
||||||
low surrogate, and a code point above `U+10FFFF` all yield a
|
|
||||||
[`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101).
|
|
||||||
|
|
||||||
The SAX event lister must follow the interface of [`json_sax`](../json_sax/index.md).
|
The SAX event lister must follow the interface of [`json_sax`](../json_sax/index.md).
|
||||||
|
|
||||||
## Template parameters
|
## Template parameters
|
||||||
@@ -46,12 +42,8 @@ The SAX event lister must follow the interface of [`json_sax`](../json_sax/index
|
|||||||
- a `FILE` pointer
|
- a `FILE` pointer
|
||||||
- a C-style array of characters
|
- a C-style array of characters
|
||||||
- a pointer to a null-terminated string of single byte characters
|
- a pointer to a null-terminated string of single byte characters
|
||||||
- a `std::string` or a `std::string_view` (C++17)
|
|
||||||
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
|
- a container `obj` for which `begin(obj)` and `end(obj)` produce a valid pair of iterators
|
||||||
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
|
(as found via ADL or member functions, with semantics compatible to `std::begin` and `std::end`)
|
||||||
- wide characters (`wchar_t`, `char16_t`, `char32_t`), which are converted to UTF-8: a wide string object
|
|
||||||
(`std::wstring`, `std::u16string`, `std::u32string`) or an array (e.g. a wide string literal); a pointer to a
|
|
||||||
null-terminated wide string (e.g. `std::u16string::c_str()`) is not supported
|
|
||||||
|
|
||||||
`IteratorType`
|
`IteratorType`
|
||||||
: a compatible iterator type for overload (2); a pair of character iterators whose `value_type` is an integral type
|
: a compatible iterator type for overload (2); a pair of character iterators whose `value_type` is an integral type
|
||||||
@@ -168,9 +160,6 @@ use the same file with stdio functions, but it must not wait for another thread
|
|||||||
([`JSON_DISABLE_STREAM_BULK_SCAN`](../macros/json_disable_stream_bulk_scan.md)).
|
([`JSON_DISABLE_STREAM_BULK_SCAN`](../macros/json_disable_stream_bulk_scan.md)).
|
||||||
- Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0
|
- Parsing from a `FILE*` locks the file once instead of once per character in version 3.13.0
|
||||||
([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md)).
|
([`JSON_DISABLE_UNLOCKED_FILE_IO`](../macros/json_disable_unlocked_file_io.md)).
|
||||||
- Ill-formed UTF-16 and UTF-32 input (a lone surrogate, a high surrogate that is not followed by its low surrogate, or
|
|
||||||
a code point above `U+10FFFF`) is rejected with `parse_error.101` in version 3.13.0; before that, some such
|
|
||||||
sequences were accepted or ended the input silently.
|
|
||||||
|
|
||||||
!!! warning "Deprecation"
|
!!! warning "Deprecation"
|
||||||
|
|
||||||
|
|||||||
@@ -182,10 +182,10 @@ Constant.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
1. Added in version 1.0.0. Exchanges the `json_base_class_t` subobject along with the value since version 3.13.0.
|
1. Since version 1.0.0. Exchanges the `json_base_class_t` subobject along with the value since version 3.13.0.
|
||||||
2. Added in version 1.0.0. Exchanges the `json_base_class_t` subobject along with the value since version 3.13.0.
|
2. Since version 1.0.0. Exchanges the `json_base_class_t` subobject along with the value since version 3.13.0.
|
||||||
3. Added in version 1.0.0.
|
3. Since version 1.0.0.
|
||||||
4. Added in version 1.0.0.
|
4. Since version 1.0.0.
|
||||||
5. Added in version 1.0.0.
|
5. Since version 1.0.0.
|
||||||
6. Added in version 3.8.0.
|
6. Since version 3.8.0.
|
||||||
7. Added in version 3.8.0.
|
7. Since version 3.8.0.
|
||||||
@@ -43,8 +43,7 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
|||||||
`#!cpp false` by default.
|
`#!cpp false` by default.
|
||||||
|
|
||||||
`version` (in)
|
`version` (in)
|
||||||
: which version of BJData to use, see [`bjdata_version_t`](bjdata_version_t.md) and the note on "Binary values" on
|
: which version of BJData to use (see note on "Binary values" on [BJData](../../features/binary_formats/bjdata.md));
|
||||||
[BJData](../../features/binary_formats/bjdata.md);
|
|
||||||
optional, `#!cpp bjdata_version_t::draft2` by default.
|
optional, `#!cpp bjdata_version_t::draft2` by default.
|
||||||
|
|
||||||
`error_handler` (in)
|
`error_handler` (in)
|
||||||
@@ -65,9 +64,7 @@ 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 or object, or a non-empty binary value (with
|
is false, and `j` contains a non-empty array, object, or binary value.
|
||||||
`version = bjdata_version_t::draft3`, any binary value, including an empty one).
|
|
||||||
Scalars, empty arrays, and empty objects 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)
|
||||||
@@ -127,3 +124,5 @@ Linear in the size of the JSON value `j`.
|
|||||||
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) throws `type_error.316`.
|
[`JSON_STRICT_BINARY_UTF8`](../macros/json_strict_binary_utf8.md) is enabled) throws `type_error.316`.
|
||||||
- Throws `type_error.321` for a discarded value since version 3.13.0; previously, a discarded value nested in an
|
- Throws `type_error.321` for a discarded value since version 3.13.0; previously, a discarded value nested in an
|
||||||
array or object was silently skipped, producing invalid BJData.
|
array or object was silently skipped, producing invalid BJData.
|
||||||
|
- Writes unsigned integers wider than 64 bits as high-precision numbers since version 3.13.0; previously, they were
|
||||||
|
silently truncated to 64 bits.
|
||||||
@@ -13,7 +13,7 @@ static void to_bson(const basic_json& j, detail::output_adapter<char> o,
|
|||||||
```
|
```
|
||||||
|
|
||||||
BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a
|
BSON (Binary JSON) is a binary format in which zero or more ordered key/value pairs are stored as a single entity (a
|
||||||
so-called document). The members of an object are written in the order in which iteration visits them.
|
so-called document).
|
||||||
|
|
||||||
1. Returns a byte vector containing the BSON serialization.
|
1. Returns a byte vector containing the BSON serialization.
|
||||||
2. Writes the BSON serialization to an output adapter.
|
2. Writes the BSON serialization to an output adapter.
|
||||||
|
|||||||
@@ -42,8 +42,7 @@ The exact mapping and its limitations are described on a [dedicated page](../../
|
|||||||
|
|
||||||
## Exception safety
|
## Exception safety
|
||||||
|
|
||||||
Strong guarantee: if an exception is thrown, there are no changes in the JSON value. Bytes that were already written to
|
Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
|
||||||
the output adapter are not taken back.
|
|
||||||
|
|
||||||
## Exceptions
|
## Exceptions
|
||||||
|
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
value, array, or object exceeds 4294967295, the maximum MessagePack can store; example:
|
value, array, or object exceeds 4294967295, the maximum MessagePack can store; example:
|
||||||
`"MessagePack length 4294967296 exceeds maximum of 4294967295"`
|
`"MessagePack length 4294967296 exceeds maximum of 4294967295"`
|
||||||
- Throws [`out_of_range.415`](../../home/exceptions.md#jsonexceptionout_of_range415) if the subtype of a binary value
|
- Throws [`out_of_range.415`](../../home/exceptions.md#jsonexceptionout_of_range415) if the subtype of a binary value
|
||||||
exceeds 255, the largest value the library can store in the one-byte MessagePack ext type; example:
|
exceeds 255, the maximum of the MessagePack ext type; example:
|
||||||
`"subtype 70000 is too large for the MessagePack ext type (max 255)"`
|
`"subtype 70000 is too large for the MessagePack ext type (max 255)"`
|
||||||
- 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`
|
not valid UTF-8 and `error_handler` is `strict`
|
||||||
|
|||||||
@@ -76,4 +76,4 @@ classDiagram
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.0.0.
|
- Since version 3.0.0.
|
||||||
@@ -36,9 +36,9 @@ The function can throw the following exceptions:
|
|||||||
- Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index in a key is not a
|
- Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) if an array index in a key is not a
|
||||||
number; example: `"array index 'one' is not a number"`
|
number; example: `"array index 'one' is not a number"`
|
||||||
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if a level becomes an array
|
- Throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404) if a level becomes an array
|
||||||
(because one of its keys is `0`) and another key at that level is empty (as in `/a/`), is `-`, or begins with a digit
|
(because one of its keys is `0`) and another key at that level begins with a digit but is not a valid array index
|
||||||
but is not a valid array index (such as `1a`); examples: `"unresolved reference token '-'"`,
|
(such as `1a`), or is `-`; example:
|
||||||
`"unresolved reference token ''"`
|
`"unresolved reference token '-'"`
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
@@ -46,7 +46,7 @@ Linear in the size of the JSON value.
|
|||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
Empty objects and arrays are flattened by [`flatten()`](flatten.md) to `#!json null` values and cannot be unflattened to
|
Empty objects and arrays are flattened by [`flatten()`](flatten.md) to `#!json null` values and cannot unflattened to
|
||||||
their original type.
|
their original type.
|
||||||
|
|
||||||
A flattened array and a flattened object whose keys are array indices are indistinguishable, because both are
|
A flattened array and a flattened object whose keys are array indices are indistinguishable, because both are
|
||||||
@@ -54,12 +54,7 @@ described by the same JSON pointers. A value is therefore restored as an array i
|
|||||||
reference token `0`, and as an object otherwise: `#!json {"2": 1}` is restored unchanged, whereas `#!json {"0": 1}` is
|
reference token `0`, and as an object otherwise: `#!json {"2": 1}` is restored unchanged, whereas `#!json {"0": 1}` is
|
||||||
restored as `#!json [1]`. This decision does not depend on the order in which the flattened object is iterated.
|
restored as `#!json [1]`. This decision does not depend on the order in which the flattened object is iterated.
|
||||||
|
|
||||||
A third case is an object that has the key `0` next to a key that is not a valid array index: `#!json {"0": 1, "b": 2}`
|
Apart from these two cases, for a JSON value `j`, the following is always true:
|
||||||
is flattened to `#!json {"/0": 1, "/b": 2}`, and unflatten treats the level as an array and throws
|
|
||||||
[`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109) because `b` is not a number. (For the keys
|
|
||||||
`-`, `1a`, or the empty key instead of `b`, it throws [`out_of_range.404`](../../home/exceptions.md#jsonexceptionout_of_range404).)
|
|
||||||
|
|
||||||
Apart from these three cases, for a JSON value `j`, the following is always true:
|
|
||||||
`#!cpp j == j.flatten().unflatten()`.
|
`#!cpp j == j.flatten().unflatten()`.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|||||||
@@ -45,9 +45,6 @@ This is equivalent to Python's `dict.get(key, default)`.
|
|||||||
return default_value;
|
return default_value;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
with one exception: if the JSON value is neither an array nor an object, the function throws
|
|
||||||
[`type_error.306`](../../home/exceptions.md#jsonexceptiontype_error306) before looking at `ptr`, even for the empty
|
|
||||||
pointer, whereas `at` returns the value itself for the empty pointer.
|
|
||||||
|
|
||||||
!!! note "Differences to `at` and `operator[]`"
|
!!! note "Differences to `at` and `operator[]`"
|
||||||
|
|
||||||
@@ -234,8 +231,11 @@ changes to any JSON value.
|
|||||||
1. Added in version 1.0.0. Changed parameter `default_value` type from `const ValueType&` to `ValueType&&` in version
|
1. Added in version 1.0.0. Changed parameter `default_value` type from `const ValueType&` to `ValueType&&` in version
|
||||||
3.11.0. Deleted overload for integral key types added in version 3.13.0 to reject such calls at compile time
|
3.11.0. Deleted overload for integral key types added in version 3.13.0 to reject such calls at compile time
|
||||||
instead of causing undefined behavior at runtime.
|
instead of causing undefined behavior at runtime.
|
||||||
2. Added in version 3.11.0. Made `ValueType` the first template parameter in version 3.11.2. Support for keys
|
2. Added in version 3.11.0. Made `ValueType` the first template parameter in version 3.11.2. Fixed in version 3.13.0
|
||||||
convertible to `std::string_view` added in version 3.13.0, consistently across the lookup functions.
|
to consistently accept `std::string_view`-convertible keys, as already supported by
|
||||||
3. Added in version 2.0.2. Extended to work with arrays in version 3.13.0. Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109)
|
[`operator[]`](operator[].md), [`at`](at.md), [`find`](find.md), and other lookup functions.
|
||||||
|
3. Added in version 2.0.2. Extended to work with arrays in version 3.13.0, including fixing an issue where resolving
|
||||||
|
`ptr` through an array unexpectedly threw `out_of_range` instead of returning the resolved element (or
|
||||||
|
`default_value`, as documented). Throws [`parse_error.109`](../../home/exceptions.md#jsonexceptionparse_error109)
|
||||||
instead of returning `default_value` for a one-character array index that is not a digit (e.g., `/x`) in version
|
instead of returning `default_value` for a one-character array index that is not a digit (e.g., `/x`) in version
|
||||||
3.13.0, as it already did for longer ones.
|
3.13.0, as it already did for longer ones.
|
||||||
@@ -12,7 +12,7 @@ void erase(view_type array, I idx);
|
|||||||
std::size_t erase(const json_pointer& ptr);
|
std::size_t erase(const json_pointer& ptr);
|
||||||
```
|
```
|
||||||
|
|
||||||
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md))
|
Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
|
||||||
has `erase`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
|
has `erase`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
|
||||||
|
|
||||||
1. Removes every member of `object` whose key is `key` (see [Notes](#notes) on duplicate keys) and returns how many
|
1. Removes every member of `object` whose key is `key` (see [Notes](#notes) on duplicate keys) and returns how many
|
||||||
|
|||||||
@@ -3,14 +3,14 @@
|
|||||||
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
template<typename BasicJsonType, view_mode Mode = view_mode::read_only>
|
template<typename BasicJsonType, bool Editable = false>
|
||||||
class basic_json_document;
|
class basic_json_document;
|
||||||
```
|
```
|
||||||
|
|
||||||
A parsed JSON text, held as a flat index of its values
|
A parsed JSON text, held as a flat index of its values
|
||||||
([16 bytes per value](../../home/architecture.md#node-index-of-json-views)) instead of a tree of `BasicJsonType` values.
|
([16 bytes per value](../../home/architecture.md#node-index-of-json-views)) instead of a tree of `BasicJsonType` values.
|
||||||
Strings and floating-point numbers stay in the source text; integers are converted while parsing, and strings that
|
Strings and numbers stay in the source text; only strings that contain escapes are decoded, into one buffer owned by
|
||||||
contain escapes are decoded into one buffer owned by the document. [`basic_json_view`](../basic_json_view/index.md) is a read-only handle to one value of a
|
the document. [`basic_json_view`](../basic_json_view/index.md) is a read-only handle to one value of a
|
||||||
`basic_json_document`; [`materialize()`](../basic_json_view/materialize.md) turns a subtree into the
|
`basic_json_document`; [`materialize()`](../basic_json_view/materialize.md) turns a subtree into the
|
||||||
`BasicJsonType` value that [`BasicJsonType::parse()`](../basic_json/parse.md) would have produced for it.
|
`BasicJsonType` value that [`BasicJsonType::parse()`](../basic_json/parse.md) would have produced for it.
|
||||||
|
|
||||||
@@ -20,9 +20,9 @@ is move-only: a copy would duplicate the whole index, the decoded-string buffer,
|
|||||||
enough in every use we know of. The index, the decoded strings, and the owned text live in one heap block that the
|
enough in every use we know of. The index, the decoded strings, and the owned text live in one heap block that the
|
||||||
document holds through a `#!cpp std::unique_ptr`; this is also why views survive a move of the document.
|
document holds through a `#!cpp std::unique_ptr`; this is also why views survive a move of the document.
|
||||||
|
|
||||||
With `#!cpp Mode == view_mode::editable`, the document also offers [`set`](set.md), [`push_back`](push_back.md),
|
With `#!cpp Editable == true`, the document also offers [`set`](set.md), [`push_back`](push_back.md),
|
||||||
[`insert`](insert.md), and [`erase`](erase.md) to change values in place, see [Edits](#edits) below. The source text
|
[`insert`](insert.md), and [`erase`](erase.md) to change values in place, see [Edits](#edits) below. The source text
|
||||||
itself is never written; a read-only document (`#!cpp view_mode::read_only`, the default) does not carry any of the
|
itself is never written; a read-only document (`#!cpp Editable == false`, the default) does not carry any of the
|
||||||
bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp static_assert`).
|
bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp static_assert`).
|
||||||
|
|
||||||
## Template parameters
|
## Template parameters
|
||||||
@@ -32,10 +32,9 @@ bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp
|
|||||||
[`ordered_json`](../ordered_json.md). Only 64-bit `number_integer_t`/`number_unsigned_t` types are supported; this
|
[`ordered_json`](../ordered_json.md). Only 64-bit `number_integer_t`/`number_unsigned_t` types are supported; this
|
||||||
is checked with a `static_assert`.
|
is checked with a `static_assert`.
|
||||||
|
|
||||||
`Mode`
|
`Editable`
|
||||||
: a [`view_mode`](../view_mode.md): `#!cpp view_mode::editable` if the document supports [`set`](set.md),
|
: whether the document supports [`set`](set.md), [`push_back`](push_back.md), [`insert`](insert.md), and
|
||||||
[`push_back`](push_back.md), [`insert`](insert.md), and [`erase`](erase.md) (optional, `#!cpp view_mode::read_only`
|
[`erase`](erase.md) (optional, `#!cpp false` by default). See [Edits](#edits) below.
|
||||||
by default). See [Edits](#edits) below.
|
|
||||||
|
|
||||||
## Specializations
|
## Specializations
|
||||||
|
|
||||||
@@ -48,7 +47,7 @@ bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp
|
|||||||
|
|
||||||
## Member types
|
## Member types
|
||||||
|
|
||||||
- **view_type** - the type of view returned by [`root()`](root.md) (`#!cpp basic_json_view<BasicJsonType, Mode>`)
|
- **view_type** - the type of view returned by [`root()`](root.md) (`#!cpp basic_json_view<BasicJsonType, Editable>`)
|
||||||
- **value_t** - the JSON type enumeration, see [`basic_json::value_t`](../basic_json/value_t.md)
|
- **value_t** - the JSON type enumeration, see [`basic_json::value_t`](../basic_json/value_t.md)
|
||||||
- **image_check** - how [`load()`](load.md) validates an image (`full`, `bounds`, `none`), see [`image_check`](load.md#image_check)
|
- **image_check** - how [`load()`](load.md) validates an image (`full`, `bounds`, `none`), see [`image_check`](load.md#image_check)
|
||||||
|
|
||||||
@@ -81,15 +80,15 @@ bookkeeping edits need, and calling any of them on one fails to compile (`#!cpp
|
|||||||
### Edits
|
### Edits
|
||||||
|
|
||||||
- [**set**](set.md) - replace a value, or set an object member, an array element, or the value a JSON pointer refers
|
- [**set**](set.md) - replace a value, or set an object member, an array element, or the value a JSON pointer refers
|
||||||
to (editable documents only)
|
to (`#!cpp Editable` documents only)
|
||||||
- [**push_back**](push_back.md) - append to an array (editable documents only)
|
- [**push_back**](push_back.md) - append to an array (`#!cpp Editable` documents only)
|
||||||
- [**insert**](insert.md) - insert an element into an array before a given position (editable documents only)
|
- [**insert**](insert.md) - insert an element into an array before a given position (`#!cpp Editable` documents only)
|
||||||
- [**erase**](erase.md) - remove an object member, an array element, or the value a JSON pointer refers to
|
- [**erase**](erase.md) - remove an object member, an array element, or the value a JSON pointer refers to
|
||||||
(editable documents only)
|
(`#!cpp Editable` documents only)
|
||||||
|
|
||||||
## Edits
|
## Edits
|
||||||
|
|
||||||
An editable document (`#!cpp view_mode::editable`) can be changed after parsing, with [`set`](set.md),
|
An editable document (`#!cpp Editable == true`) can be changed after parsing, with [`set`](set.md),
|
||||||
[`push_back`](push_back.md), [`insert`](insert.md), and [`erase`](erase.md);
|
[`push_back`](push_back.md), [`insert`](insert.md), and [`erase`](erase.md);
|
||||||
[`json_editable_document`](../json_editable_document.md) and
|
[`json_editable_document`](../json_editable_document.md) and
|
||||||
[`ordered_json_editable_document`](../ordered_json_editable_document.md) are the corresponding specializations. A few
|
[`ordered_json_editable_document`](../ordered_json_editable_document.md) are the corresponding specializations. A few
|
||||||
@@ -116,13 +115,6 @@ points apply to every edit:
|
|||||||
[`source_offset()`](../basic_json_view/source_offset.md) of a value introduced by an edit is
|
[`source_offset()`](../basic_json_view/source_offset.md) of a value introduced by an edit is
|
||||||
`#!cpp static_cast<std::size_t>(-1)`, the same value it reports for a decoded string.
|
`#!cpp static_cast<std::size_t>(-1)`, the same value it reports for a decoded string.
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
- [Zero-copy JSON views](../../features/json_view.md) - guide to documents and views
|
|
||||||
- [**basic_json_view**](../basic_json_view/index.md) - the non-owning handle to a value in a document
|
|
||||||
- [**json_document**](../json_document.md) and [**json_editable_document**](../json_editable_document.md) - the default specializations
|
|
||||||
- [**materialize**](../basic_json_view/materialize.md) - build a `basic_json` value from a view
|
|
||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.13.0.
|
- Added in version 3.13.0.
|
||||||
@@ -5,7 +5,7 @@ template<typename I, typename V>
|
|||||||
view_type insert(view_type array, I idx, V&& value);
|
view_type insert(view_type array, I idx, V&& value);
|
||||||
```
|
```
|
||||||
|
|
||||||
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md))
|
Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
|
||||||
has `insert`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
|
has `insert`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
|
||||||
|
|
||||||
Inserts `value` into `array` as a new element before position `idx`, which must not be past the end
|
Inserts `value` into `array` as a new element before position `idx`, which must not be past the end
|
||||||
|
|||||||
@@ -55,7 +55,10 @@ validate with overload 2 first, if the original vector must survive a failed loa
|
|||||||
|
|
||||||
## Exceptions
|
## Exceptions
|
||||||
|
|
||||||
Throws [`parse_error.116`](../../home/exceptions.md#jsonexceptionparse_error116) if `image` is not one
|
On a big-endian target, throws [`type_error.320`](../../home/exceptions.md#jsonexceptiontype_error320) -- the same
|
||||||
|
exception [`save()`](save.md#exceptions) throws there, since the image format is little-endian only.
|
||||||
|
|
||||||
|
Otherwise throws [`parse_error.116`](../../home/exceptions.md#jsonexceptionparse_error116) if `image` is not one
|
||||||
`save()` could have written, or fails the requested `check`:
|
`save()` could have written, or fails the requested `check`:
|
||||||
|
|
||||||
| message | when |
|
| message | when |
|
||||||
@@ -92,9 +95,6 @@ are checked once, however many nodes refer to it.
|
|||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
**Little-endian targets only.** On a big-endian target, `load()` does not exist, just like
|
|
||||||
[`save()`](save.md#notes): the image format is little-endian, so a call fails to compile.
|
|
||||||
|
|
||||||
### `image_check` { #image_check }
|
### `image_check` { #image_check }
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
|||||||
@@ -21,19 +21,17 @@ static basic_json_document parse(IteratorType first, IteratorType last,
|
|||||||
|
|
||||||
Both overloads accept the same JSON text as [`BasicJsonType::parse()`](../basic_json/parse.md), with the same
|
Both overloads accept the same JSON text as [`BasicJsonType::parse()`](../basic_json/parse.md), with the same
|
||||||
`ignore_comments`/`ignore_trailing_commas` options, but build a [`basic_json_document`](index.md) (a flat index into
|
`ignore_comments`/`ignore_trailing_commas` options, but build a [`basic_json_document`](index.md) (a flat index into
|
||||||
the input) instead of a tree of `BasicJsonType` values. They accept every input `BasicJsonType::parse()` takes, and in
|
the input) instead of a tree of `BasicJsonType` values. They take the same inputs as `BasicJsonType::parse()`. Byte
|
||||||
addition a pointer to a null-terminated wide string (which `BasicJsonType::parse()` rejects). Contiguous inputs of
|
inputs that stay alive can be borrowed; all others (streams, `#!cpp FILE*`, wide characters, non-contiguous
|
||||||
single-byte characters that stay alive can be borrowed; all others (streams, `#!cpp FILE*`, wide characters, which
|
iterators) are read into a copy that the document owns -- see the Notes. Use [`parse_copy()`](parse_copy.md) to get an
|
||||||
must be converted to UTF-8, and non-contiguous iterators) are read into a copy that the document owns -- see the
|
owned copy of a borrowable input as well.
|
||||||
Notes. Use [`parse_copy()`](parse_copy.md) to get an owned copy of a borrowable input
|
|
||||||
as well.
|
|
||||||
|
|
||||||
## Template parameters
|
## Template parameters
|
||||||
|
|
||||||
`InputType`
|
`InputType`
|
||||||
: A compatible input, for instance:
|
: A compatible input, for instance:
|
||||||
|
|
||||||
- a `#!cpp std::string`, a `#!cpp std::string_view` (C++17), or a C-style array of single-byte characters
|
- a `#!cpp std::string`, `#!cpp std::string_view`, or a C-style array of single-byte characters
|
||||||
- a pointer to a null-terminated string of single-byte characters (`#!cpp char`, `#!cpp signed char`,
|
- a pointer to a null-terminated string of single-byte characters (`#!cpp char`, `#!cpp signed char`,
|
||||||
`#!cpp unsigned char`, `#!cpp std::uint8_t`)
|
`#!cpp unsigned char`, `#!cpp std::uint8_t`)
|
||||||
- a container for which `#!cpp obj.data()` and `#!cpp obj.size()` give contiguous single-byte access, e.g.
|
- a container for which `#!cpp obj.data()` and `#!cpp obj.size()` give contiguous single-byte access, e.g.
|
||||||
@@ -49,10 +47,6 @@ as well.
|
|||||||
`#!cpp std::string::iterator`; the iterators of single-byte characters are borrowed or read like the byte inputs
|
`#!cpp std::string::iterator`; the iterators of single-byte characters are borrowed or read like the byte inputs
|
||||||
above, those of wide characters are converted to UTF-8
|
above, those of wide characters are converted to UTF-8
|
||||||
|
|
||||||
Ill-formed UTF-16 and UTF-32 sequences are rejected: a lone surrogate, a high surrogate that is not followed by its low
|
|
||||||
surrogate, and a code point above `U+10FFFF` all yield a
|
|
||||||
[`parse_error.101`](../../home/exceptions.md#jsonexceptionparse_error101).
|
|
||||||
|
|
||||||
## Parameters
|
## Parameters
|
||||||
|
|
||||||
`input` (in)
|
`input` (in)
|
||||||
@@ -161,5 +155,3 @@ buffer of a given length, pass a pair of pointers: `#!cpp parse(ptr, ptr + len)`
|
|||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.13.0.
|
- Added in version 3.13.0.
|
||||||
- Ill-formed UTF-16 and UTF-32 input (a lone surrogate, a high surrogate that is not followed by its low surrogate, or
|
|
||||||
a code point above `U+10FFFF`) is rejected with `parse_error.101`.
|
|
||||||
@@ -13,7 +13,7 @@ editable, and it does not have to be `array`'s own document -- which is copied s
|
|||||||
source document afterward; a `BasicJsonType` value; or anything `BasicJsonType` can be constructed from (numbers,
|
source document afterward; a `BasicJsonType` value; or anything `BasicJsonType` can be constructed from (numbers,
|
||||||
strings, `#!cpp bool`, `#!cpp nullptr`, containers, ...).
|
strings, `#!cpp bool`, `#!cpp nullptr`, containers, ...).
|
||||||
|
|
||||||
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md))
|
Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
|
||||||
has `push_back`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
|
has `push_back`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
|
||||||
|
|
||||||
## Template parameters
|
## Template parameters
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ An edited document is written in its *current* state, with its values in documen
|
|||||||
parser would have produced them for that JSON text: a member [`set`](set.md) added goes at the end, an
|
parser would have produced them for that JSON text: a member [`set`](set.md) added goes at the end, an
|
||||||
[`erase`](erase.md)d member leaves no trace, and a float that is not finite (NaN or positive/negative infinity)
|
[`erase`](erase.md)d member leaves no trace, and a float that is not finite (NaN or positive/negative infinity)
|
||||||
becomes null, the same substitution [`dump()`](../basic_json_view/dump.md) makes. The same document always saves to
|
becomes null, the same substitution [`dump()`](../basic_json_view/dump.md) makes. The same document always saves to
|
||||||
the same bytes -- also across `BasicJsonType` and the [`view_mode`](../view_mode.md), since the image reflects document order and
|
the same bytes -- also across `BasicJsonType` and `#!cpp Editable`, since the image reflects document order and
|
||||||
values only, not which specialization produced them.
|
values only, not which specialization produced them.
|
||||||
|
|
||||||
## Return value
|
## Return value
|
||||||
@@ -31,6 +31,9 @@ Throws [`type_error.320`](../../home/exceptions.md#jsonexceptiontype_error320) i
|
|||||||
[discarded](is_discarded.md) -- a default-constructed document, or one a failed [`parse()`](parse.md)/
|
[discarded](is_discarded.md) -- a default-constructed document, or one a failed [`parse()`](parse.md)/
|
||||||
[`read()`](read.md) with `allow_exceptions == false` left discarded.
|
[`read()`](read.md) with `allow_exceptions == false` left discarded.
|
||||||
|
|
||||||
|
On a big-endian target, throws `type_error.320` with a different message instead: the image format is little-endian
|
||||||
|
only (see [Notes](#notes)).
|
||||||
|
|
||||||
Throws [`out_of_range.416`](../../home/exceptions.md#jsonexceptionout_of_range416) if the node count, the text, or
|
Throws [`out_of_range.416`](../../home/exceptions.md#jsonexceptionout_of_range416) if the node count, the text, or
|
||||||
the decoded strings of the image would individually reach 4 GiB -- the same 32-bit offsets
|
the decoded strings of the image would individually reach 4 GiB -- the same 32-bit offsets
|
||||||
[`parse()`](parse.md#exceptions) and, for edits, [`set`](set.md)/[`push_back`](push_back.md) are already limited to.
|
[`parse()`](parse.md#exceptions) and, for edits, [`set`](set.md)/[`push_back`](push_back.md) are already limited to.
|
||||||
@@ -41,6 +44,9 @@ the decoded strings of the image would individually reach 4 GiB -- the same 32-b
|
|||||||
[json.exception.type_error.320] cannot save a discarded json_document
|
[json.exception.type_error.320] cannot save a discarded json_document
|
||||||
```
|
```
|
||||||
```
|
```
|
||||||
|
[json.exception.type_error.320] json_document images need a little-endian target
|
||||||
|
```
|
||||||
|
```
|
||||||
[json.exception.out_of_range.416] images of 4 GiB or more are not supported by json_document
|
[json.exception.out_of_range.416] images of 4 GiB or more are not supported by json_document
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -64,9 +70,9 @@ else -- see [`load`'s Exceptions](load.md#exceptions).
|
|||||||
the *same* build on the *same* (little-endian) machine -- not as a long-term storage format. Keep the original
|
the *same* build on the *same* (little-endian) machine -- not as a long-term storage format. Keep the original
|
||||||
JSON text if you need to read a saved document back with a future library version.
|
JSON text if you need to read a saved document back with a future library version.
|
||||||
|
|
||||||
**Little-endian targets only.** The image is written as raw little-endian bytes, with no byte-swapping. On a
|
**Little-endian only.** The image is written as raw little-endian bytes, with no byte-swapping. `save()` (and
|
||||||
big-endian target, `save()` and [`load`](load.md) do not exist: a call fails to compile rather than produce bytes a
|
[`load`](load.md)) throw `type_error.320` on a big-endian target rather than silently produce bytes a big-endian
|
||||||
big-endian reader could not interpret correctly.
|
reader could not interpret correctly.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ template<typename V>
|
|||||||
view_type set(const json_pointer& ptr, V&& value);
|
view_type set(const json_pointer& ptr, V&& value);
|
||||||
```
|
```
|
||||||
|
|
||||||
Only an **editable** document (`#!cpp view_mode::editable`, e.g. [`json_editable_document`](../json_editable_document.md))
|
Only an **editable** document (`#!cpp Editable == true`, e.g. [`json_editable_document`](../json_editable_document.md))
|
||||||
has `set`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
|
has `set`; calling it on a read-only `basic_json_document` fails to compile (`#!cpp static_assert`).
|
||||||
|
|
||||||
1. Replaces the value `target` refers to with `value`.
|
1. Replaces the value `target` refers to with `value`.
|
||||||
|
|||||||
@@ -80,8 +80,7 @@ None of these exceptions carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics
|
|||||||
Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes
|
Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes
|
||||||
constant time on average.
|
constant time on average.
|
||||||
2. Linear in `idx`: elements are skipped one at a time from the first one, since they are not a fixed size in the
|
2. Linear in `idx`: elements are skipped one at a time from the first one, since they are not a fixed size in the
|
||||||
index (unlike `BasicJsonType`'s array, which is random-access). Constant if no element of the array is an array
|
index (unlike `BasicJsonType`'s array, which is random-access).
|
||||||
or object, since the elements are then adjacent in the index.
|
|
||||||
3. Linear in the number of reference tokens of `ptr` and, for each token, in the number of members of the object at
|
3. Linear in the number of reference tokens of `ptr` and, for each token, in the number of members of the object at
|
||||||
that level (as 1.) or the index into the array (as 2.).
|
that level (as 1.) or the index into the array (as 2.).
|
||||||
|
|
||||||
|
|||||||
@@ -28,8 +28,7 @@ This exception does not carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics.
|
|||||||
|
|
||||||
Linear in the size of the array or object: unlike [`front()`](front.md), which only ever looks at the first
|
Linear in the size of the array or object: unlike [`front()`](front.md), which only ever looks at the first
|
||||||
element, `back()` has to walk every element to find where they end, since elements are not a fixed size in the
|
element, `back()` has to walk every element to find where they end, since elements are not a fixed size in the
|
||||||
index. Constant if no element of the array is an array or object, since the elements are then adjacent
|
index.
|
||||||
in the index.
|
|
||||||
|
|
||||||
## Notes
|
## Notes
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,20 @@
|
|||||||
# <small>nlohmann::basic_json_view::</small>basic_json_view
|
# <small>nlohmann::basic_json_view::</small>basic_json_view
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
// (1)
|
|
||||||
basic_json_view() noexcept = default;
|
basic_json_view() noexcept = default;
|
||||||
|
|
||||||
// (2) only for Mode == view_mode::editable
|
|
||||||
basic_json_view(const basic_json_view<BasicJsonType, view_mode::read_only>& other) noexcept;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
1. Creates an invalid (discarded) view: [`type()`](type.md) is `#!cpp value_t::discarded` and
|
Creates an invalid (discarded) view: [`type()`](type.md) is `#!cpp value_t::discarded` and
|
||||||
[`is_discarded()`](is_discarded.md) is `#!cpp true`.
|
[`is_discarded()`](is_discarded.md) is `#!cpp true`.
|
||||||
2. Converts a view of a read-only document into a view of an editable document (implicitly), so that a function
|
|
||||||
taking a [`json_editable_view`](../json_editable_view.md) accepts both. The result is a view of the same value of
|
|
||||||
the same read-only document. See [`view_mode`](../view_mode.md) for why there is no conversion the other way.
|
|
||||||
|
|
||||||
## Parameters
|
This is the only constructor a caller can use directly. Every other view is obtained from a
|
||||||
|
|
||||||
`other` (in)
|
|
||||||
: a view of a read-only document with the same `BasicJsonType`
|
|
||||||
|
|
||||||
Apart from these, a view is obtained from a
|
|
||||||
[`basic_json_document`](../basic_json_document/index.md), via [`root()`](../basic_json_document/root.md) or by
|
[`basic_json_document`](../basic_json_document/index.md), via [`root()`](../basic_json_document/root.md) or by
|
||||||
navigating into a container with [`operator[]`](operator[].md), [`at`](at.md), [`front`](front.md), [`back`](back.md),
|
navigating into a container with [`operator[]`](operator[].md), [`at`](at.md), [`front`](front.md), [`back`](back.md),
|
||||||
[`find`](find.md), or iteration.
|
[`find`](find.md), or iteration.
|
||||||
|
|
||||||
## Exception safety
|
## Exception safety
|
||||||
|
|
||||||
No-throw guarantee: these constructors never throw exceptions.
|
No-throw guarantee: this constructor never throws exceptions.
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
@@ -57,7 +45,6 @@ placeholder for "no value yet" and later be assigned a real view.
|
|||||||
|
|
||||||
- [is_discarded](is_discarded.md) - return whether the view is invalid
|
- [is_discarded](is_discarded.md) - return whether the view is invalid
|
||||||
- [root](../basic_json_document/root.md) - the view of a document's root value
|
- [root](../basic_json_document/root.md) - the view of a document's root value
|
||||||
- [view_mode](../view_mode.md) - whether a document and its views can be edited
|
|
||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
|
|||||||
@@ -19,36 +19,16 @@ For the types below, the conversion works directly on the flat index -- no `Basi
|
|||||||
document's own buffer of decoded strings (see [`get_string()`](get_string.md))
|
document's own buffer of decoded strings (see [`get_string()`](get_string.md))
|
||||||
- `BasicJsonType` -- equivalent to [`materialize()`](materialize.md)
|
- `BasicJsonType` -- equivalent to [`materialize()`](materialize.md)
|
||||||
- `basic_json_view` -- returns `#!cpp *this`
|
- `basic_json_view` -- returns `#!cpp *this`
|
||||||
- `#!cpp std::vector<U, A>` of the types above -- from an array, element by element, each converted with
|
- `#!cpp std::vector<U, A>` -- from an array, element by element, each converted with `#!cpp get<U>()`; `#!cpp
|
||||||
`#!cpp get<U>()`; `#!cpp std::vector<basic_json_view>` keeps a view of every element instead of a value
|
std::vector<basic_json_view>` keeps a view of every element instead of a value
|
||||||
- `#!cpp std::map<K, basic_json_view, C, A>` and `#!cpp std::unordered_map<K, basic_json_view, H, E, A>`, if `K` is
|
- `#!cpp std::map<K, V, C, A>` and `#!cpp std::unordered_map<K, V, H, E, A>`, if `K` is constructible from a `#!cpp
|
||||||
constructible from a `#!cpp (const char*, std::size_t)` pair -- views of the members of an object; with a repeated
|
(const char*, std::size_t)` pair -- from an object, member by member, each value converted with `#!cpp get<V>()`; with a repeated
|
||||||
key, the view of the *last* member is kept, as [`BasicJsonType::parse()`](../basic_json/parse.md) keeps its value
|
key, the *last* value is kept, as [`BasicJsonType::parse()`](../basic_json/parse.md) (and
|
||||||
|
[`materialize()`](materialize.md)) does; `#!cpp std::map<std::string, basic_json_view>` keeps views of the members
|
||||||
|
instead of values
|
||||||
|
|
||||||
A default-constructible `T` whose `from_json()` accepts views reads the view directly as well, without building a
|
Every other `T` -- `#!cpp std::list`, `#!cpp std::pair`, `#!cpp std::array`, enumerations, user types with a
|
||||||
`BasicJsonType` value. This covers the library's own conversions of the standard library types -- all other
|
`from_json()`, ... -- is converted by `#!cpp materialize().get<T>()`: the subtree is built into a real `BasicJsonType`
|
||||||
`#!cpp std::vector`s, `#!cpp std::map`/`#!cpp std::unordered_map` (also with other key types), `#!cpp std::list`,
|
|
||||||
`#!cpp std::deque`, `#!cpp std::set`, `#!cpp std::forward_list`, `#!cpp std::valarray`, `#!cpp std::array`, C arrays,
|
|
||||||
`#!cpp std::pair`, `#!cpp std::tuple`, `#!cpp std::optional`, enumerations, `#!cpp std::filesystem::path`, ... --
|
|
||||||
which give the same values as `#!cpp materialize().get<T>()` (from an object, a repeated key keeps the *last* value,
|
|
||||||
as [`BasicJsonType::parse()`](../basic_json/parse.md) does), as well as the types of the
|
|
||||||
[`NLOHMANN_DEFINE_TYPE_*`](../macros/nlohmann_define_type_intrusive.md) and
|
|
||||||
[`NLOHMANN_DEFINE_DERIVED_TYPE_*`](../macros/nlohmann_define_derived_type.md) macros and the enumerations of
|
|
||||||
[`NLOHMANN_JSON_SERIALIZE_ENUM`](../macros/nlohmann_json_serialize_enum.md), and any `from_json()` constrained to
|
|
||||||
accept views, e.g. with [`is_json_view`](../is_json_view.md). A `from_json()` that also accepts types that are no JSON
|
|
||||||
values at all, such as the unconstrained `#!cpp template<typename BasicJsonType> void from_json(const BasicJsonType&,
|
|
||||||
T&)` of [arbitrary type conversions](../../features/arbitrary_types.md), is not called with a view: it may use
|
|
||||||
`BasicJsonType` functions a view does not have. See [Writing generic code](../../features/json_view.md#writing-generic-code).
|
|
||||||
|
|
||||||
!!! warning "Duplicate keys"
|
|
||||||
|
|
||||||
Such a `from_json()` looks members up in the view, where a repeated key finds the **first** member, while
|
|
||||||
`#!cpp materialize().get<T>()` sees the **last** one, as [`BasicJsonType::parse()`](../basic_json/parse.md)
|
|
||||||
keeps it: for `#!cpp {"x":1,"x":2}`, `#!cpp get<point>()` reads `#!cpp x == 1`. See
|
|
||||||
[Duplicate keys](../../features/json_view.md#duplicate-keys).
|
|
||||||
|
|
||||||
Every other `T` -- user types whose `from_json()` is written for `BasicJsonType` only, types without a default
|
|
||||||
constructor, `#!cpp std::tuple`s holding references, ... -- is converted by `#!cpp materialize().get<T>()`: the subtree is built into a real `BasicJsonType`
|
|
||||||
value first (as [`BasicJsonType::parse()`](../basic_json/parse.md) would), and converted from there exactly as
|
value first (as [`BasicJsonType::parse()`](../basic_json/parse.md) would), and converted from there exactly as
|
||||||
[`BasicJsonType::get<T>()`](../basic_json/get.md) would convert it.
|
[`BasicJsonType::get<T>()`](../basic_json/get.md) would convert it.
|
||||||
|
|
||||||
@@ -73,15 +53,12 @@ Strong guarantee: there are no changes to the view or the document it refers to.
|
|||||||
throws for the same JSON type and `T`.
|
throws for the same JSON type and `T`.
|
||||||
- For `#!cpp std::vector<U, A>`: throws `type_error.302` if the value is not an array; otherwise, whatever converting
|
- For `#!cpp std::vector<U, A>`: throws `type_error.302` if the value is not an array; otherwise, whatever converting
|
||||||
an element to `U` throws.
|
an element to `U` throws.
|
||||||
- For `#!cpp std::map`/`#!cpp std::unordered_map` of views: throws `type_error.302` if the value is not an object.
|
- For `#!cpp std::map`/`#!cpp std::unordered_map`: throws `type_error.302` if the value is not an object; otherwise,
|
||||||
- For a `T` whose `from_json()` reads the view: whatever that `from_json()` throws; for the library's conversions and
|
whatever converting a member to the mapped type throws.
|
||||||
the macros, the same exceptions with the same messages as [`BasicJsonType::get<T>()`](../basic_json/get.md) of the
|
|
||||||
same value.
|
|
||||||
- For every other `T`: whatever [`materialize().get<T>()`](../basic_json/get.md) throws -- typically `type_error.302`,
|
- For every other `T`: whatever [`materialize().get<T>()`](../basic_json/get.md) throws -- typically `type_error.302`,
|
||||||
or whatever a user-provided `from_json()` throws.
|
or whatever a user-provided `from_json()` throws.
|
||||||
|
|
||||||
None of the exceptions thrown directly by this function or by a `from_json()` that reads the view (the first four
|
None of the exceptions thrown directly by this function (the first three bullets above) carry a
|
||||||
bullets above) carry a
|
|
||||||
[`JSON_DIAGNOSTICS`](../macros/json_diagnostics.md) path: the view has no `BasicJsonType` value to point at. An
|
[`JSON_DIAGNOSTICS`](../macros/json_diagnostics.md) path: the view has no `BasicJsonType` value to point at. An
|
||||||
exception thrown while converting through `materialize()` (the last bullet) is different: it is thrown by a real
|
exception thrown while converting through `materialize()` (the last bullet) is different: it is thrown by a real
|
||||||
`BasicJsonType` value, so it **does** carry a `JSON_DIAGNOSTICS` path if `BasicJsonType` was built with it enabled.
|
`BasicJsonType` value, so it **does** carry a `JSON_DIAGNOSTICS` path if `BasicJsonType` was built with it enabled.
|
||||||
@@ -99,8 +76,6 @@ document.
|
|||||||
- `#!cpp std::map`/`#!cpp std::unordered_map`: linear in the number of members for walking them, plus the container's
|
- `#!cpp std::map`/`#!cpp std::unordered_map`: linear in the number of members for walking them, plus the container's
|
||||||
own insertion cost per member (logarithmic for `#!cpp std::map`, amortized constant for `#!cpp
|
own insertion cost per member (logarithmic for `#!cpp std::map`, amortized constant for `#!cpp
|
||||||
std::unordered_map`), times the complexity of converting one member to the mapped type.
|
std::unordered_map`), times the complexity of converting one member to the mapped type.
|
||||||
- a `T` whose `from_json()` reads the view: the cost of that `from_json()`; for the macros, linear in the number of
|
|
||||||
members times the cost of a lookup, plus the cost of converting the members.
|
|
||||||
- every other `T`: the cost of [`materialize()`](materialize.md) and of destroying the `BasicJsonType` value
|
- every other `T`: the cost of [`materialize()`](materialize.md) and of destroying the `BasicJsonType` value
|
||||||
afterwards, plus that of [`BasicJsonType::get<T>()`](../basic_json/get.md).
|
afterwards, plus that of [`BasicJsonType::get<T>()`](../basic_json/get.md).
|
||||||
|
|
||||||
|
|||||||
@@ -5,13 +5,12 @@ template<typename T>
|
|||||||
T& get_to(T& v) const;
|
T& get_to(T& v) const;
|
||||||
```
|
```
|
||||||
|
|
||||||
Converts the value to `T` and stores it in `v`, in one of three ways:
|
Converts the value to `T` and stores it in `v`, in one of two ways:
|
||||||
|
|
||||||
- For the types that [`get<T>()`](get.md) converts directly -- arithmetic types, `#!cpp bool`,
|
- For the types that [`get<T>()`](get.md) converts directly -- arithmetic types, `#!cpp bool`,
|
||||||
`#!cpp std::nullptr_t`, [`string_view_t`](index.md#member-types), `#!cpp std::basic_string`, `BasicJsonType`,
|
`#!cpp std::nullptr_t`, [`string_view_t`](index.md#member-types), `#!cpp std::basic_string`, `BasicJsonType`,
|
||||||
`basic_json_view`, vectors of these, and maps of views -- `v` is assigned: `#!cpp v = get<T>()`.
|
`basic_json_view`, `#!cpp std::vector`, and `#!cpp std::map`/`#!cpp std::unordered_map` with string keys -- `v` is
|
||||||
- For a type whose `from_json()` accepts views (see [`get<T>()`](get.md)), that `from_json()` is called with the
|
assigned: `#!cpp v = get<T>()`.
|
||||||
view and the existing `v`, without building a `BasicJsonType` value.
|
|
||||||
- For every other type, the subtree is built into a `BasicJsonType` value and `#!cpp materialize().get_to(v)` is
|
- For every other type, the subtree is built into a `BasicJsonType` value and `#!cpp materialize().get_to(v)` is
|
||||||
called, i.e. `from_json()` is invoked with the existing `v`, exactly as
|
called, i.e. `from_json()` is invoked with the existing `v`, exactly as
|
||||||
[`BasicJsonType::get_to()`](../basic_json/get_to.md) does.
|
[`BasicJsonType::get_to()`](../basic_json/get_to.md) does.
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
template<typename BasicJsonType, view_mode Mode = view_mode::read_only>
|
template<typename BasicJsonType, bool Editable = false>
|
||||||
class basic_json_view;
|
class basic_json_view;
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -37,7 +37,7 @@ Moving the document itself does not invalidate its views: the index is heap-allo
|
|||||||
`#!cpp operator ValueType()`), and no `#!cpp get_ptr()` or `#!cpp get_ref()`; see [`get`](get.md).
|
`#!cpp operator ValueType()`), and no `#!cpp get_ptr()` or `#!cpp get_ref()`; see [`get`](get.md).
|
||||||
|
|
||||||
`basic_json_view` itself is always read-only -- it never has a `set` or `push_back` of its own. A view of an
|
`basic_json_view` itself is always read-only -- it never has a `set` or `push_back` of its own. A view of an
|
||||||
**editable** document (`#!cpp view_mode::editable`) sees every edit made through
|
**editable** document (`#!cpp Editable == true`) sees every edit made through
|
||||||
[`basic_json_document::set`](../basic_json_document/set.md) and
|
[`basic_json_document::set`](../basic_json_document/set.md) and
|
||||||
[`basic_json_document::push_back`](../basic_json_document/push_back.md): once a value is changed, every view that
|
[`basic_json_document::push_back`](../basic_json_document/push_back.md): once a value is changed, every view that
|
||||||
still refers to it -- including ones taken before the change -- reads the new value. See
|
still refers to it -- including ones taken before the change -- reads the new value. See
|
||||||
@@ -49,10 +49,9 @@ still refers to it -- including ones taken before the change -- reads the new va
|
|||||||
: a specialization of [`basic_json`](../basic_json/index.md), matching the
|
: a specialization of [`basic_json`](../basic_json/index.md), matching the
|
||||||
[`basic_json_document`](../basic_json_document/index.md) the view was taken from.
|
[`basic_json_document`](../basic_json_document/index.md) the view was taken from.
|
||||||
|
|
||||||
`Mode`
|
`Editable`
|
||||||
: a [`view_mode`](../view_mode.md): whether the view is of an editable document, matching the
|
: whether the view is of an editable document, matching the [`basic_json_document`](../basic_json_document/index.md)
|
||||||
[`basic_json_document`](../basic_json_document/index.md) it was taken from (optional, `#!cpp view_mode::read_only` by
|
it was taken from (optional, `#!cpp false` by default). See [Edits](../basic_json_document/index.md#edits).
|
||||||
default). See [Edits](../basic_json_document/index.md#edits).
|
|
||||||
|
|
||||||
## Specializations
|
## Specializations
|
||||||
|
|
||||||
@@ -64,7 +63,6 @@ still refers to it -- including ones taken before the change -- reads the new va
|
|||||||
|
|
||||||
## Member types
|
## Member types
|
||||||
|
|
||||||
- **basic_json_type** - `BasicJsonType` (it also marks the class for [`is_json_view`](../is_json_view.md))
|
|
||||||
- **value_t** - the JSON type enumeration, see [`basic_json::value_t`](../basic_json/value_t.md)
|
- **value_t** - the JSON type enumeration, see [`basic_json::value_t`](../basic_json/value_t.md)
|
||||||
- **string_t**, **number_integer_t**, **number_unsigned_t**, **number_float_t**, **json_pointer** - the corresponding
|
- **string_t**, **number_integer_t**, **number_unsigned_t**, **number_float_t**, **json_pointer** - the corresponding
|
||||||
member types of `BasicJsonType`
|
member types of `BasicJsonType`
|
||||||
@@ -145,13 +143,6 @@ still refers to it -- including ones taken before the change -- reads the new va
|
|||||||
|
|
||||||
- [**source_offset**](source_offset.md) - byte offset of this value in the document's source text
|
- [**source_offset**](source_offset.md) - byte offset of this value in the document's source text
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
- [Zero-copy JSON views](../../features/json_view.md) - guide to documents and views
|
|
||||||
- [**basic_json_document**](../basic_json_document/index.md) - the document that owns the parsed data
|
|
||||||
- [**json_view**](../json_view.md) and [**json_editable_view**](../json_editable_view.md) - the default specializations
|
|
||||||
- [**materialize**](materialize.md) - build a `basic_json` value from a view
|
|
||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.13.0.
|
- Added in version 3.13.0.
|
||||||
@@ -86,8 +86,7 @@ None of these exceptions carry a [`JSON_DIAGNOSTICS`](../macros/json_diagnostics
|
|||||||
Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes
|
Objects with 128 or more members get a hash index of their keys while parsing, so that a lookup in them takes
|
||||||
constant time on average.
|
constant time on average.
|
||||||
2. Linear in `idx`: elements are skipped one at a time from the first one, since they are not a fixed size in the
|
2. Linear in `idx`: elements are skipped one at a time from the first one, since they are not a fixed size in the
|
||||||
index (unlike `BasicJsonType`'s array, which is random-access). Constant if no element of the array is an array
|
index (unlike `BasicJsonType`'s array, which is random-access).
|
||||||
or object, since the elements are then adjacent in the index.
|
|
||||||
3. Linear in the number of reference tokens of `ptr` and, for each token, in the number of members of the object at
|
3. Linear in the number of reference tokens of `ptr` and, for each token, in the number of members of the object at
|
||||||
that level (as 1.) or the index into the array (as 2.).
|
that level (as 1.) or the index into the array (as 2.).
|
||||||
|
|
||||||
|
|||||||
@@ -51,4 +51,4 @@ byte_container_with_subtype(container_type&& container, subtype_type subtype);
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
Added in version 3.8.0.
|
Since version 3.8.0.
|
||||||
@@ -39,4 +39,4 @@ Constant.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
Added in version 3.8.0.
|
Since version 3.8.0.
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
# <small>nlohmann::byte_container_with_subtype::</small>container_type
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
using container_type = BinaryType;
|
||||||
|
```
|
||||||
|
|
||||||
|
The type of the underlying binary container, forwarded from the `BinaryType` template parameter that
|
||||||
|
`byte_container_with_subtype` is instantiated with. `byte_container_with_subtype` publicly inherits from
|
||||||
|
`container_type`.
|
||||||
|
|
||||||
|
See [`basic_json::binary_t`](../basic_json/binary_t.md) for the type typically used to instantiate
|
||||||
|
`BinaryType`.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
??? example
|
||||||
|
|
||||||
|
The example shows the type `container_type`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
--8<-- "examples/byte_container_with_subtype__container_type.cpp"
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```
|
||||||
|
--8<-- "examples/byte_container_with_subtype__container_type.output"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Version history
|
||||||
|
|
||||||
|
- Since version 3.8.0.
|
||||||
@@ -42,4 +42,4 @@ Constant.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
Added in version 3.8.0.
|
Since version 3.8.0.
|
||||||
@@ -6,7 +6,7 @@ class byte_container_with_subtype : public BinaryType;
|
|||||||
```
|
```
|
||||||
|
|
||||||
This type extends the template parameter `BinaryType` provided to [`basic_json`](../basic_json/index.md) with a subtype
|
This type extends the template parameter `BinaryType` provided to [`basic_json`](../basic_json/index.md) with a subtype
|
||||||
used by BSON, CBOR, and MessagePack. This type exists so that the user does not have to specify a type themselves with a
|
used by BSON and MessagePack. This type exists so that the user does not have to specify a type themselves with a
|
||||||
specific naming scheme in order to override the binary type.
|
specific naming scheme in order to override the binary type.
|
||||||
|
|
||||||
## Template parameters
|
## Template parameters
|
||||||
@@ -29,14 +29,6 @@ specific naming scheme in order to override the binary type.
|
|||||||
- [**has_subtype**](has_subtype.md) - return whether the value has a subtype
|
- [**has_subtype**](has_subtype.md) - return whether the value has a subtype
|
||||||
- [**clear_subtype**](clear_subtype.md) - clears the binary subtype
|
- [**clear_subtype**](clear_subtype.md) - clears the binary subtype
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
- [Binary values](../../features/binary_values.md) - how binary values and subtypes are handled
|
|
||||||
- [**binary**](../basic_json/binary.md) - create a binary array value
|
|
||||||
- [**get_binary**](../basic_json/get_binary.md) - get a reference to the stored binary value
|
|
||||||
- [**binary_t**](../basic_json/binary_t.md) - type for binary arrays
|
|
||||||
- [Binary formats](../../features/binary_formats/index.md) - formats that can store binary values
|
|
||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 3.8.0.
|
- Added in version 3.8.0.
|
||||||
|
|||||||
@@ -44,4 +44,4 @@ Constant.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
Added in version 3.8.0.
|
Since version 3.8.0.
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# <small>nlohmann::byte_container_with_subtype::</small>subtype_type
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
using subtype_type = std::uint64_t;
|
||||||
|
```
|
||||||
|
|
||||||
|
The type used to store the optional binary subtype tag. See [`subtype`](subtype.md) and
|
||||||
|
[`set_subtype`](set_subtype.md).
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
??? example
|
||||||
|
|
||||||
|
The example shows the type `subtype_type`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
--8<-- "examples/byte_container_with_subtype__subtype_type.cpp"
|
||||||
|
```
|
||||||
|
|
||||||
|
Output:
|
||||||
|
|
||||||
|
```
|
||||||
|
--8<-- "examples/byte_container_with_subtype__subtype_type.output"
|
||||||
|
```
|
||||||
|
|
||||||
|
## Version history
|
||||||
|
|
||||||
|
- Since version 3.8.0.
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
# <small>nlohmann::</small>is_json_view
|
|
||||||
|
|
||||||
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
template<typename T>
|
|
||||||
struct is_json_view;
|
|
||||||
```
|
|
||||||
|
|
||||||
A type trait: `#!cpp is_json_view<T>::value` is `#!cpp true` if `T` is a [`basic_json_view`](basic_json_view/index.md)
|
|
||||||
of any `BasicJsonType` and any [`view_mode`](view_mode.md) -- [`json_view`](json_view.md),
|
|
||||||
[`json_editable_view`](json_editable_view.md), [`ordered_json_view`](ordered_json_view.md), ... -- and
|
|
||||||
`#!cpp false` otherwise, for [`basic_json`](basic_json/index.md) as well.
|
|
||||||
|
|
||||||
It is meant for constraining templates that accept views, in particular a `from_json()` that should read views
|
|
||||||
directly: [`get<T>()`](basic_json_view/get.md) on a view calls a `from_json()` with the view only if that `from_json()`
|
|
||||||
accepts views but not arbitrary types.
|
|
||||||
|
|
||||||
## Template parameters
|
|
||||||
|
|
||||||
`T`
|
|
||||||
: the type to check (without cv-qualifiers or references)
|
|
||||||
|
|
||||||
## Notes
|
|
||||||
|
|
||||||
A view is recognized by its member type `basic_json_type` (a [`basic_json`](basic_json/index.md) specialization) and
|
|
||||||
its [`materialize()`](basic_json_view/materialize.md) returning that type, so the trait needs no declaration of the
|
|
||||||
view class.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
??? example
|
|
||||||
|
|
||||||
The example below shows the trait, and a `from_json()` constrained to `json` values and views: `get<T>()` on a view
|
|
||||||
reads the view directly.
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
--8<-- "examples/is_json_view.cpp"
|
|
||||||
```
|
|
||||||
|
|
||||||
Output:
|
|
||||||
|
|
||||||
```
|
|
||||||
--8<-- "examples/is_json_view.output"
|
|
||||||
```
|
|
||||||
|
|
||||||
## See also
|
|
||||||
|
|
||||||
- [basic_json_view](basic_json_view/index.md) - a handle to one value of a document
|
|
||||||
- [get](basic_json_view/get.md) - which conversions read the view directly
|
|
||||||
- [view_mode](view_mode.md) - read-only or editable
|
|
||||||
- [Writing generic code](../features/json_view.md#writing-generic-code) - functions and `from_json()` for views
|
|
||||||
|
|
||||||
## Version history
|
|
||||||
|
|
||||||
Added in version 3.13.0.
|
|
||||||
@@ -30,4 +30,4 @@ types.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
Added in version 1.0.0.
|
Since version 1.0.0.
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using json_document = basic_json_document<json, view_mode::read_only>;
|
using json_document = basic_json_document<json>;
|
||||||
```
|
```
|
||||||
|
|
||||||
This type is a [`basic_json_document`](basic_json_document/index.md) of the default [`json`](json.md)
|
This type is a [`basic_json_document`](basic_json_document/index.md) of the default [`json`](json.md)
|
||||||
@@ -32,4 +32,4 @@ specialization.
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
Added in version 3.13.0.
|
Since version 3.13.0.
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using json_editable_document = basic_json_document<json, view_mode::editable>;
|
using json_editable_document = basic_json_document<json, true>;
|
||||||
```
|
```
|
||||||
|
|
||||||
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
|
||||||
@@ -40,4 +40,4 @@ parsing, without ever rewriting the source text -- see [Edits](basic_json_docume
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
Added in version 3.13.0.
|
Since version 3.13.0.
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
<small>Defined in header `<nlohmann/json_view.hpp>`</small>
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
using json_editable_view = basic_json_view<json, view_mode::editable>;
|
using json_editable_view = basic_json_view<json, true>;
|
||||||
```
|
```
|
||||||
|
|
||||||
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
|
||||||
@@ -38,4 +38,4 @@ still referring to it seeing the change, see [Edits](basic_json_document/index.m
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
Added in version 3.13.0.
|
Since version 3.13.0.
|
||||||
@@ -51,5 +51,5 @@ operator string_t() const
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 2.0.0.
|
- Since version 2.0.0.
|
||||||
- Changed type to `string_t` and deprecated in version 3.11.0.
|
- Changed type to `string_t` and deprecated in version 3.11.0.
|
||||||
@@ -12,8 +12,7 @@ Parent of this JSON pointer; in case this JSON pointer is the root, the root its
|
|||||||
|
|
||||||
## Exception safety
|
## Exception safety
|
||||||
|
|
||||||
Strong exception safety: if an exception occurs, the original value stays intact. The function is not `#!cpp noexcept`
|
No-throw guarantee: this function never throws exceptions.
|
||||||
and allocates a new JSON pointer, so it can throw `#!cpp std::bad_alloc`; it throws no library exceptions.
|
|
||||||
|
|
||||||
## Complexity
|
## Complexity
|
||||||
|
|
||||||
|
|||||||
@@ -49,5 +49,5 @@ ptr == json_pointer(ptr.to_string());
|
|||||||
|
|
||||||
## Version history
|
## Version history
|
||||||
|
|
||||||
- Added in version 2.0.0.
|
- Since version 2.0.0.
|
||||||
- Changed return type to `string_t` in version 3.11.0.
|
- Changed return type to `string_t` in version 3.11.0.
|
||||||
Loaded 100 of 791 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user