]> source.dussan.org Git - vaadin-framework.git/commitdiff
Encode enum as String for legacy updates to be Vaadin 6 compatibile
authorArtur Signell <artur@vaadin.com>
Wed, 9 May 2012 14:16:27 +0000 (17:16 +0300)
committerArtur Signell <artur@vaadin.com>
Sun, 13 May 2012 17:35:29 +0000 (20:35 +0300)
src/com/vaadin/terminal/gwt/client/ApplicationConnection.java
src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java
src/com/vaadin/terminal/gwt/client/communication/URLReference_Serializer.java
src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java

index cc1615d4c76b72cf4358a2494455cde3aa85d80a..924a8924fe10ca1b9946d4c80dea34ad09e2fe81 100644 (file)
@@ -44,7 +44,6 @@ import com.vaadin.terminal.gwt.client.communication.JsonEncoder;
 import com.vaadin.terminal.gwt.client.communication.MethodInvocation;
 import com.vaadin.terminal.gwt.client.communication.RpcManager;
 import com.vaadin.terminal.gwt.client.communication.SerializerMap;
-import com.vaadin.terminal.gwt.client.communication.SharedState;
 import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
 import com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector;
 import com.vaadin.terminal.gwt.client.ui.VContextMenu;
@@ -1688,11 +1687,12 @@ public class ApplicationConnection {
                 invocationJson.set(2,
                         new JSONString(invocation.getMethodName()));
                 JSONArray paramJson = new JSONArray();
+                boolean restrictToInternalTypes = isLegacyVariableChange(invocation);
                 for (int i = 0; i < invocation.getParameters().length; ++i) {
                     // TODO non-static encoder? type registration?
                     paramJson.set(i, JsonEncoder.encode(
-                            invocation.getParameters()[i], getConnectorMap(),
-                            this));
+                            invocation.getParameters()[i],
+                            restrictToInternalTypes, getConnectorMap(), this));
                 }
                 invocationJson.set(3, paramJson);
                 reqJson.set(reqJson.size(), invocationJson);
@@ -1731,6 +1731,13 @@ public class ApplicationConnection {
         makeUidlRequest(req.toString(), extraParams, forceSync);
     }
 
+    private boolean isLegacyVariableChange(MethodInvocation invocation) {
+        return ApplicationConnection.UPDATE_VARIABLE_METHOD.equals(invocation
+                .getInterfaceName())
+                && ApplicationConnection.UPDATE_VARIABLE_METHOD
+                        .equals(invocation.getMethodName());
+    }
+
     /**
      * Sends a new value for the given paintables given variable to the server.
      * <p>
index 599022b78cfcea7cea6371ac52915a766ed4444c..10b6f49a79a688e29d5970e00c92ac08727e88f4 100644 (file)
@@ -61,7 +61,8 @@ public class JsonEncoder {
      * @param connection
      * @return JSON representation of the value
      */
