Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse returns desired array contained in array when JSON text begins with square bracket on gcc 7.5.0 #2339

Closed
1 of 3 tasks
sbashford opened this issue Aug 4, 2020 · 3 comments
Labels
kind: bug solution: duplicate the issue is a duplicate; refer to the linked issue instead

Comments

@sbashford
Copy link

What is the issue you have?

When compiled with gcc 7.5.0, passing a JSON array to parse returns an array containing the desired array. This is not the same behavior on clang 10.0.0 where the desired array is instead returned.

Please describe the steps to reproduce the issue.

on macOS 10.15.6 with cmake 3.18.1 and brew 2.4.9 in a directory containing the CMakeLists.txt and main.cpp listed below:

  1. brew install gcc@7
  2. cmake -S . -B build -DCMAKE_CXX_COMPILER=/usr/local/bin/g++-7
  3. cmake --build build
  4. ./build/example

Can you provide a small but working code example?

# CMakeLists.txt
include(FetchContent)

set(JSON_BuildTests
    OFF
    CACHE INTERNAL "")
FetchContent_Declare(
  json
  GIT_REPOSITORY
    https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
  GIT_TAG v3.9.0)
FetchContent_MakeAvailable(json)

add_executable(example main.cpp)
target_link_libraries(example nlohmann_json::nlohmann_json)
// main.cpp
#include <nlohmann/json.hpp>
#include <iostream>
int main() {
  const auto example{nlohmann::json::parse(R"([{"pi": 3.141}])")};
  std::cout << example;
}

What is the expected behavior?

[{"pi":3.141}]

And what is the actual behavior instead?

[[{"pi":3.141}]]

Which compiler and operating system are you using?

  • Compiler: gcc 7.5.0
  • Operating system: macOS 10.15.6

Which version of the library did you use?

  • latest release version 3.9.0
  • other release - please state the version: ___
  • the develop branch
@nlohmann
Copy link
Owner

nlohmann commented Aug 5, 2020

The issue is that braces are interpreted as array or object, so json j {1}; creates [1] rather than 1. You can fix the example by changing

const auto example{nlohmann::json::parse(R"([{"pi": 3.141}])")};

to

const auto example = nlohmann::json::parse(R"([{"pi": 3.141}])");

I have no idea how to fix this without breaking existing code. Maybe an option like we introduced for implicit conversions?

@sbashford
Copy link
Author

That solves my use case. Thanks for your quick reply.

The behavior appears to be different on clang 10.0.0. But this is not an issue when using an assignment as you've described.

@nlohmann
Copy link
Owner

nlohmann commented Aug 5, 2020

Duplicate of #2311.

@nlohmann nlohmann closed this as completed Aug 5, 2020
@nlohmann nlohmann added the solution: duplicate the issue is a duplicate; refer to the linked issue instead label Aug 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug solution: duplicate the issue is a duplicate; refer to the linked issue instead
Projects
None yet
Development

No branches or pull requests

2 participants