]> source.dussan.org Git - vaadin-framework.git/commitdiff
Use String instead of char as type tag in JSON (#8304 shared state).
authorHenri Sara <hesara@vaadin.com>
Tue, 21 Feb 2012 12:10:13 +0000 (14:10 +0200)
committerHenri Sara <hesara@vaadin.com>
Tue, 21 Feb 2012 12:10:13 +0000 (14:10 +0200)
src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java
src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java
src/com/vaadin/terminal/gwt/server/JsonCodec.java

index e7369e1a8c20fe5ca3db8b430f9f641859c76c6e..c66dc8a0361b95c045f0d0c5cec62b9fc8ec3926 100644 (file)
@@ -38,56 +38,43 @@ public class JsonDecoder {
      * @return converted value (does not contain JSON types)
      */
     public static Object convertValue(JSONArray value, VPaintableMap idMapper) {
-        return convertValue(
-                ((JSONString) value.get(0)).stringValue().charAt(0),
+        return convertValue(((JSONString) value.get(0)).stringValue(),
                 value.get(1), idMapper);
     }
 
-    private static Object convertValue(char variableType, Object value,
+    private static Object convertValue(String variableType, Object value,
             VPaintableMap idMapper) {
         Object val = null;
         // TODO type checks etc.
-        switch (variableType) {
-        case JsonEncoder.VTYPE_ARRAY:
+        if (JsonEncoder.VTYPE_ARRAY.equals(variableType)) {
             val = convertArray((JSONArray) value, idMapper);
-            break;
-        case JsonEncoder.VTYPE_MAP:
+        } else if (JsonEncoder.VTYPE_MAP.equals(variableType)) {
             val = convertMap((JSONObject) value, idMapper);
-            break;
-        case JsonEncoder.VTYPE_STRINGARRAY:
+        } else if (JsonEncoder.VTYPE_STRINGARRAY.equals(variableType)) {
             val = convertStringArray((JSONArray) value);
-            break;
-        case JsonEncoder.VTYPE_STRING:
+        } else if (JsonEncoder.VTYPE_STRING.equals(variableType)) {
             val = ((JSONString) value).stringValue();
-            break;
-        case JsonEncoder.VTYPE_INTEGER:
+        } else if (JsonEncoder.VTYPE_INTEGER.equals(variableType)) {
             // TODO handle properly
             val = Integer.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_LONG:
+        } else if (JsonEncoder.VTYPE_LONG.equals(variableType)) {
             // TODO handle properly
             val = Long.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_FLOAT:
+        } else if (JsonEncoder.VTYPE_FLOAT.equals(variableType)) {
             // TODO handle properly
             val = Float.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_DOUBLE:
+        } else if (JsonEncoder.VTYPE_DOUBLE.equals(variableType)) {
             // TODO handle properly
             val = Double.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_BOOLEAN:
+        } else if (JsonEncoder.VTYPE_BOOLEAN.equals(variableType)) {
             // TODO handle properly
             val = Boolean.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_PAINTABLE:
+        } else if (JsonEncoder.VTYPE_PAINTABLE.equals(variableType)) {
             // TODO handle properly
             val = idMapper.getPaintable(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_SHAREDSTATE:
+        } else if (JsonEncoder.VTYPE_SHAREDSTATE.equals(variableType)) {
             val = convertMap((JSONObject) value, idMapper);
             // TODO convert to a SharedState instance
-            break;
         }
 
         return val;
index 61af9294d9f28d24bd69eb4c29e9b0c8ffdd8052..d1cb9a3ab99196973abc22858934039bcac840e7 100644 (file)
@@ -28,21 +28,21 @@ import com.vaadin.terminal.gwt.client.VPaintableMap;
  */
 public class JsonEncoder {
 
-    public static final char VTYPE_PAINTABLE = 'p';
-    public static final char VTYPE_BOOLEAN = 'b';
-    public static final char VTYPE_DOUBLE = 'd';
-    public static final char VTYPE_FLOAT = 'f';
-    public static final char VTYPE_LONG = 'l';
-    public static final char VTYPE_INTEGER = 'i';
-    public static final char VTYPE_STRING = 's';
-    public static final char VTYPE_ARRAY = 'a';
-    public static final char VTYPE_STRINGARRAY = 'c';
-    public static final char VTYPE_MAP = 'm';
+    public static final String VTYPE_PAINTABLE = "p";
+    public static final String VTYPE_BOOLEAN = "b";
+    public static final String VTYPE_DOUBLE = "d";
+    public static final String VTYPE_FLOAT = "f";
+    public static final String VTYPE_LONG = "l";
+    public static final String VTYPE_INTEGER = "i";
+    public static final String VTYPE_STRING = "s";
+    public static final String VTYPE_ARRAY = "a";
+    public static final String VTYPE_STRINGARRAY = "c";
+    public static final String VTYPE_MAP = "m";
     // TODO this will be replaced by the shared state class name
-    public static final char VTYPE_SHAREDSTATE = 't';
+    public static final String VTYPE_SHAREDSTATE = "t";
 
     // TODO is this needed?
-    public static final char VTYPE_UNDEFINED = 'u';
+    public static final String VTYPE_UNDEFINED = "u";
 
     /**
      * Encode a value to a JSON representation for transport from the client to
@@ -98,14 +98,14 @@ public class JsonEncoder {
         }
     }
 
-    private static JSONValue combineTypeAndValue(char type, JSONValue value) {
+    private static JSONValue combineTypeAndValue(String type, JSONValue value) {
         JSONArray outerArray = new JSONArray();
-        outerArray.set(0, new JSONString(String.valueOf(type)));
+        outerArray.set(0, new JSONString(type));
         outerArray.set(1, value);
         return outerArray;
     }
 
-    private static char getTransportType(Object value) {
+    private static String getTransportType(Object value) {
         if (value instanceof String) {
             return VTYPE_STRING;
         } else if (value instanceof VPaintable) {
index 4d7501e7393cfb949ac1a5ac894adf8fa7e9934c..e525c690c01a0b8a3bc1ae994da620008d3ed836 100644 (file)
@@ -26,6 +26,27 @@ import com.vaadin.terminal.gwt.client.communication.SharedState;
  */
 public class JsonCodec implements Serializable {
 
+    private static Map<Class<?>, String> typeToTransportType = new HashMap<Class<?>, String>();
+
+    static {
+        registerType(String.class, JsonEncoder.VTYPE_STRING);
+        registerType(Paintable.class, JsonEncoder.VTYPE_PAINTABLE);
+        registerType(Boolean.class, JsonEncoder.VTYPE_BOOLEAN);
+        registerType(Integer.class, JsonEncoder.VTYPE_INTEGER);
+        registerType(Float.class, JsonEncoder.VTYPE_FLOAT);
+        registerType(Double.class, JsonEncoder.VTYPE_DOUBLE);
+        registerType(Long.class, JsonEncoder.VTYPE_LONG);
+        // transported as string representation
+        registerType(Enum.class, JsonEncoder.VTYPE_STRING);
+        registerType(String[].class, JsonEncoder.VTYPE_STRINGARRAY);
+        registerType(Object[].class, JsonEncoder.VTYPE_ARRAY);
+        registerType(Map.class, JsonEncoder.VTYPE_MAP);
+    }
+
+    private static void registerType(Class<?> type, String transportType) {
+        typeToTransportType.put(type, transportType);
+    }
+
     /**
      * Convert a JSON array with two elements (type and value) into a
      * server-side type, recursively if necessary.
@@ -40,51 +61,39 @@ public class JsonCodec implements Serializable {
      */
     public static Object convertVariableValue(JSONArray value,
             PaintableIdMapper idMapper) throws JSONException {
-        return convertVariableValue(value.getString(0).charAt(0), value.get(1),
-                idMapper);
+        return convertVariableValue(value.getString(0), value.get(1), idMapper);
     }
 
-    private static Object convertVariableValue(char variableType, Object value,
-            PaintableIdMapper idMapper) throws JSONException {
+    private static Object convertVariableValue(String variableType,
+            Object value, PaintableIdMapper idMapper) throws JSONException {
         Object val = null;
         // TODO type checks etc.
-        switch (variableType) {
-        case JsonEncoder.VTYPE_ARRAY:
+        if (JsonEncoder.VTYPE_ARRAY.equals(variableType)) {
             val = convertArray((JSONArray) value, idMapper);
-            break;
-        case JsonEncoder.VTYPE_MAP:
+        } else if (JsonEncoder.VTYPE_MAP.equals(variableType)) {
             val = convertMap((JSONObject) value, idMapper);
-            break;
-        case JsonEncoder.VTYPE_STRINGARRAY:
+        } else if (JsonEncoder.VTYPE_STRINGARRAY.equals(variableType)) {
             val = convertStringArray((JSONArray) value);
-            break;
-        case JsonEncoder.VTYPE_STRING:
+        } else if (JsonEncoder.VTYPE_STRING.equals(variableType)) {
             val = value;
-            break;
-        case JsonEncoder.VTYPE_INTEGER:
+        } else if (JsonEncoder.VTYPE_INTEGER.equals(variableType)) {
             // TODO handle properly
             val = Integer.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_LONG:
+        } else if (JsonEncoder.VTYPE_LONG.equals(variableType)) {
             // TODO handle properly
             val = Long.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_FLOAT:
+        } else if (JsonEncoder.VTYPE_FLOAT.equals(variableType)) {
             // TODO handle properly
             val = Float.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_DOUBLE:
+        } else if (JsonEncoder.VTYPE_DOUBLE.equals(variableType)) {
             // TODO handle properly
             val = Double.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_BOOLEAN:
+        } else if (JsonEncoder.VTYPE_BOOLEAN.equals(variableType)) {
             // TODO handle properly
             val = Boolean.valueOf(String.valueOf(value));
-            break;
-        case JsonEncoder.VTYPE_PAINTABLE:
+        } else if (JsonEncoder.VTYPE_PAINTABLE.equals(variableType)) {
             // TODO handle properly
             val = idMapper.getPaintable(String.valueOf(value));
-            break;
         }
 
         return val;
@@ -196,37 +205,20 @@ public class JsonCodec implements Serializable {
         return jsonMap;
     }
 
-    private static JSONArray combineTypeAndValue(char type, Object value) {
+    private static JSONArray combineTypeAndValue(String type, Object value) {
         JSONArray outerArray = new JSONArray();
-        outerArray.put(String.valueOf(type));
+        outerArray.put(type);
         outerArray.put(value);
         return outerArray;
     }
 
-    private static char getTransportType(Object value) {
-        if (value instanceof String) {
-            return JsonEncoder.VTYPE_STRING;
-        } else if (value instanceof Paintable) {
-            return JsonEncoder.VTYPE_PAINTABLE;
-        } else if (value instanceof Boolean) {
-            return JsonEncoder.VTYPE_BOOLEAN;
-        } else if (value instanceof Integer) {
-            return JsonEncoder.VTYPE_INTEGER;
-        } else if (value instanceof Float) {
-            return JsonEncoder.VTYPE_FLOAT;
-        } else if (value instanceof Double) {
-            return JsonEncoder.VTYPE_DOUBLE;
-        } else if (value instanceof Long) {
-            return JsonEncoder.VTYPE_LONG;
-        } else if (value instanceof Enum) {
-            // transported as string representation
-            return JsonEncoder.VTYPE_STRING;
-        } else if (value instanceof String[]) {
-            return JsonEncoder.VTYPE_STRINGARRAY;
-        } else if (value instanceof Object[]) {
-            return JsonEncoder.VTYPE_ARRAY;
-        } else if (value instanceof Map) {
-            return JsonEncoder.VTYPE_MAP;
+    private static String getTransportType(Object value) {
+        if (null == value) {
+            return JsonEncoder.VTYPE_UNDEFINED;
+        }
+        String transportType = typeToTransportType.get(value.getClass());
+        if (null != transportType) {
+            return transportType;
         }
         // TODO throw exception?
         return JsonEncoder.VTYPE_UNDEFINED;