fix: treat single-element brace-init as copy/move instead of wrapping in array (#5074) (#5090)

* 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>
This commit is contained in:
SamareshSingh
2026-05-08 01:20:24 -05:00
committed by GitHub
parent fd17b0889e
commit 62f3b41b30
9 changed files with 196 additions and 0 deletions
+1
View File
@@ -40,6 +40,7 @@ header. See also the [macro overview page](../../features/macros.md).
## Type conversions
- [**JSON_BRACE_INIT_COPY_SEMANTICS**](json_brace_init_copy_semantics.md) - opt in to copy/move semantics for single-element brace initialization
- [**JSON_DISABLE_ENUM_SERIALIZATION**](json_disable_enum_serialization.md) - switch off default serialization/deserialization functions for enums
- [**JSON_USE_IMPLICIT_CONVERSIONS**](json_use_implicit_conversions.md) - control implicit conversions
@@ -0,0 +1,95 @@
# JSON_BRACE_INIT_COPY_SEMANTICS
```cpp
#define JSON_BRACE_INIT_COPY_SEMANTICS /* value */
```
When defined to `1`, single-element brace initialization of a `basic_json` value is treated as a copy/move of the
element rather than wrapping it in a single-element array.
## Default definition
The default value is `0` (disabled — existing behavior is preserved).
```cpp
#define JSON_BRACE_INIT_COPY_SEMANTICS 0
```
## Notes
!!! note "Background"
C++ always prefers the `initializer_list` constructor over the copy/move constructor for brace initialization. This
means that code like
```cpp
json obj = {{"key", "value"}};
json j{obj};
```
creates a single-element **array** `[{"key":"value"}]` instead of a copy of `obj`. This behavior is
compiler-dependent for older compilers (GCC wrapped, Clang did not), but starting from Clang 20, both compilers
behave the same way.
Enabling this macro opts into copy/move semantics for this case
(see [#5074](https://github.com/nlohmann/json/issues/5074)).
!!! warning "Opt-in only"
This macro must be defined **before** including `<nlohmann/json.hpp>`. Defining it after the include has no effect.
!!! tip "Workaround without the macro"
To explicitly create a single-element array without enabling this macro, use `json::array()`:
```cpp
json j = json::array({obj}); // always creates [obj]
```
## Examples
??? example "Default behavior (macro not defined)"
Without the macro, single-element brace initialization wraps the value in an array:
```cpp
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
json obj = {{"key", "value"}};
json j{obj};
// j is [{"key":"value"}] -- single-element array, NOT a copy of obj
}
```
??? example "Opt-in copy semantics (macro defined to 1)"
With the macro, single-element brace initialization copies/moves the value:
```cpp
#define JSON_BRACE_INIT_COPY_SEMANTICS 1
#include <nlohmann/json.hpp>
using json = nlohmann::json;
int main()
{
json obj = {{"key", "value"}};
json j{obj};
// j is {"key":"value"} -- copy of obj
}
```
## See also
- [FAQ: Brace initialization yields arrays](../../home/faq.md#brace-initialization-yields-arrays)
- [**basic_json(initializer_list_t)**](../basic_json/basic_json.md) - the affected constructor
## Version history
- Added in version 3.12.0.
+20
View File
@@ -38,6 +38,26 @@ for objects.
To avoid any confusion and ensure portable code, **do not** use brace initialization with the types `basic_json`, `json`, or `ordered_json` unless you want to create an object or array as shown in the examples above.
To explicitly create a single-element array, use `json::array({value})`:
```cpp
json j = json::array({true}); // [true]
```
**Opt-in copy semantics (since version 3.12.0)**
If you define `JSON_BRACE_INIT_COPY_SEMANTICS` to `1` before including the library, single-element brace initialization is treated as copy/move instead of creating a single-element array:
```cpp
#define JSON_BRACE_INIT_COPY_SEMANTICS 1
#include <nlohmann/json.hpp>
json obj = {{"key", "value"}};
json j{obj}; // -> {"key":"value"} (copy, not array)
```
Without the macro (default behavior), `json j{obj}` creates `[{"key":"value"}]`. This opt-in macro fixes issue #5074 while preserving backwards compatibility for existing code.
## Limitations
### Relaxed parsing