-    public static JSONValue encode(Object value, ConnectorMap connectorMap,
+    public static JSONValue encode(Object value,
+            boolean restrictToInternalTypes, ConnectorMap connectorMap,
             ApplicationConnection connection) {
         if (null == value) {
             return combineTypeAndValue(VTYPE_NULL, JSONNull.getInstance());
@@ -79,10 +80,18 @@ public class JsonEncoder {
             return combineTypeAndValue(VTYPE_BOOLEAN,
                     JSONBoolean.getInstance((Boolean) value));
         } else if (value instanceof Object[]) {
-            return encodeObjectArray((Object[]) value, connectorMap, connection);
+            return encodeObjectArray((Object[]) value, restrictToInternalTypes,
+                    connectorMap, connection);
         } else if (value instanceof Enum) {
-            Enum e = (Enum) value;
-            return encodeEnum(e, connectorMap, connection);
+            if (restrictToInternalTypes) {
+                // Enums are encoded as strings in Vaadin 6 so we still do that
+                // for backwards copmatibility.
+                return encode(value.toString(), restrictToInternalTypes,
+                        connectorMap, connection);
+            } else {
+                Enum e = (Enum) value;
+                return encodeEnum(e, connectorMap, connection);
+            }
         } else if (value instanceof Map) {
             Map<Object, Object> map = (Map<Object, Object>) value;
             JSONObject jsonMap = new JSONObject();
@@ -100,8 +109,10 @@ public class JsonEncoder {
                                     + " Failed map used "
                                     + mapKey.getClass().getName() + " as keys");
                 }
-                jsonMap.put((String) mapKey,
-                        encode(mapValue, connectorMap, connection));
+                jsonMap.put(
+                        (String) mapKey,
+                        encode(mapValue, restrictToInternalTypes, connectorMap,
+                                connection));
             }
             return combineTypeAndValue(type, jsonMap);
         } else if (value instanceof Connector) {
@@ -109,8 +120,8 @@ public class JsonEncoder {
             return combineTypeAndValue(VTYPE_CONNECTOR, new JSONString(
                     connector.getConnectorId()));
         } else if (value instanceof Collection) {
-            return encodeCollection((Collection) value, connectorMap,
-                    connection);
+            return encodeCollection((Collection) value,
+                    restrictToInternalTypes, connectorMap, connection);
         } else {
             String transportType = getTransportType(value);
             if (transportType != null) {
@@ -137,21 +148,27 @@ public class JsonEncoder {
     }
 
     private static JSONValue encodeObjectArray(Object[] array,
-            ConnectorMap connectorMap, ApplicationConnection connection) {
+            boolean restrictToInternalTypes, ConnectorMap connectorMap,
+            ApplicationConnection connection) {
         JSONArray jsonArray = new JSONArray();
         for (int i = 0; i < array.length; ++i) {
             // TODO handle object graph loops?
-            jsonArray.set(i, encode(array[i], connectorMap, connection));
+            jsonArray.set(
+                    i,
+                    encode(array[i], restrictToInternalTypes, connectorMap,
+                            connection));
         }
         return combineTypeAndValue(VTYPE_ARRAY, jsonArray);
     }
 
     private static JSONValue encodeCollection(Collection collection,
-            ConnectorMap connectorMap, ApplicationConnection connection) {
+            boolean restrictToInternalTypes, ConnectorMap connectorMap,
+            ApplicationConnection connection) {
         JSONArray jsonArray = new JSONArray();
         int idx = 0;
         for (Object o : collection) {
-            JSONValue encodedObject = encode(o, connectorMap, connection);
+            JSONValue encodedObject = encode(o, restrictToInternalTypes,
+                    connectorMap, connection);
             jsonArray.set(idx++, encodedObject);
         }
         if (collection instanceof Set) {
index 3ddae493b722e9eaecce93c7c49851e4d8bdbf14..2ee7df7f6dc0c15d15b905db4534ee538f6a1e62 100644 (file)
@@ -29,7 +29,7 @@ public class URLReference_Serializer implements JSONSerializer<URLReference> {
             ApplicationConnection connection) {
         JSONObject json = new JSONObject();
         json.put("URL",
-                JsonEncoder.encode(value.getURL(), idMapper, connection));
+                JsonEncoder.encode(value.getURL(), true, idMapper, connection));
         return json;
     }
 
index 0f0ca8042699c2813a3fb30b769302667924acf1..bc031f4bdb6b19db2ba228b76049a671fa3363a2 100644 (file)
@@ -288,11 +288,11 @@ public class SerializerGenerator extends Generator {
                         + ". Serialization will likely fail");
             }
             // json.put("button",
-            // JsonEncoder.encode(castedValue.getButton(), idMapper,
+            // JsonEncoder.encode(castedValue.getButton(), false, idMapper,
             // connection));
-            sourceWriter.println("json.put(\"" + fieldName + "\", "
+            sourceWriter.println("json.put(\"" + fieldName + "\",  "
                     + JsonEncoder.class.getName() + ".encode(castedValue."
-                    + getterName + "(), idMapper, connection));");
+                    + getterName + "(), false, idMapper, connection));");
         }
         // return json;
         sourceWriter.println("return json;");