Class JacksonCodec

java.lang.Object
com.vaadin.flow.internal.JacksonCodec

public class JacksonCodec extends Object
Utility for encoding objects to and from JSON.

Supported types are

  • String
  • Boolean and boolean
  • Integer and int
  • Double and double (NaN and infinity not supported)
  • JsonNode and all its sub types
  • Element (encoded as a reference to the element)
  • Component (encoded as a reference to the root element)
  • JsFunction (encoded as a JS function literal that closes over its captured parameters on the client, or for one declared by a JsDefinition interface as the identifier of the function in the bundle and the captured parameters)
  • an implementation of an interface that extends JsSnippet, which JsSnippets hands out (encoded like a declared JsFunction)

For internal use only. May be renamed or removed in a future release.

Since:
24.7
Author:
Vaadin Ltd
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The key of the JSON object that a JsFunction is encoded as.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    Helper for checking whether the type is supported by encodeWithoutTypeInfo(Object).
    static <T> T
    decodeAs(tools.jackson.databind.JsonNode json, Class<T> type)
    Decodes a JSON value as an instance of the given class, for example String, Integer, a primitive type such as int, a bean class or a JsonNode type.
    static <T> T
    decodeAs(tools.jackson.databind.JsonNode json, tools.jackson.core.type.TypeReference<T> typeReference)
    Decodes a JSON value as an instance of the type described by the given type reference.
    static <T> T
    decodeAs(tools.jackson.databind.JsonNode json, tools.jackson.databind.JavaType type)
    Decodes a JSON value as an instance of the given type.
    decodeWithoutTypeInfo(tools.jackson.databind.node.BaseJsonNode json)
    Helper for decoding any "primitive" value that is directly supported in JSON.
    static tools.jackson.databind.JsonNode
    Encodes a "primitive" value or a constant pool reference to JSON.
    static tools.jackson.databind.JsonNode
    Helper for encoding any "primitive" value that is directly supported in JSON.
    static tools.jackson.databind.JsonNode
    Helper for encoding values that might not have a native representation in JSON.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • encodeWithTypeInfo

      public static tools.jackson.databind.JsonNode encodeWithTypeInfo(Object value)
      Helper for encoding values that might not have a native representation in JSON. Such types are encoded as an JSON array starting with an id defining the actual type and followed by the actual data. Supported value types are any native JSON type supported by encodeWithoutTypeInfo(Object), and all other types are handled by Jackson serialization with custom serializers for Component and Node types.
      Parameters:
      value - the value to encode
      Returns:
      the value encoded as JSON
    • canEncodeWithoutTypeInfo

      public static boolean canEncodeWithoutTypeInfo(Class<?> type)
      Helper for checking whether the type is supported by encodeWithoutTypeInfo(Object). Supported value types are String, Integer, Double, Boolean, JsonNode.
      Parameters:
      type - the type to check
      Returns:
      whether the type can be encoded
    • encodeWithConstantPool

      public static tools.jackson.databind.JsonNode encodeWithConstantPool(Object value, ConstantPool constantPool)
      Encodes a "primitive" value or a constant pool reference to JSON. This methods supports ConstantPoolKey and ParameterizedConstantPoolKey in addition to the types supported by encodeWithoutTypeInfo(Object).
      Parameters:
      value - the value to encode
      constantPool - the constant pool to use for encoding constant pool references
      Returns:
      the value encoded as JSON
    • encodeWithoutTypeInfo

      public static tools.jackson.databind.JsonNode encodeWithoutTypeInfo(Object value)
      Helper for encoding any "primitive" value that is directly supported in JSON. Supported values types are String, Number, Boolean, JsonNode. null is also supported.
      Parameters:
      value - the value to encode
      Returns:
      the value encoded as JSON
    • decodeWithoutTypeInfo

      public static Serializable decodeWithoutTypeInfo(tools.jackson.databind.node.BaseJsonNode json)
      Helper for decoding any "primitive" value that is directly supported in JSON. Supported values types are String, Number, Boolean, JsonNode. NullNode is also supported.
      Parameters:
      json - the JSON value to decode
      Returns:
      the decoded value
      Since:
      25.0
    • decodeAs

      public static <T> T decodeAs(tools.jackson.databind.JsonNode json, Class<T> type)
      Decodes a JSON value as an instance of the given class, for example String, Integer, a primitive type such as int, a bean class or a JsonNode type.

      A JSON null is decoded as null, except for a primitive type, which gets the primitive's default value. For a JSON node type, such as JsonNode or ObjectNode, the value is returned as is if it is of that node type.

      Type Parameters:
      T - the type to decode as
      Parameters:
      json - the JSON value to decode, not null
      type - the class to decode as, not null
      Returns:
      the value decoded as the given type
      Throws:
      IllegalArgumentException - if deserialization failed, or if the type is a JSON node type that the value is not, such as an object node for an array
      NullPointerException - if the JSON value or the type is null
    • decodeAs

      public static <T> T decodeAs(tools.jackson.databind.JsonNode json, tools.jackson.databind.JavaType type)
      Decodes a JSON value as an instance of the given type. The type can be a generic type, for example List<MyBean>.

      A JSON null is decoded as null, except for a primitive type, which gets the primitive's default value. For a JSON node type, such as JsonNode or ObjectNode, the value is returned as is if it is of that node type.

      Type Parameters:
      T - the type to decode as
      Parameters:
      json - the JSON value to decode, not null
      type - the type to decode as, not null
      Returns:
      the value decoded as the given type
      Throws:
      IllegalArgumentException - if deserialization failed, or if the type is a JSON node type that the value is not, such as an object node for an array
      NullPointerException - if the JSON value or the type is null
      Since:
      25.4
    • decodeAs

      public static <T> T decodeAs(tools.jackson.databind.JsonNode json, tools.jackson.core.type.TypeReference<T> typeReference)
      Decodes a JSON value as an instance of the type described by the given type reference. The type can be a generic type, for example List<MyBean>:
       List<MyBean> result = JacksonCodec.decodeAs(json,
               new TypeReference<List<MyBean>>() {
               });
       

      A JSON null is decoded as null, except for a primitive type, which gets the primitive's default value. For a JSON node type, such as JsonNode or ObjectNode, the value is returned as is if it is of that node type.

      Type Parameters:
      T - the type to decode as
      Parameters:
      json - the JSON value to decode, not null
      typeReference - the type reference describing the type to decode as, not null
      Returns:
      the value decoded as the given type
      Throws:
      IllegalArgumentException - if deserialization failed, or if the type is a JSON node type that the value is not, such as an object node for an array
      NullPointerException - if the JSON value or the type reference is null
      Since:
      25.0