Support for singular subobject in a property of a JSON object classloader
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 20 Jun 2013 12:51:26 +0200
branchclassloader
changeset 136c2a7fea090ae
parent 135 645fb6e38a40
child 137 936e2d60ca03
Support for singular subobject in a property of a JSON object
json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java
json-tck/src/main/java/net/java/html/json/tests/PersonImpl.java
json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java
     1.1 --- a/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Thu Jun 20 12:50:03 2013 +0200
     1.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/ConvertTypesTest.java	Thu Jun 20 12:51:26 2013 +0200
     1.3 @@ -36,13 +36,16 @@
     1.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     1.5   */
     1.6  public final class ConvertTypesTest {
     1.7 -    private static InputStream createIS(boolean includeSex) 
     1.8 +    private static InputStream createIS(boolean includeSex, boolean includeAddress) 
     1.9      throws UnsupportedEncodingException {
    1.10          StringBuilder sb = new StringBuilder();
    1.11          sb.append("{ \"firstName\" : \"son\",\n");
    1.12          sb.append("  \"lastName\" : \"dj\" \n");
    1.13          if (includeSex) {
    1.14 -            sb.append(",  \"sex\" : \"MALE\" ");
    1.15 +            sb.append(",  \"sex\" : \"MALE\" \n");
    1.16 +        }
    1.17 +        if (includeAddress) {
    1.18 +            sb.append(",  \"address\" : { \"street\" : \"Schnirchova\" } \n");
    1.19          }
    1.20          sb.append("}\n");
    1.21          return new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
    1.22 @@ -72,7 +75,7 @@
    1.23      @BrwsrTest
    1.24      public void parseConvertToPeople() throws Exception {
    1.25          final BrwsrCtx c = newContext();
    1.26 -        final InputStream o = createIS(true);
    1.27 +        final InputStream o = createIS(true, false);
    1.28          
    1.29          Person p = Models.parse(c, Person.class, o);
    1.30          
    1.31 @@ -80,6 +83,20 @@
    1.32          assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
    1.33          assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
    1.34      }
    1.35 +    
    1.36 +    @BrwsrTest
    1.37 +    public void parseConvertToPeopleWithAddress() throws Exception {
    1.38 +        final BrwsrCtx c = newContext();
    1.39 +        final InputStream o = createIS(true, true);
    1.40 +        
    1.41 +        Person p = Models.parse(c, Person.class, o);
    1.42 +        
    1.43 +        assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
    1.44 +        assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
    1.45 +        assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
    1.46 +        assert p.getAddress() != null : "Some address provided";
    1.47 +        assert p.getAddress().getStreet().equals("Schnirchova") : "Is Schnirchova: " + p.getAddress();
    1.48 +    }
    1.49  
    1.50      @BrwsrTest
    1.51      public void testConvertToPeopleWithoutSex() throws Exception {
    1.52 @@ -95,7 +112,7 @@
    1.53      @BrwsrTest
    1.54      public void parseConvertToPeopleWithoutSex() throws Exception {
    1.55          final BrwsrCtx c = newContext();
    1.56 -        final InputStream o = createIS(false);
    1.57 +        final InputStream o = createIS(false, false);
    1.58          Person p = Models.parse(c, Person.class, o);
    1.59          
    1.60          assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
     2.1 --- a/json-tck/src/main/java/net/java/html/json/tests/PersonImpl.java	Thu Jun 20 12:50:03 2013 +0200
     2.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/PersonImpl.java	Thu Jun 20 12:51:26 2013 +0200
     2.3 @@ -32,7 +32,8 @@
     2.4  @Model(className = "Person", properties = {
     2.5      @Property(name = "firstName", type = String.class),
     2.6      @Property(name = "lastName", type = String.class),
     2.7 -    @Property(name = "sex", type = Sex.class)
     2.8 +    @Property(name = "sex", type = Sex.class),
     2.9 +    @Property(name = "address", type = Address.class)
    2.10  })
    2.11  final class PersonImpl {
    2.12      @ComputedProperty 
    2.13 @@ -62,4 +63,10 @@
    2.14      })
    2.15      public class PeopleImpl {
    2.16      }
    2.17 +    
    2.18 +    @Model(className = "Address", properties = {
    2.19 +        @Property(name = "street", type = String.class)
    2.20 +    })
    2.21 +    static class Addrss {
    2.22 +    }
    2.23  }
     3.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Thu Jun 20 12:50:03 2013 +0200
     3.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java	Thu Jun 20 12:51:26 2013 +0200
     3.3 @@ -325,7 +325,11 @@
     3.4                              }
     3.5                              w.append("ret[" + cnt + "]).");
     3.6                              w.append(type).append("Value();\n");
     3.7 -                        } else {
     3.8 +                        } else if (isModel[0]) {
     3.9 +                            w.append("    this.prop_").append(pn).append(" = org.apidesign.html.json.impl.JSON.read");
    3.10 +                            w.append("(c, " + type + ".class, ");
    3.11 +                            w.append("ret[" + cnt + "]);\n");
    3.12 +                        }else {
    3.13                              w.append("    this.prop_").append(pn);
    3.14                              w.append(" = (").append(type).append(')');
    3.15                              w.append("ret[" + cnt + "];\n");