Interface MediaTypeCodec


  • public interface MediaTypeCodec
    Represents a codec for a particular media type. For example JSON.
    Since:
    1.0
    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default <T> T decode​(io.micronaut.core.type.Argument<T> type, byte[] bytes)
      Decode the given type from the given bytes.
      default <T> T decode​(io.micronaut.core.type.Argument<T> type, io.micronaut.core.io.buffer.ByteBuffer<?> buffer)
      Decode the given type from the given buffer.
      <T> T decode​(io.micronaut.core.type.Argument<T> type, java.io.InputStream inputStream)
      Decode the given type from the given InputStream.
      default <T> T decode​(io.micronaut.core.type.Argument<T> type, java.lang.String data)
      Decode the given type from the given bytes.
      default <T> T decode​(java.lang.Class<T> type, byte[] bytes)
      Decode the given type from the given bytes.
      default <T> T decode​(java.lang.Class<T> type, io.micronaut.core.io.buffer.ByteBuffer<?> buffer)
      Decode the given type from the given buffer.
      default <T> T decode​(java.lang.Class<T> type, java.io.InputStream inputStream)
      Decode the given type from the given InputStream.
      default <T> T decode​(java.lang.Class<T> type, java.lang.String data)
      Decode the given type from the given bytes.
      default <T> byte[] encode​(io.micronaut.core.type.Argument<T> type, T object)
      Encode the given type returning the object as a byte[].
      default <T,​B>
      io.micronaut.core.io.buffer.ByteBuffer<B>
      encode​(io.micronaut.core.type.Argument<T> type, T object, io.micronaut.core.io.buffer.ByteBufferFactory<?,​B> allocator)
      Encode the given type returning the object as a ByteBuffer.
      default <T> void encode​(io.micronaut.core.type.Argument<T> type, T object, java.io.OutputStream outputStream)
      Encode the given type to the given OutputStream.
      <T> byte[] encode​(T object)
      Encode the given type returning the object as a byte[].
      <T,​B>
      io.micronaut.core.io.buffer.ByteBuffer<B>
      encode​(T object, io.micronaut.core.io.buffer.ByteBufferFactory<?,​B> allocator)
      Encode the given type returning the object as a ByteBuffer.
      <T> void encode​(T object, java.io.OutputStream outputStream)
      Encode the given type to the given OutputStream.
      java.util.Collection<MediaType> getMediaTypes()  
      default boolean supportsType​(java.lang.Class<?> type)
      Whether the codec can decode the given type.
    • Method Detail

      • getMediaTypes

        java.util.Collection<MediaType> getMediaTypes()
        Returns:
        The media type of the codec
      • decode

        <T> T decode​(io.micronaut.core.type.Argument<T> type,
                     java.io.InputStream inputStream)
              throws CodecException
        Decode the given type from the given InputStream.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type
        inputStream - The input stream
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be decoded
      • encode

        <T> void encode​(T object,
                        java.io.OutputStream outputStream)
                 throws CodecException
        Encode the given type to the given OutputStream.
        Type Parameters:
        T - The generic type
        Parameters:
        object - The object to encode
        outputStream - The output stream
        Throws:
        CodecException - When the result cannot be encoded
      • encode

        <T> byte[] encode​(T object)
                   throws CodecException
        Encode the given type returning the object as a byte[].
        Type Parameters:
        T - The generic type
        Parameters:
        object - The object to encode
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be encoded
      • encode

        <T,​B> io.micronaut.core.io.buffer.ByteBuffer<B> encode​(T object,
                                                                     io.micronaut.core.io.buffer.ByteBufferFactory<?,​B> allocator)
                                                              throws CodecException
        Encode the given type returning the object as a ByteBuffer.
        Type Parameters:
        T - The generic type
        B - The buffer type
        Parameters:
        object - The object to encode
        allocator - The allocator
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be encoded
      • encode

        default <T> void encode​(@NonNull
                                io.micronaut.core.type.Argument<T> type,
                                @NonNull
                                T object,
                                @NonNull
                                java.io.OutputStream outputStream)
                         throws CodecException
        Encode the given type to the given OutputStream.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type including any generics and/or metadata.
        object - The object to encode
        outputStream - The output stream
        Throws:
        CodecException - When the result cannot be encoded
        Since:
        3.2.0
      • encode

        @NonNull
        default <T> byte[] encode​(@NonNull
                                  io.micronaut.core.type.Argument<T> type,
                                  T object)
                           throws CodecException
        Encode the given type returning the object as a byte[].
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type including any generics and/or metadata
        object - The object to encode
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be encoded
        Since:
        3.2.0
      • encode

        @NonNull
        default <T,​B> io.micronaut.core.io.buffer.ByteBuffer<B> encode​(@NonNull
                                                                             io.micronaut.core.type.Argument<T> type,
                                                                             T object,
                                                                             @NonNull
                                                                             io.micronaut.core.io.buffer.ByteBufferFactory<?,​B> allocator)
                                                                      throws CodecException
        Encode the given type returning the object as a ByteBuffer.
        Type Parameters:
        T - The generic type
        B - The buffer type
        Parameters:
        type - The type including any generics and/or metadata
        object - The object to encode
        allocator - The allocator
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be encoded
        Since:
        3.2.0
      • decode

        default <T> T decode​(java.lang.Class<T> type,
                             java.io.InputStream inputStream)
                      throws CodecException
        Decode the given type from the given InputStream.
        Type Parameters:
        T - The generic type
        Parameters:
        type - The type
        inputStream - The input stream
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be decoded
      • decode

        default <T> T decode​(java.lang.Class<T> type,
                             byte[] bytes)
                      throws CodecException
        Decode the given type from the given bytes.
        Type Parameters:
        T - The decoded type
        Parameters:
        type - The type
        bytes - The bytes
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be decoded
      • decode

        default <T> T decode​(io.micronaut.core.type.Argument<T> type,
                             byte[] bytes)
                      throws CodecException
        Decode the given type from the given bytes.
        Type Parameters:
        T - The decoded type
        Parameters:
        type - The type
        bytes - The bytes
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be decoded
      • decode

        default <T> T decode​(java.lang.Class<T> type,
                             io.micronaut.core.io.buffer.ByteBuffer<?> buffer)
                      throws CodecException
        Decode the given type from the given buffer. Implementations optimized to handle ByteBuffer instances should override this method.
        Type Parameters:
        T - The decoded type
        Parameters:
        type - The type
        buffer - the buffer
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be decoded
      • decode

        default <T> T decode​(io.micronaut.core.type.Argument<T> type,
                             io.micronaut.core.io.buffer.ByteBuffer<?> buffer)
                      throws CodecException
        Decode the given type from the given buffer. Implementations optimized to handle ByteBuffer instances should override this method.
        Type Parameters:
        T - The decoded type
        Parameters:
        type - The type
        buffer - the buffer
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be decoded
      • decode

        default <T> T decode​(java.lang.Class<T> type,
                             java.lang.String data)
                      throws CodecException
        Decode the given type from the given bytes.
        Type Parameters:
        T - The decoded type
        Parameters:
        type - The type
        data - The data as a string
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be decoded
      • decode

        default <T> T decode​(io.micronaut.core.type.Argument<T> type,
                             java.lang.String data)
                      throws CodecException
        Decode the given type from the given bytes.
        Type Parameters:
        T - The decoded type
        Parameters:
        type - The type
        data - The data as a string
        Returns:
        The decoded result
        Throws:
        CodecException - When the result cannot be decoded
      • supportsType

        default boolean supportsType​(java.lang.Class<?> type)
        Whether the codec can decode the given type.
        Parameters:
        type - The type
        Returns:
        True if it can