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 8.1KB

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