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

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