Class Json

java.lang.Object
io.vertx.core.json.Json

public class Json extends Object
Author:
Tim Fox
  • Field Details

    • CODEC

      public static final JsonCodec CODEC
      This will load the default codec.
  • Constructor Details

    • Json

      public Json()
  • Method Details

    • load

      public static JsonFactory load()
      Load the factory with the ServiceLoader, when no factory is found then a factory using Jackson will be returned.
      When jackson-databind is available then a codec using it will be used otherwise the codec will only use jackson-core and provide best effort mapping.
    • encode

      public static String encode(Object obj) throws EncodeException
      Encode a POJO to JSON using the underlying Jackson mapper.
      Parameters:
      obj - a POJO
      Returns:
      a String containing the JSON representation of the given POJO.
      Throws:
      EncodeException - if a property cannot be encoded.
    • encodeToBuffer

      public static Buffer encodeToBuffer(Object obj) throws EncodeException
      Encode a POJO to JSON using the underlying Jackson mapper.
      Parameters:
      obj - a POJO
      Returns:
      a Buffer containing the JSON representation of the given POJO.
      Throws:
      EncodeException - if a property cannot be encoded.
    • encodePrettily

      public static String encodePrettily(Object obj) throws EncodeException
      Encode a POJO to JSON with pretty indentation, using the underlying Jackson mapper.
      Parameters:
      obj - a POJO
      Returns:
      a String containing the JSON representation of the given POJO.
      Throws:
      EncodeException - if a property cannot be encoded.
    • decodeValue

      public static <T> T decodeValue(String str, Class<T> clazz) throws DecodeException
      Decode a given JSON string to a POJO of the given class type.
      Type Parameters:
      T - the generic type.
      Parameters:
      str - the JSON string.
      clazz - the class to map to.
      Returns:
      an instance of T
      Throws:
      DecodeException - when there is a parsing or invalid mapping.
    • decodeValue

      public static Object decodeValue(String str) throws DecodeException
      Decode a given JSON string.
      Parameters:
      str - the JSON string.
      Returns:
      a JSON element which can be a JsonArray, JsonObject, String, ...etc if the content is an array, object, string, ...etc
      Throws:
      DecodeException - when there is a parsing or invalid mapping.
    • decodeValue

      public static Object decodeValue(Buffer buf) throws DecodeException
      Decode a given JSON buffer.
      Parameters:
      buf - the JSON buffer.
      Returns:
      a JSON element which can be a JsonArray, JsonObject, String, ...etc if the buffer contains an array, object, string, ...etc
      Throws:
      DecodeException - when there is a parsing or invalid mapping.
    • decodeValue

      public static <T> T decodeValue(Buffer buf, Class<T> clazz) throws DecodeException
      Decode a given JSON buffer to a POJO of the given class type.
      Type Parameters:
      T - the generic type.
      Parameters:
      buf - the JSON buffer.
      clazz - the class to map to.
      Returns:
      an instance of T
      Throws:
      DecodeException - when there is a parsing or invalid mapping.