Skip to content

fix: compile error when deserializing into types using ordered_map#5128

Open
ssam18 wants to merge 4 commits intonlohmann:developfrom
ssam18:fix/ordered-map-from-json-const-key
Open

fix: compile error when deserializing into types using ordered_map#5128
ssam18 wants to merge 4 commits intonlohmann:developfrom
ssam18:fix/ordered-map-from-json-const-key

Conversation

@ssam18
Copy link
Copy Markdown
Contributor

@ssam18 ssam18 commented Apr 1, 2026

When deserializing into a struct that contains an ordered_map field (e.g. via NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT), GCC fails to compile because std::transform + std::inserter triggers the element-shifting path in <algorithm>, which requires pair::operator= deleted when the key is const.

Replaced the transform with a simple range-for loop and emplace, which constructs in-place and never tries to assign.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @ssam18
Please read and follow the Contribution Guidelines.

ssam18 added 3 commits March 31, 2026 22:48
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>
std::transform + std::inserter on vector-backed ordered_map triggers element shifting code in GCC's <algorithm>, which requires pair::operator=.
That is deleted when the key is const (e.g. ordered_map<string, string>).

Replace with a range-for + emplace so no assignment is ever needed. Fix for the bug nlohmann#5122

Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
@ssam18 ssam18 force-pushed the fix/ordered-map-from-json-const-key branch from 6401b3b to f906482 Compare April 1, 2026 03:48
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @ssam18
Please read and follow the Contribution Guidelines.

Signed-off-by: Samaresh Kumar Singh <ssam3003@gmail.com>
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @ssam18
Please read and follow the Contribution Guidelines.

1 similar comment
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @ssam18
Please read and follow the Contribution Guidelines.

yield different results (`#!json [true]` vs. `#!json true`)?

This is a known issue, and -- even worse -- the behavior differs between GCC and Clang. The "culprit" for this is the library's constructor overloads for initializer lists to allow syntax like
Starting from this version, single-value brace initialization is treated as copy/move instead of wrapping in a single-element array:
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change we cannot accept. Please revert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants