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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.ConnectorMap;
  20. import com.vaadin.terminal.gwt.client.ServerConnector;
  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 jsonArray
  38. * JSON array with two elements
  39. * @param idMapper
  40. * mapper between connector ID and {@link ServerConnector}
  41. * objects
  42. * @param connection
  43. * reference to the current ApplicationConnection
  44. * @return decoded value (does not contain JSON types)
  45. */
  46. public static Object decodeValue(JSONArray jsonArray, Object target,
  47. ApplicationConnection connection) {
  48. String type = ((JSONString) jsonArray.get(0)).stringValue();
  49. return decodeValue(type, jsonArray.get(1), target, connection);
  50. }
  51. private static Object decodeValue(String variableType, JSONValue value,
  52. Object target, ApplicationConnection connection) {
  53. Object val = null;
  54. // TODO type checks etc.
  55. if (JsonEncoder.VTYPE_NULL.equals(variableType)) {
  56. val = null;
  57. } else if (JsonEncoder.VTYPE_ARRAY.equals(variableType)) {
  58. val = decodeArray((JSONArray) value, connection);
  59. } else if (JsonEncoder.VTYPE_MAP.equals(variableType)) {
  60. val = decodeMap((JSONObject) value, connection);
  61. } else if (JsonEncoder.VTYPE_LIST.equals(variableType)) {
  62. val = decodeList((JSONArray) value, connection);
  63. } else if (JsonEncoder.VTYPE_SET.equals(variableType)) {
  64. val = decodeSet((JSONArray) value, connection);
  65. } else if (JsonEncoder.VTYPE_STRINGARRAY.equals(variableType)) {
  66. val = decodeStringArray((JSONArray) value);
  67. } else if (JsonEncoder.VTYPE_STRING.equals(variableType)) {
  68. val = ((JSONString) value).stringValue();
  69. } else if (JsonEncoder.VTYPE_INTEGER.equals(variableType)) {
  70. // TODO handle properly
  71. val = Integer.valueOf(String.valueOf(value));
  72. } else if (JsonEncoder.VTYPE_LONG.equals(variableType)) {
  73. // TODO handle properly
  74. val = Long.valueOf(String.valueOf(value));
  75. } else if (JsonEncoder.VTYPE_FLOAT.equals(variableType)) {
  76. // TODO handle properly
  77. val = Float.valueOf(String.valueOf(value));
  78. } else if (JsonEncoder.VTYPE_DOUBLE.equals(variableType)) {
  79. // TODO handle properly
  80. val = Double.valueOf(String.valueOf(value));
  81. } else if (JsonEncoder.VTYPE_BOOLEAN.equals(variableType)) {
  82. // TODO handle properly
  83. val = Boolean.valueOf(String.valueOf(value));
  84. } else if (JsonEncoder.VTYPE_CONNECTOR.equals(variableType)) {
  85. val = ConnectorMap.get(connection).getConnector(
  86. ((JSONString) value).stringValue());
  87. } else {
  88. return decodeObject(new Type(variableType, null), value, target,
  89. connection);
  90. }
  91. return val;
  92. }
  93. private static Object decodeObject(Type type, JSONValue encodedValue,
  94. Object target, ApplicationConnection connection) {
  95. // object, class name as type
  96. JSONSerializer<Object> serializer = connection.getSerializerMap()
  97. .getSerializer(type.getBaseTypeName());
  98. // TODO handle case with no serializer found
  99. if (target != null && serializer instanceof DiffJSONSerializer<?>) {
  100. DiffJSONSerializer<Object> diffSerializer = (DiffJSONSerializer<Object>) serializer;
  101. diffSerializer.update(target, type, encodedValue, connection);
  102. return target;
  103. } else {
  104. Object object = serializer.deserialize(type, encodedValue,
  105. connection);
  106. return object;
  107. }
  108. }
  109. private static Map<Object, Object> decodeMap(JSONObject jsonMap,
  110. ApplicationConnection connection) {
  111. HashMap<Object, Object> map = new HashMap<Object, Object>();
  112. Iterator<String> it = jsonMap.keySet().iterator();
  113. while (it.hasNext()) {
  114. String key = it.next();
  115. JSONArray encodedKey = (JSONArray) JSONParser.parseStrict(key);
  116. JSONArray encodedValue = (JSONArray) jsonMap.get(key);
  117. Object decodedKey = decodeValue(encodedKey, null, connection);
  118. Object decodedValue = decodeValue(encodedValue, null, connection);
  119. map.put(decodedKey, decodedValue);
  120. }
  121. return map;
  122. }
  123. private static String[] decodeStringArray(JSONArray jsonArray) {
  124. int size = jsonArray.size();
  125. List<String> tokens = new ArrayList<String>(size);
  126. for (int i = 0; i < size; ++i) {
  127. tokens.add(String.valueOf(jsonArray.get(i)));
  128. }
  129. return tokens.toArray(new String[tokens.size()]);
  130. }
  131. private static Object[] decodeArray(JSONArray jsonArray,
  132. ApplicationConnection connection) {
  133. List<Object> list = decodeList(jsonArray, connection);
  134. return list.toArray(new Object[list.size()]);
  135. }
  136. private static List<Object> decodeList(JSONArray jsonArray,
  137. ApplicationConnection connection) {
  138. List<Object> tokens = new ArrayList<Object>();
  139. decodeIntoCollection(jsonArray, connection, tokens);
  140. return tokens;
  141. }
  142. private static Set<Object> decodeSet(JSONArray jsonArray,
  143. ApplicationConnection connection) {
  144. Set<Object> tokens = new HashSet<Object>();
  145. decodeIntoCollection(jsonArray, connection, tokens);
  146. return tokens;
  147. }
  148. private static void decodeIntoCollection(JSONArray jsonArray,
  149. ApplicationConnection connection, Collection<Object> tokens) {
  150. for (int i = 0; i < jsonArray.size(); ++i) {
  151. // each entry always has two elements: type and value
  152. JSONArray entryArray = (JSONArray) jsonArray.get(i);
  153. tokens.add(decodeValue(entryArray, null, connection));
  154. }
  155. }
  156. }