com.vaadin.flow.internal.
Class 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
FieldsModifier and TypeFieldDescriptionstatic final int
Type id for a complex type array containing a
JsonArray
.static final int
Type id for a complex type array containing an
Element
.static final int
Type id for a complex type array identifying a
ReturnChannelRegistration
reference. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canEncodeWithoutTypeInfo
(Class<?> type) Helper for checking whether the type is supported by
encodeWithoutTypeInfo(Object)
.static boolean
canEncodeWithTypeInfo
(Class<?> type) Helper for checking whether the type is supported by
encodeWithTypeInfo(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 an
Element
.See Also:
-
ARRAY_TYPE
public static final int ARRAY_TYPEType id for a complex type array containing a
JsonArray
.See Also:
-
RETURN_CHANNEL_TYPE
public static final int RETURN_CHANNEL_TYPEType id for a complex type array identifying a
ReturnChannelRegistration
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 by
encodeWithoutTypeInfo(Object)
,Element
andComponent
(encoded as its root element).Parameters:
value
- the value to encodeReturns:
the value encoded as JSON
-
canEncodeWithoutTypeInfo
Helper for checking whether the type is supported by
encodeWithoutTypeInfo(Object)
. Supported value types areString
,Integer
,Double
,Boolean
,JsonValue
.Parameters:
type
- the type to checkReturns:
whether the type can be encoded
-
canEncodeWithTypeInfo
Helper for checking whether the type is supported by
encodeWithTypeInfo(Object)
. Supported values types areNode
,Component
,ReturnChannelRegistration
and anything accepted bycanEncodeWithoutTypeInfo(Class)
.Parameters:
type
- the type to checkReturns:
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 supports
ConstantPoolKey
in addition to the types supported byencodeWithoutTypeInfo(Object)
.Parameters:
value
- the value to encodeconstantPool
- the constant pool to use for encoding constant pool referencesReturns:
the value encoded as JSON
-
encodeWithoutTypeInfo
Helper for encoding any "primitive" value that is directly supported in JSON. Supported values types are
String
,Number
,Boolean
,JsonValue
.null
is also supported.Parameters:
value
- the value to encodeReturns:
the value encoded as JSON
-
decodeWithoutTypeInfo
Helper for decoding any "primitive" value that is directly supported in JSON. Supported values types are
String
,Number
,Boolean
,JsonValue
.null
is also supported.Parameters:
json
- the JSON value to decodeReturns:
the decoded value
-
decodeAs
Decodes the given JSON value as the given type.
Supported types are
String
,Boolean
,Integer
,Double
and primitives boolean, int, doubleType Parameters:
T
- the decoded typeParameters:
json
- the JSON valuetype
- the type to decode asReturns:
the value decoded as the given type
Throws:
IllegalArgumentException
- if the type was unsupported
-