import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
+import com.google.gwt.json.client.JSONValue;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Connector;
import com.vaadin.terminal.gwt.client.ConnectorMap;
return decodeValue(type, jsonArray.get(1), target, idMapper, connection);
}
- private static Object decodeValue(String variableType, Object value,
+ private static Object decodeValue(String variableType, JSONValue value,
Object target, ConnectorMap idMapper,
ApplicationConnection connection) {
Object val = null;
} else if (JsonEncoder.VTYPE_CONNECTOR.equals(variableType)) {
val = idMapper.getConnector(((JSONString) value).stringValue());
} else {
- return decodeObject(variableType, (JSONObject) value, target,
- idMapper, connection);
+ return decodeObject(variableType, value, target, idMapper,
+ connection);
}
return val;
}
private static Object decodeObject(String variableType,
- JSONObject encodedValue, Object target, ConnectorMap idMapper,
+ JSONValue encodedValue, Object target, ConnectorMap idMapper,
ApplicationConnection connection) {
// object, class name as type
JSONSerializer<Object> serializer = connection.getSerializerMap()
import com.google.gwt.core.ext.TreeLogger.Type;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
+import com.google.gwt.core.ext.typeinfo.JEnumConstant;
+import com.google.gwt.core.ext.typeinfo.JEnumType;
import com.google.gwt.core.ext.typeinfo.JMethod;
import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
import com.google.gwt.core.ext.typeinfo.JType;
import com.google.gwt.core.ext.typeinfo.TypeOracle;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
+import com.google.gwt.json.client.JSONString;
+import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
import com.google.gwt.user.rebind.SourceWriter;
import com.vaadin.terminal.gwt.client.ApplicationConnection;
if (printWriter == null) {
return;
}
+ boolean isEnum = (beanType.isEnum() != null);
+
Date date = new Date();
TypeOracle typeOracle = context.getTypeOracle();
String beanQualifiedSourceName = beanType.getQualifiedSourceName();
// public JSONValue serialize(Object value, ConnectorMap idMapper,
// ApplicationConnection connection) {
- sourceWriter.println("public " + JSONObject.class.getName()
+ sourceWriter.println("public " + JSONValue.class.getName()
+ " serialize(" + beanQualifiedSourceName + " value, "
+ ConnectorMap.class.getName() + " idMapper, "
+ ApplicationConnection.class.getName() + " connection) {");
// MouseEventDetails castedValue = (MouseEventDetails) value;
sourceWriter.println(beanQualifiedSourceName + " castedValue = ("
+ beanQualifiedSourceName + ") value;");
- // JSONObject json = new JSONObject();
- sourceWriter.println(JSONObject.class.getName() + " json = new "
- + JSONObject.class.getName() + "();");
- for (JMethod setterMethod : getSetters(beanType)) {
- String setterName = setterMethod.getName();
- String fieldName = setterName.substring(3); // setZindex() -> ZIndex
- String getterName = findGetter(beanType, setterMethod);
-
- if (getterName == null) {
- logger.log(TreeLogger.ERROR, "No getter found for " + fieldName
- + ". Serialization will likely fail");
- }
- // json.put("button",
- // JsonEncoder.encode(castedValue.getButton(), idMapper,
- // connection));
- sourceWriter.println("json.put(\"" + fieldName + "\", "
- + JsonEncoder.class.getName() + ".encode(castedValue."
- + getterName + "(), idMapper, connection));");
+ if (isEnum) {
+ writeEnumSerializer(logger, sourceWriter, beanType);
+ } else {
+ writeBeanSerializer(logger, sourceWriter, beanType);
}
- // return json;
- sourceWriter.println("return json;");
// }
sourceWriter.println("}");
// Deserializer
sourceWriter.println("public " + beanQualifiedSourceName
- + " deserialize(" + JSONObject.class.getName() + " jsonValue, "
+ + " deserialize(" + JSONValue.class.getName() + " jsonValue, "
+ beanQualifiedSourceName + " target, "
+ ConnectorMap.class.getName() + " idMapper, "
+ ApplicationConnection.class.getName() + " connection) {");
sourceWriter.indent();
+ if (isEnum) {
+ writeEnumDeserializer(logger, sourceWriter, beanType.isEnum());
+ } else {
+ writeBeanDeserializer(logger, sourceWriter, beanType);
+ }
+ sourceWriter.println("}");
+ sourceWriter.outdent();
+
+ // End of class
+ sourceWriter.println("}");
+ sourceWriter.outdent();
+
+ // commit generated class
+ context.commit(logger, printWriter);
+ logger.log(TreeLogger.INFO, "Generated Serializer class "
+ + getFullyQualifiedSerializerClassName(beanType));
+ }
+
+ private void writeEnumDeserializer(TreeLogger logger,
+ SourceWriter sourceWriter, JEnumType enumType) {
+ sourceWriter.println("String enumIdentifier = (("
+ + JSONString.class.getName() + ")jsonValue).stringValue();");
+ for (JEnumConstant e : enumType.getEnumConstants()) {
+ sourceWriter.println("if (\"" + e.getName()
+ + "\".equals(enumIdentifier)) {");
+ sourceWriter.indent();
+ sourceWriter.println("return " + enumType.getQualifiedSourceName()
+ + "." + e.getName() + ";");
+ sourceWriter.outdent();
+ sourceWriter.println("}");
+ }
+ sourceWriter.println("return null;");
+ }
+
+ private void writeBeanDeserializer(TreeLogger logger,
+ SourceWriter sourceWriter, JClassType beanType) {
+ String beanQualifiedSourceName = beanType.getQualifiedSourceName();
+
// if (target == null) {
sourceWriter.println("if (target == null) {");
sourceWriter.indent();
+
// target = GWT.create(VButtonState.class);
sourceWriter.println("target = GWT.create(" + beanQualifiedSourceName
+ ".class);");
sourceWriter.outdent();
sourceWriter.println("}");
+ // JSONOBject json = (JSONObject)jsonValue;
+ sourceWriter.println(JSONObject.class.getName() + " json = ("
+ + JSONObject.class.getName() + ")jsonValue;");
+
for (JMethod method : getSetters(beanType)) {
String setterName = method.getName();
String fieldName = setterName.substring(3); // setZIndex() -> ZIndex
logger.log(Type.DEBUG, "* Processing field " + fieldName + " in "
+ beanQualifiedSourceName + " (" + beanType.getName() + ")");
- // if (jsonValue.containsKey("height")) {
- sourceWriter.println("if (jsonValue.containsKey(\"" + fieldName
+ // if (json.containsKey("height")) {
+ sourceWriter.println("if (json.containsKey(\"" + fieldName
+ "\")) {");
sourceWriter.indent();
String jsonFieldName = "json_" + fieldName;
- // JSONArray json_Height = (JSONArray) jsonValue.get("height");
+ // JSONArray json_Height = (JSONArray) json.get("height");
sourceWriter.println("JSONArray " + jsonFieldName
- + " = (JSONArray) jsonValue.get(\"" + fieldName + "\");");
+ + " = (JSONArray) json.get(\"" + fieldName + "\");");
String fieldType;
String getterName = "get" + fieldName;
// return target;
sourceWriter.println("return target;");
- sourceWriter.println("}");
- sourceWriter.outdent();
- // End of class
- sourceWriter.println("}");
- sourceWriter.outdent();
+ }
- // commit generated class
- context.commit(logger, printWriter);
- logger.log(TreeLogger.INFO, "Generated Serializer class "
- + getFullyQualifiedSerializerClassName(beanType));
+ private void writeEnumSerializer(TreeLogger logger,
+ SourceWriter sourceWriter, JClassType beanType) {
+ // return new JSONString(castedValue.name());
+ sourceWriter.println("return new " + JSONString.class.getName()
+ + "(castedValue.name());");
+ }
+
+ private void writeBeanSerializer(TreeLogger logger,
+ SourceWriter sourceWriter, JClassType beanType) {
+
+ // JSONObject json = new JSONObject();
+ sourceWriter.println(JSONObject.class.getName() + " json = new "
+ + JSONObject.class.getName() + "();");
+
+ for (JMethod setterMethod : getSetters(beanType)) {
+ String setterName = setterMethod.getName();
+ String fieldName = setterName.substring(3); // setZIndex() -> ZIndex
+ String getterName = findGetter(beanType, setterMethod);
+
+ if (getterName == null) {
+ logger.log(TreeLogger.ERROR, "No getter found for " + fieldName
+ + ". Serialization will likely fail");
+ }
+ // json.put("button",
+ // JsonEncoder.encode(castedValue.getButton(), idMapper,
+ // connection));
+ sourceWriter.println("json.put(\"" + fieldName + "\", "
+ + JsonEncoder.class.getName() + ".encode(castedValue."
+ + getterName + "(), idMapper, connection));");
+ }
+ // return json;
+ sourceWriter.println("return json;");
}
return setterMethods;
}
- private String decapitalize(String name) {
- return name.substring(0, 1).toLowerCase() + name.substring(1);
- }
-
private static String getSerializerSimpleClassName(JClassType beanType) {
return getSimpleClassName(beanType) + "_Serializer";
}