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)

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

Since:
24.7
Author:
Vaadin Ltd
  • 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 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
    • 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.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