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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.Iterator;
  10. import java.util.List;
  11. import java.util.Map;
  12. import java.util.Set;
  13. import com.google.gwt.json.client.JSONArray;
  14. import com.google.gwt.json.client.JSONObject;
  15. import com.google.gwt.json.client.JSONParser;
  16. import com.google.gwt.json.client.JSONString;
  17. import com.google.gwt.json.client.JSONValue;
  18. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  19. import com.vaadin.terminal.gwt.client.Connector;
  20. import com.vaadin.terminal.gwt.client.ConnectorMap;
  21. import com.vaadin.terminal.gwt.client.ServerConnector;
  22. /**
  23. * Client side decoder for decodeing shared state and other values from JSON
  24. * received from the server.
  25. *
  26. * Currently, basic data types as well as Map, String[] and Object[] are
  27. * supported, where maps and Object[] can contain other supported data types.
  28. *
  29. * TODO extensible type support
  30. *
  31. * @since 7.0
  32. */
  33. public class JsonDecoder {
  34. /**
  35. * Decode a JSON array with two elements (type and value) into a client-side
  36. * type, recursively if necessary.
  37. *
  38. * @param jsonArray
  39. * JSON array with two elements
  40. * @param idMapper
  41. * mapper between connector ID and {@link ServerConnector}
  42. * objects
  43. * @param connection
  44. * reference to the current ApplicationConnection
  45. * @return decoded value (does not contain JSON types)
  46. */
  47. public static Object decodeValue(Type type, JSONArray jsonArray,
  48. Object target, ApplicationConnection connection) {
  49. return decodeValue(type, jsonArray.get(1), target, connection);
  50. }
  51. private static Object decodeValue(Type type, JSONValue jsonValue,
  52. Object target, ApplicationConnection connection) {
  53. // Null is null, regardless of type
  54. if (jsonValue.isNull() != null) {
  55. return null;
  56. }
  57. String baseTypeName = type.getBaseTypeName();
  58. if (baseTypeName.endsWith("[]")) {
  59. return decodeArray(type, (JSONArray) jsonValue, connection);
  60. } else if (Map.class.getName().equals(baseTypeName)
  61. || HashMap.class.getName().equals(baseTypeName)) {
  62. return decodeMap(type, (JSONObject) jsonValue, connection);
  63. } else if (List.class.getName().equals(baseTypeName)
  64. || ArrayList.class.getName().equals(baseTypeName)) {
  65. return decodeList(type, (JSONArray) jsonValue, connection);
  66. } else if (Set.class.getName().equals(baseTypeName)) {
  67. return decodeSet(type, (JSONArray) jsonValue, connection);
  68. } else if (String.class.getName().equals(baseTypeName)) {
  69. return ((JSONString) jsonValue).stringValue();
  70. } else if (Integer.class.getName().equals(baseTypeName)) {
  71. return Integer.valueOf(String.valueOf(jsonValue));
  72. } else if (Long.class.getName().equals(baseTypeName)) {
  73. // TODO handle properly
  74. return Long.valueOf(String.valueOf(jsonValue));
  75. } else if (Float.class.getName().equals(baseTypeName)) {
  76. // TODO handle properly
  77. return Float.valueOf(String.valueOf(jsonValue));
  78. } else if (Double.class.getName().equals(baseTypeName)) {
  79. // TODO handle properly
  80. return Double.valueOf(String.valueOf(jsonValue));
  81. } else if (Boolean.class.getName().equals(baseTypeName)) {
  82. // TODO handle properly
  83. return Boolean.valueOf(String.valueOf(jsonValue));
  84. } else if (Connector.class.getName().equals(baseTypeName)) {
  85. return ConnectorMap.get(connection).getConnector(
  86. ((JSONString) jsonValue).stringValue());
  87. } else {
  88. return decodeObject(type, jsonValue, target, connection);
  89. }
  90. }
  91. private static Object decodeObject(Type type, JSONValue jsonValue,
  92. Object target, ApplicationConnection connection) {
  93. JSONSerializer<Object> serializer = connection.getSerializerMap()
  94. .getSerializer(type.getBaseTypeName());
  95. // TODO handle case with no serializer found
  96. // Currently getSerializer throws exception if not found
  97. if (target != null && serializer instanceof DiffJSONSerializer<?>) {
  98. DiffJSONSerializer<Object> diffSerializer = (DiffJSONSerializer<Object>) serializer;
  99. diffSerializer.update(target, type, jsonValue, connection);
  100. return target;
  101. } else {
  102. Object object = serializer.deserialize(type, jsonValue, connection);
  103. return object;
  104. }
  105. }
  106. private static Map<Object, Object> decodeMap(Type type, JSONObject jsonMap,
  107. ApplicationConnection connection) {
  108. HashMap<Object, Object> map = new HashMap<Object, Object>();
  109. Iterator<String> it = jsonMap.keySet().iterator();
  110. while (it.hasNext()) {
  111. String key = it.next();
  112. JSONArray encodedKey = (JSONArray) JSONParser.parseStrict(key);
  113. JSONArray encodedValue = (JSONArray) jsonMap.get(key);
  114. Object decodedKey = decodeValue(type.getParameterTypes()[0],
  115. encodedKey, null, connection);
  116. Object decodedValue = decodeValue(type.getParameterTypes()[1],
  117. encodedValue, null, connection);
  118. map.put(decodedKey, decodedValue);
  119. }
  120. return map;
  121. }
  122. private static Object[] decodeArray(Type type, JSONArray jsonArray,
  123. ApplicationConnection connection) {
  124. String arrayTypeName = type.getBaseTypeName();
  125. String chldTypeName = arrayTypeName.substring(0,
  126. arrayTypeName.length() - 2);
  127. List<Object> list = decodeList(new Type(chldTypeName, null), jsonArray,
  128. connection);
  129. return list.toArray(new Object[list.size()]);
  130. }
  131. private static List<Object> decodeList(Type type, JSONArray jsonArray,
  132. ApplicationConnection connection) {
  133. List<Object> tokens = new ArrayList<Object>();
  134. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  135. connection, tokens);
  136. return tokens;
  137. }
  138. private static Set<Object> decodeSet(Type type, JSONArray jsonArray,
  139. ApplicationConnection connection) {
  140. Set<Object> tokens = new HashSet<Object>();
  141. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  142. connection, tokens);
  143. return tokens;
  144. }
  145. private static void decodeIntoCollection(Type childType,
  146. JSONArray jsonArray, ApplicationConnection connection,
  147. Collection<Object> tokens) {
  148. for (int i = 0; i < jsonArray.size(); ++i) {
  149. // each entry always has two elements: type and value
  150. JSONArray entryArray = (JSONArray) jsonArray.get(i);
  151. tokens.add(decodeValue(childType, entryArray, null, connection));
  152. }
  153. }
  154. }