Throw EOFEx when the array is empty JsonArray
authorJaroslav Tulach <jtulach@netbeans.org>
Tue, 22 Jul 2014 18:06:57 +0200
branchJsonArray
changeset 7465a1cc4708c34
parent 745 4f12b1d9c695
child 747 df2a61ad64e2
Throw EOFEx when the array is empty
json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java
json/src/main/java/org/apidesign/html/json/spi/Transfer.java
json/src/main/java/org/netbeans/html/json/impl/JSON.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Tue Jul 22 17:55:18 2014 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Tue Jul 22 18:06:57 2014 +0200
     1.3 @@ -43,6 +43,7 @@
     1.4  package net.java.html.json.tests;
     1.5  
     1.6  import java.io.ByteArrayInputStream;
     1.7 +import java.io.EOFException;
     1.8  import java.io.InputStream;
     1.9  import java.io.UnsupportedEncodingException;
    1.10  import java.util.HashMap;
    1.11 @@ -171,6 +172,20 @@
    1.12          assert p.getSex() == null : "No sex: " + p.getSex();
    1.13      }
    1.14      
    1.15 +    @KOTest
    1.16 +    public void parseOnEmptyArray() throws Exception {
    1.17 +        final BrwsrCtx c = newContext();
    1.18 +        final InputStream o = new ByteArrayInputStream("[]".getBytes("UTF-8"));
    1.19 +        
    1.20 +        try {
    1.21 +            Models.parse(c, Person.class, o);
    1.22 +        } catch (EOFException ex) {
    1.23 +            // OK
    1.24 +            return;
    1.25 +        }
    1.26 +        throw new IllegalStateException("Should throw end of file exception, as the array is empty");
    1.27 +    }
    1.28 +    
    1.29      private static BrwsrCtx newContext() {
    1.30          return Utils.newContext(ConvertTypesTest.class);
    1.31      }
     2.1 --- a/json/src/main/java/org/apidesign/html/json/spi/Transfer.java	Tue Jul 22 17:55:18 2014 +0200
     2.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/Transfer.java	Tue Jul 22 18:06:57 2014 +0200
     2.3 @@ -68,7 +68,10 @@
     2.4      /** Reads content of a stream and creates its JSON representation.
     2.5       * The returned object is implementation dependant. It however needs
     2.6       * to be acceptable as first argument of {@link #extract(java.lang.Object, java.lang.String[], java.lang.Object[]) extract}
     2.7 -     * method.
     2.8 +     * method. If the stream contains representation or a JSON array,
     2.9 +     * an Object[] should be returned - each of its members should, by itself
    2.10 +     * be acceptable argument to 
    2.11 +     * the {@link #extract(java.lang.Object, java.lang.String[], java.lang.Object[]) extract} method.
    2.12       * 
    2.13       * @param is input stream to read data from
    2.14       * @return an object representing the JSON data
     3.1 --- a/json/src/main/java/org/netbeans/html/json/impl/JSON.java	Tue Jul 22 17:55:18 2014 +0200
     3.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSON.java	Tue Jul 22 18:06:57 2014 +0200
     3.3 @@ -42,6 +42,7 @@
     3.4   */
     3.5  package org.netbeans.html.json.impl;
     3.6  
     3.7 +import java.io.EOFException;
     3.8  import java.io.IOException;
     3.9  import java.io.InputStream;
    3.10  import java.util.Collection;
    3.11 @@ -410,7 +411,10 @@
    3.12          Object rawJSON = tr.toJSON((InputStream)data);
    3.13          if (rawJSON instanceof Object[]) {
    3.14              final Object[] arr = (Object[])rawJSON;
    3.15 -            rawJSON = arr.length > 0 ? arr[0] : null;
    3.16 +            if (arr.length == 0) {
    3.17 +                throw new EOFException("Recieved an empty array");
    3.18 +            }
    3.19 +            rawJSON = arr[0];
    3.20          }
    3.21          return read(c, modelClazz, rawJSON);
    3.22      }