-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
How to create a json variable? #990
Comments
You need to have a look at the values you are creating:
To create an object with the initializer list syntax, you need to write: json v = { {"a", { {"b", { {"c", { {"value", 900} }} }} }} }; But it is much easier to write: json v;
v["a"]["b"]["c"]["value"] = 900; |
Thanks Niels. Your code works. I agree, easier to write the way you showed for this simple example, but I want a compile-time initializer list for a much larger json (and still trying to get my own real case to work, but at least your example shows the way). A few followups:
And, btw, thanks for creating this tool and providing great support! |
To your questions:
|
|
Sure, if there is a way to determine if the value exists (and I'll take it that there is, and hopefully a method that doesn't throw if the value doesn't exist). But, the value() method requires the default value parameter. So might as well let me know if it was used or not. Alternatively, default value could be made an optional parameter - and then perhaps I would not have made the suggestion to begin with ;). Of course, that would require a throw if the value doesn't exist. In terms of [] and -> operators, [] seems to work fine everywhere. C++ allows it to be overridden to be anything you want, but natively it is for selecting an array element and intuitively that's how I thought about it for accessing json array elements. -> seems more natural for objects and values (as does . operator, but can't override that). I'm good since [] is working fine. You might consider overriding operator -> to allow developer preference. I will close. I'm a GitHub newbie. Hopefully you can re-open if you want to add further comments. But I'm good, so closed from my perspective. |
Yes,
You can do the same. |
Resurrecting / followup on the .value() and .find(). The commented line (line 6) throws an exception where I feel it should return the value 20. Because it throws, I worked around it by the code below line 6 which uses find() and is a bit messy. Try/catch perhaps better but also a little messy if there are a lot of accesses. Wondering if this is by design or is fixable. Or if there is a better way.
|
You try to call You could chain the #include "json.hpp"
#include <iostream>
using json = nlohmann::json;
int main()
{
json j;
j["a"]["b"]["c"] = 10;
int ade = j.value("a", json::object())
.value("d", json::object())
.value("e", 20);
std::cout << ade << std::endl;
} |
Sure, but what is different about j["a"]["b"]["c"] = 10? Isn't j["a"]["b"] an empty object before it's not? In my actual scenario, I'm reading json from the network. If the sender sends me bad json, I want to either a) in this case, assign a default value if an optional value is missing or b) in other cases, be able to validate the schema adheres to a required schema. |
For Your usecase may be similar to #1007 (comment). You may want to create an object of default values and call |
Yes, this strategy works. Thank you. |
Bug Report
This is more of a question than an issue. The sample code illustrates what I'm trying to do, and the included output shows it's not working the way I hope/expect. I've tried other variations than what is in the sample - can't get anything to work.
Build and run this code:
All cases print the value 900.
This is the output of the code:
value1: 900
value2 : exception
value3 : exception
Visual Studio 2017, Windows 10
develop
branch?Released version 3.1.1.
Feature Request
Describe the feature in as much detail as possible.
Include sample usage where appropriate.
The text was updated successfully, but these errors were encountered: