1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/FromJSON.java Tue Apr 23 13:30:02 2013 +0200
1.3 @@ -0,0 +1,33 @@
1.4 +/**
1.5 + * HTML via Java(tm) Language Bindings
1.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details. apidesign.org
1.16 + * designates this particular file as subject to the
1.17 + * "Classpath" exception as provided by apidesign.org
1.18 + * in the License file that accompanied this code.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License
1.21 + * along with this program. Look for COPYING file in the top folder.
1.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
1.23 + */
1.24 +
1.25 +package org.apidesign.html.json.impl;
1.26 +
1.27 +import net.java.html.json.Context;
1.28 +
1.29 +/**
1.30 + *
1.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.32 + */
1.33 +public interface FromJSON<Data> {
1.34 + public Class<Data> factoryFor();
1.35 + public Data read(Context c, Object d);
1.36 +}
2.1 --- a/json/src/main/java/org/apidesign/html/json/impl/JSON.java Tue Apr 23 13:19:14 2013 +0200
2.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/JSON.java Tue Apr 23 13:30:02 2013 +0200
2.3 @@ -20,6 +20,9 @@
2.4 */
2.5 package org.apidesign.html.json.impl;
2.6
2.7 +import java.util.HashMap;
2.8 +import java.util.Map;
2.9 +import java.util.WeakHashMap;
2.10 import net.java.html.json.Context;
2.11 import org.apidesign.html.json.spi.JSONCall;
2.12 import org.apidesign.html.json.spi.Transfer;
2.13 @@ -87,4 +90,26 @@
2.14 Transfer t = ContextAccessor.findTransfer(c);
2.15 t.loadJSON(call);
2.16 }
2.17 +
2.18 + private static final Map<Class,FromJSON<?>> froms;
2.19 + static {
2.20 + Map<Class,FromJSON<?>> m;
2.21 + try {
2.22 + m = new WeakHashMap<>();
2.23 + } catch (LinkageError ex) {
2.24 + m = new HashMap<>();
2.25 + }
2.26 + froms = m;
2.27 + }
2.28 + public static void register(FromJSON<?> from) {
2.29 + froms.put(from.getClass(), from);
2.30 + }
2.31 +
2.32 + public static <T> T read(Context c, Class<T> modelClazz, Object data) {
2.33 + FromJSON<?> from = froms.get(modelClazz);
2.34 + if (from == null) {
2.35 + throw new NullPointerException();
2.36 + }
2.37 + return modelClazz.cast(from.read(c, data));
2.38 + }
2.39 }
3.1 --- a/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java Tue Apr 23 13:19:14 2013 +0200
3.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/ModelProcessor.java Tue Apr 23 13:30:02 2013 +0200
3.3 @@ -199,7 +199,8 @@
3.4 w.append(" return ko;\n");
3.5 w.append(" };\n");
3.6 w.append(" private static final class P implements org.apidesign.html.json.impl.SetAndGet<" + className + ">,\n");
3.7 - w.append(" org.apidesign.html.json.impl.Callback<" + className + "> {\n");
3.8 + w.append(" org.apidesign.html.json.impl.Callback<" + className + ">,\n");
3.9 + w.append(" org.apidesign.html.json.impl.FromJSON<" + className + "> {\n");
3.10 w.append(" private final int type;\n");
3.11 w.append(" P(int t) { type = t; };\n");
3.12 w.append(" public void setValue(" + className + " data, Object value) {\n");
3.13 @@ -233,8 +234,11 @@
3.14 w.append(" }\n");
3.15 w.append(" throw new UnsupportedOperationException();\n");
3.16 w.append(" }\n");
3.17 + w.append(" public Class<" + className + "> factoryFor() { return " + className + ".class; }\n");
3.18 + w.append(" public " + className + " read(Context c, Object json) { return new " + className + "(c, json); }\n");
3.19 w.append(" }\n");
3.20 - w.append(" ").append(className).append("(Context c, Object json) {\n");
3.21 + w.append(" static { org.apidesign.html.json.impl.JSON.register(new P(0)); }\n");
3.22 + w.append(" private ").append(className).append("(Context c, Object json) {\n");
3.23 w.append(" this.context = c;\n");
3.24 int values = 0;
3.25 for (int i = 0; i < propsGetSet.size(); i += 5) {
3.26 @@ -268,8 +272,8 @@
3.27 w.append("if (ret[" + cnt + "] instanceof Object[]) {\n");
3.28 w.append(" for (Object e : ((Object[])ret[" + cnt + "])) {\n");
3.29 if (isModel[0]) {
3.30 - w.append(" this.prop_").append(pn).append(".add(new ");
3.31 - w.append(type).append("(c, e));\n");
3.32 + w.append(" this.prop_").append(pn).append(".add(org.apidesign.html.json.impl.JSON.read");
3.33 + w.append("(c, " + type + ".class, e));\n");
3.34 } else if (isEnum[0]) {
3.35 w.append(" this.prop_").append(pn);
3.36 w.append(".add(e == null ? null : ");
3.37 @@ -777,11 +781,11 @@
3.38 " Object[] data = ((Object[])value);\n" +
3.39 " arr = new " + modelClass + "[data.length];\n" +
3.40 " for (int i = 0; i < data.length; i++) {\n" +
3.41 - " arr[i] = new " + modelClass + "(context, data[i]);\n" +
3.42 + " arr[i] = org.apidesign.html.json.impl.JSON.read(context, " + modelClass + ".class, data[i]);\n" +
3.43 " }\n" +
3.44 " } else {\n" +
3.45 " arr = new " + modelClass + "[1];\n" +
3.46 - " arr[0] = new " + modelClass + "(context, value);\n" +
3.47 + " arr[0] = org.apidesign.html.json.impl.JSON.read(context, " + modelClass + ".class, value);\n" +
3.48 " }\n"
3.49 );
3.50 {
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/json/src/test/java/org/apidesign/html/json/impl/EmployeeImpl.java Tue Apr 23 13:30:02 2013 +0200
4.3 @@ -0,0 +1,42 @@
4.4 +/**
4.5 + * HTML via Java(tm) Language Bindings
4.6 + * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
4.7 + *
4.8 + * This program is free software: you can redistribute it and/or modify
4.9 + * it under the terms of the GNU General Public License as published by
4.10 + * the Free Software Foundation, version 2 of the License.
4.11 + *
4.12 + * This program is distributed in the hope that it will be useful,
4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4.15 + * GNU General Public License for more details. apidesign.org
4.16 + * designates this particular file as subject to the
4.17 + * "Classpath" exception as provided by apidesign.org
4.18 + * in the License file that accompanied this code.
4.19 + *
4.20 + * You should have received a copy of the GNU General Public License
4.21 + * along with this program. Look for COPYING file in the top folder.
4.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
4.23 + */
4.24 +
4.25 +package org.apidesign.html.json.impl;
4.26 +
4.27 +import net.java.html.json.Model;
4.28 +import net.java.html.json.OnReceive;
4.29 +import net.java.html.json.Person;
4.30 +import net.java.html.json.Property;
4.31 +
4.32 +/**
4.33 + *
4.34 + * @author Jaroslav Tulach <jtulach@netbeans.org>
4.35 + */
4.36 +@Model(className = "Employee", properties = {
4.37 + @Property(name = "person", type = Person.class),
4.38 + @Property(name = "employer", type = String.class)
4.39 +})
4.40 +public class EmployeeImpl {
4.41 + @OnReceive(url = "some/url")
4.42 + static void changePersonality(Employee e, Person p) {
4.43 + e.setPerson(p);
4.44 + }
4.45 +}