[ardoJSON] Exception on non-existing token path

[ardoJSON] Exception on non-existing token path

Forge Component
Published on 30 Aug by Ricardo Silva
20 votes
Published on 30 Aug by Ricardo Silva
I use ardoJSON to integrate my application with an iOS app. This works nicely and fast! (I sometimes receive JSON-strings that are almost 10KB and it's still fast)

One little annoyance was the 'Object not instance of an object' messages in the error log when I tried to look up a token that doesn't exist in the Json. Therefore I've made a little change to your code to prevent WriteTo to be called when SelectToken doesn't return a token object.
JToken token = JToken.Parse(ssJSONIn).SelectToken(ssPath); 
if ( token != null ) 

Maybe an idea to include this in a new version?

Kind regards,
This is not the first time such a request is made from me.

I might consider changing the API of JSONSelect a bit based on these comments, since it seems unwanted and cumbersome to have errors in these cases.
I think that if an 'unexpected' error occurs, you must show/throw it. Silently failing code can cause you lots of hours of debugging. Problem is that I don't know if a token is included before I query it.

The component you're using does already provide an overloaded SelectToken method to pass a flag to indicate whether an error should be thrown if no tokens are found when evaluating part of the expression. If you include that one, it is up to the user to set if an error is shown.

But, once again, I like ardoJSON because it works easy and fast :-)
Certainly that silently ignoring the error is not the way to go.

I will probably change the API so that it adds a Success boolean output parameter letting you know wheter or not the path existed in the given JSON.

It may be accompanied (at first, to not break existing behavior) with an input parameter controlling the behavior (throw exception or not).
Long time overdue, but ardoJSON 1.6 has changes to JSONSelect which include this use case (not throwing error when path doesn't reach anything).

Another change is that selecting a string will return the string value instead of the json serialization. Dates still get weird in .NET, tho.