dropbox / json11

A tiny JSON library for C++11.
MIT License
2.54k stars 613 forks source link

Check if a json object key exists #123

Closed hugoam closed 6 years ago

hugoam commented 6 years ago

It seems like there is no way to check if a json object contains a given key. The only way, if I'm not mistaken, is to call operator[] and check the returned object for null, however this will not produce the appropriate behavior if the object actually contains a null value for this key (in which case there is not way to verify if the key is present in the json or not). It would be nice if there was something like a has_key() function ? Happy to be corrected if I missed something.

hugoam commented 6 years ago

I have an example use case if you don't see why a user could possibly need this : When deserializing the delta values of a c++ object from a json representation (by which I mean, only what has changed), a null value for the key would mean that the value needs to be set to null, and an absence of key would mean that the value is not set. Currently I believe this library doesn't provide a way to differentiate between the two.

j4cbo commented 6 years ago

You could use .object_items().count(key)