Class JacksonCodec
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)JsFunction(encoded as a JS function literal that closes over its captured parameters on the client, or for one declared by aJsDefinitioninterface as the identifier of the function in the bundle and the captured parameters)- an implementation of an interface that extends
JsSnippet, whichJsSnippetshands out (encoded like a declaredJsFunction)
For internal use only. May be renamed or removed in a future release.
- Since:
- 24.7
- Author:
- Vaadin Ltd
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe key of the JSON object that aJsFunctionis encoded as. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanEncodeWithoutTypeInfo(Class<?> type) Helper for checking whether the type is supported byencodeWithoutTypeInfo(Object).static <T> Tstatic <T> TdecodeAs(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> TdecodeAs(tools.jackson.databind.JsonNode json, tools.jackson.databind.JavaType type) Decodes a JSON value as an instance of the given type.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.
-
Field Details
-
FUNCTION_KEY
The key of the JSON object that aJsFunctionis encoded as.- Since:
- 25.4
- See Also:
-
-
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 supportsConstantPoolKeyandParameterizedConstantPoolKeyin 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
- Since:
- 25.0
-
decodeAs
Decodes a JSON value as an instance of the given class, for exampleString,Integer, a primitive type such asint, a bean class or aJsonNodetype.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 asJsonNodeorObjectNode, 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, notnulltype- the class to decode as, notnull- 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 arrayNullPointerException- if the JSON value or the type isnull
-
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 exampleList<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 asJsonNodeorObjectNode, 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, notnulltype- the type to decode as, notnull- 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 arrayNullPointerException- if the JSON value or the type isnull- 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 exampleList<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 asJsonNodeorObjectNode, 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, notnulltypeReference- the type reference describing the type to decode as, notnull- 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 arrayNullPointerException- if the JSON value or the type reference isnull- Since:
- 25.0
-