nlohmann / json

JSON for Modern C++
https://json.nlohmann.me
MIT License
42.18k stars 6.65k forks source link

Parser adds wrapping array when compiled with GCC #3897

Closed Jairard closed 1 year ago

Jairard commented 1 year ago

Description

I wrote a simple progam that just read a JSON file, parses it and dumps the result. It appears that the output is not the same when compiled with GCC or Clang, and the GCC executable seems to produce an error: the input JSON is encapsulated in an extraneous array.

Here is a simple makefile to try it (the content of main.cpp is given later):

clang:
    clang++ main.cpp -I . -o clang_parser
gcc:
    g++ main.cpp -I . -o gcc_parser

Reproduction steps

Simply read and parse the following JSON:

[{"id": 1819645}]

Expected vs. actual results

The expected output would be identical to the input:

[{"id": 1819645}]

But I get

[[{"id":1819645}]]

This only occurs using GCC, not with Clang or Clang-cl.

Minimal code example

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

int main(void) {
  std::ifstream t("input.json");
  std::stringstream buffer;
  buffer << t.rdbuf();
  std::string str{buffer.str()};

  auto const json{nlohmann::json::parse(str)};
  std::cout << json.dump() << std::endl;

  return 0;
}

Error messages

No response

Compiler and operating system

g++ (GCC) 12.2.0 (occuring) and clang version 14.0.6 (not occuring). OS is 5.13.19-2-MANJARO

Library version

3.11.2

Validation

nlohmann commented 1 year ago

See #2583.