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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.Iterator;
  8. import java.util.List;
  9. import java.util.Map;
  10. import com.google.gwt.core.client.GWT;
  11. import com.google.gwt.json.client.JSONArray;
  12. import com.google.gwt.json.client.JSONObject;
  13. import com.google.gwt.json.client.JSONString;
  14. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  15. import com.vaadin.terminal.gwt.client.ServerConnector;
  16. import com.vaadin.terminal.gwt.client.ConnectorMap;
  17. /**
  18. * Client side decoder for decodeing shared state and other values from JSON
  19. * received from the server.
  20. *
  21. * Currently, basic data types as well as Map, String[] and Object[] are
  22. * supported, where maps and Object[] can contain other supported data types.
  23. *
  24. * TODO extensible type support
  25. *
  26. * @since 7.0
  27. */
  28. public class JsonDecoder {
  29. static SerializerMap serializerMap = GWT.create(SerializerMap.class);
  30. /**
  31. * Decode a JSON array with two elements (type and value) into a client-side
  32. * type, recursively if necessary.
  33. *
  34. * @param jsonArray
  35. * JSON array with two elements
  36. * @param idMapper
  37. * mapper between connector ID and {@link ServerConnector} objects
  38. * @param connection
  39. * reference to the current ApplicationConnection
  40. * @return decoded value (does not contain JSON types)
  41. */
  42. public static Object decodeValue(JSONArray jsonArray,
  43. ConnectorMap idMapper, ApplicationConnection connection) {
  44. String type = ((JSONString) jsonArray.get(0)).stringValue();
  45. return decodeValue(type, jsonArray.get(1), idMapper, connection);
  46. }
  47. private static Object decodeValue(String variableType, Object value,
  48. ConnectorMap idMapper, ApplicationConnection connection) {
  49. Object val = null;
  50. // TODO type checks etc.
  51. if (JsonEncoder.VTYPE_UNDEFINED.equals(variableType)) {
  52. val = null;
  53. } else if (JsonEncoder.VTYPE_ARRAY.equals(variableType)) {
  54. val = decodeArray((JSONArray) value, idMapper, connection);
  55. } else if (JsonEncoder.VTYPE_MAP.equals(variableType)) {
  56. val = decodeMap((JSONObject) value, idMapper, connection);
  57. } else if (JsonEncoder.VTYPE_LIST.equals(variableType)) {
  58. val = decodeList((JSONArray) value, idMapper, connection);
  59. } else if (JsonEncoder.VTYPE_STRINGARRAY.equals(variableType)) {
  60. val = decodeStringArray((JSONArray) value);
  61. } else if (JsonEncoder.VTYPE_STRING.equals(variableType)) {
  62. val = ((JSONString) value).stringValue();
  63. } else if (JsonEncoder.VTYPE_INTEGER.equals(variableType)) {
  64. // TODO handle properly
  65. val = Integer.valueOf(String.valueOf(value));
  66. } else if (JsonEncoder.VTYPE_LONG.equals(variableType)) {
  67. // TODO handle properly
  68. val = Long.valueOf(String.valueOf(value));
  69. } else if (JsonEncoder.VTYPE_FLOAT.equals(variableType)) {
  70. // TODO handle properly
  71. val = Float.valueOf(String.valueOf(value));
  72. } else if (JsonEncoder.VTYPE_DOUBLE.equals(variableType)) {
  73. // TODO handle properly
  74. val = Double.valueOf(String.valueOf(value));
  75. } else if (JsonEncoder.VTYPE_BOOLEAN.equals(variableType)) {
  76. // TODO handle properly
  77. val = Boolean.valueOf(String.valueOf(value));
  78. } else if (JsonEncoder.VTYPE_PAINTABLE.equals(variableType)) {
  79. // TODO handle properly
  80. val = idMapper.getConnector(String.valueOf(value));
  81. } else {
  82. // object, class name as type
  83. JSONSerializer serializer = serializerMap
  84. .getSerializer(variableType);
  85. // TODO handle case with no serializer found
  86. Object object = serializer.deserialize((JSONObject) value,
  87. idMapper, connection);
  88. return object;
  89. }
  90. return val;
  91. }
  92. private static Map<String, Object> decodeMap(JSONObject jsonMap,
  93. ConnectorMap idMapper, ApplicationConnection connection) {
  94. HashMap<String, Object> map = new HashMap<String, Object>();
  95. Iterator<String> it = jsonMap.keySet().iterator();
  96. while (it.hasNext()) {
  97. String key = it.next();
  98. map.put(key,
  99. decodeValue((JSONArray) jsonMap.get(key), idMapper,
  100. connection));
  101. }
  102. return map;
  103. }
  104. private static String[] decodeStringArray(JSONArray jsonArray) {
  105. int size = jsonArray.size();
  106. List<String> tokens = new ArrayList<String>(size);
  107. for (int i = 0; i < size; ++i) {
  108. tokens.add(String.valueOf(jsonArray.get(i)));
  109. }
  110. return tokens.toArray(new String[tokens.size()]);
  111. }
  112. private static Object[] decodeArray(JSONArray jsonArray,
  113. ConnectorMap idMapper, ApplicationConnection connection) {
  114. List<Object> list = decodeList(jsonArray, idMapper, connection);
  115. return list.toArray(new Object[list.size()]);
  116. }
  117. private static List<Object> decodeList(JSONArray jsonArray,
  118. ConnectorMap idMapper, ApplicationConnection connection) {
  119. List<Object> tokens = new ArrayList<Object>();
  120. for (int i = 0; i < jsonArray.size(); ++i) {
  121. // each entry always has two elements: type and value
  122. JSONArray entryArray = (JSONArray) jsonArray.get(i);
  123. tokens.add(decodeValue(entryArray, idMapper, connection));
  124. }
  125. return tokens;
  126. }
  127. }