-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Comments
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"} |
Ok, thanks for the explanation and proposed fix, that solved my problem. |
could you please mention it in "Read JSON from a file" section of readme.md explicitly? |
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. |
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. |
This is actually a bug. The initializer list is completely valid with C++20. |
Compilers behave differently, and I have no idea how to fix this. Any help appreciated. |
What is the issue you have?
When initializing
basic_json
fromstd::string
(or usingbasic_json::parse()
), then main JSON object is parsed as array, not as object. Deserialization usingstd::stringstream
with>>
operator or with_json
works as expected.Please describe the steps to reproduce the issue.
std::string
with specified JSON stringbasic_json
object and initialize it with this stringCan you provide a small but working code example?
What is the expected behavior?
And what is the actual behavior instead?
Which compiler and operating system are you using?
Which version of the library did you use?
develop
branchThe text was updated successfully, but these errors were encountered: