]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use camelCase field names -> JSON more suitable for javascript (#8888)
authorLeif Åstrand <leif@vaadin.com>
Thu, 7 Jun 2012 07:26:17 +0000 (10:26 +0300)
committerLeif Åstrand <leif@vaadin.com>
Thu, 7 Jun 2012 07:26:17 +0000 (10:26 +0300)
src/com/vaadin/terminal/gwt/client/communication/URLReference_Serializer.java
src/com/vaadin/terminal/gwt/server/JsonCodec.java
src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java

index 56c4bb96231a3b00f45840f145dae2e807b64a86..bab0f385ed6356f48c8b7f16066b25bc1881fbec 100644 (file)
@@ -10,12 +10,15 @@ import com.vaadin.terminal.gwt.client.ApplicationConnection;
 
 public class URLReference_Serializer implements JSONSerializer<URLReference> {
 
+    // setURL() -> uRL as first char becomes lower case...
+    private static final String URL_FIELD = "uRL";
+
     public URLReference deserialize(Type type, JSONValue jsonValue,
             ApplicationConnection connection) {
         URLReference reference = GWT.create(URLReference.class);
         JSONObject json = (JSONObject) jsonValue;
-        if (json.containsKey("URL")) {
-            JSONValue jsonURL = json.get("URL");
+        if (json.containsKey(URL_FIELD)) {
+            JSONValue jsonURL = json.get(URL_FIELD);
             String URL = (String) JsonDecoder.decodeValue(
                     new Type(String.class.getName(), null), jsonURL, null,
                     connection);
@@ -24,9 +27,10 @@ public class URLReference_Serializer implements JSONSerializer<URLReference> {
         return reference;
     }
 
-    public JSONValue serialize(URLReference value, ApplicationConnection connection) {
+    public JSONValue serialize(URLReference value,
+            ApplicationConnection connection) {
         JSONObject json = new JSONObject();
-        json.put("URL",
+        json.put(URL_FIELD,
                 JsonEncoder.encode(value.getURL(), true, connection));
         return json;
     }
index feaf78d2e894592b801dbe681361684fd8ec6556..f4892bd905769018f7aca32c53decab34e2ea2e7 100644 (file)
@@ -410,7 +410,7 @@ public class JsonCodec implements Serializable {
      * Returns the name that should be used as field name in the JSON. We strip
      * "set" from the setter, keeping the result - this is easy to do on both
      * server and client, avoiding some issues with cASE. E.g setZIndex()
-     * becomes "ZIndex". Also ensures that both getter and setter are present,
+     * becomes "zIndex". Also ensures that both getter and setter are present,
      * returning null otherwise.
      * 
      * @param pd
@@ -421,7 +421,10 @@ public class JsonCodec implements Serializable {
         if (pd.getReadMethod() == null || pd.getWriteMethod() == null) {
             return null;
         }
-        return pd.getWriteMethod().getName().substring(3);
+        String fieldName = pd.getWriteMethod().getName().substring(3);
+        fieldName = Character.toLowerCase(fieldName.charAt(0))
+                + fieldName.substring(1);
+        return fieldName;
     }
 
     private static Object decodeObject(Type targetType,
@@ -547,6 +550,14 @@ public class JsonCodec implements Serializable {
                     equals = equals(fieldValue, referenceFieldValue);
                 }
                 if (!equals) {
+                    if (jsonMap.has(fieldName)) {
+                        throw new RuntimeException(
+                                "Can't encode "
+                                        + value.getClass().getName()
+                                        + " as it has multiple fields with the name "
+                                        + fieldName.toLowerCase()
+                                        + ". This can happen if only casing distinguishes one property name from another.");
+                    }
                     jsonMap.put(
                             fieldName,
                             encode(fieldValue, referenceFieldValue, fieldType,
index 060d0fa96a98571ae7d7a5f3b5a55628bb467ac3..c32b54ff1cf5480b263e7016a3e0bfff0f33c754 100644 (file)
@@ -7,6 +7,7 @@ package com.vaadin.terminal.gwt.widgetsetutils;
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashSet;
 import java.util.List;
 
 import com.google.gwt.core.client.GWT;
@@ -79,10 +80,11 @@ public class SerializerGenerator extends Generator {
      *            bean type for which the serializer is to be generated
      * @param beanSerializerTypeName
      *            name of the serializer class to generate
+     * @throws UnableToCompleteException
      */
     private void generateClass(TreeLogger logger, GeneratorContext context,
             JClassType beanType, String serializerPackageName,
-            String serializerClassName) {
+            String serializerClassName) throws UnableToCompleteException {
         // get print writer that receives the source code
         PrintWriter printWriter = null;
         printWriter = context.tryCreate(logger, serializerPackageName,
@@ -222,7 +224,9 @@ public class SerializerGenerator extends Generator {
 
         for (JMethod method : getSetters(beanType)) {
             String setterName = method.getName();
-            String fieldName = setterName.substring(3); // setZIndex() -> ZIndex
+            String baseName = setterName.substring(3);
+            String fieldName = getTransportFieldName(baseName); // setZIndex()
+                                                                // -> zIndex
             JType setterParameterType = method.getParameterTypes()[0];
 
             logger.log(Type.DEBUG, "* Processing field " + fieldName + " in "
@@ -238,13 +242,13 @@ public class SerializerGenerator extends Generator {
                     + " = json.get(\"" + fieldName + "\");");
 
             String fieldType;
-            String getterName = "get" + fieldName;
+            String getterName = "get" + baseName;
             JPrimitiveType primitiveType = setterParameterType.isPrimitive();
             if (primitiveType != null) {
                 // This is a primitive type -> must used the boxed type
                 fieldType = primitiveType.getQualifiedBoxedSourceName();
                 if (primitiveType == JPrimitiveType.BOOLEAN) {
-                    getterName = "is" + fieldName;
+                    getterName = "is" + baseName;
                 }
             } else {
                 fieldType = setterParameterType.getQualifiedSourceName();
@@ -278,15 +282,29 @@ public class SerializerGenerator extends Generator {
     }
 
     private void writeBeanSerializer(TreeLogger logger,
-            SourceWriter sourceWriter, JClassType beanType) {
+            SourceWriter sourceWriter, JClassType beanType)
+            throws UnableToCompleteException {
 
         // JSONObject json = new JSONObject();
         sourceWriter.println(JSONObject.class.getName() + " json = new "
                 + JSONObject.class.getName() + "();");
 
+        HashSet<String> usedFieldNames = new HashSet<String>();
+
         for (JMethod setterMethod : getSetters(beanType)) {
             String setterName = setterMethod.getName();
-            String fieldName = setterName.substring(3); // setZIndex() -> ZIndex
+            String fieldName = getTransportFieldName(setterName.substring(3)); // setZIndex()
+            // -> zIndex
+            if (!usedFieldNames.add(fieldName)) {
+                logger.log(
+                        TreeLogger.ERROR,
+                        "Can't encode "
+                                + beanType.getQualifiedSourceName()
+                                + " as it has multiple fields with the name "
+                                + fieldName.toLowerCase()
+                                + ". This can happen if only casing distinguishes one property name from another.");
+                throw new UnableToCompleteException();
+            }
             String getterName = findGetter(beanType, setterMethod);
 
             if (getterName == null) {
@@ -305,6 +323,11 @@ public class SerializerGenerator extends Generator {
 
     }
 
+    private static String getTransportFieldName(String baseName) {
+        return Character.toLowerCase(baseName.charAt(0))
+                + baseName.substring(1);
+    }
+
     private String findGetter(JClassType beanType, JMethod setterMethod) {
         JType setterParameterType = setterMethod.getParameterTypes()[0];
         String fieldName = setterMethod.getName().substring(3);