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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.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. import com.vaadin.terminal.gwt.client.ServerConnector;
  20. /**
  21. * Client side decoder for decodeing shared state and other values from JSON
  22. * received from the server.
  23. *
  24. * Currently, basic data types as well as Map, String[] and Object[] are
  25. * supported, where maps and Object[] can contain other supported data types.
  26. *
  27. * TODO extensible type support
  28. *
  29. * @since 7.0
  30. */
  31. public class JsonDecoder {
  32. /**
  33. * Decode a JSON array with two elements (type and value) into a client-side
  34. * type, recursively if necessary.
  35. *
  36. * @param jsonArray
  37. * JSON array with two elements
  38. * @param idMapper
  39. * mapper between connector ID and {@link ServerConnector}
  40. * objects
  41. * @param connection
  42. * reference to the current ApplicationConnection
  43. * @return decoded value (does not contain JSON types)
  44. */
  45. public static Object decodeValue(JSONArray jsonArray, Object target,
  46. ConnectorMap idMapper, ApplicationConnection connection) {
  47. String type = ((JSONString) jsonArray.get(0)).stringValue();
  48. return decodeValue(type, jsonArray.get(1), target, idMapper, connection);
  49. }
  50. private static Object decodeValue(String variableType, JSONValue value,
  51. Object target, ConnectorMap idMapper,
  52. 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, idMapper, connection);
  59. } else if (JsonEncoder.VTYPE_MAP.equals(variableType)) {
  60. val = decodeMap((JSONObject) value, idMapper, connection);
  61. } else if (JsonEncoder.VTYPE_MAP_CONNECTOR.equals(variableType)) {
  62. val = decodeConnectorMap((JSONObject) value, idMapper, connection);
  63. } else if (JsonEncoder.VTYPE_LIST.equals(variableType)) {
  64. val = decodeList((JSONArray) value, idMapper, connection);
  65. } else if (JsonEncoder.VTYPE_SET.equals(variableType)) {
  66. val = decodeSet((JSONArray) value, idMapper, connection);
  67. } else if (JsonEncoder.VTYPE_STRINGARRAY.equals(variableType)) {
  68. val = decodeStringArray((JSONArray) value);
  69. } else if (JsonEncoder.VTYPE_STRING.equals(variableType)) {
  70. val = ((JSONString) value).stringValue();
  71. } else if (JsonEncoder.VTYPE_INTEGER.equals(variableType)) {
  72. // TODO handle properly
  73. val = Integer.valueOf(String.valueOf(value));
  74. } else if (JsonEncoder.VTYPE_LONG.equals(variableType)) {
  75. // TODO handle properly
  76. val = Long.valueOf(String.valueOf(value));
  77. } else if (JsonEncoder.VTYPE_FLOAT.equals(variableType)) {
  78. // TODO handle properly
  79. val = Float.valueOf(String.valueOf(value));
  80. } else if (JsonEncoder.VTYPE_DOUBLE.equals(variableType)) {
  81. // TODO handle properly
  82. val = Double.valueOf(String.valueOf(value));
  83. } else if (JsonEncoder.VTYPE_BOOLEAN.equals(variableType)) {
  84. // TODO handle properly
  85. val = Boolean.valueOf(String.valueOf(value));
  86. } else if (JsonEncoder.VTYPE_CONNECTOR.equals(variableType)) {
  87. val = idMapper.getConnector(((JSONString) value).stringValue());
  88. } else {
  89. return decodeObject(variableType, value, target, idMapper,
  90. connection);
  91. }
  92. return val;
  93. }
  94. private static Object decodeObject(String variableType,
  95. JSONValue encodedValue, Object target, ConnectorMap idMapper,
  96. ApplicationConnection connection) {
  97. // object, class name as type
  98. JSONSerializer<Object> serializer = connection.getSerializerMap()
  99. .getSerializer(variableType);
  100. // TODO handle case with no serializer found
  101. Object object = serializer.deserialize(encodedValue, target, idMapper,
  102. connection);
  103. return object;
  104. }
  105. private static Map<String, Object> decodeMap(JSONObject jsonMap,
  106. ConnectorMap idMapper, ApplicationConnection connection) {
  107. HashMap<String, Object> map = new HashMap<String, Object>();
  108. Iterator<String> it = jsonMap.keySet().iterator();
  109. while (it.hasNext()) {
  110. String key = it.next();
  111. map.put(key,
  112. decodeValue((JSONArray) jsonMap.get(key), null, idMapper,
  113. connection));
  114. }
  115. return map;
  116. }
  117. private static Map<Connector, Object> decodeConnectorMap(
  118. JSONObject jsonMap, ConnectorMap idMapper,
  119. ApplicationConnection connection) {
  120. HashMap<Connector, Object> map = new HashMap<Connector, Object>();
  121. Iterator<String> it = jsonMap.keySet().iterator();
  122. while (it.hasNext()) {
  123. String connectorId = it.next();
  124. Connector connector = idMapper.getConnector(connectorId);
  125. map.put(connector,
  126. decodeValue((JSONArray) jsonMap.get(connectorId), null,
  127. idMapper, connection));
  128. }
  129. return map;
  130. }
  131. private static String[] decodeStringArray(JSONArray jsonArray) {
  132. int size = jsonArray.size();
  133. List<String> tokens = new ArrayList<String>(size);
  134. for (int i = 0; i < size; ++i) {
  135. tokens.add(String.valueOf(jsonArray.get(i)));
  136. }
  137. return tokens.toArray(new String[tokens.size()]);
  138. }
  139. private static Object[] decodeArray(JSONArray jsonArray,
  140. ConnectorMap idMapper, ApplicationConnection connection) {
  141. List<Object> list = decodeList(jsonArray, idMapper, connection);
  142. return list.toArray(new Object[list.size()]);
  143. }
  144. private static List<Object> decodeList(JSONArray jsonArray,
  145. ConnectorMap idMapper, ApplicationConnection connection) {
  146. List<Object> tokens = new ArrayList<Object>();
  147. for (int i = 0; i < jsonArray.size(); ++i) {
  148. // each entry always has two elements: type and value
  149. JSONArray entryArray = (JSONArray) jsonArray.get(i);
  150. tokens.add(decodeValue(entryArray, null, idMapper, connection));
  151. }
  152. return tokens;
  153. }
  154. private static Set<Object> decodeSet(JSONArray jsonArray,
  155. ConnectorMap idMapper, ApplicationConnection connection) {
  156. Set<Object> tokens = new HashSet<Object>();
  157. for (int i = 0; i < jsonArray.size(); ++i) {
  158. // each entry always has two elements: type and value
  159. JSONArray entryArray = (JSONArray) jsonArray.get(i);
  160. tokens.add(decodeValue(entryArray, null, idMapper, connection));
  161. }
  162. return tokens;
  163. }
  164. }