From 26c8a73995c125476dcb6a8892ad567b7dd12784 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 16 Mar 2012 13:22:12 +0200 Subject: [PATCH] #8534 Handle null values consistently both from server to client and from client to server --- .../gwt/client/communication/JsonDecoder.java | 2 +- .../gwt/client/communication/JsonEncoder.java | 28 +++++++++---------- .../vaadin/terminal/gwt/server/JsonCodec.java | 16 +++++------ 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java b/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java index 33a415a91d..e034ae563d 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java +++ b/src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java @@ -55,7 +55,7 @@ public class JsonDecoder { ConnectorMap idMapper, ApplicationConnection connection) { Object val = null; // TODO type checks etc. - if (JsonEncoder.VTYPE_UNDEFINED.equals(variableType)) { + if (JsonEncoder.VTYPE_NULL.equals(variableType)) { val = null; } else if (JsonEncoder.VTYPE_ARRAY.equals(variableType)) { val = decodeArray((JSONArray) value, idMapper, connection); diff --git a/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java b/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java index 6cedba1b31..c704f36871 100644 --- a/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java +++ b/src/com/vaadin/terminal/gwt/client/communication/JsonEncoder.java @@ -14,8 +14,8 @@ 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.ServerConnector; import com.vaadin.terminal.gwt.client.ConnectorMap; +import com.vaadin.terminal.gwt.client.ServerConnector; /** * Encoder for converting RPC parameters and other values to JSON for transfer @@ -41,9 +41,7 @@ public class JsonEncoder { public static final String VTYPE_STRINGARRAY = "c"; public static final String VTYPE_MAP = "m"; public static final String VTYPE_LIST = "L"; - - // TODO is this needed? - public static final String VTYPE_UNDEFINED = "u"; + public static final String VTYPE_NULL = "n"; /** * Encode a value to a JSON representation for transport from the client to @@ -59,8 +57,7 @@ public class JsonEncoder { public static JSONValue encode(Object value, ConnectorMap connectorMap, ApplicationConnection connection) { if (null == value) { - // TODO as undefined type? - return combineTypeAndValue(VTYPE_UNDEFINED, JSONNull.getInstance()); + return combineTypeAndValue(VTYPE_NULL, JSONNull.getInstance()); } else if (value instanceof String[]) { String[] array = (String[]) value; JSONArray jsonArray = new JSONArray(); @@ -96,18 +93,19 @@ public class JsonEncoder { return combineTypeAndValue(VTYPE_PAINTABLE, new JSONString( connectorMap.getConnectorId(paintable))); } else { - if (getTransportType(value) != VTYPE_UNDEFINED) { - return combineTypeAndValue(getTransportType(value), + String transportType = getTransportType(value); + if (transportType != null) { + return combineTypeAndValue(transportType, new JSONString(String.valueOf(value))); } else { // Try to find a generated serializer object, class name is the // type - String type = value.getClass().getName(); + transportType = value.getClass().getName(); JSONSerializer serializer = JsonDecoder.serializerMap - .getSerializer(type); + .getSerializer(transportType); // TODO handle case with no serializer found - return combineTypeAndValue(type, + return combineTypeAndValue(transportType, serializer.serialize(value, connectorMap, connection)); } } @@ -121,7 +119,9 @@ public class JsonEncoder { } private static String getTransportType(Object value) { - if (value instanceof String) { + if (value == null) { + return VTYPE_NULL; + } else if (value instanceof String) { return VTYPE_STRING; } else if (value instanceof ServerConnector) { return VTYPE_PAINTABLE; @@ -146,8 +146,6 @@ public class JsonEncoder { } else if (value instanceof Map) { return VTYPE_MAP; } - // TODO throw exception? - return VTYPE_UNDEFINED; + return null; } - } diff --git a/src/com/vaadin/terminal/gwt/server/JsonCodec.java b/src/com/vaadin/terminal/gwt/server/JsonCodec.java index 78c1c175a5..6a2a0750ad 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonCodec.java +++ b/src/com/vaadin/terminal/gwt/server/JsonCodec.java @@ -103,6 +103,8 @@ public class JsonCodec implements Serializable { } else if (JsonEncoder.VTYPE_PAINTABLE.equals(variableType)) { // TODO handle properly val = idMapper.getPaintable(String.valueOf(value)); + } else if (JsonEncoder.VTYPE_NULL.equals(variableType)) { + val = null; } else { // Try to decode object using fields return decodeObject(variableType, (JSONObject) value, idMapper); @@ -171,9 +173,7 @@ public class JsonCodec implements Serializable { PaintableIdMapper idMapper) throws JSONException { if (null == value) { - // TODO as undefined type? - return combineTypeAndValue(JsonEncoder.VTYPE_UNDEFINED, - JSONObject.NULL); + return combineTypeAndValue(JsonEncoder.VTYPE_NULL, JSONObject.NULL); } else if (value instanceof String[]) { String[] array = (String[]) value; JSONArray jsonArray = new JSONArray(); @@ -203,7 +203,7 @@ public class JsonCodec implements Serializable { Paintable paintable = (Paintable) value; return combineTypeAndValue(JsonEncoder.VTYPE_PAINTABLE, idMapper.getPaintableId(paintable)); - } else if (getTransportType(value) != JsonEncoder.VTYPE_UNDEFINED) { + } else if (getTransportType(value) != JsonEncoder.VTYPE_NULL) { return combineTypeAndValue(getTransportType(value), String.valueOf(value)); } else { @@ -317,16 +317,16 @@ public class JsonCodec implements Serializable { return outerArray; } - private static String getTransportType(Object value) { + private static String getTransportType(Object value) throws JSONException { if (null == value) { - return JsonEncoder.VTYPE_UNDEFINED; + return JsonEncoder.VTYPE_NULL; } String transportType = typeToTransportType.get(value.getClass()); if (null != transportType) { return transportType; } - // TODO throw exception? - return JsonEncoder.VTYPE_UNDEFINED; + throw new JSONException("Unknown object type " + + value.getClass().getName()); } } -- 2.39.5