1.1 --- a/json/src/main/java/org/apidesign/html/json/spi/Proto.java Mon Dec 23 16:41:47 2013 +0100
1.2 +++ b/json/src/main/java/org/apidesign/html/json/spi/Proto.java Mon Dec 23 20:39:24 2013 +0100
1.3 @@ -117,7 +117,17 @@
1.4 public Bindings getBindings() {
1.5 return ko;
1.6 }
1.7 +
1.8 + public String toString(Object data, String propName) {
1.9 + return JSON.toString(context, data, propName);
1.10 + }
1.11 + public Number toNumber(Object data, String propName) {
1.12 + return JSON.toNumber(context, data, propName);
1.13 + }
1.14
1.15 + public <T> T toModel(Class<T> type, Object data, String propName) {
1.16 + return JSON.toModel(context, type, data, propName);
1.17 + }
1.18
1.19 /** Functionality used by the code generated by annotation
1.20 * processor for the {@link net.java.html.json.Model} annotation.
1.21 @@ -164,5 +174,135 @@
1.22 public abstract void call(Model model, int index, Object data, Object event);
1.23 public abstract Model cloneTo(Object model, BrwsrCtx ctx);
1.24 public abstract Model read(BrwsrCtx c, Object json);
1.25 +
1.26 + //
1.27 + // Various support methods the generated classes use
1.28 + //
1.29 +
1.30 + /** Compares two objects that can be converted to integers.
1.31 + * @return true if they are the same
1.32 + */
1.33 + public final boolean isSame(int a, int b) {
1.34 + return a == b;
1.35 + }
1.36 +
1.37 + /** Compares two objects that can be converted to (floating point)
1.38 + * numbers.
1.39 + * @return true if they are the same
1.40 + */
1.41 + public final boolean isSame(double a, double b) {
1.42 + return a == b;
1.43 + }
1.44 +
1.45 + /** Compares two objects for being the same - e.g. either <code>==</code>
1.46 + * or <code>equals</code>.
1.47 + * @return true if they are equals
1.48 + */
1.49 + public final boolean isSame(Object a, Object b) {
1.50 + if (a == b) {
1.51 + return true;
1.52 + }
1.53 + if (a == null || b == null) {
1.54 + return false;
1.55 + }
1.56 + return a.equals(b);
1.57 + }
1.58 +
1.59 + /** Cumulative hash function. Adds hashcode of the object to the
1.60 + * previous value.
1.61 + * @param o the object (or <code>null</code>)
1.62 + * @param h the previous value of the hash
1.63 + * @return new hash - the old one xor the object's one
1.64 + */
1.65 + public final int hashPlus(Object o, int h) {
1.66 + return o == null ? h : h ^ o.hashCode();
1.67 + }
1.68 +
1.69 + /** Converts an object to its JSON value.
1.70 + *
1.71 + * @param obj the object to convert
1.72 + * @return JSON representation of the object
1.73 + */
1.74 + public final String toJSON(Object obj) {
1.75 + return JSON.toJSON(obj);
1.76 + }
1.77 +
1.78 + /** Converts the value to string.
1.79 + *
1.80 + * @param val the value
1.81 + * @return the converted value
1.82 + */
1.83 + public final String stringValue(Object val) {
1.84 + return JSON.stringValue(val);
1.85 + }
1.86 +
1.87 + /** Converts the value to number.
1.88 + *
1.89 + * @param val the value
1.90 + * @return the converted value
1.91 + */
1.92 + public final Number numberValue(Object val) {
1.93 + return JSON.numberValue(val);
1.94 + }
1.95 +
1.96 + /** Converts the value to character.
1.97 + *
1.98 + * @param val the value
1.99 + * @return the converted value
1.100 + */
1.101 + public final Character charValue(Object val) {
1.102 + return JSON.charValue(val);
1.103 + }
1.104 +
1.105 + /** Converts the value to boolean.
1.106 + *
1.107 + * @param val the value
1.108 + * @return the converted value
1.109 + */
1.110 + public final Boolean boolValue(Object val) {
1.111 + return JSON.boolValue(val);
1.112 + }
1.113 +
1.114 + /** Extracts value of specific type from given object.
1.115 + *
1.116 + * @param <T> the type of object one is interested in
1.117 + * @param type the type
1.118 + * @param val the object to convert to type
1.119 + * @return the converted value
1.120 + */
1.121 + public final <T> T extractValue(Class<T> type, Object val) {
1.122 + if (Number.class.isAssignableFrom(type)) {
1.123 + val = numberValue(val);
1.124 + }
1.125 + if (Boolean.class == type) {
1.126 + val = boolValue(val);
1.127 + }
1.128 + if (String.class == type) {
1.129 + val = stringValue(val);
1.130 + }
1.131 + if (Character.class == type) {
1.132 + val = charValue(val);
1.133 + }
1.134 + if (Integer.class == type) {
1.135 + val = val instanceof Number ? ((Number) val).intValue() : 0;
1.136 + }
1.137 + if (Long.class == type) {
1.138 + val = val instanceof Number ? ((Number) val).longValue() : 0;
1.139 + }
1.140 + if (Short.class == type) {
1.141 + val = val instanceof Number ? ((Number) val).shortValue() : 0;
1.142 + }
1.143 + if (Byte.class == type) {
1.144 + val = val instanceof Number ? ((Number) val).byteValue() : 0;
1.145 + }
1.146 + if (Double.class == type) {
1.147 + val = val instanceof Number ? ((Number) val).doubleValue() : Double.NaN;
1.148 + }
1.149 + if (Float.class == type) {
1.150 + val = val instanceof Number ? ((Number) val).floatValue() : Float.NaN;
1.151 + }
1.152 + return type.cast(val);
1.153 + }
1.154 +
1.155 }
1.156 }
2.1 --- a/json/src/main/java/org/netbeans/html/json/impl/JSON.java Mon Dec 23 16:41:47 2013 +0100
2.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/JSON.java Mon Dec 23 20:39:24 2013 +0100
2.3 @@ -97,7 +97,7 @@
2.4 return val[0];
2.5 }
2.6
2.7 - public static Object toJSON(Object value) {
2.8 + public static String toJSON(Object value) {
2.9 if (value == null) {
2.10 return "null";
2.11 }
2.12 @@ -199,7 +199,7 @@
2.13 return type.cast(val);
2.14 }
2.15
2.16 - protected static boolean isNumeric(Object val) {
2.17 + static boolean isNumeric(Object val) {
2.18 return ((val instanceof Integer) || (val instanceof Long) || (val instanceof Short) || (val instanceof Byte));
2.19 }
2.20
2.21 @@ -218,7 +218,7 @@
2.22 }
2.23 return (String)val;
2.24 }
2.25 -
2.26 +
2.27 public static Number numberValue(Object val) {
2.28 if (val instanceof String) {
2.29 try {
3.1 --- a/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Mon Dec 23 16:41:47 2013 +0100
3.2 +++ b/json/src/main/java/org/netbeans/html/json/impl/ModelProcessor.java Mon Dec 23 20:39:24 2013 +0100
3.3 @@ -396,10 +396,10 @@
3.4 } else if (isEnum[0]) {
3.5 w.append(" this.prop_").append(pn);
3.6 w.append(".add(e == null ? null : ");
3.7 - w.append(type).append(".valueOf(org.netbeans.html.json.impl.JSON.stringValue(e)));\n");
3.8 + w.append(type).append(".valueOf(TYPE.stringValue(e)));\n");
3.9 } else {
3.10 if (isPrimitive(type)) {
3.11 - w.append(" this.prop_").append(pn).append(".add(org.netbeans.html.json.impl.JSON.numberValue(e).");
3.12 + w.append(" this.prop_").append(pn).append(".add(TYPE.numberValue(e).");
3.13 w.append(type).append("Value());\n");
3.14 } else {
3.15 w.append(" this.prop_").append(pn).append(".add((");
3.16 @@ -413,7 +413,7 @@
3.17 w.append(" try {\n");
3.18 w.append(" this.prop_").append(pn);
3.19 w.append(" = ret[" + cnt + "] == null ? null : ");
3.20 - w.append(type).append(".valueOf(org.netbeans.html.json.impl.JSON.stringValue(ret[" + cnt + "]));\n");
3.21 + w.append(type).append(".valueOf(TYPE.stringValue(ret[" + cnt + "]));\n");
3.22 w.append(" } catch (IllegalArgumentException ex) {\n");
3.23 w.append(" ex.printStackTrace();\n");
3.24 w.append(" }\n");
3.25 @@ -421,11 +421,11 @@
3.26 w.append(" this.prop_").append(pn);
3.27 w.append(" = ret[" + cnt + "] == null ? ");
3.28 if ("char".equals(type)) {
3.29 - w.append("0 : (org.netbeans.html.json.impl.JSON.charValue(");
3.30 + w.append("0 : (TYPE.charValue(");
3.31 } else if ("boolean".equals(type)) {
3.32 - w.append("false : (org.netbeans.html.json.impl.JSON.boolValue(");
3.33 + w.append("false : (TYPE.boolValue(");
3.34 } else {
3.35 - w.append("0 : (org.netbeans.html.json.impl.JSON.numberValue(");
3.36 + w.append("0 : (TYPE.numberValue(");
3.37 }
3.38 w.append("ret[" + cnt + "])).");
3.39 w.append(type).append("Value();\n");
3.40 @@ -464,14 +464,14 @@
3.41 w.write(" if (!(o instanceof " + className + ")) return false;\n");
3.42 w.write(" " + className + " p = (" + className + ")o;\n");
3.43 for (Prprt p : props) {
3.44 - w.write(" if (!org.netbeans.html.json.impl.JSON.isSame(prop_" + p.name() + ", p.prop_" + p.name() + ")) return false;\n");
3.45 + w.write(" if (!TYPE.isSame(prop_" + p.name() + ", p.prop_" + p.name() + ")) return false;\n");
3.46 }
3.47 w.write(" return true;\n");
3.48 w.write(" }\n");
3.49 w.write(" public int hashCode() {\n");
3.50 w.write(" int h = " + className + ".class.getName().hashCode();\n");
3.51 for (Prprt p : props) {
3.52 - w.write(" h = org.netbeans.html.json.impl.JSON.hashPlus(prop_" + p.name() + ", h);\n");
3.53 + w.write(" h = TYPE.hashPlus(prop_" + p.name() + ", h);\n");
3.54 }
3.55 w.write(" return h;\n");
3.56 w.write(" }\n");
3.57 @@ -517,7 +517,7 @@
3.58 w.write(" }\n");
3.59 w.write(" public void " + gs[1] + "(" + tn + " v) {\n");
3.60 w.write(" proto.checkLock();\n");
3.61 - w.write(" if (org.netbeans.html.json.impl.JSON.isSame(prop_" + p.name() + ", v)) return;\n");
3.62 + w.write(" if (TYPE.isSame(prop_" + p.name() + ", v)) return;\n");
3.63 w.write(" prop_" + p.name() + " = v;\n");
3.64 w.write(" proto.valueHasMutated(\"" + p.name() + "\");\n");
3.65 Collection<String> dependants = deps.get(p.name());
3.66 @@ -1221,18 +1221,18 @@
3.67 params.append('"').append(id).append('"');
3.68 continue;
3.69 }
3.70 - toCall = "org.netbeans.html.json.impl.JSON.toString(proto.getContext(), ";
3.71 + toCall = "proto.toString(";
3.72 }
3.73 if (ve.asType().getKind() == TypeKind.DOUBLE) {
3.74 - toCall = "org.netbeans.html.json.impl.JSON.toNumber(proto.getContext(), ";
3.75 + toCall = "proto.toNumber(";
3.76 toFinish = ".doubleValue()";
3.77 }
3.78 if (ve.asType().getKind() == TypeKind.INT) {
3.79 - toCall = "org.netbeans.html.json.impl.JSON.toNumber(proto.getContext(), ";
3.80 + toCall = "proto.toNumber(";
3.81 toFinish = ".intValue()";
3.82 }
3.83 if (dataName != null && ve.getSimpleName().contentEquals(dataName) && isModel(ve.asType())) {
3.84 - toCall = "org.netbeans.html.json.impl.JSON.toModel(proto.getContext(), " + ve.asType() + ".class, ";
3.85 + toCall = "proto.toModel(" + ve.asType() + ".class, ";
3.86 }
3.87
3.88 if (toCall != null) {
3.89 @@ -1341,7 +1341,7 @@
3.90 w.write(sep);
3.91 w.append(" sb.append('\"').append(\"" + p.name() + "\")");
3.92 w.append(".append('\"').append(\":\");\n");
3.93 - w.append(" sb.append(org.netbeans.html.json.impl.JSON.toJSON(prop_");
3.94 + w.append(" sb.append(TYPE.toJSON(prop_");
3.95 w.append(p.name()).append("));\n");
3.96 sep = " sb.append(',');\n";
3.97 }
4.1 --- a/json/src/test/java/org/netbeans/html/json/impl/JSONTest.java Mon Dec 23 16:41:47 2013 +0100
4.2 +++ b/json/src/test/java/org/netbeans/html/json/impl/JSONTest.java Mon Dec 23 20:39:24 2013 +0100
4.3 @@ -42,7 +42,6 @@
4.4 */
4.5 package org.netbeans.html.json.impl;
4.6
4.7 -import org.netbeans.html.json.impl.JSON;
4.8 import static org.testng.Assert.*;
4.9 import org.testng.annotations.Test;
4.10