# HG changeset patch # User Jaroslav Tulach # Date 1366297988 -7200 # Node ID b22c86471837565067215619d2199349aee342ee # Parent 9cc253aa94054497e40ef651bf641a6d0b80527c Process array and null JSON values diff -r 9cc253aa9405 -r b22c86471837 javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Thu Apr 18 16:58:55 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Thu Apr 18 17:13:08 2013 +0200 @@ -162,25 +162,33 @@ } try { URL u = new URL(base, url.replace(" ", "%20")); - InputStream is = u.openStream(); + PushbackInputStream is = new PushbackInputStream(u.openStream(), 1); + boolean array = false; if (jsonp != null) { - PushbackInputStream pis = new PushbackInputStream(is, 1); - is = pis; for (;;) { - int ch = pis.read(); + int ch = is.read(); if (ch == -1) { break; } + if (ch == '[') { + is.unread(ch); + array = true; + break; + } if (ch == '{') { - pis.unread(ch); + is.unread(ch); break; } } + } else { + int ch = is.read(); + array = ch == '['; + is.unread(ch); } Reader r = new InputStreamReader(is, "UTF-8"); JSONTokener tok = new JSONTokener(r); - JSONObject obj = new JSONObject(tok); + Object obj = array ? new JSONArray(tok) : new JSONObject(tok); jsonResult[0] = convertToArray(obj); } catch (JSONException | IOException ex) { jsonResult[0] = ex; @@ -229,7 +237,7 @@ JSONObject obj = (JSONObject)jsonObject; for (int i = 0; i < props.length; i++) { try { - values[i] = obj.get(props[i]); + values[i] = obj.has(props[i]) ? obj.get(props[i]) : null; } catch (JSONException ex) { LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex); } diff -r 9cc253aa9405 -r b22c86471837 javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Thu Apr 18 16:58:55 2013 +0200 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Thu Apr 18 17:13:08 2013 +0200 @@ -17,7 +17,6 @@ */ package org.apidesign.bck2brwsr.htmlpage; -import java.util.Arrays; import java.util.Iterator; import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.bck2brwsr.htmlpage.api.OnReceive; @@ -38,7 +37,7 @@ * @author Jaroslav Tulach */ @Page(xhtml = "Empty.html", className = "JSONik", properties = { - @Property(name = "fetched", type = PersonImpl.class), + @Property(name = "fetched", type = Person.class), @Property(name = "fetchedCount", type = int.class), @Property(name = "fetchedSex", type = Sex.class, array = true) })