Interface JsonCodec

All Known Implementing Classes:
DatabindCodec, JacksonCodec

public interface JsonCodec
Author:
Julien Viet
  • Method Details

    • fromString

      <T> T fromString(String json, Class<T> clazz) throws DecodeException
      Decode the provide json string to an object extending clazz.
      Parameters:
      json - the json string
      clazz - the required object's class
      Returns:
      the instance
      Throws:
      DecodeException - anything preventing the decoding
    • fromBuffer

      <T> T fromBuffer(Buffer json, Class<T> clazz) throws DecodeException
      Like fromString(String, Class) but with a json Buffer
      Throws:
      DecodeException
    • fromValue

      <T> T fromValue(Object json, Class<T> toValueType)
      Like fromString(String, Class) but with a json Object
    • toString

      default String toString(Object object) throws EncodeException
      Encode the specified object to a string.
      Throws:
      EncodeException
    • toString

      String toString(Object object, boolean pretty) throws EncodeException
      Encode the specified object to a string.
      Parameters:
      object - the object to encode
      pretty - true to format the string prettily
      Returns:
      the json encoded string
      Throws:
      DecodeException - anything preventing the encoding
      EncodeException
    • toBuffer

      Buffer toBuffer(Object object, boolean pretty) throws EncodeException
      Like toString(Object, boolean) but with a json Buffer
      Throws:
      EncodeException
    • toBuffer

      default Buffer toBuffer(Object object) throws EncodeException
      Like toString(Object) but with a json Buffer
      Throws:
      EncodeException
    • fromStream

      default <T> T fromStream(InputStream in, Class<T> clazz) throws DecodeException
      Decode JSON from the given InputStream to an object of the specified type.

      The input is expected to be UTF-8 encoded.

      The codec does not close or flush the stream; the caller retains ownership.

      Parameters:
      in - the input stream containing UTF-8 encoded JSON
      clazz - the required object's class
      Returns:
      the decoded instance
      Throws:
      DecodeException - anything preventing the decoding
    • fromStream

      default Object fromStream(InputStream in) throws DecodeException
      Decode JSON from the given InputStream.

      The input is expected to be UTF-8 encoded.

      The codec does not close or flush the stream; the caller retains ownership.

      Parameters:
      in - the input stream containing UTF-8 encoded JSON
      Returns:
      a JSON element which can be a JsonArray, JsonObject, String, etc.
      Throws:
      DecodeException - anything preventing the decoding
    • toStream

      default void toStream(Object object, OutputStream out) throws EncodeException
      Encode the specified object as JSON to the given OutputStream.

      The output is UTF-8 encoded.

      The codec does not close or flush the stream; the caller retains ownership.

      Parameters:
      object - the object to encode
      out - the output stream to write to
      Throws:
      EncodeException - anything preventing the encoding
    • fromReader

      default <T> T fromReader(Reader reader, Class<T> clazz) throws DecodeException
      Decode JSON from the given Reader to an object of the specified type.

      The codec does not close or flush the reader; the caller retains ownership.

      Parameters:
      reader - the reader containing JSON text
      clazz - the required object's class
      Returns:
      the decoded instance
      Throws:
      DecodeException - anything preventing the decoding
    • fromReader

      default Object fromReader(Reader reader) throws DecodeException
      Decode JSON from the given Reader.

      The codec does not close or flush the reader; the caller retains ownership.

      Parameters:
      reader - the reader containing JSON text
      Returns:
      a JSON element which can be a JsonArray, JsonObject, String, etc.
      Throws:
      DecodeException - anything preventing the decoding
    • toWriter

      default void toWriter(Object object, Writer writer) throws EncodeException
      Encode the specified object as JSON to the given Writer.

      The codec does not close or flush the writer; the caller retains ownership.

      Parameters:
      object - the object to encode
      writer - the writer to write to
      Throws:
      EncodeException - anything preventing the encoding