Conversion method from raw JSON object o @Model wrapper
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Nov 2013 13:31:14 +0100
changeset 326b737cd5286c6
parent 325 3dc22f6fa03f
child 327 2ed628de0f06
Conversion method from raw JSON object o @Model wrapper
json/src/main/java/net/java/html/json/Models.java
json/src/test/java/org/apidesign/html/json/impl/EmployerTest.java
     1.1 --- a/json/src/main/java/net/java/html/json/Models.java	Wed Nov 06 16:23:32 2013 +0100
     1.2 +++ b/json/src/main/java/net/java/html/json/Models.java	Thu Nov 07 13:31:14 2013 +0100
     1.3 @@ -23,7 +23,6 @@
     1.4  import net.java.html.BrwsrCtx;
     1.5  import java.io.IOException;
     1.6  import java.io.InputStream;
     1.7 -import java.lang.reflect.Method;
     1.8  import org.apidesign.html.json.impl.JSON;
     1.9  
    1.10  /** Information about and 
    1.11 @@ -71,4 +70,29 @@
    1.12      public static <M> M parse(BrwsrCtx c, Class<M> model, InputStream is) throws IOException {
    1.13          return JSON.readStream(c, model, is);
    1.14      }
    1.15 +    
    1.16 +    /** Converts an existing, raw, JSON object into a {@link Model model class}.
    1.17 +     * 
    1.18 +     * @param <M> the type of the model class
    1.19 +     * @param ctx context of the technology to use for converting
    1.20 +     * @param model the model class
    1.21 +     * @param jsonObject original instance of the JSON object
    1.22 +     * @return new instance of the model class
    1.23 +     * @since 0.7
    1.24 +     */
    1.25 +    public static <M> M fromRaw(BrwsrCtx ctx, Class<M> model, Object jsonObject) {
    1.26 +        return JSON.read(ctx, model, jsonObject);
    1.27 +    }
    1.28 +    
    1.29 +//    /** Converts an existing {@link Model model} into its associated, raw 
    1.30 +//     * JSON object. The object may, but does not have to, be the same instance
    1.31 +//     * as the model object.
    1.32 +//     * 
    1.33 +//     * @param model the model object
    1.34 +//     * @return the raw JSON object associated with the model
    1.35 +//     * @since 0.7
    1.36 +//     */
    1.37 +//    public static Object toRaw(Object model) {
    1.38 +//        return JSON.toJSON(model);
    1.39 +//    }
    1.40  }
     2.1 --- a/json/src/test/java/org/apidesign/html/json/impl/EmployerTest.java	Wed Nov 06 16:23:32 2013 +0100
     2.2 +++ b/json/src/test/java/org/apidesign/html/json/impl/EmployerTest.java	Thu Nov 07 13:31:14 2013 +0100
     2.3 @@ -22,6 +22,7 @@
     2.4  
     2.5  import net.java.html.BrwsrCtx;
     2.6  import net.java.html.json.Model;
     2.7 +import net.java.html.json.Models;
     2.8  import net.java.html.json.Property;
     2.9  import org.testng.Assert;
    2.10  import org.testng.annotations.Test;
    2.11 @@ -35,7 +36,7 @@
    2.12  })
    2.13  public class EmployerTest {
    2.14      @Test public void preLoadsTheClass() {
    2.15 -        Employer em = JSON.read(BrwsrCtx.EMPTY, Employer.class, this);
    2.16 +        Employer em = Models.fromRaw(BrwsrCtx.EMPTY, Employer.class, this);
    2.17          Assert.assertNotNull(em, "Class loaded");
    2.18          em.applyBindings();
    2.19      }