Handle 'null' property values JsonArray
authorJaroslav Tulach <jtulach@netbeans.org>
Thu, 24 Jul 2014 16:11:59 +0200
branchJsonArray
changeset 74918514ccc3ed1
parent 748 38650a8eeec7
child 750 166bc67759d0
Handle 'null' property values
json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java
ko-ws-tyrus/src/main/java/org/netbeans/html/wstyrus/LoadJSON.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Thu Jul 24 15:48:44 2014 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Thu Jul 24 16:11:59 2014 +0200
     1.3 @@ -153,6 +153,21 @@
     1.4          assert p.getAddress() != null : "Some address provided";
     1.5          assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
     1.6      }
     1.7 +    
     1.8 +    @KOTest 
     1.9 +    public void parseNullValue() throws Exception {
    1.10 +        final BrwsrCtx c = newContext();
    1.11 +        
    1.12 +        StringBuilder sb = new StringBuilder();
    1.13 +        sb.append("{ \"firstName\" : \"son\",\n");
    1.14 +        sb.append("  \"lastName\" : null } \n");  
    1.15 +        
    1.16 +        final ByteArrayInputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
    1.17 +        Person p = Models.parse(c, Person.class, is);
    1.18 +
    1.19 +        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
    1.20 +        assert null == p.getLastName() : "Last name: " + p.getLastName();
    1.21 +    }
    1.22  
    1.23      @KOTest
    1.24      public void testConvertToPeopleWithoutSex() throws Exception {
     2.1 --- a/ko-ws-tyrus/src/main/java/org/netbeans/html/wstyrus/LoadJSON.java	Thu Jul 24 15:48:44 2014 +0200
     2.2 +++ b/ko-ws-tyrus/src/main/java/org/netbeans/html/wstyrus/LoadJSON.java	Thu Jul 24 16:11:59 2014 +0200
     2.3 @@ -227,11 +227,11 @@
     2.4          if (jsonObject instanceof JSONObject) {
     2.5              JSONObject obj = (JSONObject)jsonObject;
     2.6              for (int i = 0; i < props.length; i++) {
     2.7 -                try {
     2.8 -                    values[i] = obj.has(props[i]) ? obj.get(props[i]) : null;
     2.9 -                } catch (JSONException ex) {
    2.10 -                    LoadJSON.LOG.log(Level.SEVERE, "Can't read " + props[i] + " from " + jsonObject, ex);
    2.11 +                Object val = obj.opt(props[i]);
    2.12 +                if (val == JSONObject.NULL) {
    2.13 +                    val = null;
    2.14                  }
    2.15 +                values[i] = val;
    2.16              }
    2.17              return;
    2.18          }