1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java Mon Jul 21 11:28:13 2014 +0200
1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java Tue Jul 22 17:55:18 2014 +0200
1.3 @@ -56,9 +56,12 @@
1.4 * @author Jaroslav Tulach <jtulach@netbeans.org>
1.5 */
1.6 public final class ConvertTypesTest {
1.7 - private static InputStream createIS(boolean includeSex, boolean includeAddress)
1.8 + private static InputStream createIS(boolean includeSex, boolean includeAddress, boolean array)
1.9 throws UnsupportedEncodingException {
1.10 StringBuilder sb = new StringBuilder();
1.11 + if (array) {
1.12 + sb.append("[\n");
1.13 + }
1.14 sb.append("{ \"firstName\" : \"son\",\n");
1.15 sb.append(" \"lastName\" : \"dj\" \n");
1.16 if (includeSex) {
1.17 @@ -68,6 +71,9 @@
1.18 sb.append(", \"address\" : { \"street\" : \"Schnirchova\" } \n");
1.19 }
1.20 sb.append("}\n");
1.21 + if (array) {
1.22 + sb.append(']');
1.23 + }
1.24 return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
1.25 }
1.26 private static Object createJSON(boolean includeSex)
1.27 @@ -95,7 +101,7 @@
1.28 @KOTest
1.29 public void parseConvertToPeople() throws Exception {
1.30 final BrwsrCtx c = newContext();
1.31 - final InputStream o = createIS(true, false);
1.32 + final InputStream o = createIS(true, false, false);
1.33
1.34 Person p = Models.parse(c, Person.class, o);
1.35
1.36 @@ -107,7 +113,7 @@
1.37 @KOTest
1.38 public void parseConvertToPeopleWithAddress() throws Exception {
1.39 final BrwsrCtx c = newContext();
1.40 - final InputStream o = createIS(true, true);
1.41 + final InputStream o = createIS(true, true, false);
1.42
1.43 Person p = Models.parse(c, Person.class, o);
1.44
1.45 @@ -132,7 +138,32 @@
1.46 @KOTest
1.47 public void parseConvertToPeopleWithoutSex() throws Exception {
1.48 final BrwsrCtx c = newContext();
1.49 - final InputStream o = createIS(false, false);
1.50 + final InputStream o = createIS(false, false, false);
1.51 + Person p = Models.parse(c, Person.class, o);
1.52 +
1.53 + assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
1.54 + assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
1.55 + assert p.getSex() == null : "No sex: " + p.getSex();
1.56 + }
1.57 +
1.58 + @KOTest
1.59 + public void parseConvertToPeopleWithAddressOnArray() throws Exception {
1.60 + final BrwsrCtx c = newContext();
1.61 + final InputStream o = createIS(true, true, true);
1.62 +
1.63 + Person p = Models.parse(c, Person.class, o);
1.64 +
1.65 + assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
1.66 + assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
1.67 + assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
1.68 + assert p.getAddress() != null : "Some address provided";
1.69 + assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
1.70 + }
1.71 +
1.72 + @KOTest
1.73 + public void parseConvertToPeopleWithoutSexOnArray() throws Exception {
1.74 + final BrwsrCtx c = newContext();
1.75 + final InputStream o = createIS(false, false, true);
1.76 Person p = Models.parse(c, Person.class, o);
1.77
1.78 assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/JSON.java Mon Jul 21 11:28:13 2014 +0200
2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSON.java Tue Jul 22 17:55:18 2014 +0200
2.3 @@ -407,7 +407,12 @@
2.4 public static <T> T readStream(BrwsrCtx c, Class<T> modelClazz, InputStream data)
2.5 throws IOException {
2.6 Transfer tr = findTransfer(c);
2.7 - return read(c, modelClazz, tr.toJSON((InputStream)data));
2.8 + Object rawJSON = tr.toJSON((InputStream)data);
2.9 + if (rawJSON instanceof Object[]) {
2.10 + final Object[] arr = (Object[])rawJSON;
2.11 + rawJSON = arr.length > 0 ? arr[0] : null;
2.12 + }
2.13 + return read(c, modelClazz, rawJSON);
2.14 }
2.15 public static <T> T read(BrwsrCtx c, Class<T> modelClazz, Object data) {
2.16 if (data == null) {
3.1 --- a/ko-ws-tyrus/src/main/java/org/netbeans/html/wstyrus/LoadJSON.java Mon Jul 21 11:28:13 2014 +0200
3.2 +++ b/ko-ws-tyrus/src/main/java/org/netbeans/html/wstyrus/LoadJSON.java Tue Jul 22 17:55:18 2014 +0200
3.3 @@ -123,43 +123,15 @@
3.4 final PushbackInputStream is = new PushbackInputStream(
3.5 conn.getInputStream(), 1
3.6 );
3.7 - boolean array = false;
3.8 - boolean string = false;
3.9 - if (call.isJSONP()) {
3.10 - for (;;) {
3.11 - int ch = is.read();
3.12 - if (ch == -1) {
3.13 - break;
3.14 - }
3.15 - if (ch == '[') {
3.16 - is.unread(ch);
3.17 - array = true;
3.18 - break;
3.19 - }
3.20 - if (ch == '{') {
3.21 - is.unread(ch);
3.22 - break;
3.23 - }
3.24 - }
3.25 - } else {
3.26 - int ch = is.read();
3.27 - if (ch == -1) {
3.28 - string = true;
3.29 - } else {
3.30 - array = ch == '[';
3.31 - is.unread(ch);
3.32 - if (!array && ch != '{') {
3.33 - string = true;
3.34 - }
3.35 - }
3.36 - }
3.37 + boolean[] arrayOrString = { false, false };
3.38 + detectJSONType(call.isJSONP(), is, arrayOrString);
3.39 try {
3.40 - if (string) {
3.41 + if (arrayOrString[1]) {
3.42 throw new JSONException("");
3.43 }
3.44 JSONTokener tok = createTokener(is);
3.45 Object obj;
3.46 - obj = array ? new JSONArray(tok) : new JSONObject(tok);
3.47 + obj = arrayOrString[0] ? new JSONArray(tok) : new JSONObject(tok);
3.48 json = convertToArray(obj);
3.49 } catch (JSONException ex) {
3.50 Reader r = new InputStreamReader(is, "UTF-8");
3.51 @@ -184,6 +156,34 @@
3.52 }
3.53 }
3.54
3.55 + private static void detectJSONType(boolean skipAnything, final PushbackInputStream is, boolean[] arrayOrString) throws IOException {
3.56 + for (;;) {
3.57 + int ch = is.read();
3.58 + if (ch == -1) {
3.59 + arrayOrString[1] = true;
3.60 + break;
3.61 + }
3.62 + if (Character.isWhitespace(ch)) {
3.63 + continue;
3.64 + }
3.65 +
3.66 + if (ch == '[') {
3.67 + is.unread(ch);
3.68 + arrayOrString[0] = true;
3.69 + break;
3.70 + }
3.71 + if (ch == '{') {
3.72 + is.unread(ch);
3.73 + break;
3.74 + }
3.75 + if (!skipAnything) {
3.76 + is.unread(ch);
3.77 + arrayOrString[1] = true;
3.78 + break;
3.79 + }
3.80 + }
3.81 + }
3.82 +
3.83 private static JSONTokener createTokener(InputStream is) throws IOException {
3.84 Reader r = new InputStreamReader(is, "UTF-8");
3.85 try {
3.86 @@ -252,8 +252,12 @@
3.87
3.88 public static Object parse(InputStream is) throws IOException {
3.89 try {
3.90 - JSONTokener t = createTokener(is);
3.91 - return new JSONObject(t);
3.92 + PushbackInputStream push = new PushbackInputStream(is, 1);
3.93 + boolean[] arrayOrString = { false, false };
3.94 + detectJSONType(false, push, arrayOrString);
3.95 + JSONTokener t = createTokener(push);
3.96 + Object obj = arrayOrString[0] ? new JSONArray(t) : new JSONObject(t);
3.97 + return convertToArray(obj);
3.98 } catch (JSONException ex) {
3.99 throw new IOException(ex);
3.100 }