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.

ArraySerializer.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright 2000-2014 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.server.widgetsetutils.metadata;
  17. import com.google.gwt.core.ext.TreeLogger;
  18. import com.google.gwt.core.ext.typeinfo.JArrayType;
  19. import com.google.gwt.core.ext.typeinfo.JType;
  20. import com.google.gwt.json.client.JSONArray;
  21. import com.google.gwt.user.rebind.SourceWriter;
  22. import com.vaadin.client.communication.JsonDecoder;
  23. import com.vaadin.client.communication.JsonEncoder;
  24. import com.vaadin.server.widgetsetutils.ConnectorBundleLoaderFactory;
  25. public class ArraySerializer extends JsonSerializer {
  26. private final JArrayType arrayType;
  27. public ArraySerializer(JArrayType arrayType) {
  28. super(arrayType);
  29. this.arrayType = arrayType;
  30. }
  31. @Override
  32. protected void printDeserializerBody(TreeLogger logger, SourceWriter w,
  33. String type, String jsonValue, String connection) {
  34. JType leafType = arrayType.getLeafType();
  35. int rank = arrayType.getRank();
  36. w.println(JSONArray.class.getName() + " jsonArray = " + jsonValue
  37. + ".isArray();");
  38. // Type value = new Type[jsonArray.size()][][];
  39. w.print(arrayType.getQualifiedSourceName() + " value = new "
  40. + leafType.getQualifiedSourceName() + "[jsonArray.size()]");
  41. for (int i = 1; i < rank; i++) {
  42. w.print("[]");
  43. }
  44. w.println(";");
  45. w.println("for(int i = 0 ; i < value.length; i++) {");
  46. w.indent();
  47. JType componentType = arrayType.getComponentType();
  48. w.print("value[i] = ("
  49. + ConnectorBundleLoaderFactory.getBoxedTypeName(componentType)
  50. + ") " + JsonDecoder.class.getName() + ".decodeValue(");
  51. ConnectorBundleLoaderFactory.writeTypeCreator(w, componentType);
  52. w.print(", jsonArray.get(i), null, " + connection + ")");
  53. w.println(";");
  54. w.outdent();
  55. w.println("}");
  56. w.println("return value;");
  57. }
  58. @Override
  59. protected void printSerializerBody(TreeLogger logger, SourceWriter w,
  60. String value, String applicationConnection) {
  61. JType componentType = arrayType.getComponentType();
  62. w.println(JSONArray.class.getName() + " values = new "
  63. + JSONArray.class.getName() + "();");
  64. // JPrimitiveType primitive = componentType.isPrimitive();
  65. w.println("for (int i = 0; i < " + value + ".length; i++) {");
  66. w.indent();
  67. w.print("values.set(i, ");
  68. w.print(JsonEncoder.class.getName() + ".encode(" + value + "[i],");
  69. ConnectorBundleLoaderFactory.writeTypeCreator(w, componentType);
  70. w.print(", " + applicationConnection + ")");
  71. w.println(");");
  72. w.outdent();
  73. w.println("}");
  74. w.println("return values;");
  75. }
  76. }