Package com.vaadin.flow.internal
Class JsonCodec
java.lang.Object
com.vaadin.flow.internal.JsonCodec
Utility for encoding objects to and from JSON.
Supported types are
String
Boolean
andboolean
Integer
andint
Double
anddouble
(NaN
and infinity not supported)JsonValue
and 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:
- 1.0
- Author:
- Vaadin Ltd
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Type id for a complex type array containing aJsonArray
.static final int
Type id for a complex type array containing anElement
.static final int
Type id for a complex type array identifying aReturnChannelRegistration
reference. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canEncodeWithoutTypeInfo
(Class<?> type) Helper for checking whether the type is supported byencodeWithoutTypeInfo(Object)
.static boolean
canEncodeWithTypeInfo
(Class<?> type) Helper for checking whether the type is supported byencodeWithTypeInfo(Object)
.static <T> T
Decodes the given JSON value as the given type.static Serializable
decodeWithoutTypeInfo
(elemental.json.JsonValue json) Helper for decoding any "primitive" value that is directly supported in JSON.static elemental.json.JsonValue
encodeWithConstantPool
(Object value, ConstantPool constantPool) Encodes a "primitive" value or a constant pool reference to JSON.static elemental.json.JsonValue
encodeWithoutTypeInfo
(Object value) Helper for encoding any "primitive" value that is directly supported in JSON.static elemental.json.JsonValue
encodeWithTypeInfo
(Object value) Helper for encoding values that might not have a native representation in JSON.
-
Field Details
-
NODE_TYPE
public static final int NODE_TYPEType id for a complex type array containing anElement
.- See Also:
-
ARRAY_TYPE
public static final int ARRAY_TYPEType id for a complex type array containing aJsonArray
.- See Also:
-
RETURN_CHANNEL_TYPE
public static final int RETURN_CHANNEL_TYPEType id for a complex type array identifying aReturnChannelRegistration
reference.- 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)
,Element
andComponent
(encoded as its root element).- 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
,JsonValue
.- Parameters:
type
- the type to check- Returns:
- whether the type can be encoded
-
canEncodeWithTypeInfo
Helper for checking whether the type is supported byencodeWithTypeInfo(Object)
. Supported values types areNode
,Component
,ReturnChannelRegistration
and anything accepted bycanEncodeWithoutTypeInfo(Class)
.- Parameters:
type
- the type to check- Returns:
- whether the type can be encoded
-
encodeWithConstantPool
public static elemental.json.JsonValue encodeWithConstantPool(Object value, ConstantPool constantPool) Encodes a "primitive" value or a constant pool reference to JSON. This methods supportsConstantPoolKey
in 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
,JsonValue
.null
is 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
,JsonValue
.null
is 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
and primitives boolean, int, double- 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
-