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

Object deserialized as array #2204

Closed
1 of 3 tasks
martin-vitek opened this issue Jun 21, 2020 · 8 comments
Closed
1 of 3 tasks

Object deserialized as array #2204

martin-vitek opened this issue Jun 21, 2020 · 8 comments
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@martin-vitek
Copy link

What is the issue you have?

When initializing basic_json from std::string (or using basic_json::parse()), then main JSON object is parsed as array, not as object. Deserialization using std::stringstream with >> operator or with _json works as expected.

Please describe the steps to reproduce the issue.

  1. Create std::string with specified JSON string
  2. Create basic_json object and initialize it with this string
  3. Dump json object, or try to access value via name

Can you provide a small but working code example?

std::string json_string = R"(
{
    "serviceVersion":"1.0",
    "resultCode":
    {
        "code":0,
        "text":"OK"
    },
    "challenge":"sdfg45sdg45",
    "authenticationType":0,
    "challengeId":"dsv1d51df5-dasfv551v1-asdv155551"
})";

auto json_object {nlohmann::json::parse(json_string)};
std::cout << json_object.dump() << '\n';
std::cout << json_object["challenge"] << '\n';

What is the expected behavior?

{"authenticationType":0,"challenge":"sdfg45sdg45","challengeId":"dsv1d51df5-dasfv551v1-asdv155551","resultCode":{"code":0,"text":"OK"},"serviceVersion":"1.0"}
"sdfg45sdg45"

And what is the actual behavior instead?

[{"authenticationType":0,"challenge":"sdfg45sdg45","challengeId":"dsv1d51df5-dasfv551v1-asdv155551","resultCode":{"code":0,"text":"OK"},"serviceVersion":"1.0"}]
terminate called after throwing an instance of 'nlohmann::detail::type_error'
  what():  [json.exception.type_error.305] cannot use operator[] with a string argument with array

Which compiler and operating system are you using?

  • Compiler: GCC 10.1.1
  • Operating system: Fedora 32
  • Language version: C++20

Which version of the library did you use?

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

The problem are the braces. Replace

auto json_object {nlohmann::json::parse(json_string)};

with

auto json_object = nlohmann::json::parse(json_string);

Explanation: Braces are interpreted as array or object, depending on the context.

json array = {1, 2, 3}; // [1,2,3]
json object = { {"key", "value"}, {"foo", "bar"} }; // {"key":"value","foo":"bar"}

@nlohmann nlohmann added solution: proposed fix a fix for the issue has been proposed and waits for confirmation and removed kind: bug labels Jun 21, 2020
@martin-vitek
Copy link
Author

Ok, thanks for the explanation and proposed fix, that solved my problem.

@wtom76
Copy link

wtom76 commented Jan 28, 2023

could you please mention it in "Read JSON from a file" section of readme.md explicitly?

@SimonMaracine
Copy link

SimonMaracine commented Jun 15, 2024

I wasted a couple of hours trying to figure out why I couldn't extract anything from my simple json file, then why it was treated as an array, then why everything was treated as an array until I finally found this specific issue.

I personally use aggregate initialization for everything as a rule or convention in my C++ code to make it distinct from any sort of assignment. I don't know what to say if this is a mistake in this library's API design or an issue in C++'s design. If it is the former, I would greatly appreciate if you could fix it. I may open up an issue, if you wish.

@nlohmann
Copy link
Owner

See https://json.nlohmann.me/home/faq/

@SimonMaracine
Copy link

Thank you. I'm really sorry for this. I see now that lots of people already pointed out this "issue". How much patience do you have left?... :|

The problem was that I mostly looked at the examples in the README or looked at my past code (which was using copy initialization) with this library. My mistake.
Though a quick warning about this in the README might help.

@mold-boy
Copy link

mold-boy commented Nov 9, 2024

This is actually a bug. The initializer list is completely valid with C++20.

@nlohmann
Copy link
Owner

nlohmann commented Nov 9, 2024

Compilers behave differently, and I have no idea how to fix this. Any help appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

5 participants