Class JsonCodec

java.lang.Object
com.flowingcode.vaadin.jsonmigration.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)

Author:
Vaadin Ltd
  • Constructor Details

    • JsonCodec

      public JsonCodec()
  • Method Details

    • 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
    • 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
    • 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