-
-
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
About value(key, default_value) and operator[](key) #1358
Comments
|
I update the header file by pulling the source code and building install. Before I update, I used the operator[](key) to get a nullable value from one const json reference, it worked very well (already so many code used) , just like this: bool check_valid_args(const nlohmann::json& args)
{
const auto& info = args["info"];
if(!info.is_null() && !info.is_object()) return false;
const auto& name = args["name"];
if(!name.is_null() && !name.is_string()) return false;
return true;
} But after pulling the new source code and building install, all I used like that does't work any more, and make the programs crash for the assertion "object.find(key) != object.end()" Why change the usage? Is it better to return a json(null) when I call operator[](key) from an object without the key/value? If the change is necessary, the only way for me to avoid crash is to rollback to old version, otherwise too many crashes to fixed T_T |
In your code, you call In contrast, the non-const version of So:
You should use |
Thanks! It helps me a lot ^_^ As I wrote before, I wanted to use the search for a key as little as possible, but use the found result more times. Once search more use . It takes twice search time at least that find first and then use operatot[](key) , if the total count of keys is too large, it will take much more times. So I used it like that Maybe I should change my usage. I write a util function to make the old code compatible and use the correct usage you described since now |
When I use a json object(it may be object or null), I want to use the interface "value(key, default_value)" to get the property of the key, but if the json is null, the call will throw exception like "null cannot use value()", I dont't think it is better...
Why not change it to be used like:
But after I upgraded it to the newest version several days ago, more exception is thrown, and more assertions make the old programs crash... T_T
Why make the change?
The text was updated successfully, but these errors were encountered: