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

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