Class Json

java.lang.Object
io.r2dbc.postgresql.codec.Json

public abstract class Json extends Object
Value object to represent JSON values.

JSON values are divided into input and output values. Input values are provided by code that wants to bind values to a query. Input values are intended to be consumed by the driver. Input values should not be consumed by application code.

Output values are returned by the driver to be consumed by application code using the following methods:

JSON values should be generally considered for single-consumption only. Output values retain a reference to a potentially pooled memory buffer and must be consumed to avoid memory leaks.

  • Method Details

    • of

      public static Json of(ByteBuffer buffer)
      Create a Json object from a ByteBuffer.
      Parameters:
      buffer - the JSON value as ByteBuffer
      Returns:
      Json object from a ByteBuffer.
      Throws:
      IllegalArgumentException - if buffer is null
    • of

      public static Json of(io.netty.buffer.ByteBuf buffer)
      Create a Json object from a ByteBuf.

      The ByteBuf is released after encoding the value.

      Parameters:
      buffer - the JSON value as ByteBuf
      Returns:
      Json object from a ByteBuf.
      Throws:
      IllegalArgumentException - if buffer is null
    • of

      public static Json of(InputStream inputStream)
      Create a Json object from a InputStream.

      The InputStream is closed after encoding the value.

      Parameters:
      inputStream - the JSON value as InputStream
      Returns:
      Json object from a InputStream.
      Throws:
      IllegalArgumentException - if inputStream is null
    • of

      public static Json of(byte[] value)
      Create a Json object from a byte[] value.
      Parameters:
      value - the JSON value as byte[]
      Returns:
      Json object from a byte[] value.
      Throws:
      IllegalArgumentException - if value is null
    • of

      public static Json of(String value)
      Create a Json object from a String. Uses UTF-8 encoding to convert the value into its binary representation.
      Parameters:
      value - the JSON value as String
      Returns:
      Json object from a String.
      Throws:
      IllegalArgumentException - if value is null
    • mapBuffer

      @Nullable public abstract <T> T mapBuffer(Function<ByteBuffer, ? extends T> mappingFunction)
      Returns an object consisting of the result of applying the given mapping Function to the ByteBuffer of this JSON value.

      Consumption methods should be called exactly once as the underlying JSON value is typically released after consumption.

      Type Parameters:
      T - return type.
      Parameters:
      mappingFunction - mapping function that gets applied to the ByteBuffer representation of this JSON value.
      Returns:
      the mapped value. Can be null.
    • mapByteBuf

      @Nullable public abstract <T> T mapByteBuf(Function<io.netty.buffer.ByteBuf, ? extends T> mappingFunction)
      Returns an object consisting of the result of applying the given mapping Function to the ByteBuf of this JSON value.

      Consumption methods should be called exactly once as the underlying JSON value is typically released after consumption.

      Type Parameters:
      T - return type.
      Parameters:
      mappingFunction - mapping function that gets applied to the ByteBuf representation of this JSON value.
      Returns:
      the mapped value. Can be null.
    • mapInputStream

      @Nullable public abstract <T> T mapInputStream(Function<InputStream, ? extends T> mappingFunction)
      Returns an object consisting of the result of applying the given mapping Function to the InputStream of this JSON value.

      Consumption methods should be called exactly once as the underlying JSON value is typically released after consumption.

      Type Parameters:
      T - return type.
      Parameters:
      mappingFunction - mapping function that gets applied to the InputStream representation of this JSON value.
      Returns:
      the mapped value. Can be null.
    • asArray

      public abstract byte[] asArray()
      Returns the value as byte[].

      Consumption methods should be called exactly once as the underlying JSON value is typically released after consumption.

      Returns:
      the contents of the JSON value as byte[].
    • asString

      public abstract String asString()
      Returns the value as String.

      Consumption methods should be called exactly once as the underlying JSON value is typically released after consumption.

      Returns:
      the contents of the JSON value as String.