dropbox / json11

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

has_shape() always returns true when used with json11::Json::NUL #132

Closed jbhelm closed 4 years ago

jbhelm commented 5 years ago

Calling item.has_shape({ {"foo", json11::Json::NUL } }, err) will always return true, whether "foo" exists or not. There does not appear to be a way to determine if a key exists in an object with an actual "null" value, for example for { "foo": null }

Calling item.has_shape({ {"foo", json11::Json::NUL } }, err) on a JSON object of { "bar": 123 } will also return true, even though there is no "foo" key in the object.

It appears at though has_shape() calls the JsonObject [] operator which, of course, returns static_null() if the value is not found, which is what results in has_shape() returning true. Perhaps there's another way to determine if a key exists with a null value, or better yet a way to determine if a key exists regardless of the data type?

artwyman commented 5 years ago

See my comments on the PR. I think that your use case of checking explicitly for the presence of a null-typed field goes somewhat counter to the intent of the automatic-default behavior in this library, which seems to suggest that absent and null fields aren't really distinct. You certainly can check explicitly for the presence of a field by accessing the map of fields directly, but if that were supposed to be supported then the fact that has_shape uses operator[] and thus has side-effects becomes pretty scary.