fix: treat single-element brace-init as copy/move instead of wrapping in array (#5074)#5090
Open
ssam18 wants to merge 2 commits intonlohmann:developfrom
Open
fix: treat single-element brace-init as copy/move instead of wrapping in array (#5074)#5090ssam18 wants to merge 2 commits intonlohmann:developfrom
ssam18 wants to merge 2 commits intonlohmann:developfrom
Conversation
Contributor
|
See the first entry in the FAQ. https://json.nlohmann.me/home/faq/#brace-initialization-yields-arrays |
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @ssam18 |
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 nlohmann#5074
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
- Add missing comment from include/nlohmann/json.hpp explaining the single-element brace-init fix (issue nlohmann#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>
bee5b16 to
6ba0665
Compare
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @ssam18 |
1 similar comment
🔴 Amalgamation check failed! 🔴The source code has not been amalgamated. @ssam18 |
Owner
|
There is currently no way to fix this behavior without breaking existing code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a value is passed using single-element brace initialization e.g.,
json j{someObj}or via a by-value parametervoid foo(json j)called asfoo({someObj}). C++ always prefers theinitializer_listconstructor over the copy/move constructor. This caused the value to be silently wrapped in a single-element array.This bug was previously compiler-dependent: GCC wrapped the value while Clang did not. As of Clang 20, Clang now matches GCC behavior, making this a universal regression.
Validation
All existing unit tests pass with the fix applied.
Fixes the issue #5074.