JSONKit icon indicating copy to clipboard operation
JSONKit copied to clipboard

Added parse option JKParseOptionNormalizeOnNull

Open rabc opened this issue 13 years ago • 5 comments

Added parse option JKParseOptionNormalizeOnNull to use an empty string instead of NSNull class on null value.

A client's API return a JSON with 'null' values on server error, so to avoid app crashes while expecting a string, I'm using this new flag.

rabc avatar Feb 07 '12 16:02 rabc

Nice would be if the value could also be nil instead an Empty String.

x2on avatar Apr 13 '12 10:04 x2on

NSDictionary doesn't accept nil as value. It's only nil when key doesn't exist, so JSONKit would have to ignore the key and it could lead to more app crashes.

rabc avatar Apr 13 '12 12:04 rabc

Why assume that a string is the proper thing to return? If code is expecting a dictionary or a number and gets a string, bad things happen. What if the response is an empty string?

NSNull is explicit about being NULL. An empty string is not.

zadr avatar Apr 13 '12 17:04 zadr

If it's an empty string, it will be an empty string. This pull request doesn't enforce use the new way, just add an extra flag.

NSNull could lead to troubles if you don't have control over the JSON you're using (that's my case, as I told in pull request comment).

rabc avatar Apr 13 '12 17:04 rabc

I agree with zadr- I don't see how replacing NSNull instances with empty strings could be construed as correct behavior. Obviously runtime errors arise by sending messages to NSNull that it doesn't respond to. If a programmer was expecting an NSDictionary and got NSString it probably wouldn't work either. t think it would be better just to post-process your JSON and remove the NSNulls.

sbooth avatar Apr 13 '12 23:04 sbooth