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

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