You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JsonCodec.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.beans.Introspector;
  6. import java.beans.PropertyDescriptor;
  7. import java.io.Serializable;
  8. import java.lang.reflect.Method;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.Iterator;
  12. import java.util.List;
  13. import java.util.Map;
  14. import com.vaadin.external.json.JSONArray;
  15. import com.vaadin.external.json.JSONException;
  16. import com.vaadin.external.json.JSONObject;
  17. import com.vaadin.terminal.Paintable;
  18. import com.vaadin.terminal.gwt.client.communication.JsonEncoder;
  19. import com.vaadin.terminal.gwt.client.communication.SharedState;
  20. /**
  21. * Decoder for converting RPC parameters and other values from JSON in transfer
  22. * between the client and the server and vice versa.
  23. *
  24. * @since 7.0
  25. */
  26. public class JsonCodec implements Serializable {
  27. private static Map<Class<?>, String> typeToTransportType = new HashMap<Class<?>, String>();
  28. static {
  29. registerType(String.class, JsonEncoder.VTYPE_STRING);
  30. registerType(Paintable.class, JsonEncoder.VTYPE_PAINTABLE);
  31. registerType(Boolean.class, JsonEncoder.VTYPE_BOOLEAN);
  32. registerType(Integer.class, JsonEncoder.VTYPE_INTEGER);
  33. registerType(Float.class, JsonEncoder.VTYPE_FLOAT);
  34. registerType(Double.class, JsonEncoder.VTYPE_DOUBLE);
  35. registerType(Long.class, JsonEncoder.VTYPE_LONG);
  36. // transported as string representation
  37. registerType(Enum.class, JsonEncoder.VTYPE_STRING);
  38. registerType(String[].class, JsonEncoder.VTYPE_STRINGARRAY);
  39. registerType(Object[].class, JsonEncoder.VTYPE_ARRAY);
  40. registerType(Map.class, JsonEncoder.VTYPE_MAP);
  41. }
  42. private static void registerType(Class<?> type, String transportType) {
  43. typeToTransportType.put(type, transportType);
  44. }
  45. /**
  46. * Convert a JSON array with two elements (type and value) into a
  47. * server-side type, recursively if necessary.
  48. *
  49. * @param value
  50. * JSON array with two elements
  51. * @param idMapper
  52. * mapper between paintable ID and {@link Paintable} objects
  53. * @return converted value (does not contain JSON types)
  54. * @throws JSONException
  55. * if the conversion fails
  56. */
  57. public static Object convertVariableValue(JSONArray value,
  58. PaintableIdMapper idMapper) throws JSONException {
  59. return convertVariableValue(value.getString(0), value.get(1), idMapper);
  60. }
  61. private static Object convertVariableValue(String variableType,
  62. Object value, PaintableIdMapper idMapper) throws JSONException {
  63. Object val = null;
  64. // TODO type checks etc.
  65. if (JsonEncoder.VTYPE_ARRAY.equals(variableType)) {
  66. val = convertArray((JSONArray) value, idMapper);
  67. } else if (JsonEncoder.VTYPE_MAP.equals(variableType)) {
  68. val = convertMap((JSONObject) value, idMapper);
  69. } else if (JsonEncoder.VTYPE_STRINGARRAY.equals(variableType)) {
  70. val = convertStringArray((JSONArray) value);
  71. } else if (JsonEncoder.VTYPE_STRING.equals(variableType)) {
  72. val = value;
  73. } else if (JsonEncoder.VTYPE_INTEGER.equals(variableType)) {
  74. // TODO handle properly
  75. val = Integer.valueOf(String.valueOf(value));
  76. } else if (JsonEncoder.VTYPE_LONG.equals(variableType)) {
  77. // TODO handle properly
  78. val = Long.valueOf(String.valueOf(value));
  79. } else if (JsonEncoder.VTYPE_FLOAT.equals(variableType)) {
  80. // TODO handle properly
  81. val = Float.valueOf(String.valueOf(value));
  82. } else if (JsonEncoder.VTYPE_DOUBLE.equals(variableType)) {
  83. // TODO handle properly
  84. val = Double.valueOf(String.valueOf(value));
  85. } else if (JsonEncoder.VTYPE_BOOLEAN.equals(variableType)) {
  86. // TODO handle properly
  87. val = Boolean.valueOf(String.valueOf(value));
  88. } else if (JsonEncoder.VTYPE_PAINTABLE.equals(variableType)) {
  89. // TODO handle properly
  90. val = idMapper.getPaintable(String.valueOf(value));
  91. }
  92. return val;
  93. }
  94. private static Object convertMap(JSONObject jsonMap,
  95. PaintableIdMapper idMapper) throws JSONException {
  96. HashMap<String, Object> map = new HashMap<String, Object>();
  97. Iterator<String> it = jsonMap.keys();
  98. while (it.hasNext()) {
  99. String key = it.next();
  100. map.put(key,
  101. convertVariableValue(jsonMap.getJSONArray(key), idMapper));
  102. }
  103. return map;
  104. }
  105. private static String[] convertStringArray(JSONArray jsonArray)
  106. throws JSONException {
  107. int length = jsonArray.length();
  108. List<String> tokens = new ArrayList<String>(length);
  109. for (int i = 0; i < length; ++i) {
  110. tokens.add(jsonArray.getString(i));
  111. }
  112. return tokens.toArray(new String[tokens.size()]);
  113. }
  114. private static Object convertArray(JSONArray jsonArray,
  115. PaintableIdMapper idMapper) throws JSONException {
  116. List<Object> tokens = new ArrayList<Object>();
  117. for (int i = 0; i < jsonArray.length(); ++i) {
  118. // each entry always has two elements: type and value
  119. JSONArray entryArray = jsonArray.getJSONArray(i);
  120. tokens.add(convertVariableValue(entryArray, idMapper));
  121. }
  122. return tokens.toArray(new Object[tokens.size()]);
  123. }
  124. /**
  125. * Encode a value to a JSON representation for transport from the client to
  126. * the server.
  127. *
  128. * @param value
  129. * value to convert
  130. * @param idMapper
  131. * mapper between paintable ID and {@link Paintable} objects
  132. * @return JSON representation of the value
  133. * @throws JSONException
  134. * if encoding a value fails (e.g. NaN or infinite number)
  135. */
  136. public static JSONArray encode(Object value, PaintableIdMapper idMapper)
  137. throws JSONException {
  138. if (null == value) {
  139. // TODO as undefined type?
  140. return combineTypeAndValue(JsonEncoder.VTYPE_UNDEFINED,
  141. JSONObject.NULL);
  142. } else if (value instanceof SharedState) {
  143. return combineTypeAndValue(value.getClass().getName(),
  144. encodeObject(value, idMapper));
  145. } else if (value instanceof String[]) {
  146. String[] array = (String[]) value;
  147. JSONArray jsonArray = new JSONArray();
  148. for (int i = 0; i < array.length; ++i) {
  149. jsonArray.put(array[i]);
  150. }
  151. return combineTypeAndValue(JsonEncoder.VTYPE_STRINGARRAY, jsonArray);
  152. } else if (value instanceof String) {
  153. return combineTypeAndValue(JsonEncoder.VTYPE_STRING, value);
  154. } else if (value instanceof Boolean) {
  155. return combineTypeAndValue(JsonEncoder.VTYPE_BOOLEAN, value);
  156. } else if (value instanceof Object[]) {
  157. Object[] array = (Object[]) value;
  158. JSONArray jsonArray = encodeArrayContents(array, idMapper);
  159. return combineTypeAndValue(JsonEncoder.VTYPE_ARRAY, jsonArray);
  160. } else if (value instanceof Map) {
  161. Map<String, Object> map = (Map<String, Object>) value;
  162. JSONObject jsonMap = encodeMapContents(map, idMapper);
  163. return combineTypeAndValue(JsonEncoder.VTYPE_MAP, jsonMap);
  164. } else if (value instanceof Paintable) {
  165. Paintable paintable = (Paintable) value;
  166. return combineTypeAndValue(JsonEncoder.VTYPE_PAINTABLE,
  167. idMapper.getPaintableId(paintable));
  168. } else {
  169. return combineTypeAndValue(getTransportType(value),
  170. String.valueOf(value));
  171. }
  172. }
  173. private static Object encodeObject(Object value, PaintableIdMapper idMapper)
  174. throws JSONException {
  175. JSONObject jsonMap = new JSONObject();
  176. try {
  177. for (PropertyDescriptor pd : Introspector.getBeanInfo(
  178. value.getClass()).getPropertyDescriptors()) {
  179. String fieldName = pd.getName();
  180. if (pd.getReadMethod() == null || pd.getWriteMethod() == null) {
  181. continue;
  182. }
  183. Method getterMethod = pd.getReadMethod();
  184. Object fieldValue = getterMethod.invoke(value, null);
  185. jsonMap.put(fieldName, encode(fieldValue, idMapper));
  186. }
  187. } catch (Exception e) {
  188. // TODO: Should exceptions be handled in a different way?
  189. throw new JSONException(e);
  190. }
  191. return jsonMap;
  192. }
  193. private static JSONArray encodeArrayContents(Object[] array,
  194. PaintableIdMapper idMapper) throws JSONException {
  195. JSONArray jsonArray = new JSONArray();
  196. for (int i = 0; i < array.length; ++i) {
  197. // TODO handle object graph loops?
  198. jsonArray.put(encode(array[i], idMapper));
  199. }
  200. return jsonArray;
  201. }
  202. private static JSONObject encodeMapContents(Map<String, Object> map,
  203. PaintableIdMapper idMapper) throws JSONException {
  204. JSONObject jsonMap = new JSONObject();
  205. for (String mapKey : map.keySet()) {
  206. // TODO handle object graph loops?
  207. Object mapValue = map.get(mapKey);
  208. jsonMap.put(mapKey, encode(mapValue, idMapper));
  209. }
  210. return jsonMap;
  211. }
  212. private static JSONArray combineTypeAndValue(String type, Object value) {
  213. JSONArray outerArray = new JSONArray();
  214. outerArray.put(type);
  215. outerArray.put(value);
  216. return outerArray;
  217. }
  218. private static String getTransportType(Object value) {
  219. if (null == value) {
  220. return JsonEncoder.VTYPE_UNDEFINED;
  221. }
  222. String transportType = typeToTransportType.get(value.getClass());
  223. if (null != transportType) {
  224. return transportType;
  225. }
  226. // TODO throw exception?
  227. return JsonEncoder.VTYPE_UNDEFINED;
  228. }
  229. }