Skip to content

Brace-Initialization Fails with json::parse and Key Access on Linux #4488

@mold-boy

Description

@mold-boy

Description

On Linux, using brace-initialized literals with json::parse or accessing JSON keys with auto results in unexpected behavior or compile-time errors. This issue does not occur on other platforms and requires modifying code to use assignment (=) syntax instead of brace initialization. This is a non-issue for MacOS and iOS. I have not tested Windows.

Reproduction steps

  1. Create a JSON string, e.g., std::string str = "{"key": "value"}";
  2. Attempt to parse it using auto parsed{json::parse(str)};
  3. Attempt to access a key using auto key{parsed["key"]};

Expected vs. actual results

The code should run on Linux like it does on MacOS and iOS, allowing the use of brace-initialized literals ({}) for json::parse and key access with JSON. It seems to be making an array only on Linux when using {} initialization.

Minimal code example

#include <nlohmann/json.hpp>
#include <string>

using json = nlohmann::json;

int main() {
    std::string str = "{\"key\": \"value\"}";
    auto parsed{json::parse(str)};   // Fails on Linux
    auto key{parsed["key"]};         // Fails on Linux
    return 0;
}

Error messages

On Linux, the code throws exception when using brace-initialization {}, and errors arise specifically in these cases. Changing to assignment (=) syntax for both parsing and key access, like auto parsed = json::parse(str);, works as expected.

Compiler and operating system

g++14 with C++20

Library version

Latest release

Validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    solution: proposed fixa fix for the issue has been proposed and waits for confirmation

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions