Interface CborFactory


  • public interface CborFactory
    This interface abstracts serialization/deserialization via CBOR. Registration of an implementation is done via a ServiceLoader (putting a file named org.eclipse.ditto.json.CborFactory inside the META-INF/services directory of the providing module. Use isCborAvailable() in order to check for CBOR availability.

    This is a Ditto internal class which is not intended for re-use. It therefore is not treated as API which is held binary compatible to previous versions.

    Since:
    1.2.1
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      byte[] createCborRepresentation​(java.util.List<JsonValue> list, int guessedSerializedSize)
      Creates the CBOR representation of the passed JSON value list and the estimated required serialized size of it.
      byte[] createCborRepresentation​(java.util.Map<java.lang.String,​JsonField> jsonFieldMap, int guessedSerializedSize)
      Creates the CBOR representation of the passed JSON fieldMap and the estimated required serialized size of it.
      boolean isCborAvailable()
      Determines whether the libraries providing CBOR serializations are available (classes can be loaded).
      JsonValue readFrom​(byte[] bytes)
      Deserializes a JsonValue by parsing the passed bytes with CBOR.
      JsonValue readFrom​(byte[] bytes, int offset, int length)
      Deserializes a JsonValue by parsing the passed bytes with CBOR applying a offset and length.
      JsonValue readFrom​(java.nio.ByteBuffer byteBuffer)
      Deserializes a JsonValue by parsing the passed byteBuffer with CBOR.
      byte[] toByteArray​(JsonValue jsonValue)
      Serializes a CBOR byte array from the passed jsonValue.
      java.nio.ByteBuffer toByteBuffer​(JsonValue jsonValue)
      Serializes a CBOR ByteBuffer from the passed jsonValue.
      void writeToByteBuffer​(JsonValue jsonValue, java.nio.ByteBuffer byteBuffer)
      Serializes the passed jsonValue into the passed byteBuffer applying CBOR.
    • Method Detail

      • isCborAvailable

        boolean isCborAvailable()
        Determines whether the libraries providing CBOR serializations are available (classes can be loaded).
        Returns:
        true when CBOR is available and can be used for serialization.
      • readFrom

        JsonValue readFrom​(byte[] bytes)
        Deserializes a JsonValue by parsing the passed bytes with CBOR.
        Parameters:
        bytes - the bytes to parse with CBOR.
        Returns:
        the parsed JsonValue.
        Throws:
        JsonParseException - if the content of bytes cannot be deserialized to a JsonValue.
      • readFrom

        JsonValue readFrom​(byte[] bytes,
                           int offset,
                           int length)
        Deserializes a JsonValue by parsing the passed bytes with CBOR applying a offset and length.
        Parameters:
        bytes - the bytes to parse with CBOR.
        offset - the offset where to start reading from.
        length - the length of how much bytes to read.
        Returns:
        the parsed JsonValue.
        Throws:
        JsonParseException - if the content of bytes cannot be deserialized to a JsonValue.
      • readFrom

        JsonValue readFrom​(java.nio.ByteBuffer byteBuffer)
        Deserializes a JsonValue by parsing the passed byteBuffer with CBOR.
        Parameters:
        byteBuffer - the ByteBuffer to parse with CBOR.
        Returns:
        the parsed JsonValue.
        Throws:
        JsonParseException - if the content of byteBuffer cannot be deserialized to a JsonValue.
      • toByteArray

        byte[] toByteArray​(JsonValue jsonValue)
                    throws java.io.IOException
        Serializes a CBOR byte array from the passed jsonValue.
        Parameters:
        jsonValue - the JsonValue to serialize into CBOR.
        Returns:
        the CBOR bytes.
        Throws:
        java.io.IOException - in case writing the value to the backing OutputStream causes an IOException.
      • toByteBuffer

        java.nio.ByteBuffer toByteBuffer​(JsonValue jsonValue)
                                  throws java.io.IOException
        Serializes a CBOR ByteBuffer from the passed jsonValue.
        Parameters:
        jsonValue - the JsonValue to serialize into CBOR.
        Returns:
        the CBOR ByteBuffer.
        Throws:
        java.io.IOException - in case writing the value to the backing OutputStream causes an IOException.
      • writeToByteBuffer

        void writeToByteBuffer​(JsonValue jsonValue,
                               java.nio.ByteBuffer byteBuffer)
                        throws java.io.IOException
        Serializes the passed jsonValue into the passed byteBuffer applying CBOR.
        Parameters:
        jsonValue - the JsonValue to serialize into CBOR.
        byteBuffer - the ByteBuffer to serialize into.
        Throws:
        java.io.IOException - in case writing the value to the backing OutputStream causes an IOException.
      • createCborRepresentation

        byte[] createCborRepresentation​(java.util.Map<java.lang.String,​JsonField> jsonFieldMap,
                                        int guessedSerializedSize)
                                 throws java.io.IOException
        Creates the CBOR representation of the passed JSON fieldMap and the estimated required serialized size of it.
        Parameters:
        jsonFieldMap - the map of JsonFields to create the CBOR representation for.
        guessedSerializedSize - the estimated serialized size of the CBOR structure.
        Returns:
        the CBOR representation as bytes.
        Throws:
        java.io.IOException - in case writing the value to the backing OutputStream causes an IOException.
      • createCborRepresentation

        byte[] createCborRepresentation​(java.util.List<JsonValue> list,
                                        int guessedSerializedSize)
                                 throws java.io.IOException
        Creates the CBOR representation of the passed JSON value list and the estimated required serialized size of it.
        Parameters:
        list - the list of JsonValues to create the CBOR representation for.
        guessedSerializedSize - the estimated serialized size of the CBOR structure.
        Returns:
        the CBOR representation as bytes.
        Throws:
        java.io.IOException - in case writing the value to the backing OutputStream causes an IOException.