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.

JSONSerializer.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.client.communication;
  17. import com.vaadin.client.ApplicationConnection;
  18. import com.vaadin.client.metadata.Type;
  19. import elemental.json.JsonValue;
  20. /**
  21. * Implementors of this interface knows how to serialize an Object of a given
  22. * type to JSON and how to deserialize the JSON back into an object.
  23. * <p>
  24. * The {@link #serialize(Object, ApplicationConnection)} and
  25. * {@link #deserialize(Type, JsonValue, ApplicationConnection)} methods must be
  26. * symmetric so they can be chained and produce the original result (or an equal
  27. * result).
  28. * <p>
  29. * Each {@link JSONSerializer} implementation can handle an object of a single
  30. * type - see {@link Type#findSerializer()}.
  31. * <p>
  32. * This is the client side interface, see
  33. * com.vaadin.server.communication.JSONSerializer for the server side interface.
  34. *
  35. * @since 7.0
  36. */
  37. public interface JSONSerializer<T> {
  38. /**
  39. * Creates and deserializes an object received from the server. Must be
  40. * compatible with {@link #serialize(Object, ApplicationConnection)} and
  41. * also with the server side JsonCodec.encode method.
  42. *
  43. * @param type
  44. * the type to deserialize
  45. * @param jsonValue
  46. * JSON map from property name to property value
  47. * @param connection
  48. * the application connection providing the context
  49. *
  50. * @return A deserialized object
  51. */
  52. T deserialize(Type type, JsonValue jsonValue,
  53. ApplicationConnection connection);
  54. /**
  55. * Serialize the given object into JSON. Must be compatible with
  56. * {@link #deserialize(Type, JsonValue, ApplicationConnection)} and also
  57. * with the server side JsonCodec.decodeCustomType method.
  58. *
  59. * @param value
  60. * The object to serialize
  61. * @param connection
  62. * the application connection providing the context
  63. * @return A JSON serialized version of the object
  64. */
  65. JsonValue serialize(T value, ApplicationConnection connection);
  66. }