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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.communication;
  17. import java.lang.reflect.Type;
  18. import com.vaadin.ui.ConnectorTracker;
  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, ConnectorTracker)} and
  25. * {@link #deserialize(Type, JsonValue, ConnectorTracker)} 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.
  31. * <p>
  32. * This is the server side interface, see
  33. * com.vaadin.client.communication.JSONSerializer for the client side interface.
  34. *
  35. * @since 7.2
  36. * @author Vaadin Ltd
  37. */
  38. public interface JsonSerializer<T> {
  39. /**
  40. * Creates and deserializes an object received from the client. Must be
  41. * compatible with {@link #serialize(Object, ConnectorTracker)} and also
  42. * with the client side com.vaadin.client.communication.JSONSerializer.
  43. *
  44. * @param type
  45. * The expected return type
  46. * @param jsonValue
  47. * the value from the JSON
  48. * @param connectorTracker
  49. * the connector tracker instance for the UI
  50. * @return A deserialized object
  51. */
  52. T deserialize(Type type, JsonValue jsonValue, ConnectorTracker connectorTracker);
  53. /**
  54. * Serialize the given object into JSON. Must be compatible with
  55. * {@link #deserialize(Type, JsonValue, ConnectorTracker)} and the client side
  56. * com.vaadin.client.communication.JSONSerializer
  57. *
  58. * @param value
  59. * The object to serialize
  60. * @param connectorTracker
  61. * The connector tracker instance for the UI
  62. * @return A JSON serialized version of the object
  63. */
  64. JsonValue serialize(T value, ConnectorTracker connectorTracker);
  65. }