fredsa / forplay

Automatically exported from code.google.com/p/forplay
Apache License 2.0
12 stars 4 forks source link

Java Json.Object and Json.Array don't handle missing array/object values #31

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Java's Json.Object and Json.Array getObject/getArray fail if the object/array 
at the given key/index is missing. Instead of returning null, they pass null 
into the JavaObject or JavaArray constructor, causing NPEs when trying to 
access the results:

    public Json.Object getObject(int index) {
      return new JavaObject(jsa.optJSONObject(index));
    }

    public Json.Array getArray(int index) {
      return new JavaArray(jsa.optJSONArray(index));
    }

The code should probably look like this:

    public Json.Object getObject(String key) {
      JSONObject o = jso.optJSONObject(key);
      return o == null ? null : new JavaObject(o);
    }

Original issue reported on code.google.com by mmast...@gmail.com on 1 Jun 2011 at 4:30

GoogleCodeExporter commented 9 years ago
Patch available:

http://forplay-code-reviews.appspot.com/5003/

CLA signed on GWT project :)

Original comment by matt...@mastracci.com on 1 Jun 2011 at 7:26

GoogleCodeExporter commented 9 years ago
Thanks for the fix, Matt.

Original comment by jgw@google.com on 3 Jun 2011 at 2:06