From 7468c8261d186fbfb35586571c3a0728d31116e5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 5 Jun 2012 11:21:20 +0300 Subject: [PATCH] Don't send type info from server to client (#8879) --- .../gwt/client/ApplicationConnection.java | 5 +- .../gwt/client/communication/JsonDecoder.java | 23 ++--- .../URLReference_Serializer.java | 7 +- .../server/AbstractCommunicationManager.java | 7 +- .../vaadin/terminal/gwt/server/JsonCodec.java | 84 ++++++------------- .../widgetsetutils/SerializerGenerator.java | 9 +- .../gwt/server/JSONSerializerTest.java | 11 ++- 7 files changed, 52 insertions(+), 94 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java index bf5c34f3f3..1b902e8e40 100644 --- a/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java +++ b/src/com/vaadin/terminal/gwt/client/ApplicationConnection.java @@ -28,6 +28,7 @@ import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.RequestException; import com.google.gwt.http.client.Response; 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.regexp.shared.MatchResult; import com.google.gwt.regexp.shared.RegExp; @@ -1410,7 +1411,7 @@ public class ApplicationConnection { .getConnector(connectorId); if (null != connector) { - JSONArray stateDataAndType = new JSONArray( + JSONObject stateDataAndType = new JSONObject( states.getJavaScriptObject(connectorId)); SharedState state = connector.getState(); @@ -1586,7 +1587,7 @@ public class ApplicationConnection { Object[] parameters = new Object[parametersJson.size()]; for (int j = 0; j < parametersJson.size(); ++j) { parameters[j] = JsonDecoder.decodeValue(parameterTypes[j], - (JSONArray) parametersJson.get(j), null, this); + parametersJson.get(j), null, this); } methodInvocation.setParameters(parameters); diff --git a/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java b/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java index cd255ada95..c7b33a70db 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java +++ b/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java @@ -21,7 +21,6 @@ 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; -import com.vaadin.terminal.gwt.client.ServerConnector; /** * Client side decoder for decodeing shared state and other values from JSON @@ -40,21 +39,13 @@ public class JsonDecoder { * Decode a JSON array with two elements (type and value) into a client-side * type, recursively if necessary. * - * @param jsonArray - * JSON array with two elements - * @param idMapper - * mapper between connector ID and {@link ServerConnector} - * objects + * @param jsonValue + * JSON value with encoded data * @param connection * reference to the current ApplicationConnection * @return decoded value (does not contain JSON types) */ - public static Object decodeValue(Type type, JSONArray jsonArray, - Object target, ApplicationConnection connection) { - return decodeValue(type, jsonArray.get(1), target, connection); - } - - private static Object decodeValue(Type type, JSONValue jsonValue, + public static Object decodeValue(Type type, JSONValue jsonValue, Object target, ApplicationConnection connection) { // Null is null, regardless of type @@ -120,8 +111,8 @@ public class JsonDecoder { Iterator it = jsonMap.keySet().iterator(); while (it.hasNext()) { String key = it.next(); - JSONArray encodedKey = (JSONArray) JSONParser.parseStrict(key); - JSONArray encodedValue = (JSONArray) jsonMap.get(key); + JSONValue encodedKey = JSONParser.parseStrict(key); + JSONValue encodedValue = jsonMap.get(key); Object decodedKey = decodeValue(type.getParameterTypes()[0], encodedKey, null, connection); Object decodedValue = decodeValue(type.getParameterTypes()[1], @@ -162,8 +153,8 @@ public class JsonDecoder { Collection tokens) { for (int i = 0; i < jsonArray.size(); ++i) { // each entry always has two elements: type and value - JSONArray entryArray = (JSONArray) jsonArray.get(i); - tokens.add(decodeValue(childType, entryArray, null, connection)); + JSONValue entryValue = jsonArray.get(i); + tokens.add(decodeValue(childType, entryValue, null, connection)); } } } diff --git a/src/com/vaadin/terminal/gwt/client/communication/URLReference_Serializer.java b/src/com/vaadin/terminal/gwt/client/communication/URLReference_Serializer.java index 5db487d867..f6108bbfa8 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/URLReference_Serializer.java +++ b/src/com/vaadin/terminal/gwt/client/communication/URLReference_Serializer.java @@ -4,7 +4,6 @@ package com.vaadin.terminal.gwt.client.communication; import com.google.gwt.core.client.GWT; -import com.google.gwt.json.client.JSONArray; import com.google.gwt.json.client.JSONObject; import com.google.gwt.json.client.JSONValue; import com.vaadin.terminal.gwt.client.ApplicationConnection; @@ -17,10 +16,10 @@ public class URLReference_Serializer implements JSONSerializer { URLReference reference = GWT.create(URLReference.class); JSONObject json = (JSONObject) jsonValue; if (json.containsKey("URL")) { - JSONArray jsonURL = (JSONArray) json.get("URL"); + JSONValue jsonURL = json.get("URL"); String URL = (String) JsonDecoder.decodeValue( - new Type(String.class.getCanonicalName(), null), jsonURL, - null, connection); + new Type(String.class.getName(), null), jsonURL, null, + connection); reference.setURL(connection.translateVaadinUri(URL)); } return reference; diff --git a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java index b32525a703..d6feba9dc0 100644 --- a/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java +++ b/src/com/vaadin/terminal/gwt/server/AbstractCommunicationManager.java @@ -846,11 +846,10 @@ public abstract class AbstractCommunicationManager implements Serializable { + stateType.getName()); } } - JSONArray stateJsonArray = JsonCodec.encode(state, - referenceState, stateType, application); + Object stateJson = JsonCodec.encode(state, referenceState, + stateType, application); - sharedStates - .put(connector.getConnectorId(), stateJsonArray); + sharedStates.put(connector.getConnectorId(), stateJson); } catch (JSONException e) { throw new PaintException( "Failed to serialize shared state for connector " diff --git a/src/com/vaadin/terminal/gwt/server/JsonCodec.java b/src/com/vaadin/terminal/gwt/server/JsonCodec.java index b853c90a3b..e76a3d3f3a 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonCodec.java +++ b/src/com/vaadin/terminal/gwt/server/JsonCodec.java @@ -250,7 +250,8 @@ public class JsonCodec implements Serializable { Iterator it = jsonMap.keys(); while (it.hasNext()) { String key = it.next(); - Object encodedKey = new JSONTokener(key).nextValue(); + String keyString = (String) new JSONTokener(key).nextValue(); + Object encodedKey = new JSONTokener(keyString).nextValue(); Object encodedValue = jsonMap.get(key); Object decodedKey = decodeParametrizedType(targetType, @@ -394,55 +395,43 @@ public class JsonCodec implements Serializable { } } - @Deprecated - private static JSONArray encode(Object value, Application application) - throws JSONException { - return encode(value, null, null, application); - } - - public static JSONArray encode(Object value, Object referenceValue, + public static Object encode(Object value, Object referenceValue, Type valueType, Application application) throws JSONException { - if (null == value) { - return encodeNull(); + if (valueType == null) { + throw new IllegalArgumentException("type must be defined"); } - if (valueType == null) { - valueType = value.getClass(); + if (null == value) { + return encodeNull(); } - String internalTransportType = getInternalTransportType(valueType); if (value instanceof String[]) { String[] array = (String[]) value; JSONArray jsonArray = new JSONArray(); for (int i = 0; i < array.length; ++i) { jsonArray.put(array[i]); } - return combineTypeAndValue(JsonEncoder.VTYPE_STRINGARRAY, jsonArray); + return jsonArray; } else if (value instanceof String) { - return combineTypeAndValue(JsonEncoder.VTYPE_STRING, value); + return value; } else if (value instanceof Boolean) { - return combineTypeAndValue(JsonEncoder.VTYPE_BOOLEAN, value); + return value; } else if (value instanceof Number) { - return combineTypeAndValue(internalTransportType, value); + return value; } else if (value instanceof Collection) { - if (internalTransportType == null) { - throw new RuntimeException( - "Unable to serialize unsupported type: " + valueType); - } Collection collection = (Collection) value; JSONArray jsonArray = encodeCollection(valueType, collection, application); - - return combineTypeAndValue(internalTransportType, jsonArray); + return jsonArray; } else if (value instanceof Object[]) { Object[] array = (Object[]) value; JSONArray jsonArray = encodeArrayContents(array, application); - return combineTypeAndValue(JsonEncoder.VTYPE_ARRAY, jsonArray); + return jsonArray; } else if (value instanceof Map) { JSONObject jsonMap = encodeMap(valueType, (Map) value, application); - return combineTypeAndValue(JsonEncoder.VTYPE_MAP, jsonMap); + return jsonMap; } else if (value instanceof Connector) { Connector connector = (Connector) value; if (value instanceof Component @@ -450,24 +439,18 @@ public class JsonCodec implements Serializable { .isVisible((Component) value))) { return encodeNull(); } - return combineTypeAndValue(JsonEncoder.VTYPE_CONNECTOR, - connector.getConnectorId()); - } else if (internalTransportType != null) { - return combineTypeAndValue(internalTransportType, - String.valueOf(value)); + return connector.getConnectorId(); } else if (value instanceof Enum) { - return encodeEnum((Enum) value, application); + return encodeEnum((Enum) value, application); } else { // Any object that we do not know how to encode we encode by looping // through fields - return combineTypeAndValue( - getCustomTransportType((Class) valueType), - encodeObject(value, referenceValue, application)); + return encodeObject(value, referenceValue, application); } } - private static JSONArray encodeNull() { - return combineTypeAndValue(JsonEncoder.VTYPE_NULL, JSONObject.NULL); + private static Object encodeNull() { + return JSONObject.NULL; } private static Object encodeObject(Object value, Object referenceValue, @@ -531,10 +514,9 @@ public class JsonCodec implements Serializable { return false; } - private static JSONArray encodeEnum(Enum e, Application application) + private static String encodeEnum(Enum e, Application application) throws JSONException { - String enumIdentifier = e.name(); - return combineTypeAndValue(e.getClass().getName(), enumIdentifier); + return e.name(); } private static JSONArray encodeArrayContents(Object[] array, @@ -556,15 +538,15 @@ public class JsonCodec implements Serializable { return jsonArray; } - private static JSONArray encodeChild(Type targetType, int typeIndex, - Object o, Application application) throws JSONException { + private static Object encodeChild(Type targetType, int typeIndex, Object o, + Application application) throws JSONException { if (targetType instanceof ParameterizedType) { Type childType = ((ParameterizedType) targetType) .getActualTypeArguments()[typeIndex]; // Encode using the given type return encode(o, null, childType, application); } else { - return encode(o, application); + throw new JSONException("Collection is missing generics"); } } @@ -582,25 +564,13 @@ public class JsonCodec implements Serializable { JSONObject jsonMap = new JSONObject(); for (Object mapKey : map.keySet()) { Object mapValue = map.get(mapKey); - JSONArray encodedKey = encode(mapKey, null, keyType, application); - JSONArray encodedValue = encode(mapValue, null, valueType, - application); - jsonMap.put(encodedKey.toString(), encodedValue); + Object encodedKey = encode(mapKey, null, keyType, application); + Object encodedValue = encode(mapValue, null, valueType, application); + jsonMap.put(JSONObject.quote(encodedKey.toString()), encodedValue); } return jsonMap; } - private static JSONArray combineTypeAndValue(String type, Object value) { - if (type == null) { - throw new RuntimeException("Type for value " + value - + " cannot be null!"); - } - JSONArray outerArray = new JSONArray(); - outerArray.put(type); - outerArray.put(value); - return outerArray; - } - /** * Gets the transport type for the given class. Returns null if no transport * type can be found. diff --git a/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java index 8e34e28999..6e99089434 100644 --- a/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java +++ b/src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java @@ -22,7 +22,6 @@ 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; @@ -107,7 +106,7 @@ public class SerializerGenerator extends Generator { composer = new ClassSourceFileComposerFactory(serializerPackageName, serializerClassName); composer.addImport(GWT.class.getName()); - composer.addImport(JSONArray.class.getName()); + composer.addImport(JSONValue.class.getName()); composer.addImport(com.vaadin.terminal.gwt.client.communication.Type.class .getName()); // composer.addImport(JSONObject.class.getName()); @@ -236,9 +235,9 @@ public class SerializerGenerator extends Generator { + "\")) {"); sourceWriter.indent(); String jsonFieldName = "json_" + fieldName; - // JSONArray json_Height = (JSONArray) json.get("height"); - sourceWriter.println("JSONArray " + jsonFieldName - + " = (JSONArray) json.get(\"" + fieldName + "\");"); + // JSONValue json_Height = json.get("height"); + sourceWriter.println("JSONValue " + jsonFieldName + + " = json.get(\"" + fieldName + "\");"); String fieldType; String getterName = "get" + fieldName; diff --git a/tests/client-side/com/vaadin/terminal/gwt/server/JSONSerializerTest.java b/tests/client-side/com/vaadin/terminal/gwt/server/JSONSerializerTest.java index 926f026b40..fefc2d927c 100644 --- a/tests/client-side/com/vaadin/terminal/gwt/server/JSONSerializerTest.java +++ b/tests/client-side/com/vaadin/terminal/gwt/server/JSONSerializerTest.java @@ -13,7 +13,6 @@ import java.util.Map; import junit.framework.TestCase; -import com.vaadin.external.json.JSONArray; import com.vaadin.terminal.gwt.client.communication.JsonDecoder; import com.vaadin.terminal.gwt.client.communication.JsonEncoder; import com.vaadin.terminal.gwt.client.ui.splitpanel.AbstractSplitPanelState; @@ -43,8 +42,8 @@ public class JSONSerializerTest extends TestCase { stringToStateMap.put("string - state 1", s); stringToStateMap.put("String - state 2", s2); - JSONArray encodedMap = JsonCodec.encode(stringToStateMap, null, - mapType, null); + Object encodedMap = JsonCodec.encode(stringToStateMap, null, mapType, + null); ensureDecodedCorrectly(stringToStateMap, encodedMap, mapType); } @@ -60,13 +59,13 @@ public class JSONSerializerTest extends TestCase { stateToStringMap.put(s, "string - state 1"); stateToStringMap.put(s2, "String - state 2"); - JSONArray encodedMap = JsonCodec.encode(stateToStringMap, null, - mapType, null); + Object encodedMap = JsonCodec.encode(stateToStringMap, null, mapType, + null); ensureDecodedCorrectly(stateToStringMap, encodedMap, mapType); } - private void ensureDecodedCorrectly(Object original, JSONArray encoded, + private void ensureDecodedCorrectly(Object original, Object encoded, Type type) throws Exception { Object serverSideDecoded = JsonCodec.decodeInternalOrCustomType(type, encoded, null); -- 2.39.5