dropbox / json11

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

Fixed has_shape to handle nested json properly. #22

Closed sroymdsol closed 9 years ago

sroymdsol commented 9 years ago

For nested shapes the has_shape method was examining only values of the root json object instead of looking at the actual json child element. For example:

json: {"key1": {"key2": "value2"}} shape: {{"key1", Json::Type::OBJECT}, {"key2", Json::Type::STRING}}

In this case has_shape would return false because it would look for "key2" in the root json object instead of under the key1 object.

I added a simple test to verify the behavior of has_shape.

artwyman commented 9 years ago

I agree that the current code doesn't support nested shapes in a single has_shape call. I think that your diff here no longer supports non-nested shapes at all, though (i.e. what if key1 and key2 are supposed to be siblings). I think it would take a more complex recursive implementation to support both at once. I think in our existing use cases we do multiple checks as we process different parts of the input. Check the shape of the top-level object, then extract a sub-object you want, then check its shape.

sroymdsol commented 9 years ago

True true. Tough proposition to model a shape object that can do this. Probably not worth it for my needs. Thanks!