red-owl-games / unity-json

Implements the JSON spec in a C# native way allowing easier interaction with JSON node trees
MIT License
8 stars 3 forks source link

No way to check if an entry in the JSON actually exists. #6

Open abledbody opened 2 years ago

abledbody commented 2 years ago

Say you have a JSON that looks like this: { "someThing": { "fieldA": 10.0, "fieldC": 5.0 } } And some code that looks like this: var fieldB = json["someThing"]["fieldB"] There's no way to check if you successfully found an entry in that spot. If you try and access the value, float fieldBValue = fieldB it will simply fail an assertion. I recommend changing most of the assertions in Json class to exceptions, so that they can be caught and handled by the user of the API.

abledbody commented 2 years ago

Also, adding a new entry in the internal dictionary every time you can't find a key is an extremely bad idea, because it allocates more memory for an action that shouldn't even (noticeably) mutate the class. This should also be an exception.