Package com.vaadin.flow.internal
Class JsonCodec
- java.lang.Object
-
- com.vaadin.flow.internal.JsonCodec
-
public class JsonCodec extends Object
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
Fields Modifier and Type Field Description static int
ARRAY_TYPE
Type id for a complex type array containing aJsonArray
.static int
NODE_TYPE
Type id for a complex type array containing anElement
.static int
RETURN_CHANNEL_TYPE
Type id for a complex type array identifying aReturnChannelRegistration
reference.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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
decodeAs(elemental.json.JsonValue json, Class<T> type)
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 Detail
-
NODE_TYPE
public static final int NODE_TYPE
Type id for a complex type array containing anElement
.- See Also:
- Constant Field Values
-
ARRAY_TYPE
public static final int ARRAY_TYPE
Type id for a complex type array containing aJsonArray
.- See Also:
- Constant Field Values
-
RETURN_CHANNEL_TYPE
public static final int RETURN_CHANNEL_TYPE
Type id for a complex type array identifying aReturnChannelRegistration
reference.- See Also:
- Constant Field Values
-
-
Method Detail
-
encodeWithTypeInfo
public static elemental.json.JsonValue 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 byencodeWithoutTypeInfo(Object)
,Element
andComponent
(encoded as its root element).- 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 byencodeWithoutTypeInfo(Object)
. Supported value types areString
,Integer
,Double
,Boolean
,JsonValue
.- Parameters:
type
- the type to check- Returns:
- whether the type can be encoded
-
canEncodeWithTypeInfo
public static boolean canEncodeWithTypeInfo(Class<?> type)
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
public static elemental.json.JsonValue encodeWithoutTypeInfo(Object value)
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
public static Serializable decodeWithoutTypeInfo(elemental.json.JsonValue json)
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
public static <T> T decodeAs(elemental.json.JsonValue json, Class<T> type)
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
-
-