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.

JsonDecoder.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.communication;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.HashMap;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.Set;
  12. import com.google.gwt.json.client.JSONArray;
  13. import com.google.gwt.json.client.JSONObject;
  14. import com.google.gwt.json.client.JSONString;
  15. import com.google.gwt.json.client.JSONValue;
  16. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  17. import com.vaadin.terminal.gwt.client.Connector;
  18. import com.vaadin.terminal.gwt.client.ConnectorMap;
  19. /**
  20. * Client side decoder for decodeing shared state and other values from JSON
  21. * received from the server.
  22. *
  23. * Currently, basic data types as well as Map, String[] and Object[] are
  24. * supported, where maps and Object[] can contain other supported data types.
  25. *
  26. * TODO extensible type support
  27. *
  28. * @since 7.0
  29. */
  30. public class JsonDecoder {
  31. /**
  32. * Decode a JSON array with two elements (type and value) into a client-side
  33. * type, recursively if necessary.
  34. *
  35. * @param jsonValue
  36. * JSON value with encoded data
  37. * @param connection
  38. * reference to the current ApplicationConnection
  39. * @return decoded value (does not contain JSON types)
  40. */
  41. public static Object decodeValue(Type type, JSONValue jsonValue,
  42. Object target, ApplicationConnection connection) {
  43. // Null is null, regardless of type
  44. if (jsonValue.isNull() != null) {
  45. return null;
  46. }
  47. String baseTypeName = type.getBaseTypeName();
  48. if (baseTypeName.endsWith("[]")) {
  49. return decodeArray(type, (JSONArray) jsonValue, connection);
  50. } else if (Map.class.getName().equals(baseTypeName)
  51. || HashMap.class.getName().equals(baseTypeName)) {
  52. return decodeMap(type, jsonValue, connection);
  53. } else if (List.class.getName().equals(baseTypeName)
  54. || ArrayList.class.getName().equals(baseTypeName)) {
  55. return decodeList(type, (JSONArray) jsonValue, connection);
  56. } else if (Set.class.getName().equals(baseTypeName)) {
  57. return decodeSet(type, (JSONArray) jsonValue, connection);
  58. } else if (String.class.getName().equals(baseTypeName)) {
  59. return ((JSONString) jsonValue).stringValue();
  60. } else if (Integer.class.getName().equals(baseTypeName)) {
  61. return Integer.valueOf(String.valueOf(jsonValue));
  62. } else if (Long.class.getName().equals(baseTypeName)) {
  63. // TODO handle properly
  64. return Long.valueOf(String.valueOf(jsonValue));
  65. } else if (Float.class.getName().equals(baseTypeName)) {
  66. // TODO handle properly
  67. return Float.valueOf(String.valueOf(jsonValue));
  68. } else if (Double.class.getName().equals(baseTypeName)) {
  69. // TODO handle properly
  70. return Double.valueOf(String.valueOf(jsonValue));
  71. } else if (Boolean.class.getName().equals(baseTypeName)) {
  72. // TODO handle properly
  73. return Boolean.valueOf(String.valueOf(jsonValue));
  74. } else if (Connector.class.getName().equals(baseTypeName)) {
  75. return ConnectorMap.get(connection).getConnector(
  76. ((JSONString) jsonValue).stringValue());
  77. } else {
  78. return decodeObject(type, jsonValue, target, connection);
  79. }
  80. }
  81. private static Object decodeObject(Type type, JSONValue jsonValue,
  82. Object target, ApplicationConnection connection) {
  83. JSONSerializer<Object> serializer = connection.getSerializerMap()
  84. .getSerializer(type.getBaseTypeName());
  85. // TODO handle case with no serializer found
  86. // Currently getSerializer throws exception if not found
  87. if (target != null && serializer instanceof DiffJSONSerializer<?>) {
  88. DiffJSONSerializer<Object> diffSerializer = (DiffJSONSerializer<Object>) serializer;
  89. diffSerializer.update(target, type, jsonValue, connection);
  90. return target;
  91. } else {
  92. Object object = serializer.deserialize(type, jsonValue, connection);
  93. return object;
  94. }
  95. }
  96. private static Map<Object, Object> decodeMap(Type type, JSONValue jsonMap,
  97. ApplicationConnection connection) {
  98. // Client -> server encodes empty map as an empty array because of
  99. // #8906. Do the same for server -> client to maintain symmetry.
  100. if (jsonMap instanceof JSONArray) {
  101. JSONArray array = (JSONArray) jsonMap;
  102. if (array.size() == 0) {
  103. return new HashMap<Object, Object>();
  104. }
  105. }
  106. Type keyType = type.getParameterTypes()[0];
  107. Type valueType = type.getParameterTypes()[1];
  108. if (keyType.getBaseTypeName().equals(String.class.getName())) {
  109. return decodeStringMap(valueType, jsonMap, connection);
  110. } else if (keyType.getBaseTypeName().equals(Connector.class.getName())) {
  111. return decodeConnectorMap(valueType, jsonMap, connection);
  112. } else {
  113. return decodeObjectMap(keyType, valueType, jsonMap, connection);
  114. }
  115. }
  116. private static Map<Object, Object> decodeObjectMap(Type keyType,
  117. Type valueType, JSONValue jsonValue,
  118. ApplicationConnection connection) {
  119. Map<Object, Object> map = new HashMap<Object, Object>();
  120. JSONArray mapArray = (JSONArray) jsonValue;
  121. JSONArray keys = (JSONArray) mapArray.get(0);
  122. JSONArray values = (JSONArray) mapArray.get(1);
  123. assert (keys.size() == values.size());
  124. for (int i = 0; i < keys.size(); i++) {
  125. Object decodedKey = decodeValue(keyType, keys.get(i), null,
  126. connection);
  127. Object decodedValue = decodeValue(valueType, values.get(i), null,
  128. connection);
  129. map.put(decodedKey, decodedValue);
  130. }
  131. return map;
  132. }
  133. private static Map<Object, Object> decodeConnectorMap(Type valueType,
  134. JSONValue jsonValue, ApplicationConnection connection) {
  135. Map<Object, Object> map = new HashMap<Object, Object>();
  136. JSONObject jsonMap = (JSONObject) jsonValue;
  137. ConnectorMap connectorMap = ConnectorMap.get(connection);
  138. for (String connectorId : jsonMap.keySet()) {
  139. Object value = decodeValue(valueType, jsonMap.get(connectorId),
  140. null, connection);
  141. map.put(connectorMap.getConnector(connectorId), value);
  142. }
  143. return map;
  144. }
  145. private static Map<Object, Object> decodeStringMap(Type valueType,
  146. JSONValue jsonValue, ApplicationConnection connection) {
  147. Map<Object, Object> map = new HashMap<Object, Object>();
  148. JSONObject jsonMap = (JSONObject) jsonValue;
  149. for (String key : jsonMap.keySet()) {
  150. Object value = decodeValue(valueType, jsonMap.get(key), null,
  151. connection);
  152. map.put(key, value);
  153. }
  154. return map;
  155. }
  156. private static Object[] decodeArray(Type type, JSONArray jsonArray,
  157. ApplicationConnection connection) {
  158. String arrayTypeName = type.getBaseTypeName();
  159. String chldTypeName = arrayTypeName.substring(0,
  160. arrayTypeName.length() - 2);
  161. List<Object> list = decodeList(new Type(chldTypeName, null), jsonArray,
  162. connection);
  163. return list.toArray(new Object[list.size()]);
  164. }
  165. private static List<Object> decodeList(Type type, JSONArray jsonArray,
  166. ApplicationConnection connection) {
  167. List<Object> tokens = new ArrayList<Object>();
  168. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  169. connection, tokens);
  170. return tokens;
  171. }
  172. private static Set<Object> decodeSet(Type type, JSONArray jsonArray,
  173. ApplicationConnection connection) {
  174. Set<Object> tokens = new HashSet<Object>();
  175. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  176. connection, tokens);
  177. return tokens;
  178. }
  179. private static void decodeIntoCollection(Type childType,
  180. JSONArray jsonArray, ApplicationConnection connection,
  181. Collection<Object> tokens) {
  182. for (int i = 0; i < jsonArray.size(); ++i) {
  183. // each entry always has two elements: type and value
  184. JSONValue entryValue = jsonArray.get(i);
  185. tokens.add(decodeValue(childType, entryValue, null, connection));
  186. }
  187. }
  188. }