Class JsonCodec


  • public class JsonCodec
    extends Object
    Utility for encoding objects to and from JSON.

    Supported types are

    • String
    • Boolean and boolean
    • Integer and int
    • Double and double (NaN and infinity not supported)
    • JsonValue and all its sub types
    • Element (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
    • 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 by encodeWithoutTypeInfo(Object), Element and Component (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 by encodeWithoutTypeInfo(Object). Supported value types are String, Integer, Double, Boolean, JsonValue.
        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 supports ConstantPoolKey in addition to the types supported by encodeWithoutTypeInfo(Object).
        Parameters:
        value - the value to encode
        constantPool - 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 are String, 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 are String, 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 value
        type - the type to decode as
        Returns:
        the value decoded as the given type
        Throws:
        IllegalArgumentException - if the type was unsupported