mirror of
https://github.com/nlohmann/json.git
synced 2026-09-09 17:58:00 +00:00
Compare commits
20
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
973972bb5e | ||
|
|
d91dff77e6 | ||
|
|
9edfb53906 | ||
|
|
e27d1192e0 | ||
|
|
47643785a6 | ||
|
|
d61ef62fc7 | ||
|
|
79f990dd04 | ||
|
|
b17c272f46 | ||
|
|
8e26b9d4b3 | ||
|
|
8c8d14cb4e | ||
|
|
d22c5c7641 | ||
|
|
c9f1d2d646 | ||
|
|
a316738cfa | ||
|
|
8c38e270b5 | ||
|
|
c9477ccf91 | ||
|
|
d6660cf718 | ||
|
|
c05347dd54 | ||
|
|
6d86cc0f6b | ||
|
|
faa35cc647 | ||
|
|
7b2d73cf2e |
@@ -108,7 +108,9 @@ The tests are located in [`tests/src/unit-*.cpp`](https://github.com/nlohmann/js
|
|||||||
are structured along the features of the library or the nature of the tests. Usually, it should be clear from the
|
are structured along the features of the library or the nature of the tests. Usually, it should be clear from the
|
||||||
context which existing file needs to be extended, and only very few cases require creating new test files.
|
context which existing file needs to be extended, and only very few cases require creating new test files.
|
||||||
|
|
||||||
When fixing a bug, edit `unit-regression2.cpp` and add a section referencing the fixed issue.
|
When fixing a bug, edit `unit-regression3.cpp` and add a section referencing the fixed issue.
|
||||||
|
`unit-regression2.cpp` holds the older tests; the two files exist because a single one grew large enough for the
|
||||||
|
MinGW linker to fail relocating it, so please keep adding to the smaller file rather than growing the larger one.
|
||||||
|
|
||||||
#### Exceptions
|
#### Exceptions
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ jobs:
|
|||||||
runs-on: ubuntu-22.04
|
runs-on: ubuntu-22.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ jobs:
|
|||||||
pull-requests: write
|
pull-requests: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ jobs:
|
|||||||
security-events: write
|
security-events: write
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ jobs:
|
|||||||
runs-on: ubuntu-22.04
|
runs-on: ubuntu-22.04
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -60,7 +60,7 @@ jobs:
|
|||||||
target: [ci_test_amalgamation, ci_test_single_header, ci_cppcheck, ci_cpplint, ci_reproducible_tests, ci_non_git_tests, ci_offline_testdata, ci_reuse_compliance, ci_test_valgrind]
|
target: [ci_test_amalgamation, ci_test_single_header, ci_cppcheck, ci_cpplint, ci_reproducible_tests, ci_non_git_tests, ci_offline_testdata, ci_reuse_compliance, ci_test_valgrind]
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -118,7 +118,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -369,7 +369,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
@@ -392,7 +392,7 @@ jobs:
|
|||||||
target: [ci_test_examples, ci_test_build_documentation]
|
target: [ci_test_examples, ci_test_build_documentation]
|
||||||
steps:
|
steps:
|
||||||
- name: Harden Runner
|
- name: Harden Runner
|
||||||
uses: step-security/harden-runner@05e31511f85b41b11d1cf0ef85d0992719546e2c # v2.21.0
|
uses: step-security/harden-runner@e14015d583714f6e62063499dc959a02595150a1 # v2.21.1
|
||||||
with:
|
with:
|
||||||
egress-policy: audit
|
egress-policy: audit
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
.PHONY: pretty clean ChangeLog.md release
|
.PHONY: pretty clean ChangeLog.md release update_hedley update_hedley_undef
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
# configuration
|
# configuration
|
||||||
@@ -41,6 +41,8 @@ all:
|
|||||||
@echo "fuzz_testing_ubjson - prepare fuzz testing of the UBJSON parser"
|
@echo "fuzz_testing_ubjson - prepare fuzz testing of the UBJSON parser"
|
||||||
@echo "pretty - beautify code with Artistic Style"
|
@echo "pretty - beautify code with Artistic Style"
|
||||||
@echo "run_benchmarks - build and run benchmarks"
|
@echo "run_benchmarks - build and run benchmarks"
|
||||||
|
@echo "update_hedley - download Hedley and regenerate hedley.hpp / hedley_undef.hpp"
|
||||||
|
@echo "update_hedley_undef - rebuild hedley_undef.hpp from the JSON_HEDLEY_* #define names in hedley.hpp"
|
||||||
|
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
@@ -241,11 +243,24 @@ update_hedley:
|
|||||||
rm -f include/nlohmann/thirdparty/hedley/hedley.hpp include/nlohmann/thirdparty/hedley/hedley_undef.hpp
|
rm -f include/nlohmann/thirdparty/hedley/hedley.hpp include/nlohmann/thirdparty/hedley/hedley_undef.hpp
|
||||||
curl https://raw.githubusercontent.com/nemequ/hedley/master/hedley.h -o include/nlohmann/thirdparty/hedley/hedley.hpp
|
curl https://raw.githubusercontent.com/nemequ/hedley/master/hedley.h -o include/nlohmann/thirdparty/hedley/hedley.hpp
|
||||||
$(SED) -i 's/HEDLEY_/JSON_HEDLEY_/g' include/nlohmann/thirdparty/hedley/hedley.hpp
|
$(SED) -i 's/HEDLEY_/JSON_HEDLEY_/g' include/nlohmann/thirdparty/hedley/hedley.hpp
|
||||||
grep "[[:blank:]]*#[[:blank:]]*undef" include/nlohmann/thirdparty/hedley/hedley.hpp | grep -v "__" | sort | uniq | $(SED) 's/ //g' | $(SED) 's/undef/undef /g' > include/nlohmann/thirdparty/hedley/hedley_undef.hpp
|
|
||||||
$(SED) -i '1s/^/#pragma once\n\n/' include/nlohmann/thirdparty/hedley/hedley.hpp
|
$(SED) -i '1s/^/#pragma once\n\n/' include/nlohmann/thirdparty/hedley/hedley.hpp
|
||||||
$(SED) -i '1s/^/#pragma once\n\n/' include/nlohmann/thirdparty/hedley/hedley_undef.hpp
|
$(MAKE) update_hedley_undef
|
||||||
$(MAKE) amalgamate
|
$(MAKE) amalgamate
|
||||||
|
|
||||||
|
# Rebuild hedley_undef.hpp from every JSON_HEDLEY_* name that hedley.hpp
|
||||||
|
# #defines. Hedley does not #undef all of its public macros internally (see
|
||||||
|
# #5408), so grepping those #undef lines misses names such as
|
||||||
|
# JSON_HEDLEY_PRAGMA. cmake/scripts/gen_hedley_undef_check.cmake is the
|
||||||
|
# single source of truth for this extraction (tests/CMakeLists.txt uses the
|
||||||
|
# same script, in MODE=checks, to generate the matching leak-check test), so
|
||||||
|
# the vendored header, the generated #undef list, and the regression test
|
||||||
|
# cannot drift apart.
|
||||||
|
update_hedley_undef:
|
||||||
|
cmake -DHEDLEY_HPP=include/nlohmann/thirdparty/hedley/hedley.hpp \
|
||||||
|
-DOUTPUT=include/nlohmann/thirdparty/hedley/hedley_undef.hpp \
|
||||||
|
-DMODE=undef \
|
||||||
|
-P cmake/scripts/gen_hedley_undef_check.cmake
|
||||||
|
|
||||||
##########################################################################
|
##########################################################################
|
||||||
# serve_header.py
|
# serve_header.py
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
# Shared extractor for the JSON_HEDLEY_* macro names defined in hedley.hpp.
|
||||||
|
#
|
||||||
|
# Every macro that hedley.hpp #defines must be #undef-ed again once json.hpp
|
||||||
|
# has been fully processed (see include/nlohmann/detail/macro_unscope.hpp
|
||||||
|
# and https://github.com/nlohmann/json/issues/5408). Deriving the macro list
|
||||||
|
# straight from hedley.hpp here -- instead of hand-maintaining it in two
|
||||||
|
# places -- means hedley_undef.hpp and the regression test that checks for
|
||||||
|
# leaked macros can never drift apart, even after a future `make
|
||||||
|
# update_hedley` pulls in new macros from upstream Hedley.
|
||||||
|
#
|
||||||
|
# MODE=undef (default): write hedley_undef.hpp (SPDX header, #pragma once,
|
||||||
|
# one #undef per macro name) -- used by `make update_hedley_undef`
|
||||||
|
# MODE=checks: write one #ifdef/FAIL_CHECK/#endif per macro name,
|
||||||
|
# meant to be #include-d inside a TEST_CASE -- used by
|
||||||
|
# tests/CMakeLists.txt to (re)generate the include for
|
||||||
|
# tests/src/unit-no-macro-leak.cpp
|
||||||
|
#
|
||||||
|
# Required variables:
|
||||||
|
# HEDLEY_HPP path to include/nlohmann/thirdparty/hedley/hedley.hpp
|
||||||
|
# OUTPUT path of the file to (over)write
|
||||||
|
# Optional:
|
||||||
|
# MODE "undef" (default) or "checks"
|
||||||
|
|
||||||
|
if(NOT DEFINED HEDLEY_HPP OR NOT DEFINED OUTPUT)
|
||||||
|
message(FATAL_ERROR "HEDLEY_HPP and OUTPUT must be set")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT EXISTS "${HEDLEY_HPP}")
|
||||||
|
message(FATAL_ERROR "Hedley header not found: ${HEDLEY_HPP}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT DEFINED MODE)
|
||||||
|
set(MODE undef)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
if(NOT MODE STREQUAL "undef" AND NOT MODE STREQUAL "checks")
|
||||||
|
message(FATAL_ERROR "MODE must be undef or checks, got: ${MODE}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Line-anchored, like `grep -oE "^[[:blank:]]*#[[:blank:]]*define[[:blank:]]+JSON_HEDLEY_[A-Za-z0-9_]+"`.
|
||||||
|
# Unanchored matching would also pick up JSON_HEDLEY_* mentions inside
|
||||||
|
# comments or string literals elsewhere in the file, which must not turn
|
||||||
|
# into #undef lines.
|
||||||
|
file(STRINGS "${HEDLEY_HPP}" hedley_lines)
|
||||||
|
set(macro_names)
|
||||||
|
foreach(line IN LISTS hedley_lines)
|
||||||
|
if("${line}" MATCHES "^[ \t]*#[ \t]*define[ \t]+(JSON_HEDLEY_[A-Za-z0-9_]+)")
|
||||||
|
list(APPEND macro_names "${CMAKE_MATCH_1}")
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
if(NOT macro_names)
|
||||||
|
message(FATAL_ERROR "No JSON_HEDLEY_* macros found in ${HEDLEY_HPP}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
list(REMOVE_DUPLICATES macro_names)
|
||||||
|
# Lexicographic, locale-independent (ASCII-only names) -- matches `LC_ALL=C sort`.
|
||||||
|
list(SORT macro_names COMPARE STRING)
|
||||||
|
list(LENGTH macro_names macro_count)
|
||||||
|
|
||||||
|
set(generated "")
|
||||||
|
if(MODE STREQUAL "undef")
|
||||||
|
# Same banner `make update_hedley_undef` would stamp by hand, so the
|
||||||
|
# recipe is self-contained and its output is byte-stable across reruns.
|
||||||
|
# The embedded SPDX tags below are part of the *generated* file's
|
||||||
|
# content, not a REUSE header for this .cmake script itself (which is
|
||||||
|
# already covered by the blanket "Files: *" rule in .reuse/dep5) -- keep
|
||||||
|
# them wrapped in REUSE-IgnoreStart/End so `reuse lint` does not try to
|
||||||
|
# parse "MIT\n")" as this file's own SPDX-License-Identifier value.
|
||||||
|
# REUSE-IgnoreStart
|
||||||
|
string(APPEND generated "// __ _____ _____ _____\n")
|
||||||
|
string(APPEND generated "// __| | __| | | | JSON for Modern C++\n")
|
||||||
|
string(APPEND generated "// | | |__ | | | | | | version 3.12.0\n")
|
||||||
|
string(APPEND generated "// |_____|_____|_____|_|___| https://github.com/nlohmann/json\n")
|
||||||
|
string(APPEND generated "//\n")
|
||||||
|
string(APPEND generated "// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>\n")
|
||||||
|
string(APPEND generated "// SPDX-License-Identifier: MIT\n")
|
||||||
|
# REUSE-IgnoreEnd
|
||||||
|
string(APPEND generated "\n")
|
||||||
|
string(APPEND generated "#pragma once\n")
|
||||||
|
string(APPEND generated "\n")
|
||||||
|
foreach(name IN LISTS macro_names)
|
||||||
|
string(APPEND generated "#undef ${name}\n")
|
||||||
|
endforeach()
|
||||||
|
else()
|
||||||
|
string(APPEND generated "// This file is generated by cmake/scripts/gen_hedley_undef_check.cmake\n")
|
||||||
|
string(APPEND generated "// from include/nlohmann/thirdparty/hedley/hedley.hpp. Do not edit it by\n")
|
||||||
|
string(APPEND generated "// hand -- it is regenerated on every build. ${macro_count} macros checked.\n\n")
|
||||||
|
foreach(name IN LISTS macro_names)
|
||||||
|
string(APPEND generated "#ifdef ${name}\n")
|
||||||
|
string(APPEND generated " FAIL_CHECK(\"${name} leaked after including nlohmann/json.hpp\");\n")
|
||||||
|
string(APPEND generated "#endif\n")
|
||||||
|
endforeach()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
get_filename_component(output_dir "${OUTPUT}" DIRECTORY)
|
||||||
|
if(output_dir)
|
||||||
|
file(MAKE_DIRECTORY "${output_dir}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Avoid rewriting the file (and busting downstream incremental rebuilds)
|
||||||
|
# when the content has not actually changed.
|
||||||
|
set(write_output TRUE)
|
||||||
|
if(EXISTS "${OUTPUT}")
|
||||||
|
file(READ "${OUTPUT}" existing_content)
|
||||||
|
if(existing_content STREQUAL generated)
|
||||||
|
set(write_output FALSE)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
if(write_output)
|
||||||
|
file(WRITE "${OUTPUT}" "${generated}")
|
||||||
|
endif()
|
||||||
@@ -34,6 +34,10 @@ Strong guarantee: if an exception is thrown, there are no changes in the JSON va
|
|||||||
("add", "remove", "move")
|
("add", "remove", "move")
|
||||||
- Throws [`out_of_range.411`](../../home/exceptions.md#jsonexceptionout_of_range411) if an "add" operation's target
|
- Throws [`out_of_range.411`](../../home/exceptions.md#jsonexceptionout_of_range411) if an "add" operation's target
|
||||||
location has a parent that is neither an object nor an array.
|
location has a parent that is neither an object nor an array.
|
||||||
|
- Throws [`out_of_range.413`](../../home/exceptions.md#jsonexceptionout_of_range413) if a "remove" operation's target
|
||||||
|
location has a parent that is neither an object nor an array.
|
||||||
|
- 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.
|
||||||
- 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.
|
unsuccessful.
|
||||||
|
|
||||||
@@ -75,3 +79,7 @@ is thrown. In any case, the original value is not changed: the patch is applied
|
|||||||
- 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.
|
||||||
|
- 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.
|
||||||
|
- Added [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) and rejected a "move" operation whose "from" location is a proper
|
||||||
|
prefix of its "path" location instead of silently producing a corrupted result in version 3.13.0.
|
||||||
|
|||||||
@@ -30,6 +30,10 @@ No guarantees, value may be corrupted by an unsuccessful patch operation.
|
|||||||
("add", "remove", "move")
|
("add", "remove", "move")
|
||||||
- Throws [`out_of_range.411`](../../home/exceptions.md#jsonexceptionout_of_range411) if an "add" operation's target
|
- Throws [`out_of_range.411`](../../home/exceptions.md#jsonexceptionout_of_range411) if an "add" operation's target
|
||||||
location has a parent that is neither an object nor an array.
|
location has a parent that is neither an object nor an array.
|
||||||
|
- Throws [`out_of_range.413`](../../home/exceptions.md#jsonexceptionout_of_range413) if a "remove" operation's target
|
||||||
|
location has a parent that is neither an object nor an array.
|
||||||
|
- 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.
|
||||||
- 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.
|
unsuccessful.
|
||||||
|
|
||||||
@@ -72,3 +76,7 @@ function throws an exception.
|
|||||||
- 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.
|
||||||
|
- 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.
|
||||||
|
- Added [`out_of_range.414`](../../home/exceptions.md#jsonexceptionout_of_range414) and rejected a "move" operation whose "from" location is a proper
|
||||||
|
prefix of its "path" location instead of silently producing a corrupted result in version 3.13.0.
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
Hello, world!
|
Hello, world!
|
||||||
1 2 3 4 5
|
1 2 3 4 5
|
||||||
|
|
||||||
string: "Hello, world!"
|
|
||||||
number: {"floating-point":17.23,"integer":42}
|
number: {"floating-point":17.23,"integer":42}
|
||||||
null: null
|
null: null
|
||||||
|
string: "Hello, world!"
|
||||||
boolean: true
|
boolean: true
|
||||||
array: [1,2,3,4,5]
|
array: [1,2,3,4,5]
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
Hello, world!
|
Hello, world!
|
||||||
1 2 3 4 5
|
1 2 3 4 5
|
||||||
|
|
||||||
string: "Hello, world!"
|
|
||||||
number: {"floating-point":17.23,"integer":42}
|
number: {"floating-point":17.23,"integer":42}
|
||||||
null: null
|
null: null
|
||||||
|
string: "Hello, world!"
|
||||||
boolean: true
|
boolean: true
|
||||||
array: [1,2,3,4,5]
|
array: [1,2,3,4,5]
|
||||||
|
|||||||
@@ -4,9 +4,9 @@
|
|||||||
Hello, world!
|
Hello, world!
|
||||||
1 2 3 4 5
|
1 2 3 4 5
|
||||||
|
|
||||||
string: "Hello, world!"
|
|
||||||
number: {"floating-point":17.23,"integer":42}
|
number: {"floating-point":17.23,"integer":42}
|
||||||
null: null
|
null: null
|
||||||
|
string: "Hello, world!"
|
||||||
boolean: true
|
boolean: true
|
||||||
array: [1,2,3,4,5]
|
array: [1,2,3,4,5]
|
||||||
[json.exception.type_error.302] type must be boolean, but is string
|
[json.exception.type_error.302] type must be boolean, but is string
|
||||||
|
|||||||
@@ -69,6 +69,13 @@ The library uses the following mapping from JSON values types to UBJSON types ac
|
|||||||
Note that `use_size = true` alone may result in larger representations - the benefit of this parameter is that the
|
Note that `use_size = true` alone may result in larger representations - the benefit of this parameter is that the
|
||||||
receiving side is immediately informed on the number of elements of the container.
|
receiving side is immediately informed on the number of elements of the container.
|
||||||
|
|
||||||
|
An array whose type marker is `Z` (null), `T` (true) or `F` (false) stores no payload at all, because the marker
|
||||||
|
already is the value. Its declared count is therefore the only thing that decides how much memory the receiving side
|
||||||
|
allocates, and a handful of bytes can describe billions of elements. `from_ubjson` rejects such an array with
|
||||||
|
[`out_of_range.408`](../../home/exceptions.md#jsonexceptionout_of_range408) when the count exceeds 1,048,576
|
||||||
|
(`1 << 20`), and `to_ubjson` writes longer arrays of these types without the annotation, so any value it produces
|
||||||
|
can be read back.
|
||||||
|
|
||||||
!!! info "Binary values"
|
!!! info "Binary values"
|
||||||
|
|
||||||
If the JSON data contains the binary type, the value stored is a list of integers, as suggested by the UBJSON
|
If the JSON data contains the binary type, the value stored is a list of integers, as suggested by the UBJSON
|
||||||
|
|||||||
@@ -868,6 +868,12 @@ The size of an array or object in a [binary format](../features/binary_formats/i
|
|||||||
the size following `#` for [UBJSON](../features/binary_formats/ubjson.md)/[BJData](../features/binary_formats/bjdata.md),
|
the size following `#` for [UBJSON](../features/binary_formats/ubjson.md)/[BJData](../features/binary_formats/bjdata.md),
|
||||||
or the encoded length for [CBOR](../features/binary_formats/cbor.md).
|
or the encoded length for [CBOR](../features/binary_formats/cbor.md).
|
||||||
|
|
||||||
|
The exception is also thrown for a [UBJSON](../features/binary_formats/ubjson.md) array of a type that is encoded by its
|
||||||
|
marker alone (`Z`, `T` or `F`) whose declared count exceeds 1,048,576 (`1 << 20`). Such an array has no payload, so its
|
||||||
|
count alone decides how much memory is allocated, and a handful of bytes would otherwise describe billions of values.
|
||||||
|
[`to_ubjson`](../api/basic_json/to_ubjson.md) writes longer arrays of these types without the size and type annotation,
|
||||||
|
so any value it produces can still be read back.
|
||||||
|
|
||||||
!!! failure "Example messages"
|
!!! failure "Example messages"
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -879,6 +885,9 @@ or the encoded length for [CBOR](../features/binary_formats/cbor.md).
|
|||||||
```
|
```
|
||||||
[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive map size
|
[json.exception.out_of_range.408] syntax error while parsing CBOR size: excessive map size
|
||||||
```
|
```
|
||||||
|
```
|
||||||
|
[json.exception.out_of_range.408] syntax error while parsing UBJSON size: excessive array size
|
||||||
|
```
|
||||||
|
|
||||||
### json.exception.out_of_range.409
|
### json.exception.out_of_range.409
|
||||||
|
|
||||||
@@ -933,6 +942,34 @@ BSON stores the length of documents, arrays, strings, and binary values in a sig
|
|||||||
[`to_bson`](../api/basic_json/to_bson.md) produced documents with negative length prefixes that
|
[`to_bson`](../api/basic_json/to_bson.md) produced documents with negative length prefixes that
|
||||||
[`from_bson`](../api/basic_json/from_bson.md) rejected.
|
[`from_bson`](../api/basic_json/from_bson.md) rejected.
|
||||||
|
|
||||||
|
### json.exception.out_of_range.413
|
||||||
|
|
||||||
|
A JSON Patch `remove` operation cannot be applied because the target location's parent is neither an object nor an array. Per [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902), a `remove` target must reference a member of an existing object or an element of an existing array; a primitive value (string, number, boolean, etc.) or `null` has no members or elements to remove.
|
||||||
|
|
||||||
|
!!! failure "Example message"
|
||||||
|
|
||||||
|
```
|
||||||
|
cannot remove value: the JSON Patch 'remove' target's parent is of type number, but must be an object or array
|
||||||
|
```
|
||||||
|
|
||||||
|
!!! note
|
||||||
|
|
||||||
|
This exception was added in version 3.13.0. Before that, this situation was silently ignored (the `remove` operation had no effect).
|
||||||
|
|
||||||
|
### json.exception.out_of_range.414
|
||||||
|
|
||||||
|
A JSON Patch `move` operation's `"from"` location is a proper prefix of its `"path"` location. Per [RFC 6902](https://datatracker.ietf.org/doc/html/rfc6902) (section 4.4), a location cannot be moved into one of its own children.
|
||||||
|
|
||||||
|
!!! failure "Example message"
|
||||||
|
|
||||||
|
```
|
||||||
|
cannot move value: 'from' path '/0' is a proper prefix of 'path' '/0/0'
|
||||||
|
```
|
||||||
|
|
||||||
|
!!! note
|
||||||
|
|
||||||
|
This exception was added in version 3.13.0. Before that, this situation could succeed with a corrupted result: for an array target, removing the "from" element before the "add" step shifted subsequent indices, so "path" silently re-resolved to a different element than intended.
|
||||||
|
|
||||||
## Further exceptions
|
## Further exceptions
|
||||||
|
|
||||||
This exception is thrown in case of errors that cannot be classified with the
|
This exception is thrown in case of errors that cannot be classified with the
|
||||||
|
|||||||
@@ -398,6 +398,17 @@ inline void from_json(const BasicJsonType& j, CompatibleArrayType& bin)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename ConstructibleObjectType>
|
||||||
|
auto from_json_object_reserve(ConstructibleObjectType& obj, typename ConstructibleObjectType::size_type size, priority_tag<1> /*unused*/)
|
||||||
|
-> decltype(obj.reserve(size), void())
|
||||||
|
{
|
||||||
|
obj.reserve(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ConstructibleObjectType>
|
||||||
|
inline void from_json_object_reserve(ConstructibleObjectType& /*obj*/, std::size_t /*size*/, priority_tag<0> /*unused*/)
|
||||||
|
{}
|
||||||
|
|
||||||
template<typename BasicJsonType, typename ConstructibleObjectType,
|
template<typename BasicJsonType, typename ConstructibleObjectType,
|
||||||
enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
|
enable_if_t<is_constructible_object_type<BasicJsonType, ConstructibleObjectType>::value, int> = 0>
|
||||||
inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
|
inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
|
||||||
@@ -409,6 +420,7 @@ inline void from_json(const BasicJsonType& j, ConstructibleObjectType& obj)
|
|||||||
|
|
||||||
ConstructibleObjectType ret;
|
ConstructibleObjectType ret;
|
||||||
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
const auto* inner_object = j.template get_ptr<const typename BasicJsonType::object_t*>();
|
||||||
|
from_json_object_reserve(ret, inner_object->size(), priority_tag<1> {});
|
||||||
for (const auto& p : *inner_object)
|
for (const auto& p : *inner_object)
|
||||||
{
|
{
|
||||||
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
ret.emplace(p.first, p.second.template get<typename ConstructibleObjectType::mapped_type>());
|
||||||
|
|||||||
@@ -1075,8 +1075,8 @@ char* to_chars(char* first, const char* last, FloatType value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic push
|
JSON_HEDLEY_DIAGNOSTIC_PUSH
|
||||||
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
|
||||||
#endif
|
#endif
|
||||||
if (value == 0) // +-0
|
if (value == 0) // +-0
|
||||||
{
|
{
|
||||||
@@ -1087,7 +1087,7 @@ char* to_chars(char* first, const char* last, FloatType value)
|
|||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic pop
|
JSON_HEDLEY_DIAGNOSTIC_POP
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
|
JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);
|
||||||
|
|||||||
@@ -33,8 +33,8 @@
|
|||||||
// code stumbling over this. See https://github.com/nlohmann/json/issues/4087
|
// code stumbling over this. See https://github.com/nlohmann/json/issues/4087
|
||||||
// for a discussion.
|
// for a discussion.
|
||||||
#if defined(__clang__)
|
#if defined(__clang__)
|
||||||
#pragma clang diagnostic push
|
JSON_HEDLEY_DIAGNOSTIC_PUSH
|
||||||
#pragma clang diagnostic ignored "-Wweak-vtables"
|
JSON_HEDLEY_PRAGMA(clang diagnostic ignored "-Wweak-vtables")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
NLOHMANN_JSON_NAMESPACE_BEGIN
|
NLOHMANN_JSON_NAMESPACE_BEGIN
|
||||||
@@ -287,5 +287,5 @@ class other_error : public exception
|
|||||||
NLOHMANN_JSON_NAMESPACE_END
|
NLOHMANN_JSON_NAMESPACE_END
|
||||||
|
|
||||||
#if defined(__clang__)
|
#if defined(__clang__)
|
||||||
#pragma clang diagnostic pop
|
JSON_HEDLEY_DIAGNOSTIC_POP
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -58,6 +58,26 @@ inline bool little_endianness(int num = 1) noexcept
|
|||||||
return *reinterpret_cast<char*>(&num) == 1;
|
return *reinterpret_cast<char*>(&num) == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief largest element count accepted for a UBJSON container of a valueless type
|
||||||
|
|
||||||
|
An element of type 'Z' (null), 'T' (true) or 'F' (false) is encoded by its
|
||||||
|
type marker alone, so an optimized container of one of those types has no
|
||||||
|
payload at all and its declared count is the only thing that decides how much
|
||||||
|
is allocated: `[$Z#L` followed by a large count turns some ten bytes of input
|
||||||
|
into that many values (see #2793, which reports 35 GB and 150 seconds). Every
|
||||||
|
other type costs at least one byte per element and is bounded by the end of
|
||||||
|
the input.
|
||||||
|
|
||||||
|
This is a sanity bound rather than a security boundary, and it is far above
|
||||||
|
any container met in practice. @ref binary_writer falls back to the
|
||||||
|
unoptimized encoding for longer containers, so that a value serialized by
|
||||||
|
this library can always be read back.
|
||||||
|
|
||||||
|
@sa https://github.com/nlohmann/json/issues/2793
|
||||||
|
*/
|
||||||
|
JSON_INLINE_VARIABLE constexpr std::size_t max_valueless_container_size = 1 << 20;
|
||||||
|
|
||||||
///////////////////
|
///////////////////
|
||||||
// binary reader //
|
// binary reader //
|
||||||
///////////////////
|
///////////////////
|
||||||
@@ -110,6 +130,7 @@ class binary_reader
|
|||||||
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
|
const cbor_tag_handler_t tag_handler = cbor_tag_handler_t::error)
|
||||||
{
|
{
|
||||||
sax = sax_;
|
sax = sax_;
|
||||||
|
container_stack.clear();
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
switch (format)
|
switch (format)
|
||||||
@@ -159,6 +180,69 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
////////////////////////
|
||||||
|
// nested containers //
|
||||||
|
////////////////////////
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief a container that has been opened and not closed yet
|
||||||
|
|
||||||
|
The binary readers do not call themselves once per nesting level. Like
|
||||||
|
@ref parser::sax_parse_internal, which does the same for JSON text, they
|
||||||
|
keep the containers they are inside of on a heap-allocated stack, so that
|
||||||
|
the native call stack does not grow with the nesting depth of the input
|
||||||
|
and a deeply nested value is bounded by memory rather than by the stack
|
||||||
|
(see #5104).
|
||||||
|
|
||||||
|
The members are ordered by decreasing alignment, which is the ordering that
|
||||||
|
keeps a struct from growing as members are added to it.
|
||||||
|
*/
|
||||||
|
struct container_frame
|
||||||
|
{
|
||||||
|
container_frame(const std::size_t remaining_, const bool is_object_) noexcept
|
||||||
|
: remaining(remaining_), is_object(is_object_) {}
|
||||||
|
|
||||||
|
/// number of elements that have not been read yet
|
||||||
|
std::size_t remaining;
|
||||||
|
/// whether to close this container with end_object() or end_array()
|
||||||
|
bool is_object;
|
||||||
|
};
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief open a nested array or object
|
||||||
|
|
||||||
|
Emits the SAX start event and records the container. This is the only
|
||||||
|
place the binary readers start a container, so a check that rejects one
|
||||||
|
can be made here and is then guaranteed to run before the start event.
|
||||||
|
|
||||||
|
@param[in] is_object whether an object (true) or an array (false) begins
|
||||||
|
@param[in] len number of elements the container declares
|
||||||
|
|
||||||
|
@return whether the SAX parser accepted the start event
|
||||||
|
*/
|
||||||
|
bool enter_container(const bool is_object, const std::size_t len)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->start_object(len) : !sax->start_array(len)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
container_stack.emplace_back(len, is_object);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @copydoc enter_container
|
||||||
|
bool enter_array(const std::size_t len)
|
||||||
|
{
|
||||||
|
return enter_container(/*is_object*/false, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @copydoc enter_container
|
||||||
|
bool enter_object(const std::size_t len)
|
||||||
|
{
|
||||||
|
return enter_container(/*is_object*/true, len);
|
||||||
|
}
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
// BSON //
|
// BSON //
|
||||||
//////////
|
//////////
|
||||||
@@ -996,23 +1080,21 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a CBOR string
|
@brief reads a definite-length CBOR string
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
Reads everything @ref get_cbor_string accepts except the indefinite-length
|
||||||
string length and then copies this number of bytes into a string.
|
form, which that function handles itself. The bytes are appended to @a
|
||||||
Additionally, CBOR's strings with indefinite lengths are supported.
|
result, so consecutive chunks of an indefinite-length string can be read
|
||||||
|
into the same string.
|
||||||
|
|
||||||
@param[out] result created string
|
@param[out] result string the bytes are appended to
|
||||||
|
|
||||||
@return whether string creation completed
|
@return whether string creation completed
|
||||||
*/
|
|
||||||
bool get_cbor_string(string_t& result)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@pre @a current is not EOF
|
||||||
|
*/
|
||||||
|
bool get_cbor_string_chunk(string_t& result)
|
||||||
|
{
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// UTF-8 string (0x00..0x17 bytes follow)
|
// UTF-8 string (0x00..0x17 bytes follow)
|
||||||
@@ -1068,20 +1150,6 @@ class binary_reader
|
|||||||
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
return get_number(input_format_t::cbor, len) && get_string(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x7F: // UTF-8 string (indefinite length)
|
|
||||||
{
|
|
||||||
while (get() != 0xFF)
|
|
||||||
{
|
|
||||||
string_t chunk;
|
|
||||||
if (!get_cbor_string(chunk))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
result.append(chunk);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -1092,23 +1160,82 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief reads a CBOR byte array
|
@brief reads a CBOR string
|
||||||
|
|
||||||
This function first reads starting bytes to determine the expected
|
This function first reads starting bytes to determine the expected
|
||||||
byte array length and then copies this number of bytes into the byte array.
|
string length and then copies this number of bytes into a string.
|
||||||
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
Additionally, CBOR's strings with indefinite lengths are supported.
|
||||||
|
|
||||||
@param[out] result created byte array
|
@param[out] result created string
|
||||||
|
|
||||||
@return whether byte array creation completed
|
@return whether string creation completed
|
||||||
*/
|
*/
|
||||||
bool get_cbor_binary(binary_t& result)
|
bool get_cbor_string(string_t& result)
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
// number of indefinite-length strings that have been opened and not
|
||||||
|
// closed yet. RFC 8949, Section 3.2.3 does not permit nesting them,
|
||||||
|
// but this reader has always accepted it, so the open levels are
|
||||||
|
// counted instead of recursed through, which overflowed the stack for
|
||||||
|
// an input of repeated 0x7F bytes (see #5104). Every chunk is appended
|
||||||
|
// to the same result, so no per-level state is needed.
|
||||||
|
std::size_t open = 0;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "string")))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (current == 0x7F) // UTF-8 string (indefinite length)
|
||||||
|
{
|
||||||
|
++open;
|
||||||
|
get();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a break marker closes the innermost indefinite-length string;
|
||||||
|
// outside of one it is not a string and falls through to the error
|
||||||
|
if (open != 0 && current == 0xFF)
|
||||||
|
{
|
||||||
|
if (--open == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
get();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string_chunk(result)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (open == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief reads a definite-length CBOR byte array
|
||||||
|
|
||||||
|
Reads everything @ref get_cbor_binary accepts except the indefinite-length
|
||||||
|
form, which that function handles itself. The bytes are appended to @a
|
||||||
|
result, so consecutive chunks of an indefinite-length byte array can be
|
||||||
|
read into the same byte array.
|
||||||
|
|
||||||
|
@param[out] result byte array the bytes are appended to
|
||||||
|
|
||||||
|
@return whether byte array creation completed
|
||||||
|
|
||||||
|
@pre @a current is not EOF
|
||||||
|
*/
|
||||||
|
bool get_cbor_binary_chunk(binary_t& result)
|
||||||
|
{
|
||||||
switch (current)
|
switch (current)
|
||||||
{
|
{
|
||||||
// Binary data (0x00..0x17 bytes follow)
|
// Binary data (0x00..0x17 bytes follow)
|
||||||
@@ -1168,20 +1295,6 @@ class binary_reader
|
|||||||
get_binary(input_format_t::cbor, len, result);
|
get_binary(input_format_t::cbor, len, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0x5F: // Binary data (indefinite length)
|
|
||||||
{
|
|
||||||
while (get() != 0xFF)
|
|
||||||
{
|
|
||||||
binary_t chunk;
|
|
||||||
if (!get_cbor_binary(chunk))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
result.insert(result.end(), chunk.begin(), chunk.end());
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
auto last_token = get_token_string();
|
auto last_token = get_token_string();
|
||||||
@@ -1191,6 +1304,63 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief reads a CBOR byte array
|
||||||
|
|
||||||
|
This function first reads starting bytes to determine the expected
|
||||||
|
byte array length and then copies this number of bytes into the byte array.
|
||||||
|
Additionally, CBOR's byte arrays with indefinite lengths are supported.
|
||||||
|
|
||||||
|
@param[out] result created byte array
|
||||||
|
|
||||||
|
@return whether byte array creation completed
|
||||||
|
*/
|
||||||
|
bool get_cbor_binary(binary_t& result)
|
||||||
|
{
|
||||||
|
// the open indefinite-length byte arrays are counted rather than
|
||||||
|
// recursed through, for the reason given in @ref get_cbor_string
|
||||||
|
std::size_t open = 0;
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(input_format_t::cbor, "binary")))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (current == 0x5F) // Binary data (indefinite length)
|
||||||
|
{
|
||||||
|
++open;
|
||||||
|
get();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// a break marker closes the innermost indefinite-length byte
|
||||||
|
// array; outside of one it falls through to the error below
|
||||||
|
if (open != 0 && current == 0xFF)
|
||||||
|
{
|
||||||
|
if (--open == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
get();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(!get_cbor_binary_chunk(result)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (open == 0)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@brief narrow a definite CBOR array/map length to std::size_t
|
@brief narrow a definite CBOR array/map length to std::size_t
|
||||||
|
|
||||||
@@ -1316,7 +1486,17 @@ class binary_reader
|
|||||||
/*!
|
/*!
|
||||||
@return whether a valid MessagePack value was passed to the SAX parser
|
@return whether a valid MessagePack value was passed to the SAX parser
|
||||||
*/
|
*/
|
||||||
bool parse_msgpack_internal()
|
/*!
|
||||||
|
@brief read one MessagePack value
|
||||||
|
|
||||||
|
Reads a single value and passes it to the SAX parser. A value that begins
|
||||||
|
a container is not read to its end: the container is opened with
|
||||||
|
@ref enter_container and its elements are read by
|
||||||
|
@ref parse_msgpack_internal, so that nesting does not consume native stack.
|
||||||
|
|
||||||
|
@return whether reading the value succeeded
|
||||||
|
*/
|
||||||
|
bool parse_msgpack_value()
|
||||||
{
|
{
|
||||||
switch (get())
|
switch (get())
|
||||||
{
|
{
|
||||||
@@ -1472,7 +1652,7 @@ class binary_reader
|
|||||||
case 0x8D:
|
case 0x8D:
|
||||||
case 0x8E:
|
case 0x8E:
|
||||||
case 0x8F:
|
case 0x8F:
|
||||||
return get_msgpack_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
return enter_object(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
||||||
|
|
||||||
// fixarray
|
// fixarray
|
||||||
case 0x90:
|
case 0x90:
|
||||||
@@ -1491,7 +1671,7 @@ class binary_reader
|
|||||||
case 0x9D:
|
case 0x9D:
|
||||||
case 0x9E:
|
case 0x9E:
|
||||||
case 0x9F:
|
case 0x9F:
|
||||||
return get_msgpack_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
return enter_array(conditional_static_cast<std::size_t>(static_cast<unsigned int>(current) & 0x0Fu));
|
||||||
|
|
||||||
// fixstr
|
// fixstr
|
||||||
case 0xA0:
|
case 0xA0:
|
||||||
@@ -1622,25 +1802,25 @@ class binary_reader
|
|||||||
case 0xDC: // array 16
|
case 0xDC: // array 16
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && get_msgpack_array(static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && enter_array(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDD: // array 32
|
case 0xDD: // array 32
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && get_msgpack_array(conditional_static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && enter_array(conditional_static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDE: // map 16
|
case 0xDE: // map 16
|
||||||
{
|
{
|
||||||
std::uint16_t len{};
|
std::uint16_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && get_msgpack_object(static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && enter_object(static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
case 0xDF: // map 32
|
case 0xDF: // map 32
|
||||||
{
|
{
|
||||||
std::uint32_t len{};
|
std::uint32_t len{};
|
||||||
return get_number(input_format_t::msgpack, len) && get_msgpack_object(conditional_static_cast<std::size_t>(len));
|
return get_number(input_format_t::msgpack, len) && enter_object(conditional_static_cast<std::size_t>(len));
|
||||||
}
|
}
|
||||||
|
|
||||||
// negative fixint
|
// negative fixint
|
||||||
@@ -1888,55 +2068,69 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@param[in] len the length of the array
|
@brief read a MessagePack value and everything nested inside it
|
||||||
@return whether array creation completed
|
|
||||||
|
Reads values until the one that was begun here is complete, resuming the
|
||||||
|
enclosing container each time an element ends, so that the nesting depth
|
||||||
|
of the input costs heap rather than native stack (see #5104).
|
||||||
|
|
||||||
|
@return whether reading the value succeeded
|
||||||
*/
|
*/
|
||||||
bool get_msgpack_array(const std::size_t len)
|
bool parse_msgpack_internal()
|
||||||
{
|
{
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(len)))
|
// the key currently being read; hoisted out of the loop so that its
|
||||||
{
|
// capacity is reused across elements and across nesting levels
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (std::size_t i = 0; i < len; ++i)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sax->end_array();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*!
|
|
||||||
@param[in] len the length of the object
|
|
||||||
@return whether object creation completed
|
|
||||||
*/
|
|
||||||
bool get_msgpack_object(const std::size_t len)
|
|
||||||
{
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_object(len)))
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
string_t key;
|
string_t key;
|
||||||
for (std::size_t i = 0; i < len; ++i)
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
if (!container_stack.empty())
|
||||||
|
{
|
||||||
|
// copied out before anything can push onto the stack and
|
||||||
|
// invalidate a reference into it
|
||||||
|
const bool is_object = container_stack.back().is_object;
|
||||||
|
|
||||||
|
if (container_stack.back().remaining == 0)
|
||||||
|
{
|
||||||
|
container_stack.pop_back();
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_object ? !sax->end_object() : !sax->end_array()))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// the value begun here is complete once its container is
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// claim the element about to be read
|
||||||
|
--container_stack.back().remaining;
|
||||||
|
|
||||||
|
if (is_object)
|
||||||
{
|
{
|
||||||
get();
|
get();
|
||||||
|
key.clear();
|
||||||
if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
|
if (JSON_HEDLEY_UNLIKELY(!get_msgpack_string(key) || !sax->key(key)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_internal()))
|
if (JSON_HEDLEY_UNLIKELY(!parse_msgpack_value()))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
key.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
return sax->end_object();
|
// a value that opened a container left it on the stack; one that
|
||||||
|
// did not, and that was not inside a container, was the whole value
|
||||||
|
if (container_stack.empty())
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////
|
////////////
|
||||||
@@ -2391,7 +2585,12 @@ class binary_reader
|
|||||||
{
|
{
|
||||||
result.first = npos; // size
|
result.first = npos; // size
|
||||||
result.second = 0; // type
|
result.second = 0; // type
|
||||||
bool is_ndarray = false;
|
// seed the flag with the caller's context: inside an ndarray dimension
|
||||||
|
// vector another ndarray is not allowed, and get_ubjson_size_value()
|
||||||
|
// rejects it up front instead of reading it and reporting afterwards.
|
||||||
|
// Seeding it with `false` made every '#' of a "[#[#[..." chain descend
|
||||||
|
// another level, which overflowed the stack (see #5104).
|
||||||
|
bool is_ndarray = inside_ndarray;
|
||||||
|
|
||||||
get_ignore_noop();
|
get_ignore_noop();
|
||||||
|
|
||||||
@@ -2424,13 +2623,11 @@ class binary_reader
|
|||||||
}
|
}
|
||||||
|
|
||||||
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
||||||
if (input_format == input_format_t::bjdata && is_ndarray)
|
// an ndarray was read here only if the flag flipped; when it was
|
||||||
|
// seeded true, get_ubjson_size_value() already rejected the nested
|
||||||
|
// dimension vector
|
||||||
|
if (input_format == input_format_t::bjdata && is_ndarray && !inside_ndarray)
|
||||||
{
|
{
|
||||||
if (inside_ndarray)
|
|
||||||
{
|
|
||||||
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
|
||||||
exception_message(input_format, "ndarray can not be recursive", "size"), nullptr));
|
|
||||||
}
|
|
||||||
result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
|
result.second |= (1 << 8); // use bit 8 to indicate ndarray, all UBJSON and BJData markers should be ASCII letters
|
||||||
}
|
}
|
||||||
return is_error;
|
return is_error;
|
||||||
@@ -2439,7 +2636,7 @@ class binary_reader
|
|||||||
if (current == '#')
|
if (current == '#')
|
||||||
{
|
{
|
||||||
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
const bool is_error = get_ubjson_size_value(result.first, is_ndarray);
|
||||||
if (input_format == input_format_t::bjdata && is_ndarray)
|
if (input_format == input_format_t::bjdata && is_ndarray && !inside_ndarray)
|
||||||
{
|
{
|
||||||
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
return sax->parse_error(chars_read, get_token_string(), parse_error::create(112, chars_read,
|
||||||
exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
|
exception_message(input_format, "ndarray requires both type and size", "size"), nullptr));
|
||||||
@@ -2710,6 +2907,17 @@ class binary_reader
|
|||||||
|
|
||||||
if (size_and_type.first != npos)
|
if (size_and_type.first != npos)
|
||||||
{
|
{
|
||||||
|
// reading an element of a valueless type consumes no input, so the
|
||||||
|
// declared count alone decides how much is allocated; the check is
|
||||||
|
// made before the start event so that no container is opened that
|
||||||
|
// is then abandoned. See @ref max_valueless_container_size.
|
||||||
|
if (JSON_HEDLEY_UNLIKELY((size_and_type.second == 'Z' || size_and_type.second == 'T' || size_and_type.second == 'F')
|
||||||
|
&& size_and_type.first > max_valueless_container_size))
|
||||||
|
{
|
||||||
|
return sax->parse_error(chars_read, get_token_string(), out_of_range::create(408,
|
||||||
|
exception_message(input_format, "excessive array size", "size"), nullptr));
|
||||||
|
}
|
||||||
|
|
||||||
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
|
if (JSON_HEDLEY_UNLIKELY(!sax->start_array(size_and_type.first)))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@@ -3227,6 +3435,9 @@ class binary_reader
|
|||||||
/// the SAX parser
|
/// the SAX parser
|
||||||
json_sax_t* sax = nullptr;
|
json_sax_t* sax = nullptr;
|
||||||
|
|
||||||
|
/// the containers that have been opened and not closed yet; see @ref container_frame
|
||||||
|
std::vector<container_frame> container_stack{};
|
||||||
|
|
||||||
// excluded markers in bjdata optimized type
|
// excluded markers in bjdata optimized type
|
||||||
#define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
|
#define JSON_BINARY_READER_MAKE_BJD_OPTIMIZED_TYPE_MARKERS_ \
|
||||||
make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
|
make_array<char_int_type>('F', 'H', 'N', 'S', 'T', 'Z', '[', '{')
|
||||||
|
|||||||
@@ -149,10 +149,11 @@ class lexer : public lexer_base<BasicJsonType>
|
|||||||
public:
|
public:
|
||||||
using token_type = typename lexer_base<BasicJsonType>::token_type;
|
using token_type = typename lexer_base<BasicJsonType>::token_type;
|
||||||
|
|
||||||
explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false) noexcept
|
explicit lexer(InputAdapterType&& adapter, bool ignore_comments_ = false, bool discard_number_values_ = false) noexcept
|
||||||
: ia(std::move(adapter))
|
: ia(std::move(adapter))
|
||||||
, ignore_comments(ignore_comments_)
|
, ignore_comments(ignore_comments_)
|
||||||
, decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
|
, decimal_point_char(static_cast<char_int_type>(get_decimal_point()))
|
||||||
|
, discard_number_values(discard_number_values_)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
// deleted because of pointer members
|
// deleted because of pointer members
|
||||||
@@ -1279,6 +1280,58 @@ scan_number_done:
|
|||||||
// we are done scanning a number)
|
// we are done scanning a number)
|
||||||
unget();
|
unget();
|
||||||
|
|
||||||
|
// If the caller does not need the converted value (only whether the
|
||||||
|
// input is syntactically valid; see json_sax_acceptor/accept()), an
|
||||||
|
// unsigned/integer token can be reported without calling
|
||||||
|
// strtoull()/strtoll() at all, *provided* we can already tell from
|
||||||
|
// the digit count alone that the conversion cannot overflow 64 bits.
|
||||||
|
// Such tokens are always finite and are accepted unconditionally by
|
||||||
|
// the parser regardless of their actual value (parser::sax_parse_internal()
|
||||||
|
// never checks finiteness for value_unsigned/value_integer), so the
|
||||||
|
// classification below is all that is needed.
|
||||||
|
//
|
||||||
|
// A decimal number with up to 18 digits is always representable in
|
||||||
|
// both std::uint64_t and std::int64_t (18 nines is ~1e18, well below
|
||||||
|
// both UINT64_MAX ~1.8e19 and INT64_MAX ~9.2e18), so strtoull()/strtoll()
|
||||||
|
// could not have set errno to ERANGE for it. Numbers with more digits
|
||||||
|
// (rare in practice) fall through to the exact code below, unchanged,
|
||||||
|
// so their handling -- including reclassification to value_float when
|
||||||
|
// the value overflows 64 bits, and rejection when it is not even
|
||||||
|
// finite as a double -- is bit-for-bit identical to before this
|
||||||
|
// optimization.
|
||||||
|
//
|
||||||
|
// Note this reasons about std::uint64_t/std::int64_t, not about
|
||||||
|
// number_unsigned_t/number_integer_t (BasicJsonType's own, possibly
|
||||||
|
// narrower, template parameters -- e.g. std::uint32_t). That is fine
|
||||||
|
// *only* because discard_number_values is exclusively set by
|
||||||
|
// accept() (see json.hpp), and accept() always parses through the
|
||||||
|
// library's own json_sax_acceptor -- never a user-supplied SAX
|
||||||
|
// consumer -- whose number_unsigned()/number_integer()/number_float()
|
||||||
|
// callbacks unconditionally discard their argument and return true.
|
||||||
|
// So for every caller that can reach this branch, neither the token
|
||||||
|
// classification below nor the eventual (possibly narrowed, and on
|
||||||
|
// this fast path left stale/unset) value_unsigned/value_integer is
|
||||||
|
// ever consulted -- an unsigned/integer token is accepted outright,
|
||||||
|
// and even a >18-digit token that this fast path deliberately falls
|
||||||
|
// through for is, once reclassified to value_float, still finite
|
||||||
|
// (and thus accepted) for any digit count that fits in number_unsigned_t
|
||||||
|
// or number_integer_t regardless of that type's width. If this
|
||||||
|
// function is ever taught to run with discard_number_values true for
|
||||||
|
// a caller that *does* read the converted value, this reasoning (and
|
||||||
|
// the fast path below) would need to be revisited.
|
||||||
|
if (discard_number_values)
|
||||||
|
{
|
||||||
|
constexpr std::size_t safe_digit_count = 18;
|
||||||
|
if (number_type == token_type::value_unsigned && token_buffer.size() <= safe_digit_count)
|
||||||
|
{
|
||||||
|
return token_type::value_unsigned;
|
||||||
|
}
|
||||||
|
if (number_type == token_type::value_integer && token_buffer.size() - 1 <= safe_digit_count)
|
||||||
|
{
|
||||||
|
return token_type::value_integer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
|
char* endptr = nullptr; // NOLINT(misc-const-correctness,cppcoreguidelines-pro-type-vararg,hicpp-vararg)
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
|
||||||
@@ -1393,8 +1446,7 @@ scan_number_done:
|
|||||||
*/
|
*/
|
||||||
char_int_type get()
|
char_int_type get()
|
||||||
{
|
{
|
||||||
++position.chars_read_total;
|
advance_position();
|
||||||
++position.chars_read_current_line;
|
|
||||||
|
|
||||||
if (next_unget)
|
if (next_unget)
|
||||||
{
|
{
|
||||||
@@ -1406,6 +1458,23 @@ scan_number_done:
|
|||||||
current = ia.get_character();
|
current = ia.get_character();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return track_after_read();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// shared head of get() / get_ignoring_pending_unget(): bump the
|
||||||
|
/// per-character position counters (line-count-on-'\n' bookkeeping is
|
||||||
|
/// handled afterwards, in track_after_read(), once `current` is known)
|
||||||
|
void advance_position() noexcept
|
||||||
|
{
|
||||||
|
++position.chars_read_total;
|
||||||
|
++position.chars_read_current_line;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// shared tail of get() / get_ignoring_pending_unget(): capture the
|
||||||
|
/// character for error messages (if needed) and update line/column
|
||||||
|
/// bookkeeping for the character now in `current`
|
||||||
|
char_int_type track_after_read()
|
||||||
|
{
|
||||||
// seekable adapters reconstruct the token lazily on error (see
|
// seekable adapters reconstruct the token lazily on error (see
|
||||||
// get_token_string), so the eager per-character copy is skipped
|
// get_token_string), so the eager per-character copy is skipped
|
||||||
capture_char(std::integral_constant<bool, lazy_token_string> {});
|
capture_char(std::integral_constant<bool, lazy_token_string> {});
|
||||||
@@ -1419,6 +1488,29 @@ scan_number_done:
|
|||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*!
|
||||||
|
@brief like get(), but for call sites that can prove no unget() is pending
|
||||||
|
|
||||||
|
get() has to check the `next_unget` flag on every call, because a
|
||||||
|
previous token may have ended with unget() (e.g. scan_number() always
|
||||||
|
ungets the character that terminated the number, so the next call to
|
||||||
|
scan() can see it again). skip_whitespace() reads that first,
|
||||||
|
possibly-ungotten character via a plain get(), but every further
|
||||||
|
character it reads is guaranteed to be a fresh read: nothing between
|
||||||
|
those calls invokes unget(). This variant skips the (otherwise always
|
||||||
|
false) next_unget branch for those calls; it is not a general
|
||||||
|
replacement for get().
|
||||||
|
*/
|
||||||
|
char_int_type get_ignoring_pending_unget()
|
||||||
|
{
|
||||||
|
JSON_ASSERT(!next_unget);
|
||||||
|
|
||||||
|
advance_position();
|
||||||
|
current = ia.get_character();
|
||||||
|
|
||||||
|
return track_after_read();
|
||||||
|
}
|
||||||
|
|
||||||
/// seekable adapter: nothing to capture, the token is rebuilt on error
|
/// seekable adapter: nothing to capture, the token is rebuilt on error
|
||||||
void capture_char(std::true_type /*lazy*/) const noexcept {}
|
void capture_char(std::true_type /*lazy*/) const noexcept {}
|
||||||
|
|
||||||
@@ -1612,13 +1704,37 @@ scan_number_done:
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// whether `current` is one of the four JSON whitespace characters
|
||||||
|
bool current_is_whitespace() const noexcept
|
||||||
|
{
|
||||||
|
return current == ' ' || current == '\t' || current == '\n' || current == '\r';
|
||||||
|
}
|
||||||
|
|
||||||
void skip_whitespace()
|
void skip_whitespace()
|
||||||
{
|
{
|
||||||
|
// the first character may be a pending unget() left over from the
|
||||||
|
// previous token (see get_ignoring_pending_unget()); every
|
||||||
|
// subsequent character read by this loop is guaranteed fresh, since
|
||||||
|
// nothing below calls unget()
|
||||||
|
get();
|
||||||
|
|
||||||
|
if (!current_is_whitespace())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is written as an if-guarded do-while (rather than a plain
|
||||||
|
// while loop) because that shape is what lets both GCC and Clang
|
||||||
|
// keep the input adapter's read pointer in a register across
|
||||||
|
// iterations; the equivalent while-loop measurably defeated that
|
||||||
|
// optimization in testing, turning long whitespace runs (e.g. the
|
||||||
|
// indentation of pretty-printed JSON) from a register-only loop
|
||||||
|
// into one that reloads the pointer from memory every character
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
get();
|
get_ignoring_pending_unget();
|
||||||
}
|
}
|
||||||
while (current == ' ' || current == '\t' || current == '\n' || current == '\r');
|
while (current_is_whitespace());
|
||||||
}
|
}
|
||||||
|
|
||||||
token_type scan()
|
token_type scan()
|
||||||
@@ -1754,6 +1870,13 @@ scan_number_done:
|
|||||||
const char_int_type decimal_point_char = '.';
|
const char_int_type decimal_point_char = '.';
|
||||||
/// the position of the decimal point in the input
|
/// the position of the decimal point in the input
|
||||||
std::size_t decimal_point_position = std::string::npos;
|
std::size_t decimal_point_position = std::string::npos;
|
||||||
|
|
||||||
|
/// whether the caller (e.g. accept()/json_sax_acceptor) only needs the
|
||||||
|
/// token classification and never looks at the converted numeric value;
|
||||||
|
/// when set, scan_number() may skip strtoull()/strtoll() for
|
||||||
|
/// value_unsigned/value_integer tokens whose digit count guarantees they
|
||||||
|
/// fit into 64 bits (see scan_number())
|
||||||
|
const bool discard_number_values = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|||||||
@@ -72,9 +72,10 @@ class parser
|
|||||||
parser_callback_t<BasicJsonType> cb = nullptr,
|
parser_callback_t<BasicJsonType> cb = nullptr,
|
||||||
const bool allow_exceptions_ = true,
|
const bool allow_exceptions_ = true,
|
||||||
const bool ignore_comments = false,
|
const bool ignore_comments = false,
|
||||||
const bool ignore_trailing_commas_ = false)
|
const bool ignore_trailing_commas_ = false,
|
||||||
|
const bool discard_number_values_ = false)
|
||||||
: callback(std::move(cb))
|
: callback(std::move(cb))
|
||||||
, m_lexer(std::move(adapter), ignore_comments)
|
, m_lexer(std::move(adapter), ignore_comments, discard_number_values_)
|
||||||
, allow_exceptions(allow_exceptions_)
|
, allow_exceptions(allow_exceptions_)
|
||||||
, ignore_trailing_commas(ignore_trailing_commas_)
|
, ignore_trailing_commas(ignore_trailing_commas_)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <nlohmann/detail/abi_macros.hpp>
|
#include <nlohmann/detail/abi_macros.hpp>
|
||||||
|
#include <nlohmann/detail/macro_scope.hpp>
|
||||||
#include <nlohmann/detail/meta/type_traits.hpp>
|
#include <nlohmann/detail/meta/type_traits.hpp>
|
||||||
#include <nlohmann/detail/string_utils.hpp>
|
#include <nlohmann/detail/string_utils.hpp>
|
||||||
#include <nlohmann/detail/value_t.hpp>
|
#include <nlohmann/detail/value_t.hpp>
|
||||||
@@ -206,10 +207,10 @@ NLOHMANN_JSON_NAMESPACE_END
|
|||||||
namespace std
|
namespace std
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Fix: https://github.com/nlohmann/json/issues/1401
|
||||||
#if defined(__clang__)
|
#if defined(__clang__)
|
||||||
// Fix: https://github.com/nlohmann/json/issues/1401
|
JSON_HEDLEY_DIAGNOSTIC_PUSH
|
||||||
#pragma clang diagnostic push
|
JSON_HEDLEY_PRAGMA(clang diagnostic ignored "-Wmismatched-tags")
|
||||||
#pragma clang diagnostic ignored "-Wmismatched-tags"
|
|
||||||
#endif
|
#endif
|
||||||
template<typename IteratorType>
|
template<typename IteratorType>
|
||||||
class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
|
class tuple_size<::nlohmann::detail::iteration_proxy_value<IteratorType>> // NOLINT(cert-dcl58-cpp)
|
||||||
@@ -224,7 +225,7 @@ class tuple_element<N, ::nlohmann::detail::iteration_proxy_value<IteratorType >>
|
|||||||
::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
|
::nlohmann::detail::iteration_proxy_value<IteratorType >> ()));
|
||||||
};
|
};
|
||||||
#if defined(__clang__)
|
#if defined(__clang__)
|
||||||
#pragma clang diagnostic pop
|
JSON_HEDLEY_DIAGNOSTIC_POP
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
} // namespace std
|
} // namespace std
|
||||||
|
|||||||
@@ -748,6 +748,20 @@ class json_pointer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// the reference token consists only of digits at this point (cf. checks
|
||||||
|
// above); however, its numeric value might not be representable, in which
|
||||||
|
// case array_index() would throw out_of_range.404/410 -- contains() must
|
||||||
|
// not throw (see #5395), so such a reference token is treated as "not found"
|
||||||
|
errno = 0; // strtoull() does not reset errno on success
|
||||||
|
char* p_end = nullptr; // NOLINT(misc-const-correctness)
|
||||||
|
const unsigned long long magnitude = std::strtoull(reference_token.c_str(), &p_end, 10); // NOLINT(runtime/int)
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(errno == ERANGE // the value exceeds ULLONG_MAX
|
||||||
|
|| magnitude >= static_cast<unsigned long long>((std::numeric_limits<typename BasicJsonType::size_type>::max)()))) // NOLINT(runtime/int)
|
||||||
|
{
|
||||||
|
// the array index cannot be represented as size_type
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const auto idx = array_index<BasicJsonType>(reference_token);
|
const auto idx = array_index<BasicJsonType>(reference_token);
|
||||||
if (idx >= ptr->size())
|
if (idx >= ptr->size())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -826,7 +826,17 @@ class binary_writer
|
|||||||
|
|
||||||
std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
|
std::vector<CharType> bjdx = {'[', '{', 'S', 'H', 'T', 'F', 'N', 'Z'}; // excluded markers in bjdata optimized type
|
||||||
|
|
||||||
if (same_prefix && !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
|
// an optimized array of a valueless type carries no payload, so a
|
||||||
|
// reader has nothing but the declared count to bound the allocation
|
||||||
|
// by and refuses an excessive one. Write the unoptimized form for
|
||||||
|
// those, at one byte per element, so the result can be read back.
|
||||||
|
// Objects are not affected: every element is preceded by its key.
|
||||||
|
const bool valueless_type = (first_prefix == 'Z' || first_prefix == 'T' || first_prefix == 'F');
|
||||||
|
const bool excessive_valueless = valueless_type
|
||||||
|
&& j.m_data.m_value.array->size() > detail::max_valueless_container_size;
|
||||||
|
|
||||||
|
if (same_prefix && !excessive_valueless
|
||||||
|
&& !(use_bjdata && std::find(bjdx.begin(), bjdx.end(), first_prefix) != bjdx.end()))
|
||||||
{
|
{
|
||||||
prefix_required = false;
|
prefix_required = false;
|
||||||
oa->write_character(to_char_type('$'));
|
oa->write_character(to_char_type('$'));
|
||||||
@@ -1850,8 +1860,8 @@ class binary_writer
|
|||||||
void write_compact_float(const number_float_t n, detail::input_format_t format)
|
void write_compact_float(const number_float_t n, detail::input_format_t format)
|
||||||
{
|
{
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic push
|
JSON_HEDLEY_DIAGNOSTIC_PUSH
|
||||||
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
|
||||||
#endif
|
#endif
|
||||||
if (!std::isfinite(n) || ((static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
|
if (!std::isfinite(n) || ((static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
|
||||||
static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
|
static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
|
||||||
@@ -1870,7 +1880,7 @@ class binary_writer
|
|||||||
write_number(n);
|
write_number(n);
|
||||||
}
|
}
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic pop
|
JSON_HEDLEY_DIAGNOSTIC_POP
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+144
-39
@@ -164,11 +164,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
detail::parser_callback_t<basic_json>cb = nullptr,
|
detail::parser_callback_t<basic_json>cb = nullptr,
|
||||||
const bool allow_exceptions = true,
|
const bool allow_exceptions = true,
|
||||||
const bool ignore_comments = false,
|
const bool ignore_comments = false,
|
||||||
const bool ignore_trailing_commas = false
|
const bool ignore_trailing_commas = false,
|
||||||
|
const bool discard_number_values = false
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
|
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
|
||||||
std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas);
|
std::move(cb), allow_exceptions, ignore_comments, ignore_trailing_commas, discard_number_values);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -1335,6 +1336,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief serialization
|
/// @brief serialization
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/dump/
|
/// @sa https://json.nlohmann.me/api/basic_json/dump/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
string_t dump(const int indent = -1,
|
string_t dump(const int indent = -1,
|
||||||
const char indent_char = ' ',
|
const char indent_char = ' ',
|
||||||
const bool ensure_ascii = false,
|
const bool ensure_ascii = false,
|
||||||
@@ -1357,6 +1359,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return the type of the JSON value (explicit)
|
/// @brief return the type of the JSON value (explicit)
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/type/
|
/// @sa https://json.nlohmann.me/api/basic_json/type/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr value_t type() const noexcept
|
constexpr value_t type() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type;
|
return m_data.m_type;
|
||||||
@@ -1364,6 +1367,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether type is primitive
|
/// @brief return whether type is primitive
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_primitive/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_primitive() const noexcept
|
constexpr bool is_primitive() const noexcept
|
||||||
{
|
{
|
||||||
return is_null() || is_string() || is_boolean() || is_number() || is_binary();
|
return is_null() || is_string() || is_boolean() || is_number() || is_binary();
|
||||||
@@ -1371,6 +1375,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether type is structured
|
/// @brief return whether type is structured
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_structured/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_structured/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_structured() const noexcept
|
constexpr bool is_structured() const noexcept
|
||||||
{
|
{
|
||||||
return is_array() || is_object();
|
return is_array() || is_object();
|
||||||
@@ -1378,6 +1383,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is null
|
/// @brief return whether value is null
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_null/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_null/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_null() const noexcept
|
constexpr bool is_null() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::null;
|
return m_data.m_type == value_t::null;
|
||||||
@@ -1385,6 +1391,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is a boolean
|
/// @brief return whether value is a boolean
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_boolean/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_boolean() const noexcept
|
constexpr bool is_boolean() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::boolean;
|
return m_data.m_type == value_t::boolean;
|
||||||
@@ -1392,6 +1399,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is a number
|
/// @brief return whether value is a number
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_number/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_number/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_number() const noexcept
|
constexpr bool is_number() const noexcept
|
||||||
{
|
{
|
||||||
return is_number_integer() || is_number_float();
|
return is_number_integer() || is_number_float();
|
||||||
@@ -1399,6 +1407,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is an integer number
|
/// @brief return whether value is an integer number
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_number_integer/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_number_integer() const noexcept
|
constexpr bool is_number_integer() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
|
return m_data.m_type == value_t::number_integer || m_data.m_type == value_t::number_unsigned;
|
||||||
@@ -1406,6 +1415,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is an unsigned integer number
|
/// @brief return whether value is an unsigned integer number
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_number_unsigned/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_number_unsigned() const noexcept
|
constexpr bool is_number_unsigned() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::number_unsigned;
|
return m_data.m_type == value_t::number_unsigned;
|
||||||
@@ -1413,6 +1423,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is a floating-point number
|
/// @brief return whether value is a floating-point number
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_number_float/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_number_float() const noexcept
|
constexpr bool is_number_float() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::number_float;
|
return m_data.m_type == value_t::number_float;
|
||||||
@@ -1420,6 +1431,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is an object
|
/// @brief return whether value is an object
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_object/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_object/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_object() const noexcept
|
constexpr bool is_object() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::object;
|
return m_data.m_type == value_t::object;
|
||||||
@@ -1427,6 +1439,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is an array
|
/// @brief return whether value is an array
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_array/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_array/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_array() const noexcept
|
constexpr bool is_array() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::array;
|
return m_data.m_type == value_t::array;
|
||||||
@@ -1434,6 +1447,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is a string
|
/// @brief return whether value is a string
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_string/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_string/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_string() const noexcept
|
constexpr bool is_string() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::string;
|
return m_data.m_type == value_t::string;
|
||||||
@@ -1441,6 +1455,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is a binary array
|
/// @brief return whether value is a binary array
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_binary/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_binary/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_binary() const noexcept
|
constexpr bool is_binary() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::binary;
|
return m_data.m_type == value_t::binary;
|
||||||
@@ -1448,6 +1463,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return whether value is discarded
|
/// @brief return whether value is discarded
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
|
/// @sa https://json.nlohmann.me/api/basic_json/is_discarded/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
constexpr bool is_discarded() const noexcept
|
constexpr bool is_discarded() const noexcept
|
||||||
{
|
{
|
||||||
return m_data.m_type == value_t::discarded;
|
return m_data.m_type == value_t::discarded;
|
||||||
@@ -2779,6 +2795,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief returns the number of occurrences of a key in a JSON object
|
/// @brief returns the number of occurrences of a key in a JSON object
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/count/
|
/// @sa https://json.nlohmann.me/api/basic_json/count/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
size_type count(const typename object_t::key_type& key) const
|
size_type count(const typename object_t::key_type& key) const
|
||||||
{
|
{
|
||||||
// return 0 for all nonobject types
|
// return 0 for all nonobject types
|
||||||
@@ -2789,6 +2806,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
/// @sa https://json.nlohmann.me/api/basic_json/count/
|
/// @sa https://json.nlohmann.me/api/basic_json/count/
|
||||||
template<class KeyType, detail::enable_if_t<
|
template<class KeyType, detail::enable_if_t<
|
||||||
detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
|
detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
size_type count(KeyType && key) const
|
size_type count(KeyType && key) const
|
||||||
{
|
{
|
||||||
// return 0 for all nonobject types
|
// return 0 for all nonobject types
|
||||||
@@ -2797,6 +2815,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief check the existence of an element in a JSON object
|
/// @brief check the existence of an element in a JSON object
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/contains/
|
/// @sa https://json.nlohmann.me/api/basic_json/contains/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
bool contains(const typename object_t::key_type& key) const
|
bool contains(const typename object_t::key_type& key) const
|
||||||
{
|
{
|
||||||
return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
|
return is_object() && m_data.m_value.object->find(key) != m_data.m_value.object->end();
|
||||||
@@ -2806,6 +2825,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
/// @sa https://json.nlohmann.me/api/basic_json/contains/
|
/// @sa https://json.nlohmann.me/api/basic_json/contains/
|
||||||
template<class KeyType, detail::enable_if_t<
|
template<class KeyType, detail::enable_if_t<
|
||||||
detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
|
detail::is_usable_as_basic_json_key_type<basic_json_t, KeyType>::value, int> = 0>
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
bool contains(KeyType && key) const
|
bool contains(KeyType && key) const
|
||||||
{
|
{
|
||||||
return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
|
return is_object() && m_data.m_value.object->find(std::forward<KeyType>(key)) != m_data.m_value.object->end();
|
||||||
@@ -2813,12 +2833,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief check the existence of an element in a JSON object given a JSON pointer
|
/// @brief check the existence of an element in a JSON object given a JSON pointer
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/contains/
|
/// @sa https://json.nlohmann.me/api/basic_json/contains/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
bool contains(const json_pointer& ptr) const
|
bool contains(const json_pointer& ptr) const
|
||||||
{
|
{
|
||||||
return ptr.contains(this);
|
return ptr.contains(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
|
template<typename BasicJsonType, detail::enable_if_t<detail::is_basic_json<BasicJsonType>::value, int> = 0>
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
|
JSON_HEDLEY_DEPRECATED_FOR(3.11.0, basic_json::json_pointer or nlohmann::json_pointer<basic_json::string_t>) // NOLINT(readability/alt_tokens)
|
||||||
bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
|
bool contains(const typename ::nlohmann::json_pointer<BasicJsonType>& ptr) const
|
||||||
{
|
{
|
||||||
@@ -2974,6 +2996,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief checks whether the container is empty.
|
/// @brief checks whether the container is empty.
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/empty/
|
/// @sa https://json.nlohmann.me/api/basic_json/empty/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
bool empty() const noexcept
|
bool empty() const noexcept
|
||||||
{
|
{
|
||||||
switch (m_data.m_type)
|
switch (m_data.m_type)
|
||||||
@@ -3013,6 +3036,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief returns the number of elements
|
/// @brief returns the number of elements
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/size/
|
/// @sa https://json.nlohmann.me/api/basic_json/size/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
size_type size() const noexcept
|
size_type size() const noexcept
|
||||||
{
|
{
|
||||||
switch (m_data.m_type)
|
switch (m_data.m_type)
|
||||||
@@ -3052,6 +3076,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief returns the maximum possible number of elements
|
/// @brief returns the maximum possible number of elements
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/max_size/
|
/// @sa https://json.nlohmann.me/api/basic_json/max_size/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
size_type max_size() const noexcept
|
size_type max_size() const noexcept
|
||||||
{
|
{
|
||||||
switch (m_data.m_type)
|
switch (m_data.m_type)
|
||||||
@@ -3770,13 +3795,13 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
bool operator==(const_reference rhs) const noexcept
|
bool operator==(const_reference rhs) const noexcept
|
||||||
{
|
{
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic push
|
JSON_HEDLEY_DIAGNOSTIC_PUSH
|
||||||
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
|
||||||
#endif
|
#endif
|
||||||
const_reference lhs = *this;
|
const_reference lhs = *this;
|
||||||
JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
|
JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic pop
|
JSON_HEDLEY_DIAGNOSTIC_POP
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3863,12 +3888,12 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
|
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
|
||||||
{
|
{
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic push
|
JSON_HEDLEY_DIAGNOSTIC_PUSH
|
||||||
#pragma GCC diagnostic ignored "-Wfloat-equal"
|
JSON_HEDLEY_PRAGMA(GCC diagnostic ignored "-Wfloat-equal")
|
||||||
#endif
|
#endif
|
||||||
JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
|
JSON_IMPLEMENT_OPERATOR( ==, true, false, false)
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
#pragma GCC diagnostic pop
|
JSON_HEDLEY_DIAGNOSTIC_POP
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -4129,22 +4154,24 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
/// @brief check if the input is valid JSON
|
/// @brief check if the input is valid JSON
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/accept/
|
/// @sa https://json.nlohmann.me/api/basic_json/accept/
|
||||||
template<typename InputType>
|
template<typename InputType>
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
static bool accept(InputType&& i,
|
static bool accept(InputType&& i,
|
||||||
const bool ignore_comments = false,
|
const bool ignore_comments = false,
|
||||||
const bool ignore_trailing_commas = false)
|
const bool ignore_trailing_commas = false)
|
||||||
{
|
{
|
||||||
return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments, ignore_trailing_commas).accept(true);
|
return parser(detail::input_adapter(std::forward<InputType>(i)), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief check if the input is valid JSON (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief check if the input is valid JSON (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/accept/
|
/// @sa https://json.nlohmann.me/api/basic_json/accept/
|
||||||
template<typename IteratorType, typename SentinelType = IteratorType,
|
template<typename IteratorType, typename SentinelType = IteratorType,
|
||||||
detail::enable_if_t<detail::can_compare_ne<IteratorType, SentinelType>::value, int> = 0>
|
detail::enable_if_t<detail::can_compare_ne<IteratorType, SentinelType>::value, int> = 0>
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
static bool accept(IteratorType first, SentinelType last,
|
static bool accept(IteratorType first, SentinelType last,
|
||||||
const bool ignore_comments = false,
|
const bool ignore_comments = false,
|
||||||
const bool ignore_trailing_commas = false)
|
const bool ignore_trailing_commas = false)
|
||||||
{
|
{
|
||||||
return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments, ignore_trailing_commas).accept(true);
|
return parser(detail::input_adapter(std::move(first), std::move(last)), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON_HEDLEY_WARN_UNUSED_RESULT
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
@@ -4153,7 +4180,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
const bool ignore_comments = false,
|
const bool ignore_comments = false,
|
||||||
const bool ignore_trailing_commas = false)
|
const bool ignore_trailing_commas = false)
|
||||||
{
|
{
|
||||||
return parser(i.get(), nullptr, false, ignore_comments, ignore_trailing_commas).accept(true);
|
return parser(i.get(), nullptr, false, ignore_comments, ignore_trailing_commas, true).accept(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief generate SAX events
|
/// @brief generate SAX events
|
||||||
@@ -4239,6 +4266,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
|
|
||||||
/// @brief return the type as string
|
/// @brief return the type as string
|
||||||
/// @sa https://json.nlohmann.me/api/basic_json/type_name/
|
/// @sa https://json.nlohmann.me/api/basic_json/type_name/
|
||||||
|
JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
JSON_HEDLEY_RETURNS_NON_NULL
|
JSON_HEDLEY_RETURNS_NON_NULL
|
||||||
const char* type_name() const noexcept
|
const char* type_name() const noexcept
|
||||||
{
|
{
|
||||||
@@ -4473,8 +4501,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in CBOR format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in CBOR format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4490,8 +4521,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -4516,8 +4550,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::cbor).sax_parse(input_format_t::cbor, &sdp, strict, tag_handler)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in MessagePack format
|
/// @brief create a JSON value from an input in MessagePack format
|
||||||
@@ -4531,8 +4568,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in MessagePack format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in MessagePack format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4547,8 +4587,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -4571,8 +4614,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::msgpack).sax_parse(input_format_t::msgpack, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in UBJSON format
|
/// @brief create a JSON value from an input in UBJSON format
|
||||||
@@ -4586,8 +4632,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in UBJSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in UBJSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4602,8 +4651,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -4626,8 +4678,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::ubjson).sax_parse(input_format_t::ubjson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BJData format
|
/// @brief create a JSON value from an input in BJData format
|
||||||
@@ -4641,8 +4696,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BJData format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in BJData format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4657,8 +4715,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bjdata).sax_parse(input_format_t::bjdata, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BSON format
|
/// @brief create a JSON value from an input in BSON format
|
||||||
@@ -4672,8 +4733,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
auto ia = detail::input_adapter(std::forward<InputType>(i));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// @brief create a JSON value from an input in BSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
/// @brief create a JSON value from an input in BSON format (iterator pair, or iterator+sentinel pair for C++20 ranges support)
|
||||||
@@ -4688,8 +4752,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
basic_json result;
|
basic_json result;
|
||||||
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
auto ia = detail::input_adapter(std::move(first), std::move(last));
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@@ -4712,8 +4779,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
auto ia = i.get();
|
auto ia = i.get();
|
||||||
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
detail::json_sax_dom_parser<basic_json, decltype(ia)> sdp(result, allow_exceptions);
|
||||||
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
// NOLINTNEXTLINE(hicpp-move-const-arg,performance-move-const-arg)
|
||||||
const bool res = binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict); // cppcheck-suppress[accessMoved]
|
if (!binary_reader<decltype(ia)>(std::move(ia), input_format_t::bson).sax_parse(input_format_t::bson, &sdp, strict)) // cppcheck-suppress[accessMoved]
|
||||||
return res ? result : basic_json(value_t::discarded);
|
{
|
||||||
|
result = value_t::discarded;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
/// @}
|
/// @}
|
||||||
|
|
||||||
@@ -4939,6 +5009,36 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
// note erase performs range check
|
// note erase performs range check
|
||||||
parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
|
parent.erase(json_pointer::template array_index<basic_json_t>(last_path));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// the parent of a "remove" target must be an object or array
|
||||||
|
// (see #5396)
|
||||||
|
JSON_THROW(out_of_range::create(413, detail::concat("cannot remove value: the JSON Patch 'remove' target's parent is of type ", parent.type_name(), ", but must be an object or array"), &parent));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// RFC 6902 (section 4.4) forbids "from" from being a proper prefix
|
||||||
|
// of "path" for a "move" operation: a location cannot be moved into
|
||||||
|
// one of its own children. Compares reference tokens (already
|
||||||
|
// unescaped by json_pointer's parser) rather than the raw pointer
|
||||||
|
// strings, since a token may itself contain an escaped '/' or '~'
|
||||||
|
// that would defeat a naive string-prefix comparison. "from" equal
|
||||||
|
// to "path" is *not* a proper prefix and must return false.
|
||||||
|
const auto is_proper_prefix = [](const json_pointer & from, const json_pointer & to)
|
||||||
|
{
|
||||||
|
const auto from_size = from.reference_tokens.size();
|
||||||
|
if (from_size >= to.reference_tokens.size())
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (std::size_t i = 0; i < from_size; ++i)
|
||||||
|
{
|
||||||
|
if (!(from.reference_tokens[i] == to.reference_tokens[i]))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
// type check: top level value must be an array
|
// type check: top level value must be an array
|
||||||
@@ -5016,6 +5116,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
|||||||
const auto from_path = get_value("move", "from", true).template get<string_t>();
|
const auto from_path = get_value("move", "from", true).template get<string_t>();
|
||||||
json_pointer from_ptr(from_path);
|
json_pointer from_ptr(from_path);
|
||||||
|
|
||||||
|
if (JSON_HEDLEY_UNLIKELY(is_proper_prefix(from_ptr, ptr)))
|
||||||
|
{
|
||||||
|
JSON_THROW(out_of_range::create(414, detail::concat("cannot move value: 'from' path '", from_path, "' is a proper prefix of 'path' '", path, "'"), &result));
|
||||||
|
}
|
||||||
|
|
||||||
// the "from" location must exist - use at()
|
// the "from" location must exist - use at()
|
||||||
basic_json const v = result.at(from_ptr);
|
basic_json const v = result.at(from_ptr);
|
||||||
|
|
||||||
|
|||||||
+4
-1
@@ -17,7 +17,7 @@
|
|||||||
#undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
|
#undef JSON_HEDLEY_CLANG_HAS_ATTRIBUTE
|
||||||
#undef JSON_HEDLEY_CLANG_HAS_BUILTIN
|
#undef JSON_HEDLEY_CLANG_HAS_BUILTIN
|
||||||
#undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
|
#undef JSON_HEDLEY_CLANG_HAS_CPP_ATTRIBUTE
|
||||||
#undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_DECLSPEC_ATTRIBUTE
|
#undef JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE
|
||||||
#undef JSON_HEDLEY_CLANG_HAS_EXTENSION
|
#undef JSON_HEDLEY_CLANG_HAS_EXTENSION
|
||||||
#undef JSON_HEDLEY_CLANG_HAS_FEATURE
|
#undef JSON_HEDLEY_CLANG_HAS_FEATURE
|
||||||
#undef JSON_HEDLEY_CLANG_HAS_WARNING
|
#undef JSON_HEDLEY_CLANG_HAS_WARNING
|
||||||
@@ -108,7 +108,10 @@
|
|||||||
#undef JSON_HEDLEY_PELLES_VERSION_CHECK
|
#undef JSON_HEDLEY_PELLES_VERSION_CHECK
|
||||||
#undef JSON_HEDLEY_PGI_VERSION
|
#undef JSON_HEDLEY_PGI_VERSION
|
||||||
#undef JSON_HEDLEY_PGI_VERSION_CHECK
|
#undef JSON_HEDLEY_PGI_VERSION_CHECK
|
||||||
|
#undef JSON_HEDLEY_PRAGMA
|
||||||
#undef JSON_HEDLEY_PREDICT
|
#undef JSON_HEDLEY_PREDICT
|
||||||
|
#undef JSON_HEDLEY_PREDICT_FALSE
|
||||||
|
#undef JSON_HEDLEY_PREDICT_TRUE
|
||||||
#undef JSON_HEDLEY_PRINTF_FORMAT
|
#undef JSON_HEDLEY_PRINTF_FORMAT
|
||||||
#undef JSON_HEDLEY_PRIVATE
|
#undef JSON_HEDLEY_PRIVATE
|
||||||
#undef JSON_HEDLEY_PUBLIC
|
#undef JSON_HEDLEY_PUBLIC
|
||||||
|
|||||||
+639
-158
File diff suppressed because it is too large
Load Diff
@@ -125,6 +125,51 @@ json_test_set_test_options(test-unicode4 TEST_PROPERTIES TIMEOUT 3000)
|
|||||||
# add unit tests
|
# add unit tests
|
||||||
#############################################################################
|
#############################################################################
|
||||||
|
|
||||||
|
# Generate the leak checks for every JSON_HEDLEY_* macro defined in
|
||||||
|
# hedley.hpp; tests/src/unit-no-macro-leak.cpp #include-s the result after
|
||||||
|
# nlohmann/json.hpp (see issue #5408). Using the shared
|
||||||
|
# cmake/scripts/gen_hedley_undef_check.cmake script (also used by `make
|
||||||
|
# update_hedley_undef`) instead of a hand-maintained list of macro names
|
||||||
|
# means this test can never go stale after a future `make update_hedley`.
|
||||||
|
set(hedley_hpp "${PROJECT_SOURCE_DIR}/include/nlohmann/thirdparty/hedley/hedley.hpp")
|
||||||
|
set(hedley_undef_check_script "${PROJECT_SOURCE_DIR}/cmake/scripts/gen_hedley_undef_check.cmake")
|
||||||
|
set(hedley_undef_checks "${PROJECT_BINARY_DIR}/include/hedley_undef_checks.inc")
|
||||||
|
|
||||||
|
# Reconfigure whenever the vendored header or the generator script changes,
|
||||||
|
# so a `cmake --build` after `make update_hedley` does not silently keep a
|
||||||
|
# stale generated file around.
|
||||||
|
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
|
||||||
|
"${hedley_hpp}"
|
||||||
|
"${hedley_undef_check_script}")
|
||||||
|
|
||||||
|
# Generate once at configure time, so the very first build (before any
|
||||||
|
# custom-command build step has run) already has an up-to-date file.
|
||||||
|
execute_process(
|
||||||
|
COMMAND ${CMAKE_COMMAND}
|
||||||
|
"-DHEDLEY_HPP=${hedley_hpp}"
|
||||||
|
"-DOUTPUT=${hedley_undef_checks}"
|
||||||
|
-DMODE=checks
|
||||||
|
-P "${hedley_undef_check_script}"
|
||||||
|
RESULT_VARIABLE hedley_undef_check_result
|
||||||
|
)
|
||||||
|
if(NOT hedley_undef_check_result EQUAL 0)
|
||||||
|
message(FATAL_ERROR "Failed to generate ${hedley_undef_checks}")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Also (re)generate as a build step, so an incremental build after editing
|
||||||
|
# hedley.hpp without a full reconfigure still picks up the change.
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT "${hedley_undef_checks}"
|
||||||
|
COMMAND ${CMAKE_COMMAND}
|
||||||
|
"-DHEDLEY_HPP=${hedley_hpp}"
|
||||||
|
"-DOUTPUT=${hedley_undef_checks}"
|
||||||
|
-DMODE=checks
|
||||||
|
-P "${hedley_undef_check_script}"
|
||||||
|
DEPENDS "${hedley_hpp}" "${hedley_undef_check_script}"
|
||||||
|
COMMENT "Generating Hedley undef leak checks"
|
||||||
|
VERBATIM)
|
||||||
|
add_custom_target(generate_hedley_undef_checks DEPENDS "${hedley_undef_checks}")
|
||||||
|
|
||||||
if("${JSON_TestStandards}" STREQUAL "")
|
if("${JSON_TestStandards}" STREQUAL "")
|
||||||
set(test_cxx_standards 11 14 17 20 23)
|
set(test_cxx_standards 11 14 17 20 23)
|
||||||
unset(test_force)
|
unset(test_force)
|
||||||
@@ -163,6 +208,14 @@ foreach(file ${files})
|
|||||||
json_test_add_test_for(${file} MAIN test_main CXX_STANDARDS ${test_cxx_standards} ${test_force})
|
json_test_add_test_for(${file} MAIN test_main CXX_STANDARDS ${test_cxx_standards} ${test_force})
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
|
# tests/src/unit-no-macro-leak.cpp #include-s the generated leak-check file,
|
||||||
|
# so its test targets must be built after generate_hedley_undef_checks.
|
||||||
|
foreach(cxx_standard ${test_cxx_standards})
|
||||||
|
if(TARGET test-no-macro-leak_cpp${cxx_standard})
|
||||||
|
add_dependencies(test-no-macro-leak_cpp${cxx_standard} generate_hedley_undef_checks)
|
||||||
|
endif()
|
||||||
|
endforeach()
|
||||||
|
|
||||||
if(json_32bit_test_only)
|
if(json_32bit_test_only)
|
||||||
# Skip all other tests in this file
|
# Skip all other tests in this file
|
||||||
return()
|
return()
|
||||||
|
|||||||
@@ -15,6 +15,15 @@
|
|||||||
namespace utils
|
namespace utils
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Some tests intentionally discard the [[nodiscard]]/JSON_HEDLEY_WARN_UNUSED_RESULT
|
||||||
|
// return value of a call they only make to exercise its side effects (e.g. checking
|
||||||
|
// that it does not throw). A plain (void) cast on the call expression does not
|
||||||
|
// suppress GCC's warning for functions using the GNU __attribute__((warn_unused_result))
|
||||||
|
// form (as opposed to the C++17 [[nodiscard]] attribute) -- passing the value into an
|
||||||
|
// ordinary function call does.
|
||||||
|
template<typename T>
|
||||||
|
inline void ignore_return_value(T&& /*unused*/) noexcept {}
|
||||||
|
|
||||||
inline std::vector<std::uint8_t> read_binary_file(const std::string& filename)
|
inline std::vector<std::uint8_t> read_binary_file(const std::string& filename)
|
||||||
{
|
{
|
||||||
std::ifstream file(filename, std::ios::binary);
|
std::ifstream file(filename, std::ios::binary);
|
||||||
|
|||||||
@@ -3288,8 +3288,10 @@ TEST_CASE("BJData")
|
|||||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR1), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR1), "[json.exception.parse_error.113] parse error at byte 6: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||||
CHECK(json::from_bjdata(vR1, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR1, true, false).is_discarded());
|
||||||
|
|
||||||
|
// a dimension vector that opens another one is rejected where the
|
||||||
|
// nested '[' is read, rather than after it has been descended into
|
||||||
std::vector<uint8_t> const vR2 = {'[', '$', 'i', '#', '[', '#', '[', 'i', 1, ']', ']', 1};
|
std::vector<uint8_t> const vR2 = {'[', '$', 'i', '#', '[', '#', '[', 'i', 1, ']', ']', 1};
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR2), "[json.exception.parse_error.113] parse error at byte 11: syntax error while parsing BJData size: expected length type specification (U, i, u, I, m, l, M, L) after '#'; last byte: 0x5D", json::parse_error&);
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR2), "[json.exception.parse_error.113] parse error at byte 7: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||||
CHECK(json::from_bjdata(vR2, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR2, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vR3 = {'[', '#', '[', 'i', '2', 'i', 2, ']'};
|
std::vector<uint8_t> const vR3 = {'[', '#', '[', 'i', '2', 'i', 2, ']'};
|
||||||
@@ -3297,7 +3299,7 @@ TEST_CASE("BJData")
|
|||||||
CHECK(json::from_bjdata(vR3, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR3, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vR4 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', 1, ']', 1};
|
std::vector<uint8_t> const vR4 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', 1, ']', 1};
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR4), "[json.exception.parse_error.110] parse error at byte 14: syntax error while parsing BJData number: unexpected end of input", json::parse_error&);
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR4), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||||
CHECK(json::from_bjdata(vR4, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR4, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vR5 = {'[', '$', 'i', '#', '[', '[', '[', ']', ']', ']'};
|
std::vector<uint8_t> const vR5 = {'[', '$', 'i', '#', '[', '[', '[', ']', ']', ']'};
|
||||||
@@ -3305,12 +3307,25 @@ TEST_CASE("BJData")
|
|||||||
CHECK(json::from_bjdata(vR5, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR5, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vR6 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
std::vector<uint8_t> const vR6 = {'[', '$', 'i', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR6), "[json.exception.parse_error.112] parse error at byte 14: syntax error while parsing BJData size: ndarray can not be recursive", json::parse_error&);
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vR6), "[json.exception.parse_error.113] parse error at byte 9: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||||
CHECK(json::from_bjdata(vR6, true, false).is_discarded());
|
CHECK(json::from_bjdata(vR6, true, false).is_discarded());
|
||||||
|
|
||||||
std::vector<uint8_t> const vH = {'[', 'H', '[', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
std::vector<uint8_t> const vH = {'[', 'H', '[', '#', '[', '$', 'i', '#', '[', 'i', '2', 'i', 2, ']'};
|
||||||
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vH), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vH), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||||
CHECK(json::from_bjdata(vH, true, false).is_discarded());
|
CHECK(json::from_bjdata(vH, true, false).is_discarded());
|
||||||
|
|
||||||
|
// Every "#[" of this chain used to open another dimension vector
|
||||||
|
// and cost several stack frames before anything was rejected, so a
|
||||||
|
// long enough chain crashed the process (see #5104). The nested
|
||||||
|
// vector is refused where it is read, so the length is irrelevant.
|
||||||
|
std::vector<uint8_t> vRdeep = {'['};
|
||||||
|
for (std::size_t i = 0; i < 100000; ++i)
|
||||||
|
{
|
||||||
|
vRdeep.push_back('#');
|
||||||
|
vRdeep.push_back('[');
|
||||||
|
}
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_bjdata(vRdeep), "[json.exception.parse_error.113] parse error at byte 5: syntax error while parsing BJData size: ndarray dimensional vector is not allowed", json::parse_error&);
|
||||||
|
CHECK(json::from_bjdata(vRdeep, true, false).is_discarded());
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("objects")
|
SECTION("objects")
|
||||||
|
|||||||
@@ -38,6 +38,54 @@ class huge_binary_t : public std::vector<std::uint8_t>
|
|||||||
using huge_binary_json = nlohmann::basic_json <
|
using huge_binary_json = nlohmann::basic_json <
|
||||||
std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t,
|
std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t,
|
||||||
double, std::allocator, nlohmann::adl_serializer, huge_binary_t, void >;
|
double, std::allocator, nlohmann::adl_serializer, huge_binary_t, void >;
|
||||||
|
|
||||||
|
// a string type that can be made to report a size beyond INT32_MAX without
|
||||||
|
// allocating that much memory, so BSON length overflow can be tested for
|
||||||
|
// strings and (embedded) documents as well, following the same idea as
|
||||||
|
// huge_binary_t.
|
||||||
|
//
|
||||||
|
// Unlike huge_binary_t (which is only ever used as the BSON *value* type),
|
||||||
|
// this type doubles as basic_json's StringType and is therefore also used
|
||||||
|
// for *object keys* (e.g. "s" or "nested" below). Only the designated test
|
||||||
|
// value is meant to lie about its size - if every huge_string_t (including
|
||||||
|
// keys) reported a huge size, the running totals computed while walking the
|
||||||
|
// BSON document (see calc_bson_object_size & friends in binary_writer.hpp)
|
||||||
|
// would need more than 32 bits, and on platforms where std::size_t is only
|
||||||
|
// 32 bits wide that arithmetic would silently wrap around, producing wrong
|
||||||
|
// (or even unguarded) lengths. The fake size is therefore opt-in via
|
||||||
|
// as_huge(), and plain strings - in particular object keys - keep reporting
|
||||||
|
// their real, small size.
|
||||||
|
class huge_string_t : public std::string
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using std::string::string;
|
||||||
|
huge_string_t(const std::string& s) : std::string(s) {} // NOLINT(google-explicit-constructor,hicpp-explicit-conversions)
|
||||||
|
|
||||||
|
// returns a copy of @a s whose size() pretends to be huge
|
||||||
|
static huge_string_t as_huge(const std::string& s)
|
||||||
|
{
|
||||||
|
huge_string_t result(s);
|
||||||
|
result.pretend_huge = true;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_type size() const noexcept
|
||||||
|
{
|
||||||
|
if (pretend_huge)
|
||||||
|
{
|
||||||
|
// one byte more than the BSON length field can represent
|
||||||
|
return static_cast<size_type>((std::numeric_limits<std::int32_t>::max)()) + 1;
|
||||||
|
}
|
||||||
|
return std::string::size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool pretend_huge = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
using huge_string_json = nlohmann::basic_json <
|
||||||
|
std::map, std::vector, huge_string_t, bool, std::int64_t, std::uint64_t,
|
||||||
|
double, std::allocator, nlohmann::adl_serializer, std::vector<std::uint8_t>, void >;
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
TEST_CASE("BSON")
|
TEST_CASE("BSON")
|
||||||
@@ -104,6 +152,11 @@ TEST_CASE("BSON")
|
|||||||
}
|
}
|
||||||
|
|
||||||
SECTION("lengths exceeding INT32_MAX cannot be serialized to BSON")
|
SECTION("lengths exceeding INT32_MAX cannot be serialized to BSON")
|
||||||
|
{
|
||||||
|
// out_of_range.412 is thrown from a single shared helper
|
||||||
|
// (to_bson_length) that guards the BSON length fields of binary
|
||||||
|
// values, strings, and (embedded) documents alike
|
||||||
|
SECTION("binary")
|
||||||
{
|
{
|
||||||
huge_binary_json j;
|
huge_binary_json j;
|
||||||
j["b"] = huge_binary_json::binary(huge_binary_t{});
|
j["b"] = huge_binary_json::binary(huge_binary_t{});
|
||||||
@@ -111,6 +164,27 @@ TEST_CASE("BSON")
|
|||||||
CHECK_THROWS_WITH_AS(huge_binary_json::to_bson(j), "[json.exception.out_of_range.412] BSON length 2147483661 exceeds maximum of 2147483647", huge_binary_json::out_of_range&);
|
CHECK_THROWS_WITH_AS(huge_binary_json::to_bson(j), "[json.exception.out_of_range.412] BSON length 2147483661 exceeds maximum of 2147483647", huge_binary_json::out_of_range&);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("string")
|
||||||
|
{
|
||||||
|
huge_string_json j;
|
||||||
|
j["s"] = huge_string_t::as_huge("value");
|
||||||
|
|
||||||
|
CHECK_THROWS_WITH_AS(huge_string_json::to_bson(j), "[json.exception.out_of_range.412] BSON length 2147483661 exceeds maximum of 2147483647", huge_string_json::out_of_range&);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("document")
|
||||||
|
{
|
||||||
|
// an oversized string nested one level deep makes the
|
||||||
|
// *embedded* document's own length exceed INT32_MAX as well
|
||||||
|
huge_string_json nested;
|
||||||
|
nested["s"] = huge_string_t::as_huge("value");
|
||||||
|
huge_string_json j;
|
||||||
|
j["nested"] = nested;
|
||||||
|
|
||||||
|
CHECK_THROWS_WITH_AS(huge_string_json::to_bson(j), "[json.exception.out_of_range.412] BSON length 2147483674 exceeds maximum of 2147483647", huge_string_json::out_of_range&);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("string length must be at least 1")
|
SECTION("string length must be at least 1")
|
||||||
{
|
{
|
||||||
// from https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11175
|
// from https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=11175
|
||||||
@@ -193,6 +267,23 @@ TEST_CASE("BSON")
|
|||||||
CHECK(json::from_bson(result, true, false) == j);
|
CHECK(json::from_bson(result, true, false) == j);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("non-empty object with bool from a non-0/1 byte (lenient parsing)")
|
||||||
|
{
|
||||||
|
// documented lenient behavior (see gh-5333): any non-zero byte
|
||||||
|
// is accepted as `true`, not just 0x01
|
||||||
|
std::vector<std::uint8_t> const input =
|
||||||
|
{
|
||||||
|
0x0D, 0x00, 0x00, 0x00, // size (little endian)
|
||||||
|
0x08, // entry: boolean
|
||||||
|
'e', 'n', 't', 'r', 'y', '\x00',
|
||||||
|
0x02, // value = 0x02 (neither 0x00 nor 0x01)
|
||||||
|
0x00 // end marker
|
||||||
|
};
|
||||||
|
|
||||||
|
const json expected = { { "entry", true } };
|
||||||
|
CHECK(json::from_bson(input) == expected);
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("non-empty object with double")
|
SECTION("non-empty object with double")
|
||||||
{
|
{
|
||||||
json const j =
|
json const j =
|
||||||
@@ -499,6 +590,29 @@ TEST_CASE("BSON")
|
|||||||
CHECK(json::from_bson(result, true, false) == j);
|
CHECK(json::from_bson(result, true, false) == j);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("array elements with non-conforming keys (lenient parsing)")
|
||||||
|
{
|
||||||
|
// documented lenient behavior (see gh-5333): BSON array element
|
||||||
|
// keys are not checked against the required decimal sequence
|
||||||
|
// "0", "1", "2", ... - elements are taken in encoded order
|
||||||
|
std::vector<std::uint8_t> const input =
|
||||||
|
{
|
||||||
|
0x26, 0x00, 0x00, 0x00, // size (little endian)
|
||||||
|
0x04, 'e', 'n', 't', 'r', 'y', '\x00', // entry: embedded array
|
||||||
|
|
||||||
|
0x1A, 0x00, 0x00, 0x00, // size (little endian)
|
||||||
|
0x10, '5', 0x00, 0x0A, 0x00, 0x00, 0x00, // key "5" (bogus) -> 10
|
||||||
|
0x10, 'x', 0x00, 0x14, 0x00, 0x00, 0x00, // key "x" (non-numeric) -> 20
|
||||||
|
0x10, '1', 0x00, 0x1E, 0x00, 0x00, 0x00, // key "1" (out of order) -> 30
|
||||||
|
0x00, // end marker (embedded array)
|
||||||
|
|
||||||
|
0x00 // end marker
|
||||||
|
};
|
||||||
|
|
||||||
|
const json expected = { { "entry", json::array({10, 20, 30}) } };
|
||||||
|
CHECK(json::from_bson(input) == expected);
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("non-empty object with binary member")
|
SECTION("non-empty object with binary member")
|
||||||
{
|
{
|
||||||
const size_t N = 10;
|
const size_t N = 10;
|
||||||
@@ -594,6 +708,31 @@ TEST_CASE("BSON")
|
|||||||
CHECK(json::from_bson(result, true, false) == j);
|
CHECK(json::from_bson(result, true, false) == j);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("binary member with subtype 0x02 (old binary) keeps its inner length prefix (lenient parsing)")
|
||||||
|
{
|
||||||
|
// documented lenient behavior (see gh-5333): the payload for
|
||||||
|
// binary subtype 0x02 ("old binary") is returned as-is,
|
||||||
|
// including its own inner 4-byte length prefix; it is not
|
||||||
|
// stripped or reinterpreted
|
||||||
|
std::vector<std::uint8_t> const input =
|
||||||
|
{
|
||||||
|
0x17, 0x00, 0x00, 0x00, // size (little endian)
|
||||||
|
0x05, 'e', 'n', 't', 'r', 'y', '\x00', // entry: binary
|
||||||
|
|
||||||
|
0x06, 0x00, 0x00, 0x00, // size of binary (little endian)
|
||||||
|
0x02, // "old binary" subtype
|
||||||
|
0x02, 0x00, 0x00, 0x00, // inner length prefix (part of the old-binary payload)
|
||||||
|
0x68, 0x69, // payload ('h', 'i')
|
||||||
|
|
||||||
|
0x00 // end marker
|
||||||
|
};
|
||||||
|
|
||||||
|
// the inner length prefix is part of the (unmodified) payload
|
||||||
|
const std::vector<std::uint8_t> expected_payload = {0x02, 0x00, 0x00, 0x00, 0x68, 0x69};
|
||||||
|
const json expected = { { "entry", json::binary(expected_payload, 0x02) } };
|
||||||
|
CHECK(json::from_bson(input) == expected);
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("Some more complex document")
|
SECTION("Some more complex document")
|
||||||
{
|
{
|
||||||
json const j =
|
json const j =
|
||||||
|
|||||||
@@ -2035,6 +2035,58 @@ TEST_CASE("CBOR definite length equal to the indefinite-length sentinel")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("CBOR indefinite-length strings do not recurse per chunk")
|
||||||
|
{
|
||||||
|
// Reading an indefinite-length string or byte array used to call itself
|
||||||
|
// once per chunk, so a payload of repeated 0x7F (or 0x5F) bytes exhausted
|
||||||
|
// the call stack before any of the input was rejected. The open levels are
|
||||||
|
// counted now, and the levels below prove the reader still reads the same
|
||||||
|
// values and reports the same errors at the same byte offsets.
|
||||||
|
json _;
|
||||||
|
|
||||||
|
SECTION("many open levels are reported, not crashed on")
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input(200000, 0x7F);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 200001: syntax error while parsing CBOR string: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_cbor(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("many open levels are reported, not crashed on (binary)")
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input(200000, 0x5F);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(input), "[json.exception.parse_error.110] parse error at byte 200001: syntax error while parsing CBOR binary: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_cbor(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("chunks are still concatenated")
|
||||||
|
{
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0xFF})) == json(""));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x61, 0x61, 0xFF})) == json("a"));
|
||||||
|
// nested indefinite-length strings are concatenated across levels
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x61, 0x61, 0xFF, 0x61, 0x62, 0xFF})) == json("ab"));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x7F, 0x61, 0x7A, 0xFF, 0xFF, 0xFF})) == json("z"));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0xA1, 0x7F, 0x61, 0x61, 0xFF, 0x01})) == json({{"a", 1}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("chunks are still concatenated (binary)")
|
||||||
|
{
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x5F, 0x41, 0x61, 0xFF})) == json::binary({0x61}));
|
||||||
|
CHECK(json::from_cbor(std::vector<uint8_t>({0x5F, 0x5F, 0x41, 0x61, 0xFF, 0x41, 0x62, 0xFF})) == json::binary({0x61, 0x62}));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a chunk that is not a string is still rejected")
|
||||||
|
{
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0x7F, 0x7F, 0x00})), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0x00", json::parse_error&);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0x5F, 0x5F, 0x00})), "[json.exception.parse_error.113] parse error at byte 3: syntax error while parsing CBOR binary: expected length specification (0x40-0x5B) or indefinite binary array type (0x5F); last byte: 0x00", json::parse_error&);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a break marker outside an indefinite-length string is not a string")
|
||||||
|
{
|
||||||
|
// 0xFF only closes a string that was opened; on its own it is not one
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_cbor(std::vector<uint8_t>({0xA1, 0xFF, 0x01})), "[json.exception.parse_error.113] parse error at byte 2: syntax error while parsing CBOR string: expected length specification (0x60-0x7B) or indefinite string type (0x7F); last byte: 0xFF", json::parse_error&);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("CBOR roundtrips" * doctest::skip())
|
TEST_CASE("CBOR roundtrips" * doctest::skip())
|
||||||
{
|
{
|
||||||
SECTION("input from flynn")
|
SECTION("input from flynn")
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ using nlohmann::json;
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
class SaxEventLogger
|
class SaxEventLogger
|
||||||
@@ -624,7 +626,8 @@ TEST_CASE("parser class")
|
|||||||
SECTION("overflow")
|
SECTION("overflow")
|
||||||
{
|
{
|
||||||
// overflows during parsing yield an exception
|
// overflows during parsing yield an exception
|
||||||
CHECK_THROWS_WITH_AS(parser_helper("1.18973e+4932").empty(), "[json.exception.out_of_range.406] number overflow parsing '1.18973e+4932'", json::out_of_range&);
|
// empty() is nodiscard; the exception is thrown by parser_helper() itself, before empty() would run
|
||||||
|
CHECK_THROWS_WITH_AS(utils::ignore_return_value(parser_helper("1.18973e+4932").empty()), "[json.exception.out_of_range.406] number overflow parsing '1.18973e+4932'", json::out_of_range&);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("invalid numbers")
|
SECTION("invalid numbers")
|
||||||
@@ -930,6 +933,98 @@ TEST_CASE("parser class")
|
|||||||
CHECK(accept_helper("+1") == false);
|
CHECK(accept_helper("+1") == false);
|
||||||
CHECK(accept_helper("+0") == false);
|
CHECK(accept_helper("+0") == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("issue #5411 - skip conversion when accept() does not need the numeric value")
|
||||||
|
{
|
||||||
|
// lexer::scan_number() may skip strtoull()/strtoll() for
|
||||||
|
// value_unsigned/value_integer tokens when the caller (e.g.
|
||||||
|
// json::accept()) does not need the converted value, as long
|
||||||
|
// as the digit count alone guarantees no 64-bit overflow (see
|
||||||
|
// the "safe_digit_count" fast path in scan_number()). This
|
||||||
|
// differential test checks that json::accept() (which enables
|
||||||
|
// the fast path) and json::parse() (which never does) always
|
||||||
|
// agree, over a corpus that exercises both the fast path
|
||||||
|
// (<=18 digits) and the untouched, exact fallback path (>=19
|
||||||
|
// digits) -- including reclassification of huge digit-only
|
||||||
|
// integers to a (possibly non-finite) floating-point value.
|
||||||
|
const std::vector<std::pair<std::string, bool>> cases =
|
||||||
|
{
|
||||||
|
// normal small/large integers, both signs
|
||||||
|
{"0", true}, {"1", true}, {"-1", true}, {"42", true}, {"-42", true},
|
||||||
|
{"123456789", true}, {"-123456789", true},
|
||||||
|
|
||||||
|
// digit-count boundary around the 18-digit safe cutoff (both signs)
|
||||||
|
{std::string(17, '9'), true},
|
||||||
|
{std::string(18, '9'), true},
|
||||||
|
{std::string(19, '9'), true},
|
||||||
|
{std::string(20, '9'), true},
|
||||||
|
{"-" + std::string(17, '9'), true},
|
||||||
|
{"-" + std::string(18, '9'), true},
|
||||||
|
{"-" + std::string(19, '9'), true},
|
||||||
|
{"-" + std::string(20, '9'), true},
|
||||||
|
|
||||||
|
// 64-bit boundaries
|
||||||
|
{"9223372036854775807", true}, // INT64_MAX
|
||||||
|
{"-9223372036854775808", true}, // INT64_MIN
|
||||||
|
{"18446744073709551615", true}, // UINT64_MAX
|
||||||
|
{"18446744073709551616", true}, // UINT64_MAX + 1 (overflows uint64_t, finite double)
|
||||||
|
|
||||||
|
// the 28-digit example from the issue: overflows uint64_t
|
||||||
|
// but is finite as a double, so the scanner reclassifies
|
||||||
|
// it to value_float and it is accepted
|
||||||
|
{"9999999999999999999999999999", true},
|
||||||
|
|
||||||
|
// huge digit-only integers that overflow even a double -> rejected
|
||||||
|
{std::string(309, '9'), false},
|
||||||
|
{std::string(400, '9'), false},
|
||||||
|
{"1" + std::string(400, '0'), false},
|
||||||
|
|
||||||
|
// 1e999 / 1e400 style overflow -> rejected
|
||||||
|
{"1e999", false},
|
||||||
|
{"1e400", false},
|
||||||
|
{"-1e999", false},
|
||||||
|
{"1E999", false},
|
||||||
|
|
||||||
|
// values straddling DBL_MAX
|
||||||
|
{"1.7976931348623157e308", true}, // <= DBL_MAX, finite
|
||||||
|
{"1.7976931348623159e308", false}, // > DBL_MAX, overflows to inf
|
||||||
|
|
||||||
|
// a mix of other valid/invalid numeric syntax
|
||||||
|
{"3.14159", true},
|
||||||
|
{"-0.0", true},
|
||||||
|
{"1.0e10", true},
|
||||||
|
{"01", false},
|
||||||
|
{"-", false},
|
||||||
|
{"1.", false},
|
||||||
|
{"1e", false},
|
||||||
|
{"+1", false},
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& c : cases)
|
||||||
|
{
|
||||||
|
const std::string& number = c.first;
|
||||||
|
const bool expected = c.second;
|
||||||
|
CAPTURE(number)
|
||||||
|
CAPTURE(expected)
|
||||||
|
|
||||||
|
// accept() takes the fast path (skips conversion when possible)
|
||||||
|
CHECK(json::accept(number) == expected);
|
||||||
|
|
||||||
|
// parse() always performs the full conversion; it must agree
|
||||||
|
json j;
|
||||||
|
CHECK_NOTHROW(json::parser(nlohmann::detail::input_adapter(number), nullptr, false).parse(true, j));
|
||||||
|
CHECK(!j.is_discarded() == expected);
|
||||||
|
|
||||||
|
// wrap in an array so get_token() is exercised beyond the
|
||||||
|
// very first (constructor-time) scan as well
|
||||||
|
std::string wrapped = "[";
|
||||||
|
wrapped += number;
|
||||||
|
wrapped += ",";
|
||||||
|
wrapped += number;
|
||||||
|
wrapped += "]";
|
||||||
|
CHECK(json::accept(wrapped) == expected);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1394,6 +1489,71 @@ TEST_CASE("parser class")
|
|||||||
CHECK(accept_helper("\"\\uD80C\\uFFFF\"") == false);
|
CHECK(accept_helper("\"\\uD80C\\uFFFF\"") == false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(JSON_NOEXCEPTION)
|
||||||
|
SECTION("issue #5412 - whitespace skipping bookkeeping (compact vs. pretty-printed)")
|
||||||
|
{
|
||||||
|
// lexer::skip_whitespace() reads its first character with get() (to
|
||||||
|
// honor a possibly pending unget() from the previous token) and every
|
||||||
|
// further whitespace character with get_ignoring_pending_unget() (a
|
||||||
|
// get() variant that skips the then-always-false next_unget check).
|
||||||
|
// This must not change the reported byte offset, line, or column of
|
||||||
|
// a syntax error, even when a long run of whitespace containing
|
||||||
|
// multiple newlines is skipped beforehand (as with pretty-printed
|
||||||
|
// input). The expected values below were captured from the
|
||||||
|
// unmodified do-while(get()) loop, so any regression that miscounts
|
||||||
|
// characters or newlines while skipping whitespace changes them.
|
||||||
|
const auto check_error = [](const std::string & input, std::size_t expected_byte,
|
||||||
|
const std::string & expected_what)
|
||||||
|
{
|
||||||
|
CAPTURE(input)
|
||||||
|
try
|
||||||
|
{
|
||||||
|
json _ = json::parse(input);
|
||||||
|
FAIL_CHECK("expected a parse_error, but parsing succeeded");
|
||||||
|
}
|
||||||
|
catch (const json::parse_error& e)
|
||||||
|
{
|
||||||
|
CHECK(e.byte == expected_byte);
|
||||||
|
CHECK(std::string(e.what()) == expected_what);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// a nested document, serialized both compactly and pretty-printed
|
||||||
|
// (dump(4)), each truncated right before the final closing '}' so
|
||||||
|
// that the parser hits EOF after skipping all of the (in the
|
||||||
|
// pretty-printed case, substantial) indentation whitespace
|
||||||
|
const json doc =
|
||||||
|
{
|
||||||
|
{"a", 1},
|
||||||
|
{"b", json::array({true, false, nullptr, "x"})},
|
||||||
|
{"c", json::object({{"d", 3.14}, {"e", json::array({1, 2, 3})}})}
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::string compact = doc.dump();
|
||||||
|
const std::string pretty = doc.dump(4);
|
||||||
|
|
||||||
|
check_error(compact.substr(0, compact.size() - 1), 60,
|
||||||
|
"[json.exception.parse_error.101] parse error at line 1, column 60: syntax error while parsing object - unexpected end of input; expected '}'");
|
||||||
|
check_error(pretty.substr(0, pretty.size() - 1), 193,
|
||||||
|
"[json.exception.parse_error.101] parse error at line 17, column 1: syntax error while parsing object - unexpected end of input; expected '}'");
|
||||||
|
|
||||||
|
// an invalid token appearing after several indented, multi-line
|
||||||
|
// whitespace runs vs. the same document without any of that
|
||||||
|
// whitespace
|
||||||
|
check_error(R"({
|
||||||
|
"a": 1,
|
||||||
|
"b": [
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
],
|
||||||
|
"c": @
|
||||||
|
})", 70,
|
||||||
|
"[json.exception.parse_error.101] parse error at line 7, column 10: syntax error while parsing value - invalid literal; last read: '\"c\": @'");
|
||||||
|
check_error(R"({"a":1,"b":[true,false],"c":@})", 29,
|
||||||
|
"[json.exception.parse_error.101] parse error at line 1, column 29: syntax error while parsing value - invalid literal; last read: '\"c\":@'");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
SECTION("tests found by mutate++")
|
SECTION("tests found by mutate++")
|
||||||
{
|
{
|
||||||
// test case to make sure no comma precedes the first key
|
// test case to make sure no comma precedes the first key
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ using nlohmann::json;
|
|||||||
|
|
||||||
#include <valarray>
|
#include <valarray>
|
||||||
|
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
class SaxEventLogger
|
class SaxEventLogger
|
||||||
@@ -629,7 +631,8 @@ TEST_CASE("parser class")
|
|||||||
SECTION("overflow")
|
SECTION("overflow")
|
||||||
{
|
{
|
||||||
// overflows during parsing yield an exception
|
// overflows during parsing yield an exception
|
||||||
CHECK_THROWS_WITH_AS(parser_helper("1.18973e+4932").empty(), "[json.exception.out_of_range.406] number overflow parsing '1.18973e+4932'", json::out_of_range&);
|
// empty() is nodiscard; the exception is thrown by parser_helper() itself, before empty() would run
|
||||||
|
CHECK_THROWS_WITH_AS(utils::ignore_return_value(parser_helper("1.18973e+4932").empty()), "[json.exception.out_of_range.406] number overflow parsing '1.18973e+4932'", json::out_of_range&);
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("invalid numbers")
|
SECTION("invalid numbers")
|
||||||
|
|||||||
@@ -1389,6 +1389,37 @@ TEST_CASE("value conversion")
|
|||||||
// CHECK(m5["one"] == "eins");
|
// CHECK(m5["one"] == "eins");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("reserve is called on containers that support it (#5406)")
|
||||||
|
{
|
||||||
|
// build a larger object so that a missing/incorrect reserve()
|
||||||
|
// call would be more likely to corrupt or drop elements
|
||||||
|
json j_large;
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
j_large[std::to_string(i)] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("std::unordered_map (supports reserve)")
|
||||||
|
{
|
||||||
|
const auto m = j_large.get<std::unordered_map<std::string, int>>();
|
||||||
|
CHECK(m.size() == 100);
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
CHECK(m.at(std::to_string(i)) == i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("std::map (no reserve, fallback path)")
|
||||||
|
{
|
||||||
|
const auto m = j_large.get<std::map<std::string, int>>();
|
||||||
|
CHECK(m.size() == 100);
|
||||||
|
for (int i = 0; i < 100; ++i)
|
||||||
|
{
|
||||||
|
CHECK(m.at(std::to_string(i)) == i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SECTION("std::multimap")
|
SECTION("std::multimap")
|
||||||
{
|
{
|
||||||
j1.get<std::multimap<std::string, int>>();
|
j1.get<std::multimap<std::string, int>>();
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
// __ _____ _____ _____
|
||||||
|
// __| | __| | | | JSON for Modern C++ (supporting code)
|
||||||
|
// | | |__ | | | | | | version 3.12.0
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// This file contains the C++17-only part of unit-items.cpp (structured
|
||||||
|
// bindings support for json::items()). It is kept in a separate
|
||||||
|
// translation unit so the (much larger) unit-items.cpp does not need to
|
||||||
|
// be compiled a second time just for this one SECTION.
|
||||||
|
|
||||||
|
#include "doctest_compatibility.h"
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
using nlohmann::json;
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_17
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
TEST_CASE("items()")
|
||||||
|
{
|
||||||
|
SECTION("object")
|
||||||
|
{
|
||||||
|
SECTION("structured bindings")
|
||||||
|
{
|
||||||
|
json j = { {"A", 1}, {"B", 2} };
|
||||||
|
|
||||||
|
std::map<std::string, int> m;
|
||||||
|
|
||||||
|
for (auto const&[key, value] : j.items())
|
||||||
|
{
|
||||||
|
m.emplace(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
CHECK(j.get<decltype(m)>() == m);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -862,22 +862,6 @@ TEST_CASE("items()")
|
|||||||
|
|
||||||
CHECK(counter == 3);
|
CHECK(counter == 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_17
|
|
||||||
SECTION("structured bindings")
|
|
||||||
{
|
|
||||||
json j = { {"A", 1}, {"B", 2} };
|
|
||||||
|
|
||||||
std::map<std::string, int> m;
|
|
||||||
|
|
||||||
for (auto const&[key, value] : j.items())
|
|
||||||
{
|
|
||||||
m.emplace(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
CHECK(j.get<decltype(m)>() == m);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("const object")
|
SECTION("const object")
|
||||||
|
|||||||
@@ -1389,6 +1389,192 @@ TEST_CASE("JSON patch - add to a primitive parent (regression #4292)")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("JSON patch - remove with primitive or null parent (regression #5396)")
|
||||||
|
{
|
||||||
|
// Regression test for https://github.com/nlohmann/json/issues/5396
|
||||||
|
//
|
||||||
|
// RFC 6902 (§4.2) requires the target location of a "remove" operation
|
||||||
|
// to exist. When the target's parent resolves to a primitive value or
|
||||||
|
// null, the operation must fail. Previously operation_remove silently
|
||||||
|
// did nothing in this case (neither the "is_object" nor the "is_array"
|
||||||
|
// branch matched, and there was no final "else"), so the patch appeared
|
||||||
|
// to succeed without changing the document. It now throws
|
||||||
|
// out_of_range.413.
|
||||||
|
|
||||||
|
SECTION("parent is a primitive (number)")
|
||||||
|
{
|
||||||
|
json const doc = {{"a", 1}};
|
||||||
|
json const patch = {{{"op", "remove"}, {"path", "/a/b"}}};
|
||||||
|
#if JSON_DIAGNOSTICS
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.413] (/a) cannot remove value: the JSON Patch 'remove' target's parent is of type number, but must be an object or array", json::out_of_range&);
|
||||||
|
#else
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.413] cannot remove value: the JSON Patch 'remove' target's parent is of type number, but must be an object or array", json::out_of_range&);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("parent is a primitive (string)")
|
||||||
|
{
|
||||||
|
json const doc = {{"foo", {{"bar", "a string"}}}};
|
||||||
|
json const patch = {{{"op", "remove"}, {"path", "/foo/bar/baz"}}};
|
||||||
|
#if JSON_DIAGNOSTICS
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.413] (/foo/bar) cannot remove value: the JSON Patch 'remove' target's parent is of type string, but must be an object or array", json::out_of_range&);
|
||||||
|
#else
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.413] cannot remove value: the JSON Patch 'remove' target's parent is of type string, but must be an object or array", json::out_of_range&);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("top-level document is null")
|
||||||
|
{
|
||||||
|
json const doc = nullptr;
|
||||||
|
json const patch = {{{"op", "remove"}, {"path", "/a"}}};
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.413] cannot remove value: the JSON Patch 'remove' target's parent is of type null, but must be an object or array", json::out_of_range&);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("legitimate removes still work")
|
||||||
|
{
|
||||||
|
// object member
|
||||||
|
json const doc1 = {{"a", 1}, {"b", 2}};
|
||||||
|
json const patch1 = {{{"op", "remove"}, {"path", "/a"}}};
|
||||||
|
CHECK(doc1.patch(patch1) == json({{"b", 2}}));
|
||||||
|
|
||||||
|
// array element
|
||||||
|
json const doc2 = R"([1, 2, 3])"_json;
|
||||||
|
json const patch2 = {{{"op", "remove"}, {"path", "/1"}}};
|
||||||
|
CHECK(doc2.patch(patch2) == R"([1, 3])"_json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("JSON patch - move where 'from' is a proper prefix of 'path' (regression #5397)")
|
||||||
|
{
|
||||||
|
// Regression test for https://github.com/nlohmann/json/issues/5397
|
||||||
|
//
|
||||||
|
// RFC 6902 (§4.4) forbids "from" from being a proper prefix of "path"
|
||||||
|
// for a "move" operation: "a location cannot be moved into one of its
|
||||||
|
// children." "move" is implemented as remove-then-add; for an object
|
||||||
|
// target this happened to throw anyway as a side effect of the "add"
|
||||||
|
// step re-resolving through the now-removed parent, but for an array
|
||||||
|
// target the removal shifted subsequent indices, so "path" silently
|
||||||
|
// re-resolved to a different element and the operation "succeeded"
|
||||||
|
// with a corrupted result. It now throws out_of_range.414 for both
|
||||||
|
// object and array targets.
|
||||||
|
|
||||||
|
SECTION("array target (from the issue)")
|
||||||
|
{
|
||||||
|
json const doc = R"([[1,2],[3]])"_json;
|
||||||
|
json const patch = {{{"op", "move"}, {"from", "/0"}, {"path", "/0/0"}}};
|
||||||
|
#if JSON_DIAGNOSTIC_POSITIONS
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] (bytes 0-11) cannot move value: 'from' path '/0' is a proper prefix of 'path' '/0/0'", json::out_of_range&);
|
||||||
|
#else
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] cannot move value: 'from' path '/0' is a proper prefix of 'path' '/0/0'", json::out_of_range&);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("object target")
|
||||||
|
{
|
||||||
|
json const doc = R"({"a": {"b": 1}})"_json;
|
||||||
|
json const patch = {{{"op", "move"}, {"from", "/a"}, {"path", "/a/b"}}};
|
||||||
|
#if JSON_DIAGNOSTIC_POSITIONS
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] (bytes 0-15) cannot move value: 'from' path '/a' is a proper prefix of 'path' '/a/b'", json::out_of_range&);
|
||||||
|
#else
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] cannot move value: 'from' path '/a' is a proper prefix of 'path' '/a/b'", json::out_of_range&);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("from == path is not a proper prefix and must not be rejected")
|
||||||
|
{
|
||||||
|
// "from" equal to "path" is a no-op move; it is not a *proper*
|
||||||
|
// prefix relationship, so this new check must not reject it.
|
||||||
|
json const doc = R"({"a": 1, "b": 2})"_json;
|
||||||
|
json const patch = {{{"op", "move"}, {"from", "/a"}, {"path", "/a"}}};
|
||||||
|
CHECK(doc.patch(patch) == doc);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("raw string prefix that is not a pointer-token prefix must be allowed")
|
||||||
|
{
|
||||||
|
// "/ab" is a string-prefix of "/abc/x" as raw text, but "ab" and
|
||||||
|
// "abc" are different reference tokens, so this is NOT a
|
||||||
|
// pointer-token prefix relationship and the move must succeed.
|
||||||
|
// This is the key case proving the check compares tokens, not
|
||||||
|
// raw pointer text (a naive std::string prefix/rfind check on
|
||||||
|
// the undecoded pointer would wrongly reject this).
|
||||||
|
json const doc = R"({"ab": 1, "abc": {"x": 2}})"_json;
|
||||||
|
json const patch = {{{"op", "move"}, {"from", "/ab"}, {"path", "/abc/x"}}};
|
||||||
|
json const result = R"({"abc": {"x": 1}})"_json;
|
||||||
|
CHECK(doc.patch(patch) == result);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("escaped reference tokens are compared unescaped")
|
||||||
|
{
|
||||||
|
// "from" is the single token "a/b" (escaped as "a~1b"); "path"
|
||||||
|
// addresses member "x" of that same value, so "from" is a
|
||||||
|
// proper (token-level) prefix of "path" and must be rejected.
|
||||||
|
json const doc = R"({"a/b": {"x": 1}})"_json;
|
||||||
|
json const patch = {{{"op", "move"}, {"from", "/a~1b"}, {"path", "/a~1b/x"}}};
|
||||||
|
#if JSON_DIAGNOSTIC_POSITIONS
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] (bytes 0-17) cannot move value: 'from' path '/a~1b' is a proper prefix of 'path' '/a~1b/x'", json::out_of_range&);
|
||||||
|
#else
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] cannot move value: 'from' path '/a~1b' is a proper prefix of 'path' '/a~1b/x'", json::out_of_range&);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("ordinary valid moves still work")
|
||||||
|
{
|
||||||
|
// unrelated top-level members
|
||||||
|
json const doc1 = R"({"a": 1, "b": 2})"_json;
|
||||||
|
json const patch1 = {{{"op", "move"}, {"from", "/a"}, {"path", "/c"}}};
|
||||||
|
CHECK(doc1.patch(patch1) == R"({"b": 2, "c": 1})"_json);
|
||||||
|
|
||||||
|
// sibling paths that share a textual prefix but are unrelated
|
||||||
|
json const doc2 = R"({"a": {"x": 1}, "b": {"y": 2}})"_json;
|
||||||
|
json const patch2 = {{{"op", "move"}, {"from", "/a/x"}, {"path", "/b/z"}}};
|
||||||
|
CHECK(doc2.patch(patch2) == R"({"a": {}, "b": {"y": 2, "z": 1}})"_json);
|
||||||
|
|
||||||
|
// "path" is a proper prefix of "from" (the reverse relationship,
|
||||||
|
// which RFC 6902 does not forbid)
|
||||||
|
json const doc3 = R"({"a": {"b": 1}})"_json;
|
||||||
|
json const patch3 = {{{"op", "move"}, {"from", "/a/b"}, {"path", "/a"}}};
|
||||||
|
CHECK(doc3.patch(patch3) == R"({"a": 1})"_json);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("root 'from' is a proper prefix of every non-root 'path'")
|
||||||
|
{
|
||||||
|
// the whole document is a proper prefix of any location inside it
|
||||||
|
json const doc = R"({"a": 1})"_json;
|
||||||
|
json const patch = {{{"op", "move"}, {"from", ""}, {"path", "/a"}}};
|
||||||
|
#if JSON_DIAGNOSTIC_POSITIONS
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] (bytes 0-8) cannot move value: 'from' path '' is a proper prefix of 'path' '/a'", json::out_of_range&);
|
||||||
|
#else
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] cannot move value: 'from' path '' is a proper prefix of 'path' '/a'", json::out_of_range&);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("root 'path' is never a proper prefix violation for a non-root 'from'")
|
||||||
|
{
|
||||||
|
// the reverse of the above: moving a non-root location to the root
|
||||||
|
// is the "path is a prefix of from" relationship, which RFC 6902
|
||||||
|
// permits (already covered generally above; this pins the root
|
||||||
|
// case specifically, since root is the one path with no reference
|
||||||
|
// tokens at all)
|
||||||
|
json const doc = R"({"a": {"b": 1}})"_json;
|
||||||
|
json const patch = {{{"op", "move"}, {"from", "/a"}, {"path", ""}}};
|
||||||
|
CHECK(doc.patch(patch) == R"({"b": 1})"_json);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("the array-append token '-' is an ordinary child token")
|
||||||
|
{
|
||||||
|
// "-" (append-to-array) addresses a location *inside* the array,
|
||||||
|
// so "from" pointing at the array is still a proper prefix of
|
||||||
|
// "path" ending in "-" and must be rejected like any other child.
|
||||||
|
json const doc = R"({"a": [1, 2]})"_json;
|
||||||
|
json const patch = {{{"op", "move"}, {"from", "/a"}, {"path", "/a/-"}}};
|
||||||
|
#if JSON_DIAGNOSTIC_POSITIONS
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] (bytes 0-13) cannot move value: 'from' path '/a' is a proper prefix of 'path' '/a/-'", json::out_of_range&);
|
||||||
|
#else
|
||||||
|
CHECK_THROWS_WITH_AS(doc.patch(patch), "[json.exception.out_of_range.414] cannot move value: 'from' path '/a' is a proper prefix of 'path' '/a/-'", json::out_of_range&);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("JSON patch - diff emits array removals in descending index order")
|
TEST_CASE("JSON patch - diff emits array removals in descending index order")
|
||||||
{
|
{
|
||||||
SECTION("array shrunk to empty")
|
SECTION("array shrunk to empty")
|
||||||
|
|||||||
@@ -319,6 +319,44 @@ TEST_CASE("JSON pointers")
|
|||||||
|
|
||||||
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
|
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
|
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
|
||||||
|
// #5395: contains() must not throw for a reference token that is a
|
||||||
|
// syntactically valid array index but numerically exceeds ULLONG_MAX
|
||||||
|
// (causing strtoull() to set errno to ERANGE) -- it should just report
|
||||||
|
// that the pointer does not resolve to an element
|
||||||
|
CHECK(!j.contains(jp));
|
||||||
|
CHECK(!j_const.contains(jp));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// #5395: same as above, but using the exact reproduction from the issue
|
||||||
|
json::json_pointer const jp("/99999999999999999999");
|
||||||
|
std::string const throw_msg = "[json.exception.out_of_range.404] unresolved reference token '99999999999999999999'";
|
||||||
|
|
||||||
|
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
CHECK_THROWS_WITH_AS(j.at(jp) = 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
CHECK_THROWS_WITH_AS(j_const.at(jp) == 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
|
||||||
|
CHECK(!j.contains(jp));
|
||||||
|
CHECK(!j_const.contains(jp));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// #5395: a reference token that is numerically representable in
|
||||||
|
// unsigned long long but exceeds size_type's max (e.g. ULLONG_MAX
|
||||||
|
// itself on typical 64-bit platforms, where size_type's max equals
|
||||||
|
// ULLONG_MAX) must not make contains() throw either
|
||||||
|
json::json_pointer const jp("/18446744073709551615");
|
||||||
|
std::string const throw_msg = "[json.exception.out_of_range.410] array index 18446744073709551615 exceeds size_type";
|
||||||
|
|
||||||
|
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
CHECK_THROWS_WITH_AS(j.at(jp) = 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
CHECK_THROWS_WITH_AS(j_const.at(jp) == 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
|
||||||
|
CHECK(!j.contains(jp));
|
||||||
|
CHECK(!j_const.contains(jp));
|
||||||
}
|
}
|
||||||
|
|
||||||
// on some machines, the check below is not constant
|
// on some machines, the check below is not constant
|
||||||
@@ -334,6 +372,10 @@ TEST_CASE("JSON pointers")
|
|||||||
|
|
||||||
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
|
CHECK_THROWS_WITH_AS(j[jp] = 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
|
CHECK_THROWS_WITH_AS(j_const[jp] == 1, throw_msg.c_str(), json::out_of_range&);
|
||||||
|
|
||||||
|
// #5395: contains() must not throw for a reference token exceeding size_type's max
|
||||||
|
CHECK(!j.contains(jp));
|
||||||
|
CHECK(!j_const.contains(jp));
|
||||||
}
|
}
|
||||||
|
|
||||||
DOCTEST_MSVC_SUPPRESS_WARNING_POP
|
DOCTEST_MSVC_SUPPRESS_WARNING_POP
|
||||||
|
|||||||
@@ -1598,6 +1598,67 @@ TEST_CASE("MessagePack")
|
|||||||
}
|
}
|
||||||
|
|
||||||
// use this testcase outside [hide] to run it with Valgrind
|
// use this testcase outside [hide] to run it with Valgrind
|
||||||
|
TEST_CASE("MessagePack nesting does not consume the call stack")
|
||||||
|
{
|
||||||
|
// Reading a container used to call back into the value reader once per
|
||||||
|
// element, so the native call stack grew with the nesting depth of the
|
||||||
|
// input: one frame per byte for repeated 0x91 (a one-element array), which
|
||||||
|
// crashes the process long before the input is exhausted (#5104). The
|
||||||
|
// containers are kept on a heap stack now.
|
||||||
|
//
|
||||||
|
// Note that deeply nested values must not be compared, copied or dumped
|
||||||
|
// here: those operations are still recursive, and would reintroduce the
|
||||||
|
// very crash this checks for. Depth is measured by descending instead.
|
||||||
|
|
||||||
|
SECTION("an unterminated chain is reported, not crashed on")
|
||||||
|
{
|
||||||
|
json _;
|
||||||
|
const std::vector<uint8_t> input(300000, 0x91);
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_msgpack(input), "[json.exception.parse_error.110] parse error at byte 300001: syntax error while parsing MessagePack value: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_msgpack(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read through the SAX interface")
|
||||||
|
{
|
||||||
|
std::vector<uint8_t> input(300000, 0x91);
|
||||||
|
input.push_back(0x01); // innermost value
|
||||||
|
|
||||||
|
SaxCountdown accept_all(600001);
|
||||||
|
CHECK(json::sax_parse(input, &accept_all, json::input_format_t::msgpack));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a well-formed deep value is read into a value")
|
||||||
|
{
|
||||||
|
const std::size_t depth = 10000;
|
||||||
|
std::vector<uint8_t> input(depth, 0x91);
|
||||||
|
input.push_back(0x01);
|
||||||
|
|
||||||
|
json j = json::from_msgpack(input);
|
||||||
|
|
||||||
|
std::size_t measured = 0;
|
||||||
|
const json* p = &j;
|
||||||
|
while (p->is_array() && !p->empty())
|
||||||
|
{
|
||||||
|
p = &p->front();
|
||||||
|
++measured;
|
||||||
|
}
|
||||||
|
CHECK(measured == depth);
|
||||||
|
CHECK(p->is_number());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("containers are still read the same way")
|
||||||
|
{
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x90})) == json::array());
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x80})) == json::object());
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x92, 0x90, 0x80})) == json({json::array(), json::object()}));
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x91, 0x91, 0x91, 0x90})) == json({{{json::array()}}}));
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0x81, 0xA1, 'a', 0x81, 0xA1, 'b', 0x92, 0x01, 0x02})) == json({{"a", {{"b", {1, 2}}}}}));
|
||||||
|
// array 16 and map 32, i.e. the counted forms
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0xDC, 0x00, 0x02, 0x01, 0x02})) == json({1, 2}));
|
||||||
|
CHECK(json::from_msgpack(std::vector<uint8_t>({0xDF, 0x00, 0x00, 0x00, 0x01, 0xA1, 'k', 0xC3})) == json({{"k", true}}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("single MessagePack roundtrip")
|
TEST_CASE("single MessagePack roundtrip")
|
||||||
{
|
{
|
||||||
SECTION("sample.json")
|
SECTION("sample.json")
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
// __ _____ _____ _____
|
||||||
|
// __| | __| | | | JSON for Modern C++
|
||||||
|
// | | |__ | | | | | | version 3.12.0
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// This file makes sure that none of the internal JSON_HEDLEY_* macros (vendored
|
||||||
|
// from https://nemequ.github.io/hedley/, see
|
||||||
|
// include/nlohmann/thirdparty/hedley/hedley.hpp) leak into the including
|
||||||
|
// translation unit. include/nlohmann/detail/macro_unscope.hpp is supposed to
|
||||||
|
// #undef every JSON_HEDLEY_* macro (via hedley_undef.hpp) once json.hpp has
|
||||||
|
// been fully processed. See https://github.com/nlohmann/json/issues/5408,
|
||||||
|
// where JSON_HEDLEY_PRAGMA, JSON_HEDLEY_PREDICT_TRUE, JSON_HEDLEY_PREDICT_FALSE,
|
||||||
|
// and JSON_HEDLEY_CLANG_HAS_DECLSPEC_ATTRIBUTE escaped this cleanup because
|
||||||
|
// hedley_undef.hpp had no matching #undef for them.
|
||||||
|
//
|
||||||
|
// hedley_undef_checks.inc (included below) is generated at CMake configure/
|
||||||
|
// build time by cmake/scripts/gen_hedley_undef_check.cmake, which derives the
|
||||||
|
// full list of JSON_HEDLEY_* macro names directly from hedley.hpp. That way
|
||||||
|
// this test covers every macro Hedley actually defines -- not a hardcoded
|
||||||
|
// snapshot that would silently go stale the next time `make update_hedley`
|
||||||
|
// runs -- and can never drift from the vendored header.
|
||||||
|
|
||||||
|
#include "doctest_compatibility.h"
|
||||||
|
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
|
TEST_CASE("JSON_HEDLEY macros do not leak after including json.hpp")
|
||||||
|
{
|
||||||
|
#include "hedley_undef_checks.inc"
|
||||||
|
CHECK(true); // keep an assertion when nothing leaked
|
||||||
|
}
|
||||||
@@ -29,10 +29,7 @@ using nlohmann::json;
|
|||||||
#include <limits>
|
#include <limits>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include "make_test_data_available.hpp"
|
#include "make_test_data_available.hpp"
|
||||||
|
#include "test_utils.hpp"
|
||||||
#ifdef JSON_HAS_CPP_17
|
|
||||||
#include <variant>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "fifo_map.hpp"
|
#include "fifo_map.hpp"
|
||||||
|
|
||||||
@@ -1373,7 +1370,8 @@ TEST_CASE("regression tests 1")
|
|||||||
std::array<uint8_t, 28> key1 = {{ 103, 92, 117, 48, 48, 48, 55, 92, 114, 215, 126, 214, 95, 92, 34, 174, 40, 71, 38, 174, 40, 71, 38, 223, 134, 247, 127, 0 }};
|
std::array<uint8_t, 28> key1 = {{ 103, 92, 117, 48, 48, 48, 55, 92, 114, 215, 126, 214, 95, 92, 34, 174, 40, 71, 38, 174, 40, 71, 38, 223, 134, 247, 127, 0 }};
|
||||||
std::string const key1_str(reinterpret_cast<char*>(key1.data()));
|
std::string const key1_str(reinterpret_cast<char*>(key1.data()));
|
||||||
json const j = key1_str;
|
json const j = key1_str;
|
||||||
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E", json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
|
CHECK_THROWS_WITH_AS(utils::ignore_return_value(j.dump()), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E", json::type_error&);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if JSON_USE_IMPLICIT_CONVERSIONS
|
#if JSON_USE_IMPLICIT_CONVERSIONS
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ using ordered_json = nlohmann::ordered_json;
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_17
|
#ifdef JSON_HAS_CPP_17
|
||||||
#include <any>
|
#include <any>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
@@ -239,209 +241,6 @@ class my_allocator : public std::allocator<T>
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
// for #3077
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
class FooAlloc
|
|
||||||
{};
|
|
||||||
|
|
||||||
class Foo
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
explicit Foo(const FooAlloc& /* unused */ = FooAlloc()) {}
|
|
||||||
|
|
||||||
bool value = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
class FooBar
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Foo foo{}; // NOLINT(readability-redundant-member-init)
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void from_json(const nlohmann::json& j, FooBar& fb) // NOLINT(misc-use-internal-linkage)
|
|
||||||
{
|
|
||||||
j.at("value").get_to(fb.foo.value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
// for #3171
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct for_3171_base // NOLINT(cppcoreguidelines-special-member-functions)
|
|
||||||
{
|
|
||||||
for_3171_base(const std::string& /*unused*/ = {}) {}
|
|
||||||
virtual ~for_3171_base();
|
|
||||||
|
|
||||||
for_3171_base(const for_3171_base& other) // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)
|
|
||||||
: str(other.str)
|
|
||||||
{}
|
|
||||||
|
|
||||||
for_3171_base& operator=(const for_3171_base& other)
|
|
||||||
{
|
|
||||||
if (this != &other)
|
|
||||||
{
|
|
||||||
str = other.str;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
for_3171_base(for_3171_base&& other) noexcept
|
|
||||||
: str(std::move(other.str))
|
|
||||||
{}
|
|
||||||
|
|
||||||
for_3171_base& operator=(for_3171_base&& other) noexcept
|
|
||||||
{
|
|
||||||
if (this != &other)
|
|
||||||
{
|
|
||||||
str = std::move(other.str);
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void _from_json(const json& j)
|
|
||||||
{
|
|
||||||
j.at("str").get_to(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string str{}; // NOLINT(readability-redundant-member-init)
|
|
||||||
};
|
|
||||||
|
|
||||||
for_3171_base::~for_3171_base() = default;
|
|
||||||
|
|
||||||
struct for_3171_derived : public for_3171_base
|
|
||||||
{
|
|
||||||
for_3171_derived() = default;
|
|
||||||
~for_3171_derived() override;
|
|
||||||
explicit for_3171_derived(const std::string& /*unused*/) { }
|
|
||||||
|
|
||||||
for_3171_derived(const for_3171_derived& other) // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)
|
|
||||||
: for_3171_base(other)
|
|
||||||
{}
|
|
||||||
|
|
||||||
for_3171_derived& operator=(const for_3171_derived& other)
|
|
||||||
{
|
|
||||||
if (this != &other)
|
|
||||||
{
|
|
||||||
for_3171_base::operator=(other); // Call base class assignment operator
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
for_3171_derived(for_3171_derived&& other) noexcept
|
|
||||||
: for_3171_base(std::move(other))
|
|
||||||
{}
|
|
||||||
|
|
||||||
for_3171_derived& operator=(for_3171_derived&& other) noexcept
|
|
||||||
{
|
|
||||||
if (this != &other)
|
|
||||||
{
|
|
||||||
for_3171_base::operator=(std::move(other)); // Call base class move assignment operator
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for_3171_derived::~for_3171_derived() = default;
|
|
||||||
|
|
||||||
inline void from_json(const json& j, for_3171_base& tb) // NOLINT(misc-use-internal-linkage)
|
|
||||||
{
|
|
||||||
tb._from_json(j);
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
// for #3312
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_20
|
|
||||||
struct for_3312
|
|
||||||
{
|
|
||||||
std::string name;
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void from_json(const json& j, for_3312& obj) // NOLINT(misc-use-internal-linkage)
|
|
||||||
{
|
|
||||||
j.at("name").get_to(obj.name);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
// for #3204
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct for_3204_foo
|
|
||||||
{
|
|
||||||
for_3204_foo() = default;
|
|
||||||
explicit for_3204_foo(std::string /*unused*/) {} // NOLINT(performance-unnecessary-value-param)
|
|
||||||
};
|
|
||||||
|
|
||||||
struct for_3204_bar
|
|
||||||
{
|
|
||||||
enum constructed_from_t // NOLINT(cppcoreguidelines-use-enum-class)
|
|
||||||
{
|
|
||||||
constructed_from_none = 0,
|
|
||||||
constructed_from_foo = 1,
|
|
||||||
constructed_from_json = 2
|
|
||||||
};
|
|
||||||
|
|
||||||
explicit for_3204_bar(std::function<void(for_3204_foo)> /*unused*/) noexcept // NOLINT(performance-unnecessary-value-param)
|
|
||||||
: constructed_from(constructed_from_foo) {}
|
|
||||||
explicit for_3204_bar(std::function<void(json)> /*unused*/) noexcept // NOLINT(performance-unnecessary-value-param)
|
|
||||||
: constructed_from(constructed_from_json) {}
|
|
||||||
|
|
||||||
constructed_from_t constructed_from = constructed_from_none;
|
|
||||||
};
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
// for #3333
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct for_3333 final
|
|
||||||
{
|
|
||||||
for_3333(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
for_3333(const T& /*unused*/)
|
|
||||||
{
|
|
||||||
CHECK(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
int x = 0;
|
|
||||||
int y = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
inline for_3333::for_3333(const json& j)
|
|
||||||
: for_3333(j.value("x", 0), j.value("y", 0))
|
|
||||||
{}
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
// for #3810
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
struct Example_3810
|
|
||||||
{
|
|
||||||
int bla{};
|
|
||||||
|
|
||||||
Example_3810() = default;
|
|
||||||
};
|
|
||||||
|
|
||||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Example_3810, bla) // NOLINT(misc-use-internal-linkage)
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
// for #4740
|
|
||||||
/////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_17
|
|
||||||
struct Example_4740
|
|
||||||
{
|
|
||||||
std::optional<std::string> host = std::nullopt;
|
|
||||||
std::optional<int> port = std::nullopt;
|
|
||||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Example_4740, host, port)
|
|
||||||
};
|
|
||||||
#endif
|
|
||||||
|
|
||||||
TEST_CASE("regression tests 2")
|
TEST_CASE("regression tests 2")
|
||||||
{
|
{
|
||||||
SECTION("issue #1001 - Fix memory leak during parser callback")
|
SECTION("issue #1001 - Fix memory leak during parser callback")
|
||||||
@@ -639,7 +438,8 @@ TEST_CASE("regression tests 2")
|
|||||||
s += static_cast<char>(i);
|
s += static_cast<char>(i);
|
||||||
}
|
}
|
||||||
dump_test["1"] = s;
|
dump_test["1"] = s;
|
||||||
dump_test.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace);
|
// dump() is nodiscard; this only checks that dumping does not throw/crash
|
||||||
|
utils::ignore_return_value(dump_test.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -731,12 +531,14 @@ TEST_CASE("regression tests 2")
|
|||||||
{
|
{
|
||||||
const std::array<unsigned char, 23> data = {{0x81, 0xA4, 0x64, 0x61, 0x74, 0x61, 0xC4, 0x0F, 0x33, 0x30, 0x30, 0x32, 0x33, 0x34, 0x30, 0x31, 0x30, 0x37, 0x30, 0x35, 0x30, 0x31, 0x30}};
|
const std::array<unsigned char, 23> data = {{0x81, 0xA4, 0x64, 0x61, 0x74, 0x61, 0xC4, 0x0F, 0x33, 0x30, 0x30, 0x32, 0x33, 0x34, 0x30, 0x31, 0x30, 0x37, 0x30, 0x35, 0x30, 0x31, 0x30}};
|
||||||
const json j = json::from_msgpack(data.data(), data.size());
|
const json j = json::from_msgpack(data.data(), data.size());
|
||||||
|
// dump() is nodiscard; this only checks that dumping does not throw
|
||||||
CHECK_NOTHROW(
|
CHECK_NOTHROW(
|
||||||
|
utils::ignore_return_value(
|
||||||
j.dump(4, // Indent
|
j.dump(4, // Indent
|
||||||
' ', // Indent char
|
' ', // Indent char
|
||||||
false, // Ensure ascii
|
false, // Ensure ascii
|
||||||
json::error_handler_t::strict // Error
|
json::error_handler_t::strict // Error
|
||||||
));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
SECTION("PR #2181 - regression bug with lvalue")
|
SECTION("PR #2181 - regression bug with lvalue")
|
||||||
@@ -959,611 +761,6 @@ TEST_CASE("regression tests 2")
|
|||||||
CHECK(j == k);
|
CHECK(j == k);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
|
||||||
// JSON_HAS_CPP_17 (do not remove; see note at top of file)
|
|
||||||
SECTION("issue #3070 - Version 3.10.3 breaks backward-compatibility with 3.10.2 ")
|
|
||||||
{
|
|
||||||
nlohmann::detail::std_fs::path text_path("/tmp/text.txt");
|
|
||||||
const json j(text_path);
|
|
||||||
|
|
||||||
const auto j_path = j.get<nlohmann::detail::std_fs::path>();
|
|
||||||
CHECK(j_path == text_path);
|
|
||||||
|
|
||||||
#if DOCTEST_CLANG || DOCTEST_GCC >= DOCTEST_COMPILER(8, 4, 0)
|
|
||||||
// only known to work on Clang and GCC >=8.4
|
|
||||||
CHECK_THROWS_WITH_AS(nlohmann::detail::std_fs::path(json(1)), "[json.exception.type_error.302] type must be string, but is number", json::type_error);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SECTION("issue #3077 - explicit constructor with default does not compile")
|
|
||||||
{
|
|
||||||
json j;
|
|
||||||
j[0]["value"] = true;
|
|
||||||
std::vector<FooBar> foo;
|
|
||||||
j.get_to(foo);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("issue #3108 - ordered_json doesn't support range based erase")
|
|
||||||
{
|
|
||||||
ordered_json j = {1, 2, 2, 4};
|
|
||||||
|
|
||||||
auto last = std::unique(j.begin(), j.end());
|
|
||||||
j.erase(last, j.end());
|
|
||||||
|
|
||||||
CHECK(j.dump() == "[1,2,4]");
|
|
||||||
|
|
||||||
j.erase(std::remove_if(j.begin(), j.end(), [](const ordered_json & val)
|
|
||||||
{
|
|
||||||
return val == 2;
|
|
||||||
}), j.end());
|
|
||||||
|
|
||||||
CHECK(j.dump() == "[1,4]");
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("issue #3343 - json and ordered_json are not interchangeable")
|
|
||||||
{
|
|
||||||
json::object_t jobj({ { "product", "one" } });
|
|
||||||
ordered_json::object_t ojobj({{"product", "one"}});
|
|
||||||
|
|
||||||
auto jit = jobj.begin();
|
|
||||||
auto ojit = ojobj.begin();
|
|
||||||
|
|
||||||
CHECK(jit->first == ojit->first);
|
|
||||||
CHECK(jit->second.get<std::string>() == ojit->second.get<std::string>());
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("issue #3171 - if class is_constructible from std::string wrong from_json overload is being selected, compilation failed")
|
|
||||||
{
|
|
||||||
const json j{{ "str", "value"}};
|
|
||||||
|
|
||||||
// failed with: error: no match for ‘operator=’ (operand types are ‘for_3171_derived’ and ‘const nlohmann::basic_json<>::string_t’
|
|
||||||
// {aka ‘const std::__cxx11::basic_string<char>’})
|
|
||||||
// s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
|
|
||||||
auto td = j.get<for_3171_derived>();
|
|
||||||
|
|
||||||
CHECK(td.str == "value");
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_20
|
|
||||||
SECTION("issue #3312 - Parse to custom class from unordered_json breaks on G++11.2.0 with C++20")
|
|
||||||
{
|
|
||||||
// see test for #3171
|
|
||||||
const ordered_json j = {{"name", "class"}};
|
|
||||||
for_3312 obj{};
|
|
||||||
|
|
||||||
j.get_to(obj);
|
|
||||||
|
|
||||||
CHECK(obj.name == "class");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if defined(JSON_HAS_CPP_17) && JSON_USE_IMPLICIT_CONVERSIONS
|
|
||||||
SECTION("issue #3428 - Error occurred when converting nlohmann::json to std::any")
|
|
||||||
{
|
|
||||||
const json j;
|
|
||||||
const std::any a1 = j;
|
|
||||||
std::any&& a2 = j;
|
|
||||||
|
|
||||||
CHECK(a1.type() == typeid(j));
|
|
||||||
CHECK(a2.type() == typeid(j));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SECTION("issue #3204 - ambiguous regression")
|
|
||||||
{
|
|
||||||
const for_3204_bar bar_from_foo([](for_3204_foo) noexcept {}); // NOLINT(performance-unnecessary-value-param)
|
|
||||||
const for_3204_bar bar_from_json([](json) noexcept {}); // NOLINT(performance-unnecessary-value-param)
|
|
||||||
|
|
||||||
CHECK(bar_from_foo.constructed_from == for_3204_bar::constructed_from_foo);
|
|
||||||
CHECK(bar_from_json.constructed_from == for_3204_bar::constructed_from_json);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("issue #3333 - Ambiguous conversion from nlohmann::basic_json<> to custom class")
|
|
||||||
{
|
|
||||||
const json j
|
|
||||||
{
|
|
||||||
{"x", 1},
|
|
||||||
{"y", 2}
|
|
||||||
};
|
|
||||||
const for_3333 p = j;
|
|
||||||
|
|
||||||
CHECK(p.x == 1);
|
|
||||||
CHECK(p.y == 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("issue #3810 - ordered_json doesn't support construction from C array of custom type")
|
|
||||||
{
|
|
||||||
Example_3810 states[45]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
|
||||||
|
|
||||||
// fix "not used" warning
|
|
||||||
states[0].bla = 1;
|
|
||||||
|
|
||||||
const auto* const expected = R"([{"bla":1},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0}])";
|
|
||||||
|
|
||||||
// This works:
|
|
||||||
nlohmann::json j;
|
|
||||||
j["test"] = states;
|
|
||||||
CHECK(j["test"].dump() == expected);
|
|
||||||
|
|
||||||
// This doesn't compile:
|
|
||||||
nlohmann::ordered_json oj;
|
|
||||||
oj["test"] = states;
|
|
||||||
CHECK(oj["test"].dump() == expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_17
|
|
||||||
SECTION("issue #4740 - build issue with std::optional")
|
|
||||||
{
|
|
||||||
const auto t1 = Example_4740();
|
|
||||||
const auto j1 = nlohmann::json(t1);
|
|
||||||
CHECK(j1.dump() == "{\"host\":null,\"port\":null}");
|
|
||||||
const auto t2 = j1.get<Example_4740>();
|
|
||||||
CHECK(!t2.host.has_value());
|
|
||||||
CHECK(!t2.port.has_value());
|
|
||||||
|
|
||||||
// improve coverage
|
|
||||||
auto t3 = Example_4740();
|
|
||||||
t3.port = 80;
|
|
||||||
t3.host = "example.com";
|
|
||||||
const auto j2 = nlohmann::json(t3);
|
|
||||||
CHECK(j2.dump() == "{\"host\":\"example.com\",\"port\":80}");
|
|
||||||
const auto t4 = j2.get<Example_4740>();
|
|
||||||
CHECK(t4.host.has_value());
|
|
||||||
CHECK(t4.port.has_value());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if !defined(_MSVC_LANG)
|
|
||||||
// MSVC returns garbage on invalid enum values, so this test is excluded
|
|
||||||
// there.
|
|
||||||
SECTION("issue #4762 - json exception 302 with unhelpful explanation : type must be number, but is number")
|
|
||||||
{
|
|
||||||
// In #4762, the main issue was that a json object with an invalid type
|
|
||||||
// returned "number" as type_name(), because this was the default case.
|
|
||||||
// This test makes sure we now return "invalid" instead.
|
|
||||||
json j;
|
|
||||||
j.m_data.m_type = static_cast<json::value_t>(100); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
|
|
||||||
CHECK(j.type_name() == "invalid");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef JSON_HAS_CPP_17
|
|
||||||
SECTION("issue #4804: from_cbor incompatible with std::vector<std::byte> as binary_t")
|
|
||||||
{
|
|
||||||
const std::vector<std::uint8_t> data = {0x80};
|
|
||||||
const auto decoded = json_4804::from_cbor(data);
|
|
||||||
CHECK((decoded == json_4804::array()));
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("discussion #4209 - custom BinaryType direct assignment and round-tripping")
|
|
||||||
{
|
|
||||||
// Test that assigning a custom BinaryType directly creates a binary value, not an array
|
|
||||||
const std::vector<std::byte> original{std::byte{1}, std::byte{2}, std::byte{3}};
|
|
||||||
const json_4804 j = original;
|
|
||||||
CHECK(j.is_binary());
|
|
||||||
CHECK(!j.is_array());
|
|
||||||
|
|
||||||
// Test round-tripping: extracting the binary value back as the custom container type
|
|
||||||
const auto extracted = j.get<std::vector<std::byte>>();
|
|
||||||
CHECK(extracted == original);
|
|
||||||
|
|
||||||
// Test that the default json alias behavior is unchanged: std::vector<uint8_t> -> array
|
|
||||||
const json default_json = std::vector<std::uint8_t> {1, 2, 3};
|
|
||||||
CHECK(default_json.is_array());
|
|
||||||
CHECK(!default_json.is_binary());
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("discussion #4209 - custom BinaryType extraction from parsed array")
|
|
||||||
{
|
|
||||||
// Test that extracting a custom BinaryType from a parsed JSON array still works
|
|
||||||
// (not just from a binary-typed node)
|
|
||||||
const auto j = json_4804::parse("[1,2,3]");
|
|
||||||
CHECK(j.is_array());
|
|
||||||
CHECK(!j.is_binary());
|
|
||||||
|
|
||||||
// Extracting as custom BinaryType should work from arrays
|
|
||||||
const auto extracted = j.get<std::vector<std::byte>>();
|
|
||||||
CHECK(extracted.size() == 3);
|
|
||||||
CHECK(extracted[0] == std::byte{1});
|
|
||||||
CHECK(extracted[1] == std::byte{2});
|
|
||||||
CHECK(extracted[2] == std::byte{3});
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTION("issue #5046 - implicit conversion of return json to std::optional no longer implicit")
|
|
||||||
{
|
|
||||||
const json jval{};
|
|
||||||
auto GetValue = [](const json & valRoot) -> std::optional<json>
|
|
||||||
{
|
|
||||||
if (valRoot.contains("default"))
|
|
||||||
{
|
|
||||||
return valRoot.at("default");
|
|
||||||
}
|
|
||||||
return std::nullopt;
|
|
||||||
};
|
|
||||||
auto result = GetValue(jval);
|
|
||||||
CHECK(!result.has_value());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if JSON_HAS_RANGES == 1
|
|
||||||
SECTION("issue #4440 - assert when using std::views::filter and GCC 10")
|
|
||||||
{
|
|
||||||
auto noOpFilter = std::views::filter([](auto&&) noexcept
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
json j = {1, 2, 3};
|
|
||||||
auto filtered = j | noOpFilter;
|
|
||||||
CHECK(*filtered.begin() == 1);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if JSON_HAS_RANGES && !defined(__MINGW32__)
|
|
||||||
SECTION("issue #4916 - constructing array from C++20 ranges view does not work")
|
|
||||||
{
|
|
||||||
std::vector<int> nums{1, 2, 37, 42, 21};
|
|
||||||
auto filteredNums = nums | std::views::filter([](int i)
|
|
||||||
{
|
|
||||||
return i > 10;
|
|
||||||
});
|
|
||||||
json const j(filteredNums);
|
|
||||||
CHECK(j.type() == json::value_t::array);
|
|
||||||
CHECK(j == json({37, 42, 21}));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// owning_view is not available in libstdc++ < 12
|
|
||||||
#if JSON_HAS_RANGES && !defined(__MINGW32__) && !(defined(__GLIBCXX__) && _GLIBCXX_RELEASE < 12)
|
|
||||||
SECTION("issue #4916 - constructing array from prvalue C++20 ranges view (owning_view)")
|
|
||||||
{
|
|
||||||
json const j(std::vector<int> {1, 2, 37, 42, 21} | std::views::filter([](int i)
|
|
||||||
{
|
|
||||||
return i > 10;
|
|
||||||
}));
|
|
||||||
CHECK(j.type() == json::value_t::array);
|
|
||||||
CHECK(j == json({37, 42, 21}));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if JSON_HAS_RANGES && !defined(__MINGW32__)
|
|
||||||
SECTION("issue #4916 - constructing array from C++20 transform view (prvalue elements)")
|
|
||||||
{
|
|
||||||
std::vector<int> nums{1, 2, 3};
|
|
||||||
auto t = nums | std::views::transform([](int i) noexcept
|
|
||||||
{
|
|
||||||
return i * 2;
|
|
||||||
});
|
|
||||||
json const j(t);
|
|
||||||
CHECK(j.type() == json::value_t::array);
|
|
||||||
CHECK(j == json({2, 4, 6}));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE_TEMPLATE("issue #4798 - nlohmann::json::to_msgpack() encode float NaN as double", T, double, float) // NOLINT(readability-math-missing-parentheses, bugprone-throwing-static-initialization)
|
|
||||||
{
|
|
||||||
// With issue #4798, we encode NaN, infinity, and -infinity as float instead
|
|
||||||
// of double to allow for smaller encodings.
|
|
||||||
const json jx = std::numeric_limits<T>::quiet_NaN();
|
|
||||||
const json jy = std::numeric_limits<T>::infinity();
|
|
||||||
const json jz = -std::numeric_limits<T>::infinity();
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////
|
|
||||||
// MessagePack
|
|
||||||
/////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
// expected MessagePack values
|
|
||||||
const std::vector<std::uint8_t> msgpack_x = {{0xCA, 0x7F, 0xC0, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> msgpack_y = {{0xCA, 0x7F, 0x80, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> msgpack_z = {{0xCA, 0xFF, 0x80, 0x00, 0x00}};
|
|
||||||
|
|
||||||
CHECK(json::to_msgpack(jx) == msgpack_x);
|
|
||||||
CHECK(json::to_msgpack(jy) == msgpack_y);
|
|
||||||
CHECK(json::to_msgpack(jz) == msgpack_z);
|
|
||||||
|
|
||||||
CHECK(std::isnan(json::from_msgpack(msgpack_x).get<T>()));
|
|
||||||
CHECK(json::from_msgpack(msgpack_y).get<T>() == std::numeric_limits<T>::infinity());
|
|
||||||
CHECK(json::from_msgpack(msgpack_z).get<T>() == -std::numeric_limits<T>::infinity());
|
|
||||||
|
|
||||||
// Make sure the other MessagePakc encodings for NaN, infinity, and
|
|
||||||
// -infinity are still supported.
|
|
||||||
const std::vector<std::uint8_t> msgpack_x_2 = {{0xCB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> msgpack_y_2 = {{0xCB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> msgpack_z_2 = {{0xCB, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
|
||||||
CHECK(std::isnan(json::from_msgpack(msgpack_x_2).get<T>()));
|
|
||||||
CHECK(json::from_msgpack(msgpack_y_2).get<T>() == std::numeric_limits<T>::infinity());
|
|
||||||
CHECK(json::from_msgpack(msgpack_z_2).get<T>() == -std::numeric_limits<T>::infinity());
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////
|
|
||||||
// CBOR
|
|
||||||
/////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
// expected CBOR values
|
|
||||||
const std::vector<std::uint8_t> cbor_x = {{0xF9, 0x7E, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> cbor_y = {{0xF9, 0x7C, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> cbor_z = {{0xF9, 0xfC, 0x00}};
|
|
||||||
|
|
||||||
CHECK(json::to_cbor(jx) == cbor_x);
|
|
||||||
CHECK(json::to_cbor(jy) == cbor_y);
|
|
||||||
CHECK(json::to_cbor(jz) == cbor_z);
|
|
||||||
|
|
||||||
CHECK(std::isnan(json::from_cbor(cbor_x).get<T>()));
|
|
||||||
CHECK(json::from_cbor(cbor_y).get<T>() == std::numeric_limits<T>::infinity());
|
|
||||||
CHECK(json::from_cbor(cbor_z).get<T>() == -std::numeric_limits<T>::infinity());
|
|
||||||
|
|
||||||
// Make sure the other CBOR encodings for NaN, infinity, and -infinity are
|
|
||||||
// still supported.
|
|
||||||
const std::vector<std::uint8_t> cbor_x_2 = {{0xFA, 0x7F, 0xC0, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> cbor_y_2 = {{0xFA, 0x7F, 0x80, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> cbor_z_2 = {{0xFA, 0xFF, 0x80, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> cbor_x_3 = {{0xFB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> cbor_y_3 = {{0xFB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
|
||||||
const std::vector<std::uint8_t> cbor_z_3 = {{0xFB, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
|
||||||
CHECK(std::isnan(json::from_cbor(cbor_x_2).get<T>()));
|
|
||||||
CHECK(json::from_cbor(cbor_y_2).get<T>() == std::numeric_limits<T>::infinity());
|
|
||||||
CHECK(json::from_cbor(cbor_z_2).get<T>() == -std::numeric_limits<T>::infinity());
|
|
||||||
CHECK(std::isnan(json::from_cbor(cbor_x_3).get<T>()));
|
|
||||||
CHECK(json::from_cbor(cbor_y_3).get<T>() == std::numeric_limits<T>::infinity());
|
|
||||||
CHECK(json::from_cbor(cbor_z_3).get<T>() == -std::numeric_limits<T>::infinity());
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("regression test #5074 - portable workaround for single-element brace init")
|
|
||||||
{
|
|
||||||
json const j_obj = {{"key", "value"}};
|
|
||||||
|
|
||||||
json const j = json::array({j_obj});
|
|
||||||
CHECK(j.is_array());
|
|
||||||
CHECK(j.size() == 1);
|
|
||||||
CHECK(j[0] == j_obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(JSON_BRACE_INIT_COPY_SEMANTICS) && (JSON_BRACE_INIT_COPY_SEMANTICS == 1)
|
|
||||||
TEST_CASE("regression test #5074 - single-element brace init with JSON_BRACE_INIT_COPY_SEMANTICS")
|
|
||||||
{
|
|
||||||
// with JSON_BRACE_INIT_COPY_SEMANTICS: single-element brace init copies/moves
|
|
||||||
json const j_obj = {{"key", "value"}, {"num", 42}};
|
|
||||||
json const j_arr = {1, 2, 3};
|
|
||||||
|
|
||||||
// object: brace init copies instead of wrapping
|
|
||||||
json const j1{j_obj};
|
|
||||||
CHECK(j1.is_object());
|
|
||||||
CHECK(j1 == j_obj);
|
|
||||||
|
|
||||||
// array: brace init copies instead of wrapping
|
|
||||||
json const j2{j_arr};
|
|
||||||
CHECK(j2.is_array());
|
|
||||||
CHECK(j2.size() == 3);
|
|
||||||
CHECK(j2 == j_arr);
|
|
||||||
|
|
||||||
// primitives still work as initializer lists
|
|
||||||
json const j3{true};
|
|
||||||
CHECK(j3.is_boolean());
|
|
||||||
|
|
||||||
json const j4{42};
|
|
||||||
CHECK(j4.is_number_integer());
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
struct Example_5122
|
|
||||||
{
|
|
||||||
float b = 2;
|
|
||||||
nlohmann::ordered_map<std::string, std::string> c{}; // NOLINT(readability-redundant-member-init): needed for GCC -Weffc++
|
|
||||||
int a = 1;
|
|
||||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Example_5122, b, c, a)
|
|
||||||
};
|
|
||||||
|
|
||||||
TEST_CASE("regression test #5122 - from_json into types holding nlohmann::ordered_map")
|
|
||||||
{
|
|
||||||
Example_5122 src;
|
|
||||||
src.c.emplace("first", "1");
|
|
||||||
src.c.emplace("second", "2");
|
|
||||||
|
|
||||||
ordered_json const j = src;
|
|
||||||
Example_5122 const dst = j.get<Example_5122>();
|
|
||||||
|
|
||||||
CHECK(dst.b == src.b);
|
|
||||||
CHECK(dst.a == src.a);
|
|
||||||
REQUIRE(dst.c.size() == src.c.size());
|
|
||||||
auto src_it = src.c.begin();
|
|
||||||
auto dst_it = dst.c.begin();
|
|
||||||
for (; src_it != src.c.end(); ++src_it, ++dst_it)
|
|
||||||
{
|
|
||||||
CHECK(dst_it->first == src_it->first);
|
|
||||||
CHECK(dst_it->second == src_it->second);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// -Wself-assign-overloaded was introduced in Clang 7. Gate the pragma on
|
|
||||||
// __has_warning so older Clang versions do not error with "unknown warning
|
|
||||||
// group". The __has_warning check has to stay inside the __clang__ branch
|
|
||||||
// because GCC does not provide it and would tokenize-error on the argument.
|
|
||||||
#if defined(__clang__) && defined(__has_warning)
|
|
||||||
#if __has_warning("-Wself-assign-overloaded")
|
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING("-Wself-assign-overloaded")
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
TEST_CASE("regression test #5122 - nlohmann::ordered_map copy-assignment is self-assignment safe")
|
|
||||||
{
|
|
||||||
nlohmann::ordered_map<std::string, std::string> m;
|
|
||||||
m.emplace("first", "1");
|
|
||||||
m.emplace("second", "2");
|
|
||||||
|
|
||||||
// Insertion order is preserved by ordered_map, so we can check it directly.
|
|
||||||
m = m;
|
|
||||||
|
|
||||||
REQUIRE(m.size() == 2);
|
|
||||||
auto it = m.begin();
|
|
||||||
CHECK(it->first == "first");
|
|
||||||
CHECK(it->second == "1");
|
|
||||||
++it;
|
|
||||||
CHECK(it->first == "second");
|
|
||||||
CHECK(it->second == "2");
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(__clang__) && defined(__has_warning)
|
|
||||||
#if __has_warning("-Wself-assign-overloaded")
|
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING_POP
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
|
|
||||||
TEST_CASE("regression test #5122 - nlohmann::ordered_map move-assignment transfers contents")
|
|
||||||
{
|
|
||||||
nlohmann::ordered_map<std::string, std::string> src;
|
|
||||||
src.emplace("first", "1");
|
|
||||||
src.emplace("second", "2");
|
|
||||||
|
|
||||||
nlohmann::ordered_map<std::string, std::string> dst;
|
|
||||||
dst.emplace("stale", "x");
|
|
||||||
dst = std::move(src);
|
|
||||||
|
|
||||||
REQUIRE(dst.size() == 2);
|
|
||||||
auto it = dst.begin();
|
|
||||||
CHECK(it->first == "first");
|
|
||||||
CHECK(it->second == "1");
|
|
||||||
++it;
|
|
||||||
CHECK(it->first == "second");
|
|
||||||
CHECK(it->second == "2");
|
|
||||||
|
|
||||||
// Re-assigning into the moved-from object must leave it in a usable state.
|
|
||||||
src = nlohmann::ordered_map<std::string, std::string> {};
|
|
||||||
src.emplace("after-move", "3");
|
|
||||||
REQUIRE(src.size() == 1);
|
|
||||||
CHECK(src.begin()->first == "after-move");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Stand-in for a third-party library (e.g., Eigen as of 3.4, which added
|
|
||||||
// STL-compatible begin()/end() to its vector types), living in its own
|
|
||||||
// namespace with its own to_json overload for its vector type.
|
|
||||||
namespace issue_4320_eigen
|
|
||||||
{
|
|
||||||
// "array-compatible" from the library's point of view (it has begin()/end()),
|
|
||||||
// but for which this (fake) third-party namespace provides its own to_json.
|
|
||||||
struct vector3
|
|
||||||
{
|
|
||||||
double v[3]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-use-default-member-init,modernize-use-default-member-init)
|
|
||||||
vector3(double x, double y, double z) : v{x, y, z} {} // NOLINT(hicpp-member-init,cppcoreguidelines-pro-type-member-init)
|
|
||||||
double x() const
|
|
||||||
{
|
|
||||||
return v[0];
|
|
||||||
}
|
|
||||||
double y() const
|
|
||||||
{
|
|
||||||
return v[1];
|
|
||||||
}
|
|
||||||
double z() const
|
|
||||||
{
|
|
||||||
return v[2];
|
|
||||||
}
|
|
||||||
double* begin()
|
|
||||||
{
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
double* end()
|
|
||||||
{
|
|
||||||
return v + 3;
|
|
||||||
}
|
|
||||||
const double* begin() const
|
|
||||||
{
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
const double* end() const
|
|
||||||
{
|
|
||||||
return v + 3;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void to_json(json& j, const vector3& v) // NOLINT(misc-use-internal-linkage)
|
|
||||||
{
|
|
||||||
j = {{"x", v.x()}, {"y", v.y()}, {"z", v.z()}};
|
|
||||||
}
|
|
||||||
} // namespace issue_4320_eigen
|
|
||||||
|
|
||||||
// The user's own namespace, using the (fake) Eigen type as an implementation
|
|
||||||
// detail behind a payload type that has nothing to do with vectors/arrays.
|
|
||||||
namespace issue_4320
|
|
||||||
{
|
|
||||||
// Publicly derives from issue_4320_eigen::vector3 but does *not* define its
|
|
||||||
// own to_json - it is only ever used as a temporary to reach the base
|
|
||||||
// class's to_json via ADL.
|
|
||||||
struct vector3_wrapper : issue_4320_eigen::vector3
|
|
||||||
{
|
|
||||||
using issue_4320_eigen::vector3::vector3;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct payload
|
|
||||||
{
|
|
||||||
double x, y, z;
|
|
||||||
};
|
|
||||||
|
|
||||||
inline vector3_wrapper to_eigen(const payload& p) // NOLINT(misc-use-internal-linkage)
|
|
||||||
{
|
|
||||||
return {p.x, p.y, p.z};
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void to_json(json& j, const payload& p) // NOLINT(misc-use-internal-linkage)
|
|
||||||
{
|
|
||||||
// Unqualified call, passing a *derived* vector3_wrapper: relies on ADL
|
|
||||||
// finding issue_4320_eigen::to_json(json&, const vector3&) through the
|
|
||||||
// vector3 base class, via a derived-to-base conversion. Must NOT resolve
|
|
||||||
// to the library's own generic array-compatible to_json (an exact-match
|
|
||||||
// template for vector3_wrapper, since it also has begin()/end()), which
|
|
||||||
// would serialize this as [x, y, z] instead of {"x":x, "y":y, "z":z}.
|
|
||||||
to_json(j, to_eigen(p));
|
|
||||||
}
|
|
||||||
} // namespace issue_4320
|
|
||||||
|
|
||||||
TEST_CASE("issue #4320 - custom base class must not leak nlohmann::detail into ADL")
|
|
||||||
{
|
|
||||||
// Before the fix, basic_json unconditionally derived from a type living in
|
|
||||||
// nlohmann::detail (json_default_base), which made nlohmann::detail an
|
|
||||||
// associated namespace of every basic_json for ADL purposes. That leaked
|
|
||||||
// the library's internal generic-array to_json overload into unqualified
|
|
||||||
// to_json() calls made from user code, silently bypassing user-defined
|
|
||||||
// to_json overloads reached via a derived-to-base conversion.
|
|
||||||
const issue_4320::payload p{1.0, 2.0, 3.0};
|
|
||||||
|
|
||||||
json j;
|
|
||||||
to_json(j, p);
|
|
||||||
CHECK(j == json({{"x", 1.0}, {"y", 2.0}, {"z", 3.0}}));
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("issue #5338 - truncated CBOR tagged binary subtype is rejected")
|
|
||||||
{
|
|
||||||
const std::vector<std::vector<std::uint8_t>> truncated_tags =
|
|
||||||
{
|
|
||||||
{0xD8},
|
|
||||||
{0xD9, 0x00},
|
|
||||||
{0xDA, 0x00, 0x00, 0x00},
|
|
||||||
{0xDB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const auto& data : truncated_tags)
|
|
||||||
{
|
|
||||||
CAPTURE(data);
|
|
||||||
for (const auto tag_handler :
|
|
||||||
{
|
|
||||||
json::cbor_tag_handler_t::ignore, json::cbor_tag_handler_t::store
|
|
||||||
})
|
|
||||||
{
|
|
||||||
CAPTURE(tag_handler);
|
|
||||||
const auto result = json::from_cbor(data, true, false, tag_handler);
|
|
||||||
CHECK(result.is_discarded());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE("issue #5402 - update(merge_objects=true) overwrites a primitive with an object")
|
|
||||||
{
|
|
||||||
json t = {{"k", 1}};
|
|
||||||
t.update(json{{"k", {{"x", 2}}}}, true);
|
|
||||||
CHECK(t == json({{"k", {{"x", 2}}}}));
|
|
||||||
|
|
||||||
json mixed = {{"keep", {{"a", 1}}}, {"replace", 1}};
|
|
||||||
mixed.update(json{{"keep", {{"b", 2}}}, {"replace", {{"x", 2}}}}, true);
|
|
||||||
CHECK(mixed == json({{"keep", {{"a", 1}, {"b", 2}}}, {"replace", {{"x", 2}}}}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING_POP
|
DOCTEST_CLANG_SUPPRESS_WARNING_POP
|
||||||
|
|||||||
@@ -0,0 +1,899 @@
|
|||||||
|
// __ _____ _____ _____
|
||||||
|
// __| | __| | | | JSON for Modern C++ (supporting code)
|
||||||
|
// | | |__ | | | | | | version 3.12.0
|
||||||
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
||||||
|
//
|
||||||
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
// cmake/test.cmake selects the C++ standard versions with which to build a
|
||||||
|
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
|
||||||
|
// When using macros that are only defined for particular versions of the standard
|
||||||
|
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
|
||||||
|
// version macro in a comment close by, like this:
|
||||||
|
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
|
||||||
|
|
||||||
|
#include "doctest_compatibility.h"
|
||||||
|
|
||||||
|
// for some reason including this after the json header leads to linker errors with VS 2017...
|
||||||
|
#include <locale>
|
||||||
|
|
||||||
|
#define JSON_TESTS_PRIVATE
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
using json = nlohmann::json;
|
||||||
|
using ordered_json = nlohmann::ordered_json;
|
||||||
|
#ifdef JSON_TEST_NO_GLOBAL_UDLS
|
||||||
|
using namespace nlohmann::literals; // NOLINT(google-build-using-namespace)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <cstdio>
|
||||||
|
#include <list>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_17
|
||||||
|
#include <any>
|
||||||
|
#include <variant>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_17
|
||||||
|
#if __has_include(<optional>)
|
||||||
|
#include <optional>
|
||||||
|
#elif __has_include(<experimental/optional>)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #4804
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
using json_4804 = nlohmann::basic_json<std::map, // ObjectType
|
||||||
|
std::vector, // ArrayType
|
||||||
|
std::string, // StringType
|
||||||
|
bool, // BooleanType
|
||||||
|
std::int64_t, // NumberIntegerType
|
||||||
|
std::uint64_t, // NumberUnsignedType
|
||||||
|
double, // NumberFloatType
|
||||||
|
std::allocator, // AllocatorType
|
||||||
|
nlohmann::adl_serializer, // JSONSerializer
|
||||||
|
std::vector<std::byte>, // BinaryType
|
||||||
|
void // CustomBaseClass
|
||||||
|
>;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_20
|
||||||
|
#if __has_include(<span>)
|
||||||
|
#include <span>
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #4825 - explicitly instantiating basic_json must compile; this
|
||||||
|
// forces instantiation of binary_writer::write_bjdata_ndarray, whose
|
||||||
|
// static_cast<string_t> was ambiguous under explicit instantiation on
|
||||||
|
// C++17. Merely compiling this translation unit is the regression test.
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
template class nlohmann::basic_json<>;
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #4440
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
#if JSON_HAS_RANGES == 1
|
||||||
|
#include <ranges>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// NLOHMANN_JSON_SERIALIZE_ENUM uses a static std::pair
|
||||||
|
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
||||||
|
DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors")
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #3077
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
class FooAlloc
|
||||||
|
{};
|
||||||
|
|
||||||
|
class Foo
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit Foo(const FooAlloc& /* unused */ = FooAlloc()) {}
|
||||||
|
|
||||||
|
bool value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
class FooBar
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Foo foo{}; // NOLINT(readability-redundant-member-init)
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void from_json(const nlohmann::json& j, FooBar& fb) // NOLINT(misc-use-internal-linkage)
|
||||||
|
{
|
||||||
|
j.at("value").get_to(fb.foo.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #3171
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct for_3171_base // NOLINT(cppcoreguidelines-special-member-functions)
|
||||||
|
{
|
||||||
|
for_3171_base(const std::string& /*unused*/ = {}) {}
|
||||||
|
virtual ~for_3171_base();
|
||||||
|
|
||||||
|
for_3171_base(const for_3171_base& other) // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)
|
||||||
|
: str(other.str)
|
||||||
|
{}
|
||||||
|
|
||||||
|
for_3171_base& operator=(const for_3171_base& other)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
str = other.str;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
for_3171_base(for_3171_base&& other) noexcept
|
||||||
|
: str(std::move(other.str))
|
||||||
|
{}
|
||||||
|
|
||||||
|
for_3171_base& operator=(for_3171_base&& other) noexcept
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
str = std::move(other.str);
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void _from_json(const json& j)
|
||||||
|
{
|
||||||
|
j.at("str").get_to(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string str{}; // NOLINT(readability-redundant-member-init)
|
||||||
|
};
|
||||||
|
|
||||||
|
for_3171_base::~for_3171_base() = default;
|
||||||
|
|
||||||
|
struct for_3171_derived : public for_3171_base
|
||||||
|
{
|
||||||
|
for_3171_derived() = default;
|
||||||
|
~for_3171_derived() override;
|
||||||
|
explicit for_3171_derived(const std::string& /*unused*/) { }
|
||||||
|
|
||||||
|
for_3171_derived(const for_3171_derived& other) // NOLINT(hicpp-use-equals-default,modernize-use-equals-default)
|
||||||
|
: for_3171_base(other)
|
||||||
|
{}
|
||||||
|
|
||||||
|
for_3171_derived& operator=(const for_3171_derived& other)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
for_3171_base::operator=(other); // Call base class assignment operator
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
for_3171_derived(for_3171_derived&& other) noexcept
|
||||||
|
: for_3171_base(std::move(other))
|
||||||
|
{}
|
||||||
|
|
||||||
|
for_3171_derived& operator=(for_3171_derived&& other) noexcept
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
for_3171_base::operator=(std::move(other)); // Call base class move assignment operator
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
for_3171_derived::~for_3171_derived() = default;
|
||||||
|
|
||||||
|
inline void from_json(const json& j, for_3171_base& tb) // NOLINT(misc-use-internal-linkage)
|
||||||
|
{
|
||||||
|
tb._from_json(j);
|
||||||
|
}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #3312
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_20
|
||||||
|
struct for_3312
|
||||||
|
{
|
||||||
|
std::string name;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void from_json(const json& j, for_3312& obj) // NOLINT(misc-use-internal-linkage)
|
||||||
|
{
|
||||||
|
j.at("name").get_to(obj.name);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #3204
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct for_3204_foo
|
||||||
|
{
|
||||||
|
for_3204_foo() = default;
|
||||||
|
explicit for_3204_foo(std::string /*unused*/) {} // NOLINT(performance-unnecessary-value-param)
|
||||||
|
};
|
||||||
|
|
||||||
|
struct for_3204_bar
|
||||||
|
{
|
||||||
|
enum constructed_from_t // NOLINT(cppcoreguidelines-use-enum-class)
|
||||||
|
{
|
||||||
|
constructed_from_none = 0,
|
||||||
|
constructed_from_foo = 1,
|
||||||
|
constructed_from_json = 2
|
||||||
|
};
|
||||||
|
|
||||||
|
explicit for_3204_bar(std::function<void(for_3204_foo)> /*unused*/) noexcept // NOLINT(performance-unnecessary-value-param)
|
||||||
|
: constructed_from(constructed_from_foo) {}
|
||||||
|
explicit for_3204_bar(std::function<void(json)> /*unused*/) noexcept // NOLINT(performance-unnecessary-value-param)
|
||||||
|
: constructed_from(constructed_from_json) {}
|
||||||
|
|
||||||
|
constructed_from_t constructed_from = constructed_from_none;
|
||||||
|
};
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #3333
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct for_3333 final
|
||||||
|
{
|
||||||
|
for_3333(int x_ = 0, int y_ = 0) : x(x_), y(y_) {}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
for_3333(const T& /*unused*/)
|
||||||
|
{
|
||||||
|
CHECK(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
inline for_3333::for_3333(const json& j)
|
||||||
|
: for_3333(j.value("x", 0), j.value("y", 0))
|
||||||
|
{}
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #3810
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
struct Example_3810
|
||||||
|
{
|
||||||
|
int bla{};
|
||||||
|
|
||||||
|
Example_3810() = default;
|
||||||
|
};
|
||||||
|
|
||||||
|
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Example_3810, bla) // NOLINT(misc-use-internal-linkage)
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
// for #4740
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_17
|
||||||
|
struct Example_4740
|
||||||
|
{
|
||||||
|
std::optional<std::string> host = std::nullopt;
|
||||||
|
std::optional<int> port = std::nullopt;
|
||||||
|
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Example_4740, host, port)
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
TEST_CASE("regression tests 3")
|
||||||
|
{
|
||||||
|
#if JSON_HAS_FILESYSTEM || JSON_HAS_EXPERIMENTAL_FILESYSTEM
|
||||||
|
// JSON_HAS_CPP_17 (do not remove; see note at top of file)
|
||||||
|
SECTION("issue #3070 - Version 3.10.3 breaks backward-compatibility with 3.10.2 ")
|
||||||
|
{
|
||||||
|
nlohmann::detail::std_fs::path text_path("/tmp/text.txt");
|
||||||
|
const json j(text_path);
|
||||||
|
|
||||||
|
const auto j_path = j.get<nlohmann::detail::std_fs::path>();
|
||||||
|
CHECK(j_path == text_path);
|
||||||
|
|
||||||
|
#if DOCTEST_CLANG || DOCTEST_GCC >= DOCTEST_COMPILER(8, 4, 0)
|
||||||
|
// only known to work on Clang and GCC >=8.4
|
||||||
|
CHECK_THROWS_WITH_AS(nlohmann::detail::std_fs::path(json(1)), "[json.exception.type_error.302] type must be string, but is number", json::type_error);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SECTION("issue #3077 - explicit constructor with default does not compile")
|
||||||
|
{
|
||||||
|
json j;
|
||||||
|
j[0]["value"] = true;
|
||||||
|
std::vector<FooBar> foo;
|
||||||
|
j.get_to(foo);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("issue #3108 - ordered_json doesn't support range based erase")
|
||||||
|
{
|
||||||
|
ordered_json j = {1, 2, 2, 4};
|
||||||
|
|
||||||
|
auto last = std::unique(j.begin(), j.end());
|
||||||
|
j.erase(last, j.end());
|
||||||
|
|
||||||
|
CHECK(j.dump() == "[1,2,4]");
|
||||||
|
|
||||||
|
j.erase(std::remove_if(j.begin(), j.end(), [](const ordered_json & val)
|
||||||
|
{
|
||||||
|
return val == 2;
|
||||||
|
}), j.end());
|
||||||
|
|
||||||
|
CHECK(j.dump() == "[1,4]");
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("issue #3343 - json and ordered_json are not interchangeable")
|
||||||
|
{
|
||||||
|
json::object_t jobj({ { "product", "one" } });
|
||||||
|
ordered_json::object_t ojobj({{"product", "one"}});
|
||||||
|
|
||||||
|
auto jit = jobj.begin();
|
||||||
|
auto ojit = ojobj.begin();
|
||||||
|
|
||||||
|
CHECK(jit->first == ojit->first);
|
||||||
|
CHECK(jit->second.get<std::string>() == ojit->second.get<std::string>());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("issue #3171 - if class is_constructible from std::string wrong from_json overload is being selected, compilation failed")
|
||||||
|
{
|
||||||
|
const json j{{ "str", "value"}};
|
||||||
|
|
||||||
|
// failed with: error: no match for ‘operator=’ (operand types are ‘for_3171_derived’ and ‘const nlohmann::basic_json<>::string_t’
|
||||||
|
// {aka ‘const std::__cxx11::basic_string<char>’})
|
||||||
|
// s = *j.template get_ptr<const typename BasicJsonType::string_t*>();
|
||||||
|
auto td = j.get<for_3171_derived>();
|
||||||
|
|
||||||
|
CHECK(td.str == "value");
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_20
|
||||||
|
SECTION("issue #3312 - Parse to custom class from unordered_json breaks on G++11.2.0 with C++20")
|
||||||
|
{
|
||||||
|
// see test for #3171
|
||||||
|
const ordered_json j = {{"name", "class"}};
|
||||||
|
for_3312 obj{};
|
||||||
|
|
||||||
|
j.get_to(obj);
|
||||||
|
|
||||||
|
CHECK(obj.name == "class");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(JSON_HAS_CPP_17) && JSON_USE_IMPLICIT_CONVERSIONS
|
||||||
|
SECTION("issue #3428 - Error occurred when converting nlohmann::json to std::any")
|
||||||
|
{
|
||||||
|
const json j;
|
||||||
|
const std::any a1 = j;
|
||||||
|
std::any&& a2 = j;
|
||||||
|
|
||||||
|
CHECK(a1.type() == typeid(j));
|
||||||
|
CHECK(a2.type() == typeid(j));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SECTION("issue #3204 - ambiguous regression")
|
||||||
|
{
|
||||||
|
const for_3204_bar bar_from_foo([](for_3204_foo) noexcept {}); // NOLINT(performance-unnecessary-value-param)
|
||||||
|
const for_3204_bar bar_from_json([](json) noexcept {}); // NOLINT(performance-unnecessary-value-param)
|
||||||
|
|
||||||
|
CHECK(bar_from_foo.constructed_from == for_3204_bar::constructed_from_foo);
|
||||||
|
CHECK(bar_from_json.constructed_from == for_3204_bar::constructed_from_json);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("issue #3333 - Ambiguous conversion from nlohmann::basic_json<> to custom class")
|
||||||
|
{
|
||||||
|
const json j
|
||||||
|
{
|
||||||
|
{"x", 1},
|
||||||
|
{"y", 2}
|
||||||
|
};
|
||||||
|
const for_3333 p = j;
|
||||||
|
|
||||||
|
CHECK(p.x == 1);
|
||||||
|
CHECK(p.y == 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("issue #3810 - ordered_json doesn't support construction from C array of custom type")
|
||||||
|
{
|
||||||
|
Example_3810 states[45]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
|
||||||
|
|
||||||
|
// fix "not used" warning
|
||||||
|
states[0].bla = 1;
|
||||||
|
|
||||||
|
const auto* const expected = R"([{"bla":1},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0},{"bla":0}])";
|
||||||
|
|
||||||
|
// This works:
|
||||||
|
nlohmann::json j;
|
||||||
|
j["test"] = states;
|
||||||
|
CHECK(j["test"].dump() == expected);
|
||||||
|
|
||||||
|
// This doesn't compile:
|
||||||
|
nlohmann::ordered_json oj;
|
||||||
|
oj["test"] = states;
|
||||||
|
CHECK(oj["test"].dump() == expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_17
|
||||||
|
SECTION("issue #4740 - build issue with std::optional")
|
||||||
|
{
|
||||||
|
const auto t1 = Example_4740();
|
||||||
|
const auto j1 = nlohmann::json(t1);
|
||||||
|
CHECK(j1.dump() == "{\"host\":null,\"port\":null}");
|
||||||
|
const auto t2 = j1.get<Example_4740>();
|
||||||
|
CHECK(!t2.host.has_value());
|
||||||
|
CHECK(!t2.port.has_value());
|
||||||
|
|
||||||
|
// improve coverage
|
||||||
|
auto t3 = Example_4740();
|
||||||
|
t3.port = 80;
|
||||||
|
t3.host = "example.com";
|
||||||
|
const auto j2 = nlohmann::json(t3);
|
||||||
|
CHECK(j2.dump() == "{\"host\":\"example.com\",\"port\":80}");
|
||||||
|
const auto t4 = j2.get<Example_4740>();
|
||||||
|
CHECK(t4.host.has_value());
|
||||||
|
CHECK(t4.port.has_value());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if !defined(_MSVC_LANG)
|
||||||
|
// MSVC returns garbage on invalid enum values, so this test is excluded
|
||||||
|
// there.
|
||||||
|
SECTION("issue #4762 - json exception 302 with unhelpful explanation : type must be number, but is number")
|
||||||
|
{
|
||||||
|
// In #4762, the main issue was that a json object with an invalid type
|
||||||
|
// returned "number" as type_name(), because this was the default case.
|
||||||
|
// This test makes sure we now return "invalid" instead.
|
||||||
|
json j;
|
||||||
|
j.m_data.m_type = static_cast<json::value_t>(100); // NOLINT(clang-analyzer-optin.core.EnumCastOutOfRange)
|
||||||
|
CHECK(j.type_name() == "invalid");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef JSON_HAS_CPP_17
|
||||||
|
SECTION("issue #4804: from_cbor incompatible with std::vector<std::byte> as binary_t")
|
||||||
|
{
|
||||||
|
const std::vector<std::uint8_t> data = {0x80};
|
||||||
|
const auto decoded = json_4804::from_cbor(data);
|
||||||
|
CHECK((decoded == json_4804::array()));
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("discussion #4209 - custom BinaryType direct assignment and round-tripping")
|
||||||
|
{
|
||||||
|
// Test that assigning a custom BinaryType directly creates a binary value, not an array
|
||||||
|
const std::vector<std::byte> original{std::byte{1}, std::byte{2}, std::byte{3}};
|
||||||
|
const json_4804 j = original;
|
||||||
|
CHECK(j.is_binary());
|
||||||
|
CHECK(!j.is_array());
|
||||||
|
|
||||||
|
// Test round-tripping: extracting the binary value back as the custom container type
|
||||||
|
const auto extracted = j.get<std::vector<std::byte>>();
|
||||||
|
CHECK(extracted == original);
|
||||||
|
|
||||||
|
// Test that the default json alias behavior is unchanged: std::vector<uint8_t> -> array
|
||||||
|
const json default_json = std::vector<std::uint8_t> {1, 2, 3};
|
||||||
|
CHECK(default_json.is_array());
|
||||||
|
CHECK(!default_json.is_binary());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("discussion #4209 - custom BinaryType extraction from parsed array")
|
||||||
|
{
|
||||||
|
// Test that extracting a custom BinaryType from a parsed JSON array still works
|
||||||
|
// (not just from a binary-typed node)
|
||||||
|
const auto j = json_4804::parse("[1,2,3]");
|
||||||
|
CHECK(j.is_array());
|
||||||
|
CHECK(!j.is_binary());
|
||||||
|
|
||||||
|
// Extracting as custom BinaryType should work from arrays
|
||||||
|
const auto extracted = j.get<std::vector<std::byte>>();
|
||||||
|
CHECK(extracted.size() == 3);
|
||||||
|
CHECK(extracted[0] == std::byte{1});
|
||||||
|
CHECK(extracted[1] == std::byte{2});
|
||||||
|
CHECK(extracted[2] == std::byte{3});
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("issue #5046 - implicit conversion of return json to std::optional no longer implicit")
|
||||||
|
{
|
||||||
|
const json jval{};
|
||||||
|
auto GetValue = [](const json & valRoot) -> std::optional<json>
|
||||||
|
{
|
||||||
|
if (valRoot.contains("default"))
|
||||||
|
{
|
||||||
|
return valRoot.at("default");
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
};
|
||||||
|
auto result = GetValue(jval);
|
||||||
|
CHECK(!result.has_value());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if JSON_HAS_RANGES == 1
|
||||||
|
SECTION("issue #4440 - assert when using std::views::filter and GCC 10")
|
||||||
|
{
|
||||||
|
auto noOpFilter = std::views::filter([](auto&&) noexcept
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
json j = {1, 2, 3};
|
||||||
|
auto filtered = j | noOpFilter;
|
||||||
|
CHECK(*filtered.begin() == 1);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if JSON_HAS_RANGES && !defined(__MINGW32__)
|
||||||
|
SECTION("issue #4916 - constructing array from C++20 ranges view does not work")
|
||||||
|
{
|
||||||
|
std::vector<int> nums{1, 2, 37, 42, 21};
|
||||||
|
auto filteredNums = nums | std::views::filter([](int i)
|
||||||
|
{
|
||||||
|
return i > 10;
|
||||||
|
});
|
||||||
|
json const j(filteredNums);
|
||||||
|
CHECK(j.type() == json::value_t::array);
|
||||||
|
CHECK(j == json({37, 42, 21}));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// owning_view is not available in libstdc++ < 12
|
||||||
|
#if JSON_HAS_RANGES && !defined(__MINGW32__) && !(defined(__GLIBCXX__) && _GLIBCXX_RELEASE < 12)
|
||||||
|
SECTION("issue #4916 - constructing array from prvalue C++20 ranges view (owning_view)")
|
||||||
|
{
|
||||||
|
json const j(std::vector<int> {1, 2, 37, 42, 21} | std::views::filter([](int i)
|
||||||
|
{
|
||||||
|
return i > 10;
|
||||||
|
}));
|
||||||
|
CHECK(j.type() == json::value_t::array);
|
||||||
|
CHECK(j == json({37, 42, 21}));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if JSON_HAS_RANGES && !defined(__MINGW32__)
|
||||||
|
SECTION("issue #4916 - constructing array from C++20 transform view (prvalue elements)")
|
||||||
|
{
|
||||||
|
std::vector<int> nums{1, 2, 3};
|
||||||
|
auto t = nums | std::views::transform([](int i) noexcept
|
||||||
|
{
|
||||||
|
return i * 2;
|
||||||
|
});
|
||||||
|
json const j(t);
|
||||||
|
CHECK(j.type() == json::value_t::array);
|
||||||
|
CHECK(j == json({2, 4, 6}));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE_TEMPLATE("issue #4798 - nlohmann::json::to_msgpack() encode float NaN as double", T, double, float) // NOLINT(readability-math-missing-parentheses, bugprone-throwing-static-initialization)
|
||||||
|
{
|
||||||
|
// With issue #4798, we encode NaN, infinity, and -infinity as float instead
|
||||||
|
// of double to allow for smaller encodings.
|
||||||
|
const json jx = std::numeric_limits<T>::quiet_NaN();
|
||||||
|
const json jy = std::numeric_limits<T>::infinity();
|
||||||
|
const json jz = -std::numeric_limits<T>::infinity();
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
// MessagePack
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// expected MessagePack values
|
||||||
|
const std::vector<std::uint8_t> msgpack_x = {{0xCA, 0x7F, 0xC0, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> msgpack_y = {{0xCA, 0x7F, 0x80, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> msgpack_z = {{0xCA, 0xFF, 0x80, 0x00, 0x00}};
|
||||||
|
|
||||||
|
CHECK(json::to_msgpack(jx) == msgpack_x);
|
||||||
|
CHECK(json::to_msgpack(jy) == msgpack_y);
|
||||||
|
CHECK(json::to_msgpack(jz) == msgpack_z);
|
||||||
|
|
||||||
|
CHECK(std::isnan(json::from_msgpack(msgpack_x).get<T>()));
|
||||||
|
CHECK(json::from_msgpack(msgpack_y).get<T>() == std::numeric_limits<T>::infinity());
|
||||||
|
CHECK(json::from_msgpack(msgpack_z).get<T>() == -std::numeric_limits<T>::infinity());
|
||||||
|
|
||||||
|
// Make sure the other MessagePakc encodings for NaN, infinity, and
|
||||||
|
// -infinity are still supported.
|
||||||
|
const std::vector<std::uint8_t> msgpack_x_2 = {{0xCB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> msgpack_y_2 = {{0xCB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> msgpack_z_2 = {{0xCB, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
||||||
|
CHECK(std::isnan(json::from_msgpack(msgpack_x_2).get<T>()));
|
||||||
|
CHECK(json::from_msgpack(msgpack_y_2).get<T>() == std::numeric_limits<T>::infinity());
|
||||||
|
CHECK(json::from_msgpack(msgpack_z_2).get<T>() == -std::numeric_limits<T>::infinity());
|
||||||
|
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
// CBOR
|
||||||
|
/////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// expected CBOR values
|
||||||
|
const std::vector<std::uint8_t> cbor_x = {{0xF9, 0x7E, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> cbor_y = {{0xF9, 0x7C, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> cbor_z = {{0xF9, 0xfC, 0x00}};
|
||||||
|
|
||||||
|
CHECK(json::to_cbor(jx) == cbor_x);
|
||||||
|
CHECK(json::to_cbor(jy) == cbor_y);
|
||||||
|
CHECK(json::to_cbor(jz) == cbor_z);
|
||||||
|
|
||||||
|
CHECK(std::isnan(json::from_cbor(cbor_x).get<T>()));
|
||||||
|
CHECK(json::from_cbor(cbor_y).get<T>() == std::numeric_limits<T>::infinity());
|
||||||
|
CHECK(json::from_cbor(cbor_z).get<T>() == -std::numeric_limits<T>::infinity());
|
||||||
|
|
||||||
|
// Make sure the other CBOR encodings for NaN, infinity, and -infinity are
|
||||||
|
// still supported.
|
||||||
|
const std::vector<std::uint8_t> cbor_x_2 = {{0xFA, 0x7F, 0xC0, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> cbor_y_2 = {{0xFA, 0x7F, 0x80, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> cbor_z_2 = {{0xFA, 0xFF, 0x80, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> cbor_x_3 = {{0xFB, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> cbor_y_3 = {{0xFB, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
||||||
|
const std::vector<std::uint8_t> cbor_z_3 = {{0xFB, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
||||||
|
CHECK(std::isnan(json::from_cbor(cbor_x_2).get<T>()));
|
||||||
|
CHECK(json::from_cbor(cbor_y_2).get<T>() == std::numeric_limits<T>::infinity());
|
||||||
|
CHECK(json::from_cbor(cbor_z_2).get<T>() == -std::numeric_limits<T>::infinity());
|
||||||
|
CHECK(std::isnan(json::from_cbor(cbor_x_3).get<T>()));
|
||||||
|
CHECK(json::from_cbor(cbor_y_3).get<T>() == std::numeric_limits<T>::infinity());
|
||||||
|
CHECK(json::from_cbor(cbor_z_3).get<T>() == -std::numeric_limits<T>::infinity());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("regression test #5074 - portable workaround for single-element brace init")
|
||||||
|
{
|
||||||
|
json const j_obj = {{"key", "value"}};
|
||||||
|
|
||||||
|
json const j = json::array({j_obj});
|
||||||
|
CHECK(j.is_array());
|
||||||
|
CHECK(j.size() == 1);
|
||||||
|
CHECK(j[0] == j_obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(JSON_BRACE_INIT_COPY_SEMANTICS) && (JSON_BRACE_INIT_COPY_SEMANTICS == 1)
|
||||||
|
TEST_CASE("regression test #5074 - single-element brace init with JSON_BRACE_INIT_COPY_SEMANTICS")
|
||||||
|
{
|
||||||
|
// with JSON_BRACE_INIT_COPY_SEMANTICS: single-element brace init copies/moves
|
||||||
|
json const j_obj = {{"key", "value"}, {"num", 42}};
|
||||||
|
json const j_arr = {1, 2, 3};
|
||||||
|
|
||||||
|
// object: brace init copies instead of wrapping
|
||||||
|
json const j1{j_obj};
|
||||||
|
CHECK(j1.is_object());
|
||||||
|
CHECK(j1 == j_obj);
|
||||||
|
|
||||||
|
// array: brace init copies instead of wrapping
|
||||||
|
json const j2{j_arr};
|
||||||
|
CHECK(j2.is_array());
|
||||||
|
CHECK(j2.size() == 3);
|
||||||
|
CHECK(j2 == j_arr);
|
||||||
|
|
||||||
|
// primitives still work as initializer lists
|
||||||
|
json const j3{true};
|
||||||
|
CHECK(j3.is_boolean());
|
||||||
|
|
||||||
|
json const j4{42};
|
||||||
|
CHECK(j4.is_number_integer());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
struct Example_5122
|
||||||
|
{
|
||||||
|
float b = 2;
|
||||||
|
nlohmann::ordered_map<std::string, std::string> c{}; // NOLINT(readability-redundant-member-init): needed for GCC -Weffc++
|
||||||
|
int a = 1;
|
||||||
|
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Example_5122, b, c, a)
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_CASE("regression test #5122 - from_json into types holding nlohmann::ordered_map")
|
||||||
|
{
|
||||||
|
Example_5122 src;
|
||||||
|
src.c.emplace("first", "1");
|
||||||
|
src.c.emplace("second", "2");
|
||||||
|
|
||||||
|
ordered_json const j = src;
|
||||||
|
Example_5122 const dst = j.get<Example_5122>();
|
||||||
|
|
||||||
|
CHECK(dst.b == src.b);
|
||||||
|
CHECK(dst.a == src.a);
|
||||||
|
REQUIRE(dst.c.size() == src.c.size());
|
||||||
|
auto src_it = src.c.begin();
|
||||||
|
auto dst_it = dst.c.begin();
|
||||||
|
for (; src_it != src.c.end(); ++src_it, ++dst_it)
|
||||||
|
{
|
||||||
|
CHECK(dst_it->first == src_it->first);
|
||||||
|
CHECK(dst_it->second == src_it->second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// -Wself-assign-overloaded was introduced in Clang 7. Gate the pragma on
|
||||||
|
// __has_warning so older Clang versions do not error with "unknown warning
|
||||||
|
// group". The __has_warning check has to stay inside the __clang__ branch
|
||||||
|
// because GCC does not provide it and would tokenize-error on the argument.
|
||||||
|
#if defined(__clang__) && defined(__has_warning)
|
||||||
|
#if __has_warning("-Wself-assign-overloaded")
|
||||||
|
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
||||||
|
DOCTEST_CLANG_SUPPRESS_WARNING("-Wself-assign-overloaded")
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
TEST_CASE("regression test #5122 - nlohmann::ordered_map copy-assignment is self-assignment safe")
|
||||||
|
{
|
||||||
|
nlohmann::ordered_map<std::string, std::string> m;
|
||||||
|
m.emplace("first", "1");
|
||||||
|
m.emplace("second", "2");
|
||||||
|
|
||||||
|
// Insertion order is preserved by ordered_map, so we can check it directly.
|
||||||
|
m = m;
|
||||||
|
|
||||||
|
REQUIRE(m.size() == 2);
|
||||||
|
auto it = m.begin();
|
||||||
|
CHECK(it->first == "first");
|
||||||
|
CHECK(it->second == "1");
|
||||||
|
++it;
|
||||||
|
CHECK(it->first == "second");
|
||||||
|
CHECK(it->second == "2");
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(__clang__) && defined(__has_warning)
|
||||||
|
#if __has_warning("-Wself-assign-overloaded")
|
||||||
|
DOCTEST_CLANG_SUPPRESS_WARNING_POP
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
TEST_CASE("regression test #5122 - nlohmann::ordered_map move-assignment transfers contents")
|
||||||
|
{
|
||||||
|
nlohmann::ordered_map<std::string, std::string> src;
|
||||||
|
src.emplace("first", "1");
|
||||||
|
src.emplace("second", "2");
|
||||||
|
|
||||||
|
nlohmann::ordered_map<std::string, std::string> dst;
|
||||||
|
dst.emplace("stale", "x");
|
||||||
|
dst = std::move(src);
|
||||||
|
|
||||||
|
REQUIRE(dst.size() == 2);
|
||||||
|
auto it = dst.begin();
|
||||||
|
CHECK(it->first == "first");
|
||||||
|
CHECK(it->second == "1");
|
||||||
|
++it;
|
||||||
|
CHECK(it->first == "second");
|
||||||
|
CHECK(it->second == "2");
|
||||||
|
|
||||||
|
// Re-assigning into the moved-from object must leave it in a usable state.
|
||||||
|
src = nlohmann::ordered_map<std::string, std::string> {};
|
||||||
|
src.emplace("after-move", "3");
|
||||||
|
REQUIRE(src.size() == 1);
|
||||||
|
CHECK(src.begin()->first == "after-move");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stand-in for a third-party library (e.g., Eigen as of 3.4, which added
|
||||||
|
// STL-compatible begin()/end() to its vector types), living in its own
|
||||||
|
// namespace with its own to_json overload for its vector type.
|
||||||
|
namespace issue_4320_eigen
|
||||||
|
{
|
||||||
|
// "array-compatible" from the library's point of view (it has begin()/end()),
|
||||||
|
// but for which this (fake) third-party namespace provides its own to_json.
|
||||||
|
struct vector3
|
||||||
|
{
|
||||||
|
double v[3]; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-use-default-member-init,modernize-use-default-member-init)
|
||||||
|
vector3(double x, double y, double z) : v{x, y, z} {} // NOLINT(hicpp-member-init,cppcoreguidelines-pro-type-member-init)
|
||||||
|
double x() const
|
||||||
|
{
|
||||||
|
return v[0];
|
||||||
|
}
|
||||||
|
double y() const
|
||||||
|
{
|
||||||
|
return v[1];
|
||||||
|
}
|
||||||
|
double z() const
|
||||||
|
{
|
||||||
|
return v[2];
|
||||||
|
}
|
||||||
|
double* begin()
|
||||||
|
{
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
double* end()
|
||||||
|
{
|
||||||
|
return v + 3;
|
||||||
|
}
|
||||||
|
const double* begin() const
|
||||||
|
{
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
const double* end() const
|
||||||
|
{
|
||||||
|
return v + 3;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void to_json(json& j, const vector3& v) // NOLINT(misc-use-internal-linkage)
|
||||||
|
{
|
||||||
|
j = {{"x", v.x()}, {"y", v.y()}, {"z", v.z()}};
|
||||||
|
}
|
||||||
|
} // namespace issue_4320_eigen
|
||||||
|
|
||||||
|
// The user's own namespace, using the (fake) Eigen type as an implementation
|
||||||
|
// detail behind a payload type that has nothing to do with vectors/arrays.
|
||||||
|
namespace issue_4320
|
||||||
|
{
|
||||||
|
// Publicly derives from issue_4320_eigen::vector3 but does *not* define its
|
||||||
|
// own to_json - it is only ever used as a temporary to reach the base
|
||||||
|
// class's to_json via ADL.
|
||||||
|
struct vector3_wrapper : issue_4320_eigen::vector3
|
||||||
|
{
|
||||||
|
using issue_4320_eigen::vector3::vector3;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct payload
|
||||||
|
{
|
||||||
|
double x, y, z;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline vector3_wrapper to_eigen(const payload& p) // NOLINT(misc-use-internal-linkage)
|
||||||
|
{
|
||||||
|
return {p.x, p.y, p.z};
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void to_json(json& j, const payload& p) // NOLINT(misc-use-internal-linkage)
|
||||||
|
{
|
||||||
|
// Unqualified call, passing a *derived* vector3_wrapper: relies on ADL
|
||||||
|
// finding issue_4320_eigen::to_json(json&, const vector3&) through the
|
||||||
|
// vector3 base class, via a derived-to-base conversion. Must NOT resolve
|
||||||
|
// to the library's own generic array-compatible to_json (an exact-match
|
||||||
|
// template for vector3_wrapper, since it also has begin()/end()), which
|
||||||
|
// would serialize this as [x, y, z] instead of {"x":x, "y":y, "z":z}.
|
||||||
|
to_json(j, to_eigen(p));
|
||||||
|
}
|
||||||
|
} // namespace issue_4320
|
||||||
|
|
||||||
|
TEST_CASE("issue #4320 - custom base class must not leak nlohmann::detail into ADL")
|
||||||
|
{
|
||||||
|
// Before the fix, basic_json unconditionally derived from a type living in
|
||||||
|
// nlohmann::detail (json_default_base), which made nlohmann::detail an
|
||||||
|
// associated namespace of every basic_json for ADL purposes. That leaked
|
||||||
|
// the library's internal generic-array to_json overload into unqualified
|
||||||
|
// to_json() calls made from user code, silently bypassing user-defined
|
||||||
|
// to_json overloads reached via a derived-to-base conversion.
|
||||||
|
const issue_4320::payload p{1.0, 2.0, 3.0};
|
||||||
|
|
||||||
|
json j;
|
||||||
|
to_json(j, p);
|
||||||
|
CHECK(j == json({{"x", 1.0}, {"y", 2.0}, {"z", 3.0}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("issue #5338 - truncated CBOR tagged binary subtype is rejected")
|
||||||
|
{
|
||||||
|
const std::vector<std::vector<std::uint8_t>> truncated_tags =
|
||||||
|
{
|
||||||
|
{0xD8},
|
||||||
|
{0xD9, 0x00},
|
||||||
|
{0xDA, 0x00, 0x00, 0x00},
|
||||||
|
{0xDB, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
|
||||||
|
};
|
||||||
|
|
||||||
|
for (const auto& data : truncated_tags)
|
||||||
|
{
|
||||||
|
CAPTURE(data);
|
||||||
|
for (const auto tag_handler :
|
||||||
|
{
|
||||||
|
json::cbor_tag_handler_t::ignore, json::cbor_tag_handler_t::store
|
||||||
|
})
|
||||||
|
{
|
||||||
|
CAPTURE(tag_handler);
|
||||||
|
const auto result = json::from_cbor(data, true, false, tag_handler);
|
||||||
|
CHECK(result.is_discarded());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("issue #5402 - update(merge_objects=true) overwrites a primitive with an object")
|
||||||
|
{
|
||||||
|
json t = {{"k", 1}};
|
||||||
|
t.update(json{{"k", {{"x", 2}}}}, true);
|
||||||
|
CHECK(t == json({{"k", {{"x", 2}}}}));
|
||||||
|
|
||||||
|
json mixed = {{"keep", {{"a", 1}}}, {"replace", 1}};
|
||||||
|
mixed.update(json{{"keep", {{"b", 2}}}, {"replace", {{"x", 2}}}}, true);
|
||||||
|
CHECK(mixed == json({{"keep", {{"a", 1}, {"b", 2}}}, {"replace", {{"x", 2}}}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DOCTEST_CLANG_SUPPRESS_WARNING_POP
|
||||||
@@ -15,6 +15,8 @@ using nlohmann::json;
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
|
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
TEST_CASE("serialization")
|
TEST_CASE("serialization")
|
||||||
{
|
{
|
||||||
SECTION("operator<<")
|
SECTION("operator<<")
|
||||||
@@ -84,8 +86,9 @@ TEST_CASE("serialization")
|
|||||||
{
|
{
|
||||||
const json j = "ä\xA9ü";
|
const json j = "ä\xA9ü";
|
||||||
|
|
||||||
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9", json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
CHECK_THROWS_WITH_AS(j.dump(1, ' ', false, json::error_handler_t::strict), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9", json::type_error&);
|
CHECK_THROWS_WITH_AS(utils::ignore_return_value(j.dump()), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9", json::type_error&);
|
||||||
|
CHECK_THROWS_WITH_AS(utils::ignore_return_value(j.dump(1, ' ', false, json::error_handler_t::strict)), "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9", json::type_error&);
|
||||||
CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"äü\"");
|
CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"äü\"");
|
||||||
CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"ä\xEF\xBF\xBDü\"");
|
CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"ä\xEF\xBF\xBDü\"");
|
||||||
CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"\\u00e4\\ufffd\\u00fc\"");
|
CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"\\u00e4\\ufffd\\u00fc\"");
|
||||||
@@ -95,8 +98,9 @@ TEST_CASE("serialization")
|
|||||||
{
|
{
|
||||||
const json j = "123\xC2";
|
const json j = "123\xC2";
|
||||||
|
|
||||||
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2", json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);
|
CHECK_THROWS_WITH_AS(utils::ignore_return_value(j.dump()), "[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2", json::type_error&);
|
||||||
|
CHECK_THROWS_AS(utils::ignore_return_value(j.dump(1, ' ', false, json::error_handler_t::strict)), json::type_error&);
|
||||||
CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"123\"");
|
CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"123\"");
|
||||||
CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"123\xEF\xBF\xBD\"");
|
CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"123\xEF\xBF\xBD\"");
|
||||||
CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"123\\ufffd\"");
|
CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"123\\ufffd\"");
|
||||||
@@ -106,8 +110,9 @@ TEST_CASE("serialization")
|
|||||||
{
|
{
|
||||||
const json j = "123\xF1\xB0\x34\x35\x36";
|
const json j = "123\xF1\xB0\x34\x35\x36";
|
||||||
|
|
||||||
CHECK_THROWS_WITH_AS(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 5: 0x34", json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
CHECK_THROWS_AS(j.dump(1, ' ', false, json::error_handler_t::strict), json::type_error&);
|
CHECK_THROWS_WITH_AS(utils::ignore_return_value(j.dump()), "[json.exception.type_error.316] invalid UTF-8 byte at index 5: 0x34", json::type_error&);
|
||||||
|
CHECK_THROWS_AS(utils::ignore_return_value(j.dump(1, ' ', false, json::error_handler_t::strict)), json::type_error&);
|
||||||
CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"123456\"");
|
CHECK(j.dump(-1, ' ', false, json::error_handler_t::ignore) == "\"123456\"");
|
||||||
CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"123\xEF\xBF\xBD\x34\x35\x36\"");
|
CHECK(j.dump(-1, ' ', false, json::error_handler_t::replace) == "\"123\xEF\xBF\xBD\x34\x35\x36\"");
|
||||||
CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"123\\ufffd456\"");
|
CHECK(j.dump(-1, ' ', true, json::error_handler_t::replace) == "\"123\\ufffd456\"");
|
||||||
|
|||||||
@@ -2149,6 +2149,67 @@ TEST_CASE("UBJSON")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("UBJSON optimized arrays of a valueless type are bounded")
|
||||||
|
{
|
||||||
|
// An element of type 'Z', 'T' or 'F' is encoded by its marker alone, so an
|
||||||
|
// optimized array of one of those has no payload and the declared count is
|
||||||
|
// the only thing deciding how much is allocated. Ten bytes used to produce
|
||||||
|
// billions of values (#2793); every other type costs at least one byte per
|
||||||
|
// element and is bounded by the end of the input.
|
||||||
|
json _;
|
||||||
|
|
||||||
|
SECTION("an excessive count is rejected")
|
||||||
|
{
|
||||||
|
// 'l' is a big-endian int32: 0x7FFFFFFF elements, about 34 GB of value
|
||||||
|
for (const auto marker :
|
||||||
|
{'Z', 'T', 'F'
|
||||||
|
})
|
||||||
|
{
|
||||||
|
const std::vector<uint8_t> input = {'[', '$', static_cast<uint8_t>(marker), '#', 'l', 0x7F, 0xFF, 0xFF, 0xFF};
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(input), "[json.exception.out_of_range.408] syntax error while parsing UBJSON size: excessive array size", json::out_of_range&);
|
||||||
|
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("ordinary counts are unaffected")
|
||||||
|
{
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'Z', '#', 'i', 3})) == json({nullptr, nullptr, nullptr}));
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'T', '#', 'i', 2})) == json({true, true}));
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'F', '#', 'i', 2})) == json({false, false}));
|
||||||
|
// 'N' is a no-op rather than a value, and still yields an empty array
|
||||||
|
CHECK(json::from_ubjson(std::vector<uint8_t>({'[', '$', 'N', '#', 'i', 2})) == json::array());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("a type with a payload is unaffected")
|
||||||
|
{
|
||||||
|
// A count past the limit is not rejected for 'U', which costs a byte
|
||||||
|
// per element and is bounded by the end of the input instead. The
|
||||||
|
// count is kept just past the limit rather than made huge, because a
|
||||||
|
// count that also exceeds the array's max_size() is reported as
|
||||||
|
// out_of_range before the input runs out, and max_size() depends on
|
||||||
|
// the width of std::size_t.
|
||||||
|
const std::vector<uint8_t> input = {'[', '$', 'U', '#', 'l', 0x00, 0x10, 0x00, 0x01};
|
||||||
|
CHECK_THROWS_WITH_AS(_ = json::from_ubjson(input), "[json.exception.parse_error.110] parse error at byte 10: syntax error while parsing UBJSON number: unexpected end of input", json::parse_error&);
|
||||||
|
CHECK(json::from_ubjson(input, true, false).is_discarded());
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("the writer stays within what the reader accepts")
|
||||||
|
{
|
||||||
|
// below the limit the optimized form is used and is tiny; above it the
|
||||||
|
// writer falls back so that the result can still be read back
|
||||||
|
json const at_limit(1048576, nullptr);
|
||||||
|
const auto v_at_limit = json::to_ubjson(at_limit, true, true);
|
||||||
|
CHECK(v_at_limit.size() == 9);
|
||||||
|
CHECK(v_at_limit.at(1) == '$');
|
||||||
|
CHECK(json::from_ubjson(v_at_limit) == at_limit);
|
||||||
|
|
||||||
|
json const above_limit(1048577, nullptr);
|
||||||
|
const auto v_above_limit = json::to_ubjson(above_limit, true, true);
|
||||||
|
CHECK(v_above_limit.at(1) != '$');
|
||||||
|
CHECK(json::from_ubjson(v_above_limit) == above_limit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("Universal Binary JSON Specification Examples 1")
|
TEST_CASE("Universal Binary JSON Specification Examples 1")
|
||||||
{
|
{
|
||||||
SECTION("Null Value")
|
SECTION("Null Value")
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ using nlohmann::json;
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "make_test_data_available.hpp"
|
#include "make_test_data_available.hpp"
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
TEST_CASE("Unicode (1/5)" * doctest::skip())
|
TEST_CASE("Unicode (1/5)" * doctest::skip())
|
||||||
{
|
{
|
||||||
@@ -240,7 +241,8 @@ void roundtrip(bool success_expected, const std::string& s)
|
|||||||
if (success_expected)
|
if (success_expected)
|
||||||
{
|
{
|
||||||
// serialization succeeds
|
// serialization succeeds
|
||||||
CHECK_NOTHROW(j.dump());
|
// dump() is nodiscard; this only checks that dumping does not throw
|
||||||
|
CHECK_NOTHROW(utils::ignore_return_value(j.dump()));
|
||||||
|
|
||||||
// exclude parse test for U+0000
|
// exclude parse test for U+0000
|
||||||
if (s[0] != '\0')
|
if (s[0] != '\0')
|
||||||
@@ -259,7 +261,8 @@ void roundtrip(bool success_expected, const std::string& s)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// serialization fails
|
// serialization fails
|
||||||
CHECK_THROWS_AS(j.dump(), json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
|
CHECK_THROWS_AS(utils::ignore_return_value(j.dump()), json::type_error&);
|
||||||
|
|
||||||
// parsing JSON text fails
|
// parsing JSON text fails
|
||||||
CHECK_THROWS_AS(_ = json::parse(ps), json::parse_error&);
|
CHECK_THROWS_AS(_ = json::parse(ps), json::parse_error&);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ using nlohmann::json;
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "make_test_data_available.hpp"
|
#include "make_test_data_available.hpp"
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
// this test suite uses static variables with non-trivial destructors
|
// this test suite uses static variables with non-trivial destructors
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
||||||
@@ -97,7 +98,8 @@ void check_utf8dump(bool success_expected, int byte1, int byte2 = -1, int byte3
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// strict mode must throw if success is not expected
|
// strict mode must throw if success is not expected
|
||||||
CHECK_THROWS_AS(j.dump(), json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
|
CHECK_THROWS_AS(utils::ignore_return_value(j.dump()), json::type_error&);
|
||||||
// ignore and replace must create different dumps
|
// ignore and replace must create different dumps
|
||||||
CHECK(s_ignored != s_replaced);
|
CHECK(s_ignored != s_replaced);
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ using nlohmann::json;
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "make_test_data_available.hpp"
|
#include "make_test_data_available.hpp"
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
// this test suite uses static variables with non-trivial destructors
|
// this test suite uses static variables with non-trivial destructors
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
||||||
@@ -97,7 +98,8 @@ void check_utf8dump(bool success_expected, int byte1, int byte2 = -1, int byte3
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// strict mode must throw if success is not expected
|
// strict mode must throw if success is not expected
|
||||||
CHECK_THROWS_AS(j.dump(), json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
|
CHECK_THROWS_AS(utils::ignore_return_value(j.dump()), json::type_error&);
|
||||||
// ignore and replace must create different dumps
|
// ignore and replace must create different dumps
|
||||||
CHECK(s_ignored != s_replaced);
|
CHECK(s_ignored != s_replaced);
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ using nlohmann::json;
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "make_test_data_available.hpp"
|
#include "make_test_data_available.hpp"
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
// this test suite uses static variables with non-trivial destructors
|
// this test suite uses static variables with non-trivial destructors
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
||||||
@@ -97,7 +98,8 @@ void check_utf8dump(bool success_expected, int byte1, int byte2 = -1, int byte3
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// strict mode must throw if success is not expected
|
// strict mode must throw if success is not expected
|
||||||
CHECK_THROWS_AS(j.dump(), json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
|
CHECK_THROWS_AS(utils::ignore_return_value(j.dump()), json::type_error&);
|
||||||
// ignore and replace must create different dumps
|
// ignore and replace must create different dumps
|
||||||
CHECK(s_ignored != s_replaced);
|
CHECK(s_ignored != s_replaced);
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ using nlohmann::json;
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "make_test_data_available.hpp"
|
#include "make_test_data_available.hpp"
|
||||||
|
#include "test_utils.hpp"
|
||||||
|
|
||||||
// this test suite uses static variables with non-trivial destructors
|
// this test suite uses static variables with non-trivial destructors
|
||||||
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
|
||||||
@@ -97,7 +98,8 @@ void check_utf8dump(bool success_expected, int byte1, int byte2 = -1, int byte3
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// strict mode must throw if success is not expected
|
// strict mode must throw if success is not expected
|
||||||
CHECK_THROWS_AS(j.dump(), json::type_error&);
|
// dump() is nodiscard; the exception is thrown by dump() itself before it would return
|
||||||
|
CHECK_THROWS_AS(utils::ignore_return_value(j.dump()), json::type_error&);
|
||||||
// ignore and replace must create different dumps
|
// ignore and replace must create different dumps
|
||||||
CHECK(s_ignored != s_replaced);
|
CHECK(s_ignored != s_replaced);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user