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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright 2000-2013 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.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.client.ApplicationConnection;
  29. import com.vaadin.client.ConnectorMap;
  30. import com.vaadin.client.metadata.NoDataException;
  31. import com.vaadin.client.metadata.Property;
  32. import com.vaadin.client.metadata.Type;
  33. import com.vaadin.shared.Connector;
  34. /**
  35. * Client side decoder for decodeing shared state and other values from JSON
  36. * received from the server.
  37. *
  38. * Currently, basic data types as well as Map, String[] and Object[] are
  39. * supported, where maps and Object[] can contain other supported data types.
  40. *
  41. * TODO extensible type support
  42. *
  43. * @since 7.0
  44. */
  45. public class JsonDecoder {
  46. /**
  47. * Decode a JSON array with two elements (type and value) into a client-side
  48. * type, recursively if necessary.
  49. *
  50. * @param jsonValue
  51. * JSON value with encoded data
  52. * @param connection
  53. * reference to the current ApplicationConnection
  54. * @return decoded value (does not contain JSON types)
  55. */
  56. public static Object decodeValue(Type type, JSONValue jsonValue,
  57. Object target, ApplicationConnection connection) {
  58. // Null is null, regardless of type
  59. if (jsonValue.isNull() != null) {
  60. return null;
  61. }
  62. String baseTypeName = type.getBaseTypeName();
  63. if (Map.class.getName().equals(baseTypeName)
  64. || HashMap.class.getName().equals(baseTypeName)) {
  65. return decodeMap(type, jsonValue, connection);
  66. } else if (List.class.getName().equals(baseTypeName)
  67. || ArrayList.class.getName().equals(baseTypeName)) {
  68. return decodeList(type, (JSONArray) jsonValue, connection);
  69. } else if (Set.class.getName().equals(baseTypeName)) {
  70. return decodeSet(type, (JSONArray) jsonValue, connection);
  71. } else if (String.class.getName().equals(baseTypeName)) {
  72. return ((JSONString) jsonValue).stringValue();
  73. } else if (Integer.class.getName().equals(baseTypeName)) {
  74. return Integer.valueOf(String.valueOf(jsonValue));
  75. } else if (Long.class.getName().equals(baseTypeName)) {
  76. // TODO handle properly
  77. return Long.valueOf(String.valueOf(jsonValue));
  78. } else if (Float.class.getName().equals(baseTypeName)) {
  79. // TODO handle properly
  80. return Float.valueOf(String.valueOf(jsonValue));
  81. } else if (Double.class.getName().equals(baseTypeName)) {
  82. // TODO handle properly
  83. return Double.valueOf(String.valueOf(jsonValue));
  84. } else if (Boolean.class.getName().equals(baseTypeName)) {
  85. // TODO handle properly
  86. return Boolean.valueOf(String.valueOf(jsonValue));
  87. } else if (Byte.class.getName().equals(baseTypeName)) {
  88. // TODO handle properly
  89. return Byte.valueOf(String.valueOf(jsonValue));
  90. } else if (Character.class.getName().equals(baseTypeName)) {
  91. // TODO handle properly
  92. return Character.valueOf(((JSONString) jsonValue).stringValue()
  93. .charAt(0));
  94. } else if (Connector.class.getName().equals(baseTypeName)) {
  95. return ConnectorMap.get(connection).getConnector(
  96. ((JSONString) jsonValue).stringValue());
  97. } else {
  98. return decodeObject(type, jsonValue, target, connection);
  99. }
  100. }
  101. private static Object decodeObject(Type type, JSONValue jsonValue,
  102. Object target, ApplicationConnection connection) {
  103. JSONSerializer<Object> serializer = (JSONSerializer<Object>) type
  104. .findSerializer();
  105. if (serializer != null) {
  106. if (target != null && serializer instanceof DiffJSONSerializer<?>) {
  107. DiffJSONSerializer<Object> diffSerializer = (DiffJSONSerializer<Object>) serializer;
  108. diffSerializer.update(target, type, jsonValue, connection);
  109. return target;
  110. } else {
  111. Object object = serializer.deserialize(type, jsonValue,
  112. connection);
  113. return object;
  114. }
  115. } else {
  116. try {
  117. Collection<Property> properties = type.getProperties();
  118. if (target == null) {
  119. target = type.createInstance();
  120. }
  121. JSONObject jsonObject = jsonValue.isObject();
  122. for (Property property : properties) {
  123. JSONValue encodedPropertyValue = jsonObject.get(property
  124. .getName());
  125. if (encodedPropertyValue == null) {
  126. continue;
  127. }
  128. Object propertyReference = property.getValue(target);
  129. Object decodedValue = decodeValue(property.getType(),
  130. encodedPropertyValue, propertyReference, connection);
  131. property.setValue(target, decodedValue);
  132. }
  133. return target;
  134. } catch (NoDataException e) {
  135. throw new RuntimeException("Can not deserialize "
  136. + type.getSignature(), e);
  137. }
  138. }
  139. }
  140. private static Map<Object, Object> decodeMap(Type type, JSONValue jsonMap,
  141. ApplicationConnection connection) {
  142. // Client -> server encodes empty map as an empty array because of
  143. // #8906. Do the same for server -> client to maintain symmetry.
  144. if (jsonMap instanceof JSONArray) {
  145. JSONArray array = (JSONArray) jsonMap;
  146. if (array.size() == 0) {
  147. return new HashMap<Object, Object>();
  148. }
  149. }
  150. Type keyType = type.getParameterTypes()[0];
  151. Type valueType = type.getParameterTypes()[1];
  152. if (keyType.getBaseTypeName().equals(String.class.getName())) {
  153. return decodeStringMap(valueType, jsonMap, connection);
  154. } else if (keyType.getBaseTypeName().equals(Connector.class.getName())) {
  155. return decodeConnectorMap(valueType, jsonMap, connection);
  156. } else {
  157. return decodeObjectMap(keyType, valueType, jsonMap, connection);
  158. }
  159. }
  160. private static Map<Object, Object> decodeObjectMap(Type keyType,
  161. Type valueType, JSONValue jsonValue,
  162. ApplicationConnection connection) {
  163. Map<Object, Object> map = new HashMap<Object, Object>();
  164. JSONArray mapArray = (JSONArray) jsonValue;
  165. JSONArray keys = (JSONArray) mapArray.get(0);
  166. JSONArray values = (JSONArray) mapArray.get(1);
  167. assert (keys.size() == values.size());
  168. for (int i = 0; i < keys.size(); i++) {
  169. Object decodedKey = decodeValue(keyType, keys.get(i), null,
  170. connection);
  171. Object decodedValue = decodeValue(valueType, values.get(i), null,
  172. connection);
  173. map.put(decodedKey, decodedValue);
  174. }
  175. return map;
  176. }
  177. private static Map<Object, Object> decodeConnectorMap(Type valueType,
  178. JSONValue jsonValue, ApplicationConnection connection) {
  179. Map<Object, Object> map = new HashMap<Object, Object>();
  180. JSONObject jsonMap = (JSONObject) jsonValue;
  181. ConnectorMap connectorMap = ConnectorMap.get(connection);
  182. for (String connectorId : jsonMap.keySet()) {
  183. Object value = decodeValue(valueType, jsonMap.get(connectorId),
  184. null, connection);
  185. map.put(connectorMap.getConnector(connectorId), value);
  186. }
  187. return map;
  188. }
  189. private static Map<Object, Object> decodeStringMap(Type valueType,
  190. JSONValue jsonValue, ApplicationConnection connection) {
  191. Map<Object, Object> map = new HashMap<Object, Object>();
  192. JSONObject jsonMap = (JSONObject) jsonValue;
  193. for (String key : jsonMap.keySet()) {
  194. Object value = decodeValue(valueType, jsonMap.get(key), null,
  195. connection);
  196. map.put(key, value);
  197. }
  198. return map;
  199. }
  200. private static List<Object> decodeList(Type type, JSONArray jsonArray,
  201. ApplicationConnection connection) {
  202. List<Object> tokens = new ArrayList<Object>();
  203. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  204. connection, tokens);
  205. return tokens;
  206. }
  207. private static Set<Object> decodeSet(Type type, JSONArray jsonArray,
  208. ApplicationConnection connection) {
  209. Set<Object> tokens = new HashSet<Object>();
  210. decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
  211. connection, tokens);
  212. return tokens;
  213. }
  214. private static void decodeIntoCollection(Type childType,
  215. JSONArray jsonArray, ApplicationConnection connection,
  216. Collection<Object> tokens) {
  217. for (int i = 0; i < jsonArray.size(); ++i) {
  218. // each entry always has two elements: type and value
  219. JSONValue entryValue = jsonArray.get(i);
  220. tokens.add(decodeValue(childType, entryValue, null, connection));
  221. }
  222. }
  223. }