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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright 2011 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.terminal.gwt.client.communication;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import com.google.gwt.json.client.JSONArray;
  25. import com.google.gwt.json.client.JSONObject;
  26. import com.google.gwt.json.client.JSONString;
  27. import com.google.gwt.json.client.JSONValue;
  28. import com.vaadin.shared.Connector;
  29. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  30. import com.vaadin.terminal.gwt.client.ConnectorMap;
  31. /**
  32. * Client side decoder for decodeing shared state and other values from JSON
  33. * received from the server.
  34. *
  35. * Currently, basic data types as well as Map, String[] and Object[] are
  36. * supported, where maps and Object[] can contain other supported data types.
  37. *
  38. * TODO extensible type support
  39. *
  40. * @since 7.0
  41. */
  42. public class JsonDecoder {
  43. /**
  44. * Decode a JSON array with two elements (type and value) into a client-side
  45. * type, recursively if necessary.
  46. *
  47. * @param jsonValue
  48. * JSON value with encoded data
  49. * @param connection
  50. * reference to the current ApplicationConnection
  51. * @return decoded value (does not contain JSON types)
  52. */
  53. public static Object decodeValue(Type type, JSONValue jsonValue,
  54. Object target, ApplicationConnection connection) {
  55. // Null is null, regardless of type
  56. if (jsonValue.isNull() != null) {
  57. return null;
  58. }
  59. String baseTypeName = type.getBaseTypeName();
  60. if (Map.class.getName().equals(baseTypeName)
  61. || HashMap.class.getName().equals(baseTypeName)) {
  62. return decodeMap(type, 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 (Byte.class.getName().equals(baseTypeName)) {
  85. // TODO handle properly
  86. return Byte.valueOf(String.valueOf(jsonValue));
  87. } else if (Character.class.getName().equals(baseTypeName)) {
  88. // TODO handle properly
  89. return Character.valueOf(((JSONString) jsonValue).stringValue()
  90. .charAt(0));
  91. } else if (Connector.class.getName().equals(baseTypeName)) {
  92. return ConnectorMap.get(connection).getConnector(
  93. ((JSONString) jsonValue).stringValue());
  94. } else {
  95. return decodeObject(type, jsonValue, target, connection);
  96. }
  97. }
  98. private static Object decodeObject(Type type, JSONValue jsonValue,
  99. Object target, ApplicationConnection connection) {
  100. JSONSerializer<Object> serializer = connection.getSerializerMap()
  101. .getSerializer(type.getBaseTypeName());
  102. // TODO handle case with no serializer found
  103. // Currently getSerializer throws exception if not found
  104. if (target != null && serializer instanceof DiffJSONSerializer<?>) {
  105. DiffJSONSerializer<Object> diffSerializer = (DiffJSONSerializer<Object>) serializer;
  106. diffSerializer.update(target, type, jsonValue, connection);
  107. return target;
  108. } else {
  109. Object object = serializer.deserialize(type, jsonValue, connection);
  110. return object;
  111. }
  112. }
  113. private static Map<Object, Object> decodeMap(Type type, JSONValue jsonMap,
  114. ApplicationConnection connection) {
  115. // Client -> server encodes empty map as an empty array because of
  116. // #8906. Do the same for server -> client to maintain symmetry.
  117. if (jsonMap instanceof JSONArray) {
  118. JSONArray array = (JSONArray) jsonMap;
  119. if (array.size() == 0) {
  120. return new HashMap<Object, Object>();
  121. }
  122. }
  123. Type keyType = type.getParameterTypes()[0];
  124. Type valueType = type.getParameterTypes()[1];
  125. if (keyType.getBaseTypeName().equals(String.class.getName())) {
  126. return decodeStringMap(valueType, jsonMap, connection);
  127. } else if (keyType.getBaseTypeName().equals(Connector.class.getName())) {
  128. return decodeConnectorMap(valueType, jsonMap, connection);
  129. } else {
  130. return decodeObjectMap(keyType, valueType, jsonMap, connection);
  131. }
  132. }
  133. private static Map<Object, Object> decodeObjectMap(Type keyType,
  134. Type valueType, JSONValue jsonValue,
  135. ApplicationConnection connection) {
  136. Map<Object, Object> map = new HashMap<Object, Object>();
  137. JSONArray mapArray = (JSONArray) jsonValue;
  138. JSONArray keys = (JSONArray) mapArray.get(0);
  139. JSONArray values = (JSONArray) mapArray.get(1);
  140. assert (keys.size() == values.size());
  141. for (int i = 0; i < keys.size(); i++) {
  142. Object decodedKey = decodeValue(keyType, keys.get(i), null,
  143. connection);
  144. Object decodedValue = decodeValue(valueType, values.get(i), null,
  145. connection);
  146. map.put(decodedKey, decodedValue);
  147. }
  148. return map;
  149. }
  150. private static Map<Object, Object> decodeConnectorMap(Type valueType,
  151. JSONValue jsonValue, ApplicationConnection connection) {
  152. Map<Object, Object> map = new HashMap<Object, Object>();
  153. JSONObject jsonMap = (JSONObject) jsonValue;
  154. ConnectorMap connectorMap = ConnectorMap.get(connection);
  155. for (String connectorId : jsonMap.keySet()) {
  156. Object value = decodeValue(valueType, jsonMap.get(connectorId),
  157. null, connection);
  158. map.put(connectorMap.getConnector(connectorId), value);
  159. }
  160. return map;
  161. }
  162. private static Map<Object, Object> decodeStringMap(Type valueType,
  163. JSONValue jsonValue, ApplicationConnection connection) {
  164. Map<Object, Object> map = new HashMap<Object, Object>();
  165. JSONObject jsonMap = (JSONObject) jsonValue;
  166. for (String key : jsonMap.keySet()) {
  167. Object value = decodeValue(valueType, jsonMap.get(key), null,
  168. connection);
  169. map.put(key, value);
  170. }
  171. return map;
  172. }
  173. private static List<Object> decodeList(Type type, JSONArray jsonArray,
  174. ApplicationConnection connection) {
  175. List<Object> tokens = new ArrayList<Object>();
  176. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  177. connection, tokens);
  178. return tokens;
  179. }
  180. private static Set<Object> decodeSet(Type type, JSONArray jsonArray,
  181. ApplicationConnection connection) {
  182. Set<Object> tokens = new HashSet<Object>();
  183. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  184. connection, tokens);
  185. return tokens;
  186. }
  187. private static void decodeIntoCollection(Type childType,
  188. JSONArray jsonArray, ApplicationConnection connection,
  189. Collection<Object> tokens) {
  190. for (int i = 0; i < jsonArray.size(); ++i) {
  191. // each entry always has two elements: type and value
  192. JSONValue entryValue = jsonArray.get(i);
  193. tokens.add(decodeValue(childType, entryValue, null, connection));
  194. }
  195. }
  196. }