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
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
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();
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));
}
}
}
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;
} else if (value instanceof Map) {
return VTYPE_MAP;
}
- // TODO throw exception?
- return VTYPE_UNDEFINED;
+ return null;
}
-
}
} 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);
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();
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 {
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());
}
}