* fix: treat single-element brace-init as copy/move
When passing a json value using brace initialization with a single element
(e.g., `json j{someObj}` or `foo({someJson})`), C++ always prefers the
initializer_list constructor over the copy/move constructor. This caused
the value to be unexpectedly wrapped in a single-element array.
This bug was previously compiler-dependent (GCC wrapped, Clang did not),
but Clang 20 started matching GCC behavior, making it a universal issue.
Fix: In the initializer_list constructor, when type deduction is enabled
and the list has exactly one element, copy/move it directly instead of
creating a single-element array.
Before:
json obj = {{"key", 1}};
json j{obj}; // -> [{"key":1}] (wrong: array)
foo({obj}); // -> [{"key":1}] (wrong: array)
After:
json j{obj}; // -> {"key":1} (correct: copy)
foo({obj}); // -> {"key":1} (correct: copy)
To explicitly create a single-element array, use json::array({value}).
Fixes the issue #5074
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
* fix: regenerate amalgamated single_include/nlohmann/json.hpp
- Add missing comment from include/nlohmann/json.hpp explaining the
single-element brace-init fix (issue #5074)
- Fix extra 4-space indentation in embedded json_fwd.hpp section
Regenerated by running: make amalgamate
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
* Revert brace-init semantics change and fix amalgamation
The single-element brace-init change was a breaking change that cannot be accepted upstream. Reverted all related source, test, and doc changes, then regenerated single_include with correct indentation to pass the amalgamation CI check.
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
* Fix: add JSON_BRACE_INIT_COPY_SEMANTICS opt-in macro for issue #5074
Single-element brace initialization wrapping in an array cannot be fixed without breaking existing code. Added JSON_BRACE_INIT_COPY_SEMANTICS as an opt-in macro (default 0) so users can enable copy/move semantics for single-element brace init without affecting anyone relying on the current behavior.
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
* docs: add dedicated macro page and CI test target for JSON_BRACE_INIT_COPY_SEMANTICS
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
* fix: remove compiler-dependent assertions from #5074 regression test
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
* fix: use defined() guard for JSON_BRACE_INIT_COPY_SEMANTICS to satisfy -Wundef
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
* docs: fix section name in json_brace_init_copy_semantics.md to pass style check
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
* docs: move Default definition section before Notes to fix style check order
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
---------
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
PR #4873 introduced a safety check in sax_parse functions to catch
nullptr passed as SAX parser object, which had been already annotated by
JSON_HEDLEY_NON_NULL macro.
Compilers (e.g. clang) which respected the non-null annotation tended to
eliminate the safety check completely in optimized builds, while
compilers which did not, compiled the safety check in. This led to
different behaviors accross different compilers/platforms and/or build
types (debug, release).
This commit reverts PR #4873 to remove this discrepancy. Passing null to
non-null annotated parameter is considered to be undefined behavior.
Fixes#5048
Signed-off-by: Richard Musil <risa2000x@gmail.com>
Co-authored-by: Richard Musil <risa2000x@gmail.com>
* Add reference handling to tuples
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
* Remove template template type because pair isn't working
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
* amalgamate std::tie changes
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
* allow the elation of a move by removing the ref requirement
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
* force all number_xxx_t to be interchangeable
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
* Finally got amalgamate to work correctly
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
* remove const version, add a test case for scrambled number representations.
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
* Use the logical set of requirements instead of decltype because VS 2015 doesn't like it
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
---------
Signed-off-by: Evelyn LePain <ava.lepain@gmail.com>
* fix(cbor): reject negative ints overflowing int64
CBOR encodes negative integers as "-1 - n" where n is uint64_t. When
n > INT64_MAX, casting to int64_t caused undefined behavior and silent
data corruption. Large negative values were incorrectly parsed as
positive integers (e.g., -9223372036854775809 became 9223372036854775807).
Add bounds check for to reject values that exceed int64_t
representable range, returning parse_error instead of silently
corrupting data.
Added regression test cases to verify.
Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
* chore: clarify tests
Add test for "n=0" case (result=-1) to cover the smallest magnitude
boundary. Update comments to explain CBOR 0x3B encoding and why
"result=0" is not possible. Clarify that n is an unsigned integer
in the formula "result = -1 - n" to help understanding the tests.
Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
* fix(cbor): extend overflow checks for other types
Extend negative integer overflow detection to all CBOR negative
integer cases (0x38, 0x39, 0x3A) for consistency with the existing
0x3B check.
Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
---------
Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
Adds pre-multiplication overflow detection to catch cases where dimension
products would exceed size_t max. The previous check only detected when
overflow resulted in exactly 0 or SIZE_MAX, missing other cases.
Retains the original post-multiplication check for backward compatibility.
Adds tests verifying overflow detection with dimensions (2^32+1)×(2^32),
which previously overflowed silently to 2^32.
This prevents custom SAX handlers from receiving incorrect array sizes
that could lead to buffer overflows.
Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
* Specialize char_traits for std::byte to fix from_msgpack (fixes#4756)
Provide a char_traits<std::byte> specialization under __cpp_lib_byte
to allow parsing MessagePack data from containers of std::byte.
Signed-off-by: xuesongtap <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>
* Fix comments for cstddef include and MessagePack tests
Signed-off-by: xuesongtap <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>
* Fix include <cstddef> only when __cpp_lib_byte is defined and sufficient
Signed-off-by: yexiaochuan <tap91624@gmail.com>
* Fix clang-tidy warnings in MessagePack std::byte tests
Signed-off-by: yexiaochuan <tap91624@gmail.com>
* Fix handle return value in MessagePack tests
Signed-off-by: yexiaochuan <tap91624@gmail.com>
---------
Signed-off-by: xuesongtap <tap91624@gmail.com>
Signed-off-by: yexiaochuan <tap91624@gmail.com>
* Make std::filesystem::path conversion to/from UTF-8 encoded JSON string explicit.
Signed-off-by: Richard Musil <risa2000x@gmail.com>
* Experimental: Changing C++ standard detection logic to accommodate potential corner cases.
Signed-off-by: Richard Musil <risa2000x@gmail.com>
* Drop C++ standard tests for compilers which do not implement required features.
Signed-off-by: Richard Musil <risa2000x@gmail.com>
* Drop C++ standard tests for MSVC versions which do not implement required features.
Signed-off-by: Richard Musil <risa2000x@gmail.com>
---------
Signed-off-by: Richard Musil <risa2000x@gmail.com>
Co-authored-by: Richard Musil <risa2000x@gmail.com>
* Support any basic_json type in NLOHMANN_DEFINE_TYPE_* macros
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Test NLOHMANN_DEFINE_TYPE_* macros also support unordered_json
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Simplify test about NLOHMANN_DEFINE_TYPE_ with many arguments
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Remove extra scope in macros tests
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Remove unused test class in macros tests
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Update documentation about NLOHMANN_DEFINE_TYPE_* macros
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Fix NLOHMANN_JSON_SERIALIZE_ENUM documentation
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Mark some variables const in macros tests, fixes clang-tidy
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Workaround clang 3.5 issue with const object initialization
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Update highlighted lines in NLOHMANN_DEFINE_TYPE_* macros examples
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Fix swapped macros in documentation
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Remove extra backslashes at the end of macros
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Require basic_json type in NLOHMANN_DEFINE_TYPE_* generated functions
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* Fix typos in macros documentation
Signed-off-by: kimci86 <kimci86@hotmail.fr>
---------
Signed-off-by: kimci86 <kimci86@hotmail.fr>
* change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance
* fix ci_static_analysis_clang (ci_clang_tidy)
* change NLOHMANN_JSON_FROM_WITH_DEFAULT to let NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT work with an empty JSON instance