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)

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 the given JSON value as the given 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 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 the given JSON value as the given type.

      Supported types are String, Boolean, Integer, Double, primitives boolean, int, double, JsonNode, and any bean object that can be deserialized from JSON.

      Type Parameters:
      T - the decoded type
      Parameters:
      json - the JSON value
      type - the type to decode as
      Returns:
      the value decoded as the given type
      Throws:
      IllegalArgumentException - if the type was unsupported or deserialization failed
    • 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>.
      Type Parameters:
      T - the type to decode as
      Parameters:
      json - the JSON value to decode
      type - the type to decode as
      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
    • 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 given type reference. This method supports generic types such as List<MyBean> and Map<String, MyBean> through Jackson's TypeReference mechanism.

      Example usage:

       TypeReference<List<MyBean>> typeRef = new TypeReference<List<MyBean>>() {
       };
       List<MyBean> result = JacksonCodec.decodeAs(jsonNode, typeRef);
       
      Type Parameters:
      T - the type to decode as
      Parameters:
      json - the JSON value to decode
      typeReference - the type reference describing the target type
      Returns:
      the value decoded as the given type
      Throws:
      IllegalArgumentException - if deserialization failed
      Since:
      25.0