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.

JsonCodec.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.server;
  5. import java.beans.IntrospectionException;
  6. import java.beans.Introspector;
  7. import java.beans.PropertyDescriptor;
  8. import java.io.Serializable;
  9. import java.lang.reflect.InvocationTargetException;
  10. import java.lang.reflect.Method;
  11. import java.lang.reflect.ParameterizedType;
  12. import java.lang.reflect.Type;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.HashMap;
  16. import java.util.HashSet;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import com.vaadin.Application;
  22. import com.vaadin.external.json.JSONArray;
  23. import com.vaadin.external.json.JSONException;
  24. import com.vaadin.external.json.JSONObject;
  25. import com.vaadin.terminal.gwt.client.Connector;
  26. import com.vaadin.terminal.gwt.client.communication.JsonEncoder;
  27. import com.vaadin.ui.Component;
  28. /**
  29. * Decoder for converting RPC parameters and other values from JSON in transfer
  30. * between the client and the server and vice versa.
  31. *
  32. * @since 7.0
  33. */
  34. public class JsonCodec implements Serializable {
  35. private static Map<Class<?>, String> typeToTransportType = new HashMap<Class<?>, String>();
  36. /**
  37. * Note! This does not contain primitives.
  38. * <p>
  39. */
  40. private static Map<String, Class<?>> transportTypeToType = new HashMap<String, Class<?>>();
  41. static {
  42. registerType(String.class, JsonEncoder.VTYPE_STRING);
  43. registerType(Connector.class, JsonEncoder.VTYPE_CONNECTOR);
  44. registerType(Boolean.class, JsonEncoder.VTYPE_BOOLEAN);
  45. registerType(boolean.class, JsonEncoder.VTYPE_BOOLEAN);
  46. registerType(Integer.class, JsonEncoder.VTYPE_INTEGER);
  47. registerType(int.class, JsonEncoder.VTYPE_INTEGER);
  48. registerType(Float.class, JsonEncoder.VTYPE_FLOAT);
  49. registerType(float.class, JsonEncoder.VTYPE_FLOAT);
  50. registerType(Double.class, JsonEncoder.VTYPE_DOUBLE);
  51. registerType(double.class, JsonEncoder.VTYPE_DOUBLE);
  52. registerType(Long.class, JsonEncoder.VTYPE_LONG);
  53. registerType(long.class, JsonEncoder.VTYPE_LONG);
  54. registerType(String[].class, JsonEncoder.VTYPE_STRINGARRAY);
  55. registerType(Object[].class, JsonEncoder.VTYPE_ARRAY);
  56. registerType(Map.class, JsonEncoder.VTYPE_MAP);
  57. registerType(List.class, JsonEncoder.VTYPE_LIST);
  58. registerType(Set.class, JsonEncoder.VTYPE_SET);
  59. }
  60. private static void registerType(Class<?> type, String transportType) {
  61. typeToTransportType.put(type, transportType);
  62. if (!type.isPrimitive()) {
  63. transportTypeToType.put(transportType, type);
  64. }
  65. }
  66. public static boolean isInternalTransportType(String transportType) {
  67. return transportTypeToType.containsKey(transportType);
  68. }
  69. public static boolean isInternalType(Type type) {
  70. if (type instanceof Class && ((Class<?>) type).isPrimitive()) {
  71. // All primitive types are handled internally
  72. return true;
  73. }
  74. return typeToTransportType.containsKey(getClassForType(type));
  75. }
  76. private static Class<?> getClassForType(Type type) {
  77. if (type instanceof ParameterizedType) {
  78. return (Class<?>) (((ParameterizedType) type).getRawType());
  79. } else {
  80. return (Class<?>) type;
  81. }
  82. }
  83. public static String getTransportType(JSONArray encodedValue)
  84. throws JSONException {
  85. return encodedValue.getString(0);
  86. }
  87. private static Class<?> getType(String transportType) {
  88. return transportTypeToType.get(transportType);
  89. }
  90. /**
  91. * Decodes the given value and type, restricted to using only internal
  92. * types.
  93. *
  94. * @param valueAndType
  95. * @param application
  96. * @throws JSONException
  97. */
  98. @Deprecated
  99. public static Object decodeInternalType(JSONArray valueAndType,
  100. Application application) throws JSONException {
  101. String transportType = getTransportType(valueAndType);
  102. return decodeInternalType(getType(transportType), true, valueAndType,
  103. application);
  104. }
  105. public static Object decodeInternalOrCustomType(Type targetType,
  106. JSONArray valueAndType, Application application)
  107. throws JSONException {
  108. if (isInternalType(targetType)) {
  109. return decodeInternalType(targetType, false, valueAndType,
  110. application);
  111. } else {
  112. return decodeCustomType(targetType, valueAndType, application);
  113. }
  114. }
  115. public static Object decodeCustomType(Type targetType,
  116. JSONArray valueAndType, Application application)
  117. throws JSONException {
  118. if (isInternalType(targetType)) {
  119. throw new JSONException("decodeCustomType cannot be used for "
  120. + targetType + ", which is an internal type");
  121. }
  122. String transportType = getCustomTransportType(getClassForType(targetType));
  123. String encodedTransportType = valueAndType.getString(0);
  124. if (!transportTypesCompatible(encodedTransportType, transportType)) {
  125. throw new JSONException("Expected a value of type " + transportType
  126. + ", received " + encodedTransportType);
  127. }
  128. // Try to decode object using fields
  129. return decodeObject(targetType, (JSONObject) valueAndType.get(1),
  130. application);
  131. }
  132. /**
  133. * Decodes a value that is of an internal type.
  134. * <p>
  135. * Ensures the encoded value is of the same type as target type.
  136. * </p>
  137. * <p>
  138. * Allows restricting collections so that they must be declared using
  139. * generics. If this is used then all objects in the collection are encoded
  140. * using the declared type. Otherwise only internal types are allowed in
  141. * collections.
  142. * </p>
  143. *
  144. * @param targetType
  145. * The type that should be returned by this method
  146. * @param valueAndType
  147. * The encoded value and type array
  148. * @param application
  149. * A reference to the application
  150. * @param enforceGenericsInCollections
  151. * true if generics should be enforce, false to only allow
  152. * internal types in collections
  153. * @return
  154. * @throws JSONException
  155. */
  156. public static Object decodeInternalType(Type targetType,
  157. boolean restrictToInternalTypes, JSONArray valueAndType,
  158. Application application) throws JSONException {
  159. String encodedTransportType = valueAndType.getString(0);
  160. if (!isInternalType(targetType)) {
  161. throw new JSONException("Type " + targetType
  162. + " is not a supported internal type.");
  163. }
  164. String transportType = getInternalTransportType(targetType);
  165. if (!transportTypesCompatible(encodedTransportType, transportType)) {
  166. throw new JSONException("Expected a value of type " + targetType
  167. + ", received " + getType(encodedTransportType));
  168. }
  169. Object encodedJsonValue = valueAndType.get(1);
  170. if (JsonEncoder.VTYPE_NULL.equals(encodedTransportType)) {
  171. return null;
  172. }
  173. // Collections
  174. if (JsonEncoder.VTYPE_LIST.equals(transportType)) {
  175. return decodeList(targetType, restrictToInternalTypes,
  176. (JSONArray) encodedJsonValue, application);
  177. } else if (JsonEncoder.VTYPE_SET.equals(transportType)) {
  178. return decodeSet(targetType, restrictToInternalTypes,
  179. (JSONArray) encodedJsonValue, application);
  180. } else if (JsonEncoder.VTYPE_MAP_CONNECTOR.equals(transportType)) {
  181. return decodeConnectorToObjectMap(targetType,
  182. restrictToInternalTypes, (JSONObject) encodedJsonValue,
  183. application);
  184. } else if (JsonEncoder.VTYPE_MAP.equals(transportType)) {
  185. return decodeStringToObjectMap(targetType, restrictToInternalTypes,
  186. (JSONObject) encodedJsonValue, application);
  187. }
  188. // Arrays
  189. if (JsonEncoder.VTYPE_ARRAY.equals(transportType)) {
  190. return decodeObjectArray(targetType, (JSONArray) encodedJsonValue,
  191. application);
  192. } else if (JsonEncoder.VTYPE_STRINGARRAY.equals(transportType)) {
  193. return decodeStringArray((JSONArray) encodedJsonValue);
  194. }
  195. // Special Vaadin types
  196. String stringValue = String.valueOf(encodedJsonValue);
  197. if (JsonEncoder.VTYPE_CONNECTOR.equals(transportType)) {
  198. return application.getConnector(stringValue);
  199. }
  200. // Standard Java types
  201. if (JsonEncoder.VTYPE_STRING.equals(transportType)) {
  202. return stringValue;
  203. } else if (JsonEncoder.VTYPE_INTEGER.equals(transportType)) {
  204. return Integer.valueOf(stringValue);
  205. } else if (JsonEncoder.VTYPE_LONG.equals(transportType)) {
  206. return Long.valueOf(stringValue);
  207. } else if (JsonEncoder.VTYPE_FLOAT.equals(transportType)) {
  208. return Float.valueOf(stringValue);
  209. } else if (JsonEncoder.VTYPE_DOUBLE.equals(transportType)) {
  210. return Double.valueOf(stringValue);
  211. } else if (JsonEncoder.VTYPE_BOOLEAN.equals(transportType)) {
  212. return Boolean.valueOf(stringValue);
  213. }
  214. throw new JSONException("Unknown type " + transportType);
  215. }
  216. private static boolean transportTypesCompatible(
  217. String encodedTransportType, String transportType) {
  218. if (encodedTransportType == null) {
  219. return false;
  220. }
  221. if (encodedTransportType.equals(transportType)) {
  222. return true;
  223. }
  224. if (encodedTransportType.equals(JsonEncoder.VTYPE_NULL)) {
  225. return true;
  226. }
  227. return false;
  228. }
  229. @Deprecated
  230. private static Map<String, Object> decodeStringToObjectMap(Type targetType,
  231. boolean restrictToInternalTypes, JSONObject jsonMap,
  232. Application application) throws JSONException {
  233. HashMap<String, Object> map = new HashMap<String, Object>();
  234. Iterator<String> it = jsonMap.keys();
  235. while (it.hasNext()) {
  236. String key = it.next();
  237. JSONArray encodedValueAndType = jsonMap.getJSONArray(key);
  238. Object decodedChild = decodeChild(targetType,
  239. restrictToInternalTypes, 1, encodedValueAndType,
  240. application);
  241. map.put(key, decodedChild);
  242. }
  243. return map;
  244. }
  245. @Deprecated
  246. private static Map<Connector, Object> decodeConnectorToObjectMap(
  247. Type targetType, boolean restrictToInternalTypes,
  248. JSONObject jsonMap, Application application) throws JSONException {
  249. HashMap<Connector, Object> map = new HashMap<Connector, Object>();
  250. Iterator<String> it = jsonMap.keys();
  251. while (it.hasNext()) {
  252. String connectorId = it.next();
  253. Connector connector = application.getConnector(connectorId);
  254. JSONArray encodedValueAndType = jsonMap.getJSONArray(connectorId);
  255. Object decodedChild = decodeChild(targetType,
  256. restrictToInternalTypes, 1, encodedValueAndType,
  257. application);
  258. map.put(connector, decodedChild);
  259. }
  260. return map;
  261. }
  262. /**
  263. * @param targetType
  264. * @param restrictToInternalTypes
  265. * @param typeIndex
  266. * The index of a generic type to use to define the child type
  267. * that should be decoded
  268. * @param encodedValueAndType
  269. * @param application
  270. * @return
  271. * @throws JSONException
  272. */
  273. private static Object decodeChild(Type targetType,
  274. boolean restrictToInternalTypes, int typeIndex,
  275. JSONArray encodedValueAndType, Application application)
  276. throws JSONException {
  277. if (!restrictToInternalTypes && targetType instanceof ParameterizedType) {
  278. Type childType = ((ParameterizedType) targetType)
  279. .getActualTypeArguments()[typeIndex];
  280. // Only decode the given type
  281. return decodeInternalOrCustomType(childType, encodedValueAndType,
  282. application);
  283. } else {
  284. // Only internal types when not enforcing a given type to avoid
  285. // security issues
  286. return decodeInternalType(encodedValueAndType, application);
  287. }
  288. }
  289. private static Object decodeEnum(Class<? extends Enum> cls, JSONObject value) {
  290. String enumIdentifier = String.valueOf(value);
  291. return Enum.valueOf(cls, enumIdentifier);
  292. }
  293. private static String[] decodeStringArray(JSONArray jsonArray)
  294. throws JSONException {
  295. int length = jsonArray.length();
  296. List<String> tokens = new ArrayList<String>(length);
  297. for (int i = 0; i < length; ++i) {
  298. tokens.add(jsonArray.getString(i));
  299. }
  300. return tokens.toArray(new String[tokens.size()]);
  301. }
  302. private static Object[] decodeObjectArray(Type targetType,
  303. JSONArray jsonArray, Application application) throws JSONException {
  304. List list = decodeList(List.class, true, jsonArray, application);
  305. return list.toArray(new Object[list.size()]);
  306. }
  307. private static List<Object> decodeList(Type targetType,
  308. boolean restrictToInternalTypes, JSONArray jsonArray,
  309. Application application) throws JSONException {
  310. List<Object> list = new ArrayList<Object>();
  311. for (int i = 0; i < jsonArray.length(); ++i) {
  312. // each entry always has two elements: type and value
  313. JSONArray encodedValueAndType = jsonArray.getJSONArray(i);
  314. Object decodedChild = decodeChild(targetType,
  315. restrictToInternalTypes, 0, encodedValueAndType,
  316. application);
  317. list.add(decodedChild);
  318. }
  319. return list;
  320. }
  321. private static Set<Object> decodeSet(Type targetType,
  322. boolean restrictToInternalTypes, JSONArray jsonArray,
  323. Application application) throws JSONException {
  324. HashSet<Object> set = new HashSet<Object>();
  325. set.addAll(decodeList(List.class, restrictToInternalTypes, jsonArray,
  326. application));
  327. return set;
  328. }
  329. /**
  330. * Returns the name that should be used as field name in the JSON. We strip
  331. * "set" from the setter, keeping the result - this is easy to do on both
  332. * server and client, avoiding some issues with cASE. E.g setZIndex()
  333. * becomes "ZIndex". Also ensures that both getter and setter are present,
  334. * returning null otherwise.
  335. *
  336. * @param pd
  337. * @return the name to be used or null if both getter and setter are not
  338. * found.
  339. */
  340. private static String getTransportFieldName(PropertyDescriptor pd) {
  341. if (pd.getReadMethod() == null || pd.getWriteMethod() == null) {
  342. return null;
  343. }
  344. return pd.getWriteMethod().getName().substring(3);
  345. }
  346. private static Object decodeObject(Type targetType,
  347. JSONObject serializedObject, Application application)
  348. throws JSONException {
  349. Class<?> targetClass = getClassForType(targetType);
  350. if (Enum.class.isAssignableFrom(targetClass)) {
  351. return decodeEnum(targetClass.asSubclass(Enum.class),
  352. serializedObject);
  353. }
  354. try {
  355. Object decodedObject = targetClass.newInstance();
  356. for (PropertyDescriptor pd : Introspector.getBeanInfo(targetClass)
  357. .getPropertyDescriptors()) {
  358. String fieldName = getTransportFieldName(pd);
  359. if (fieldName == null) {
  360. continue;
  361. }
  362. JSONArray encodedFieldValue = serializedObject
  363. .getJSONArray(fieldName);
  364. Type fieldType = pd.getReadMethod().getGenericReturnType();
  365. Object decodedFieldValue = decodeInternalOrCustomType(
  366. fieldType, encodedFieldValue, application);
  367. pd.getWriteMethod().invoke(decodedObject, decodedFieldValue);
  368. }
  369. return decodedObject;
  370. } catch (IllegalArgumentException e) {
  371. throw new JSONException(e);
  372. } catch (IllegalAccessException e) {
  373. throw new JSONException(e);
  374. } catch (InvocationTargetException e) {
  375. throw new JSONException(e);
  376. } catch (InstantiationException e) {
  377. throw new JSONException(e);
  378. } catch (IntrospectionException e) {
  379. throw new JSONException(e);
  380. }
  381. }
  382. @Deprecated
  383. private static JSONArray encode(Object value, Application application)
  384. throws JSONException {
  385. return encode(value, null, null, application);
  386. }
  387. public static JSONArray encode(Object value, Object referenceValue,
  388. Type valueType, Application application) throws JSONException {
  389. if (null == value) {
  390. return encodeNull();
  391. }
  392. if (valueType == null) {
  393. valueType = value.getClass();
  394. }
  395. String internalTransportType = getInternalTransportType(valueType);
  396. if (value instanceof String[]) {
  397. String[] array = (String[]) value;
  398. JSONArray jsonArray = new JSONArray();
  399. for (int i = 0; i < array.length; ++i) {
  400. jsonArray.put(array[i]);
  401. }
  402. return combineTypeAndValue(JsonEncoder.VTYPE_STRINGARRAY, jsonArray);
  403. } else if (value instanceof String) {
  404. return combineTypeAndValue(JsonEncoder.VTYPE_STRING, value);
  405. } else if (value instanceof Boolean) {
  406. return combineTypeAndValue(JsonEncoder.VTYPE_BOOLEAN, value);
  407. } else if (value instanceof Number) {
  408. return combineTypeAndValue(internalTransportType, value);
  409. } else if (value instanceof Collection) {
  410. if (internalTransportType == null) {
  411. throw new RuntimeException(
  412. "Unable to serialize unsupported type: " + valueType);
  413. }
  414. Collection<?> collection = (Collection<?>) value;
  415. JSONArray jsonArray = encodeCollection(valueType, collection,
  416. application);
  417. return combineTypeAndValue(internalTransportType, jsonArray);
  418. } else if (value instanceof Object[]) {
  419. Object[] array = (Object[]) value;
  420. JSONArray jsonArray = encodeArrayContents(array, application);
  421. return combineTypeAndValue(JsonEncoder.VTYPE_ARRAY, jsonArray);
  422. } else if (value instanceof Map) {
  423. Map<Object, Object> map = (Map<Object, Object>) value;
  424. JSONObject jsonMap = encodeMapContents(map, application);
  425. // Hack to support Connector as map key. Should be fixed by #
  426. if (!map.isEmpty()
  427. && map.keySet().iterator().next() instanceof Connector) {
  428. return combineTypeAndValue(JsonEncoder.VTYPE_MAP_CONNECTOR,
  429. jsonMap);
  430. } else {
  431. return combineTypeAndValue(JsonEncoder.VTYPE_MAP, jsonMap);
  432. }
  433. } else if (value instanceof Connector) {
  434. Connector connector = (Connector) value;
  435. if (value instanceof Component
  436. && !(AbstractCommunicationManager
  437. .isVisible((Component) value))) {
  438. return encodeNull();
  439. }
  440. return combineTypeAndValue(JsonEncoder.VTYPE_CONNECTOR,
  441. connector.getConnectorId());
  442. } else if (internalTransportType != null) {
  443. return combineTypeAndValue(internalTransportType,
  444. String.valueOf(value));
  445. } else if (value instanceof Enum) {
  446. return encodeEnum((Enum) value, application);
  447. } else {
  448. // Any object that we do not know how to encode we encode by looping
  449. // through fields
  450. return combineTypeAndValue(
  451. getCustomTransportType((Class<?>) valueType),
  452. encodeObject(value, referenceValue, application));
  453. }
  454. }
  455. private static JSONArray encodeNull() {
  456. return combineTypeAndValue(JsonEncoder.VTYPE_NULL, JSONObject.NULL);
  457. }
  458. private static Object encodeObject(Object value, Object referenceValue,
  459. Application application) throws JSONException {
  460. JSONObject jsonMap = new JSONObject();
  461. try {
  462. for (PropertyDescriptor pd : Introspector.getBeanInfo(
  463. value.getClass()).getPropertyDescriptors()) {
  464. String fieldName = getTransportFieldName(pd);
  465. if (fieldName == null) {
  466. continue;
  467. }
  468. Method getterMethod = pd.getReadMethod();
  469. // We can't use PropertyDescriptor.getPropertyType() as it does
  470. // not support generics
  471. Type fieldType = getterMethod.getGenericReturnType();
  472. Object fieldValue = getterMethod.invoke(value, (Object[]) null);
  473. boolean equals = false;
  474. Object referenceFieldValue = null;
  475. if (referenceValue != null) {
  476. referenceFieldValue = getterMethod.invoke(referenceValue,
  477. (Object[]) null);
  478. equals = equals(fieldValue, referenceFieldValue);
  479. }
  480. if (!equals) {
  481. jsonMap.put(
  482. fieldName,
  483. encode(fieldValue, referenceFieldValue, fieldType,
  484. application));
  485. // } else {
  486. // System.out.println("Skipping field " + fieldName
  487. // + " of type " + fieldType.getName()
  488. // + " for object " + value.getClass().getName()
  489. // + " as " + fieldValue + "==" + referenceFieldValue);
  490. }
  491. }
  492. } catch (Exception e) {
  493. // TODO: Should exceptions be handled in a different way?
  494. throw new JSONException(e);
  495. }
  496. return jsonMap;
  497. }
  498. /**
  499. * Compares the value with the reference. If they match, returns true.
  500. *
  501. * @param fieldValue
  502. * @param referenceValue
  503. * @return
  504. */
  505. private static boolean equals(Object fieldValue, Object referenceValue) {
  506. if (fieldValue == null) {
  507. return referenceValue == null;
  508. }
  509. if (fieldValue.equals(referenceValue)) {
  510. return true;
  511. }
  512. return false;
  513. }
  514. private static JSONArray encodeEnum(Enum e, Application application)
  515. throws JSONException {
  516. String enumIdentifier = e.name();
  517. return combineTypeAndValue(e.getClass().getName(), enumIdentifier);
  518. }
  519. private static JSONArray encodeArrayContents(Object[] array,
  520. Application application) throws JSONException {
  521. JSONArray jsonArray = new JSONArray();
  522. for (Object o : array) {
  523. jsonArray.put(encode(o, null, null, application));
  524. }
  525. return jsonArray;
  526. }
  527. private static JSONArray encodeCollection(Type targetType,
  528. Collection collection, Application application)
  529. throws JSONException {
  530. JSONArray jsonArray = new JSONArray();
  531. for (Object o : collection) {
  532. jsonArray.put(encodeChild(targetType, 0, o, application));
  533. }
  534. return jsonArray;
  535. }
  536. private static JSONArray encodeChild(Type targetType, int typeIndex,
  537. Object o, Application application) throws JSONException {
  538. if (targetType instanceof ParameterizedType) {
  539. Type childType = ((ParameterizedType) targetType)
  540. .getActualTypeArguments()[typeIndex];
  541. // Encode using the given type
  542. return encode(o, null, childType, application);
  543. } else {
  544. return encode(o, application);
  545. }
  546. }
  547. private static JSONObject encodeMapContents(Map<Object, Object> map,
  548. Application application) throws JSONException {
  549. JSONObject jsonMap = new JSONObject();
  550. for (Object mapKey : map.keySet()) {
  551. Object mapValue = map.get(mapKey);
  552. if (mapKey instanceof ClientConnector) {
  553. mapKey = ((ClientConnector) mapKey).getConnectorId();
  554. }
  555. if (!(mapKey instanceof String)) {
  556. throw new JSONException(
  557. "Only maps with String/Connector keys are currently supported (#8602)");
  558. }
  559. jsonMap.put((String) mapKey,
  560. encode(mapValue, null, null, application));
  561. }
  562. return jsonMap;
  563. }
  564. private static JSONArray combineTypeAndValue(String type, Object value) {
  565. if (type == null) {
  566. throw new RuntimeException("Type for value " + value
  567. + " cannot be null!");
  568. }
  569. JSONArray outerArray = new JSONArray();
  570. outerArray.put(type);
  571. outerArray.put(value);
  572. return outerArray;
  573. }
  574. /**
  575. * Gets the transport type for the given class. Returns null if no transport
  576. * type can be found.
  577. *
  578. * @param valueType
  579. * The type that should be transported
  580. * @return
  581. * @throws JSONException
  582. */
  583. private static String getInternalTransportType(Type valueType) {
  584. return typeToTransportType.get(getClassForType(valueType));
  585. }
  586. private static String getCustomTransportType(Class<?> targetType) {
  587. return targetType.getName();
  588. }
  589. }