[psa] JSONObject.optString doesn't do what you think it does

Nick Alexander nalexander at mozilla.com
Fri May 23 10:37:54 PDT 2014


On 2014-05-23, 9:00 AM, James Hugman wrote:
> Hi, mfd
>
> As another datapoint: the Obj-C category `NSObject+NSJSON` treats nulls
> as a separate object type: `NSNull`, and all instances are
> `NSNull.null`. Instances of JS's undefined is represented with the
> equivalent of Java's `null`: `nil`. This is only possible because of
> Objective-C's loose typing, and will not transliterate into Java.

This is, in fact, what JSONObject does -- there's JSONObject.NULL.  It's 
awful.

> Amongst my unsolicited opinions are these:
>
>   * code which changes behaviours based on a difference between
> `undefined` and `null` should be avoided. Better yet, it should be
> shunned or sent for re-education.

I agree, but JS encourages null and undefined to be treated as 
equivalent; and we have a lot of existing JS that does this.  I am 
seeing a badly handled message PrivateBrowsing:Data that does this very 
explicitly; that's part of what sent me down this rabbit-hole.

>   * checking if a key exists if different to checking if the value is
> `null`. JSONObject has a method `has` which does this. Javans almost
> never use the `Map.containsKey`, but if we really need to distinguish
> between JS `null` and `undefined`, then we should be using this more.

I agree, and I highlighted the fact that NativeJSObject.isNull does not 
exist on the ticket.  I'm not sure if .containsKey is there either.

Nick


More information about the mobile-firefox-dev mailing list