Skip to content

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

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I will take a look in a day or two

Copy link
Copy Markdown
Contributor

@gregmarr gregmarr left a comment

Choose a reason for hiding this comment

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

You need to drop commit 2071ba67d9923f4b5349142dc3d12887572ceb61 from this.

}
else
{
// if there is exactly one element and type deduction is enabled,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is also that breaking change that we can't accept.


#ifndef INCLUDE_NLOHMANN_JSON_FWD_HPP_
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_
#define INCLUDE_NLOHMANN_JSON_FWD_HPP_
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There is something wrong with your amalgamation setup.

@ssam18 ssam18 closed this Apr 2, 2026
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.

3 participants