Package com.vaadin.flow.internal
Class JacksonCodec
java.lang.Object
com.vaadin.flow.internal.JacksonCodec
Utility for encoding objects to and from JSON.
Supported types are
StringBooleanandbooleanIntegerandintDoubleanddouble(NaNand infinity not supported)JsonNodeand all its sub typesElement(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 Summary
Modifier and TypeMethodDescriptionstatic booleancanEncodeWithoutTypeInfo(Class<?> type) Helper for checking whether the type is supported byencodeWithoutTypeInfo(Object).static <T> TDecodes the given JSON value as the given type.static <T> TdecodeAs(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 SerializabledecodeWithoutTypeInfo(tools.jackson.databind.node.BaseJsonNode json) Helper for decoding any "primitive" value that is directly supported in JSON.static tools.jackson.databind.JsonNodeencodeWithConstantPool(Object value, ConstantPool constantPool) Encodes a "primitive" value or a constant pool reference to JSON.static tools.jackson.databind.JsonNodeencodeWithoutTypeInfo(Object value) Helper for encoding any "primitive" value that is directly supported in JSON.static tools.jackson.databind.JsonNodeencodeWithTypeInfo(Object value) Helper for encoding values that might not have a native representation in JSON.
-
Method Details
-
encodeWithTypeInfo
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 byencodeWithoutTypeInfo(Object), and all other types are handled by Jackson serialization with custom serializers forComponentand Node types.- Parameters:
value- the value to encode- Returns:
- the value encoded as JSON
-
canEncodeWithoutTypeInfo
Helper for checking whether the type is supported byencodeWithoutTypeInfo(Object). Supported value types areString,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 supportsConstantPoolKeyin addition to the types supported byencodeWithoutTypeInfo(Object).- Parameters:
value- the value to encodeconstantPool- the constant pool to use for encoding constant pool references- Returns:
- the value encoded as JSON
-
encodeWithoutTypeInfo
Helper for encoding any "primitive" value that is directly supported in JSON. Supported values types areString,Number,Boolean,JsonNode.nullis also supported.- Parameters:
value- the value to encode- Returns:
- the value encoded as JSON
-
decodeWithoutTypeInfo
Helper for decoding any "primitive" value that is directly supported in JSON. Supported values types areString,Number,Boolean,JsonNode.NullNodeis also supported.- Parameters:
json- the JSON value to decode- Returns:
- the decoded value
-
decodeAs
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 valuetype- 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 asList<MyBean>andMap<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 decodetypeReference- the type reference describing the target type- Returns:
- the value decoded as the given type
- Throws:
IllegalArgumentException- if deserialization failed
-