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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 (Map.class.getName().equals(baseTypeName)
  49. || HashMap.class.getName().equals(baseTypeName)) {
  50. return decodeMap(type, jsonValue, connection);
  51. } else if (List.class.getName().equals(baseTypeName)
  52. || ArrayList.class.getName().equals(baseTypeName)) {
  53. return decodeList(type, (JSONArray) jsonValue, connection);
  54. } else if (Set.class.getName().equals(baseTypeName)) {
  55. return decodeSet(type, (JSONArray) jsonValue, connection);
  56. } else if (String.class.getName().equals(baseTypeName)) {
  57. return ((JSONString) jsonValue).stringValue();
  58. } else if (Integer.class.getName().equals(baseTypeName)) {
  59. return Integer.valueOf(String.valueOf(jsonValue));
  60. } else if (Long.class.getName().equals(baseTypeName)) {
  61. // TODO handle properly
  62. return Long.valueOf(String.valueOf(jsonValue));
  63. } else if (Float.class.getName().equals(baseTypeName)) {
  64. // TODO handle properly
  65. return Float.valueOf(String.valueOf(jsonValue));
  66. } else if (Double.class.getName().equals(baseTypeName)) {
  67. // TODO handle properly
  68. return Double.valueOf(String.valueOf(jsonValue));
  69. } else if (Boolean.class.getName().equals(baseTypeName)) {
  70. // TODO handle properly
  71. return Boolean.valueOf(String.valueOf(jsonValue));
  72. } else if (Byte.class.getName().equals(baseTypeName)) {
  73. // TODO handle properly
  74. return Byte.valueOf(String.valueOf(jsonValue));
  75. } else if (Character.class.getName().equals(baseTypeName)) {
  76. // TODO handle properly
  77. return Character.valueOf(((JSONString) jsonValue).stringValue()
  78. .charAt(0));
  79. } else if (Connector.class.getName().equals(baseTypeName)) {
  80. return ConnectorMap.get(connection).getConnector(
  81. ((JSONString) jsonValue).stringValue());
  82. } else {
  83. return decodeObject(type, jsonValue, target, connection);
  84. }
  85. }
  86. private static Object decodeObject(Type type, JSONValue jsonValue,
  87. Object target, ApplicationConnection connection) {
  88. JSONSerializer<Object> serializer = connection.getSerializerMap()
  89. .getSerializer(type.getBaseTypeName());
  90. // TODO handle case with no serializer found
  91. // Currently getSerializer throws exception if not found
  92. if (target != null && serializer instanceof DiffJSONSerializer<?>) {
  93. DiffJSONSerializer<Object> diffSerializer = (DiffJSONSerializer<Object>) serializer;
  94. diffSerializer.update(target, type, jsonValue, connection);
  95. return target;
  96. } else {
  97. Object object = serializer.deserialize(type, jsonValue, connection);
  98. return object;
  99. }
  100. }
  101. private static Map<Object, Object> decodeMap(Type type, JSONValue jsonMap,
  102. ApplicationConnection connection) {
  103. // Client -> server encodes empty map as an empty array because of
  104. // #8906. Do the same for server -> client to maintain symmetry.
  105. if (jsonMap instanceof JSONArray) {
  106. JSONArray array = (JSONArray) jsonMap;
  107. if (array.size() == 0) {
  108. return new HashMap<Object, Object>();
  109. }
  110. }
  111. Type keyType = type.getParameterTypes()[0];
  112. Type valueType = type.getParameterTypes()[1];
  113. if (keyType.getBaseTypeName().equals(String.class.getName())) {
  114. return decodeStringMap(valueType, jsonMap, connection);
  115. } else if (keyType.getBaseTypeName().equals(Connector.class.getName())) {
  116. return decodeConnectorMap(valueType, jsonMap, connection);
  117. } else {
  118. return decodeObjectMap(keyType, valueType, jsonMap, connection);
  119. }
  120. }
  121. private static Map<Object, Object> decodeObjectMap(Type keyType,
  122. Type valueType, JSONValue jsonValue,
  123. ApplicationConnection connection) {
  124. Map<Object, Object> map = new HashMap<Object, Object>();
  125. JSONArray mapArray = (JSONArray) jsonValue;
  126. JSONArray keys = (JSONArray) mapArray.get(0);
  127. JSONArray values = (JSONArray) mapArray.get(1);
  128. assert (keys.size() == values.size());
  129. for (int i = 0; i < keys.size(); i++) {
  130. Object decodedKey = decodeValue(keyType, keys.get(i), null,
  131. connection);
  132. Object decodedValue = decodeValue(valueType, values.get(i), null,
  133. connection);
  134. map.put(decodedKey, decodedValue);
  135. }
  136. return map;
  137. }
  138. private static Map<Object, Object> decodeConnectorMap(Type valueType,
  139. JSONValue jsonValue, ApplicationConnection connection) {
  140. Map<Object, Object> map = new HashMap<Object, Object>();
  141. JSONObject jsonMap = (JSONObject) jsonValue;
  142. ConnectorMap connectorMap = ConnectorMap.get(connection);
  143. for (String connectorId : jsonMap.keySet()) {
  144. Object value = decodeValue(valueType, jsonMap.get(connectorId),
  145. null, connection);
  146. map.put(connectorMap.getConnector(connectorId), value);
  147. }
  148. return map;
  149. }
  150. private static Map<Object, Object> decodeStringMap(Type valueType,
  151. JSONValue jsonValue, ApplicationConnection connection) {
  152. Map<Object, Object> map = new HashMap<Object, Object>();
  153. JSONObject jsonMap = (JSONObject) jsonValue;
  154. for (String key : jsonMap.keySet()) {
  155. Object value = decodeValue(valueType, jsonMap.get(key), null,
  156. connection);
  157. map.put(key, value);
  158. }
  159. return map;
  160. }
  161. private static List<Object> decodeList(Type type, JSONArray jsonArray,
  162. ApplicationConnection connection) {
  163. List<Object> tokens = new ArrayList<Object>();
  164. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  165. connection, tokens);
  166. return tokens;
  167. }
  168. private static Set<Object> decodeSet(Type type, JSONArray jsonArray,
  169. ApplicationConnection connection) {
  170. Set<Object> tokens = new HashSet<Object>();
  171. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  172. connection, tokens);
  173. return tokens;
  174. }
  175. private static void decodeIntoCollection(Type childType,
  176. JSONArray jsonArray, ApplicationConnection connection,
  177. Collection<Object> tokens) {
  178. for (int i = 0; i < jsonArray.size(); ++i) {
  179. // each entry always has two elements: type and value
  180. JSONValue entryValue = jsonArray.get(i);
  181. tokens.add(decodeValue(childType, entryValue, null, connection));
  182. }
  183. }
  184. }