Class DittoCborSerializable
java.lang.Object
com.ditto.java.serialization.DittoCborSerializable
- Direct Known Subclasses:
DittoCborSerializable.Array,DittoCborSerializable.BooleanValue,DittoCborSerializable.ByteString,DittoCborSerializable.Dictionary,DittoCborSerializable.DoubleValue,DittoCborSerializable.FloatValue,DittoCborSerializable.NegativeInteger,DittoCborSerializable.NullValue,DittoCborSerializable.Tagged,DittoCborSerializable.UnsignedInteger,DittoCborSerializable.Utf8String
public abstract sealed class DittoCborSerializable
extends Object
permits DittoCborSerializable.UnsignedInteger, DittoCborSerializable.NegativeInteger, DittoCborSerializable.ByteString, DittoCborSerializable.Utf8String, DittoCborSerializable.Array, DittoCborSerializable.Dictionary, DittoCborSerializable.Tagged, DittoCborSerializable.BooleanValue, DittoCborSerializable.NullValue, DittoCborSerializable.FloatValue, DittoCborSerializable.DoubleValue
Represents a value that can be serialized to CBOR format.
CBOR (Concise Binary Object Representation) is a data format used by Ditto for efficient serialization of document data. This class provides methods for creating and manipulating CBOR-serializable values and using them to interact with Ditto APIs.
Values can be created using the static factory methods:
- [#of(String)] - for standard Java types
- [#buildArray()] - for creating CBOR arrays
- [#buildDictionary()] - for creating CBOR dictionaries
- [#nullValue()] - for null values
Values can be converted back to Java types using the various as* methods,
such as asString(), asInt(), asDictionary(), etc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a CBOR array value.static final classRepresents a CBOR boolean value.static final classRepresents a CBOR byte string value.static final classRepresents a CBOR dictionary (map) value.static final classRepresents a CBOR double value.static final classRepresents a CBOR float value.static final classRepresents a CBOR negative integer value.static final classRepresents a CBOR null value.static final classRepresents a CBOR tagged value.static final classRepresents a CBOR unsigned integer value.static final classRepresents a CBOR UTF-8 string value. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDittoCborSerializable(@NonNull com.ditto.internal.serialization.InternalDittoCborSerializable implementation) Protected constructor for internal use. -
Method Summary
Modifier and TypeMethodDescription@NonNull DittoAttachmentTokenConverts this value to aDittoAttachmentToken.@Nullable DittoAttachmentTokenConverts this value to aDittoAttachmentToken, or returnsnullif conversion fails.@NonNull BigIntegerConverts this value to aBigInteger.@Nullable BigIntegerConverts this value to aBigInteger, or returnsnullif conversion fails.booleanConverts this value to a boolean.@Nullable BooleanConverts this value to a boolean, or returnsnullif conversion fails.byteasByte()Converts this value to a byte.@Nullable ByteConverts this value to a byte, or returnsnullif conversion fails.@NonNull DittoCborSerializable.DictionaryConverts this value to aDittoCborSerializable.Dictionary.@Nullable DittoCborSerializable.DictionaryConverts this value to aDittoCborSerializable.Dictionary, or returnsnullif conversion fails.doubleasDouble()Converts this value to a double.@Nullable DoubleConverts this value to a double, or returnsnullif conversion fails.floatasFloat()Converts this value to a float.@Nullable FloatConverts this value to a float, or returnsnullif conversion fails.intasInt()Converts this value to an int.@Nullable IntegerConverts this value to an int, or returnsnullif conversion fails.@NonNull DittoCborSerializable.ArrayasList()Converts this value to anDittoCborSerializable.Array.@Nullable DittoCborSerializable.ArrayConverts this value to anDittoCborSerializable.Array, or returnsnullif conversion fails.longasLong()Converts this value to a long.@Nullable LongConverts this value to a long, or returnsnullif conversion fails.@Nullable BigIntegerConverts this value to a nullableBigInteger.@Nullable BooleanConverts this value to a nullable boolean.@Nullable ByteConverts this value to a nullable byte.@Nullable DittoCborSerializable.DictionaryConverts this value to a nullableDittoCborSerializable.Dictionary.@Nullable DoubleConverts this value to a nullable double.@Nullable FloatConverts this value to a nullable float.@Nullable IntegerConverts this value to a nullable int.@Nullable DittoCborSerializable.ArrayConverts this value to a nullableDittoCborSerializable.Array.@Nullable LongConverts this value to a nullable long.@Nullable ShortConverts this value to a nullable short.@Nullable StringConverts this value to a nullableString.@Nullable DittoCborSerializable.TaggedConverts this value to a nullableDittoCborSerializable.TaggedCBOR value.@Nullable IntegerConverts this value to a nullable unsigned byte as an int.@Nullable LongConverts this value to a nullable unsigned int as a long.@Nullable DittoULongConverts this value to a nullable unsigned long.@Nullable IntegerConverts this value to a nullable unsigned short as an int.shortasShort()Converts this value to a short.@Nullable ShortConverts this value to a short, or returnsnullif conversion fails.@NonNull StringasString()Converts this value to aString.@Nullable StringConverts this value to aString, or returnsnullif conversion fails.@NonNull DittoCborSerializable.TaggedasTagged()Converts this value to aDittoCborSerializable.TaggedCBOR value.@Nullable DittoCborSerializable.TaggedConverts this value to aDittoCborSerializable.TaggedCBOR value, or returnsnullif conversion fails.intConverts this value to an unsigned byte as an int (range 0-255).@Nullable IntegerConverts this value to an unsigned byte as an int, or returnsnullif conversion fails.longConverts this value to an unsigned int as a long.@Nullable LongConverts this value to an unsigned int as a long, or returnsnullif conversion fails.@NonNull DittoULongConverts this value to an unsigned long as aDittoULong.@Nullable DittoULongConverts this value to an unsigned long, or returnsnullif conversion fails.intConverts this value to an unsigned short as an int (range 0-65535).@Nullable IntegerConverts this value to an unsigned short as an int, or returnsnullif conversion fails.static @NonNull DittoCborSerializable.Array.BuilderCreates a new builder for constructing a CBOR array.static @NonNull DittoCborSerializable.Dictionary.BuilderCreates a new builder for constructing a CBOR dictionary.final boolean@NonNull DittoCborSerializableget(int index) Gets the value at the specified index if this is an array.@NonNull DittoCborSerializableget(@NonNull DittoCborSerializable key) Gets the value for the specified CBOR key if this is a dictionary.@NonNull DittoCborSerializableGets the value for the specified key if this is a dictionary.protected @NonNull com.ditto.internal.serialization.InternalDittoCborSerializableReturns the internal CBOR serializable implementation.inthashCode()booleanisNull()Checks if this value represents null.static @NonNull DittoCborSerializable.NullValueReturns a CBOR null value.static @NonNull DittoCborSerializable.BooleanValueof(boolean value) Creates a CBOR-serializable value from a boolean.static @NonNull DittoCborSerializableof(byte value) Creates a CBOR-serializable value from a byte.static @NonNull DittoCborSerializable.ByteStringof(byte @NonNull [] value) Creates a CBOR-serializable value from a byte array.static @NonNull DittoCborSerializable.DoubleValueof(double value) Creates a CBOR-serializable value from a double.static @NonNull DittoCborSerializable.FloatValueof(float value) Creates a CBOR-serializable value from a float.static @NonNull DittoCborSerializableof(int value) Creates a CBOR-serializable value from an int.static @NonNull DittoCborSerializableof(long value) Creates a CBOR-serializable value from a long.static @NonNull DittoCborSerializableof(short value) Creates a CBOR-serializable value from a short.static @NonNull DittoCborSerializable.Utf8StringCreates a CBOR-serializable value from a string.static @NonNull DittoCborSerializableof(@NonNull BigInteger value) Creates a CBOR-serializable value from aBigInteger.static @NonNull DittoCborSerializable.Taggedtagged(long tag, DittoCborSerializable value) Creates a tagged CBOR value.toString()
-
Constructor Details
-
DittoCborSerializable
protected DittoCborSerializable(@NonNull com.ditto.internal.serialization.InternalDittoCborSerializable implementation) Protected constructor for internal use.- Parameters:
implementation- the internal CBOR serializable implementation.
-
-
Method Details
-
getImplementation
protected @NonNull com.ditto.internal.serialization.InternalDittoCborSerializable getImplementation()Returns the internal CBOR serializable implementation.- Returns:
- the internal implementation.
-
isNull
public boolean isNull()Checks if this value represents null.- Returns:
trueif this is a null value,falseotherwise.
-
asAttachmentToken
Converts this value to aDittoAttachmentToken.- Returns:
- the attachment token.
- Throws:
DittoException- if this value is not an attachment token.
-
asAttachmentTokenOrNull
Converts this value to aDittoAttachmentToken, or returnsnullif conversion fails.- Returns:
- the attachment token, or
nullif this value cannot be converted.
-
asBoolean
Converts this value to a boolean.- Returns:
- the boolean value.
- Throws:
DittoException- if this value is not a boolean.
-
asBooleanOrNull
Converts this value to a boolean, or returnsnullif conversion fails.- Returns:
- the boolean value, or
nullif this value cannot be converted.
-
asNullableBoolean
Converts this value to a nullable boolean.- Returns:
- the boolean value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a boolean nor a CBOR null.
-
asByte
Converts this value to a byte.- Returns:
- the byte value.
- Throws:
DittoException- if this value is not a byte.
-
asByteOrNull
Converts this value to a byte, or returnsnullif conversion fails.- Returns:
- the byte value, or
nullif this value cannot be converted.
-
asNullableByte
Converts this value to a nullable byte.- Returns:
- the byte value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a byte nor a CBOR null.
-
asUnsignedByte
Converts this value to an unsigned byte as an int (range 0-255).- Returns:
- the unsigned byte value as an int.
- Throws:
DittoException- if this value is not an unsigned byte.
-
asUnsignedByteOrNull
Converts this value to an unsigned byte as an int, or returnsnullif conversion fails.- Returns:
- the unsigned byte value as an int, or
nullif this value cannot be converted.
-
asNullableUnsignedByte
Converts this value to a nullable unsigned byte as an int.- Returns:
- the unsigned byte value as an int, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither an unsigned byte nor a CBOR null.
-
asShort
Converts this value to a short.- Returns:
- the short value.
- Throws:
DittoException- if this value is not a short.
-
asShortOrNull
Converts this value to a short, or returnsnullif conversion fails.- Returns:
- the short value, or
nullif this value cannot be converted.
-
asNullableShort
Converts this value to a nullable short.- Returns:
- the short value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a short nor a CBOR null.
-
asUnsignedShort
Converts this value to an unsigned short as an int (range 0-65535).- Returns:
- the unsigned short value as an int.
- Throws:
DittoException- if this value is not an unsigned short.
-
asUnsignedShortOrNull
Converts this value to an unsigned short as an int, or returnsnullif conversion fails.- Returns:
- the unsigned short value as an int, or
nullif this value cannot be converted.
-
asNullableUnsignedShort
Converts this value to a nullable unsigned short as an int.- Returns:
- the unsigned short value as an int, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither an unsigned short nor a CBOR null.
-
asInt
Converts this value to an int.- Returns:
- the int value.
- Throws:
DittoException- if this value is not an int.
-
asIntOrNull
Converts this value to an int, or returnsnullif conversion fails.- Returns:
- the int value, or
nullif this value cannot be converted.
-
asNullableInt
Converts this value to a nullable int.- Returns:
- the int value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither an int nor a CBOR null.
-
asUnsignedInt
Converts this value to an unsigned int as a long.- Returns:
- the unsigned int value as a long.
- Throws:
DittoException- if this value is not an unsigned int.
-
asUnsignedIntOrNull
Converts this value to an unsigned int as a long, or returnsnullif conversion fails.- Returns:
- the unsigned int value as a long, or
nullif this value cannot be converted.
-
asNullableUnsignedInt
Converts this value to a nullable unsigned int as a long.- Returns:
- the unsigned int value as a long, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither an unsigned int nor a CBOR null.
-
asLong
Converts this value to a long.- Returns:
- the long value.
- Throws:
DittoException- if this value is not a long.
-
asLongOrNull
Converts this value to a long, or returnsnullif conversion fails.- Returns:
- the long value, or
nullif this value cannot be converted.
-
asNullableLong
Converts this value to a nullable long.- Returns:
- the long value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a long nor a CBOR null.
-
asUnsignedLong
Converts this value to an unsigned long as aDittoULong.- Returns:
- the unsigned long value.
- Throws:
DittoException- if this value is not an unsigned long.
-
asUnsignedLongOrNull
Converts this value to an unsigned long, or returnsnullif conversion fails.- Returns:
- the unsigned long value, or
nullif this value cannot be converted.
-
asNullableUnsignedLong
Converts this value to a nullable unsigned long.- Returns:
- the unsigned long value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither an unsigned long nor a CBOR null.
-
asBigInteger
Converts this value to aBigInteger.- Returns:
- the BigInteger value.
- Throws:
DittoException- if this value is not a BigInteger.
-
asBigIntegerOrNull
Converts this value to aBigInteger, or returnsnullif conversion fails.- Returns:
- the BigInteger value, or
nullif this value cannot be converted.
-
asNullableBigInteger
Converts this value to a nullableBigInteger.- Returns:
- the BigInteger value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a BigInteger nor a CBOR null.
-
asFloat
Converts this value to a float.- Returns:
- the float value.
- Throws:
DittoException- if this value is not a float.
-
asFloatOrNull
Converts this value to a float, or returnsnullif conversion fails.- Returns:
- the float value, or
nullif this value cannot be converted.
-
asNullableFloat
Converts this value to a nullable float.- Returns:
- the float value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a float nor a CBOR null.
-
asDouble
Converts this value to a double.- Returns:
- the double value.
- Throws:
DittoException- if this value is not a double.
-
asDoubleOrNull
Converts this value to a double, or returnsnullif conversion fails.- Returns:
- the double value, or
nullif this value cannot be converted.
-
asNullableDouble
Converts this value to a nullable double.- Returns:
- the double value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a double nor a CBOR null.
-
asString
Converts this value to aString.- Returns:
- the string value.
- Throws:
DittoException- if this value is not a string.
-
asStringOrNull
-
asNullableString
Converts this value to a nullableString.- Returns:
- the string value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a string nor a CBOR null.
-
asDictionary
Converts this value to aDittoCborSerializable.Dictionary.- Returns:
- the dictionary value.
- Throws:
DittoException- if this value is not a dictionary.
-
asDictionaryOrNull
Converts this value to aDittoCborSerializable.Dictionary, or returnsnullif conversion fails.- Returns:
- the dictionary value, or
nullif this value cannot be converted.
-
asNullableDictionary
Converts this value to a nullableDittoCborSerializable.Dictionary.- Returns:
- the dictionary value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a dictionary nor a CBOR null.
-
asList
Converts this value to anDittoCborSerializable.Array.- Returns:
- the array value.
- Throws:
DittoException- if this value is not an array.
-
asListOrNull
Converts this value to anDittoCborSerializable.Array, or returnsnullif conversion fails.- Returns:
- the array value, or
nullif this value cannot be converted.
-
asNullableList
Converts this value to a nullableDittoCborSerializable.Array.- Returns:
- the array value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither an array nor a CBOR null.
-
asTagged
Converts this value to aDittoCborSerializable.TaggedCBOR value.- Returns:
- the tagged value.
- Throws:
DittoException- if this value is not a tagged value.
-
asTaggedOrNull
Converts this value to aDittoCborSerializable.TaggedCBOR value, or returnsnullif conversion fails.- Returns:
- the tagged value, or
nullif this value cannot be converted.
-
asNullableTagged
Converts this value to a nullableDittoCborSerializable.TaggedCBOR value.- Returns:
- the tagged value, or
nullif this value is a CBOR null. - Throws:
DittoException- if this value is neither a tagged value nor a CBOR null.
-
get
Gets the value at the specified index if this is an array.- Parameters:
index- the array index.- Returns:
- the value at the specified index.
-
get
Gets the value for the specified key if this is a dictionary.- Parameters:
key- the dictionary key.- Returns:
- the value for the specified key.
-
get
Gets the value for the specified CBOR key if this is a dictionary.- Parameters:
key- the dictionary key as a CBOR-serializable value.- Returns:
- the value for the specified key.
-
toString
-
equals
-
hashCode
-
of
Creates a CBOR-serializable value from a string.- Parameters:
value- the string value.- Returns:
- a
DittoCborSerializable.Utf8Stringcontaining the value.
-
of
Creates a CBOR-serializable value from a byte array.- Parameters:
value- the byte array value.- Returns:
- a
DittoCborSerializable.ByteStringcontaining the value.
-
of
Creates a CBOR-serializable value from a boolean.- Parameters:
value- the boolean value.- Returns:
- a
DittoCborSerializable.BooleanValuecontaining the value.
-
of
Creates a CBOR-serializable value from a double.- Parameters:
value- the double value.- Returns:
- a
DittoCborSerializable.DoubleValuecontaining the value.
-
of
Creates a CBOR-serializable value from a float.- Parameters:
value- the float value.- Returns:
- a
DittoCborSerializable.FloatValuecontaining the value.
-
of
Creates a CBOR-serializable value from a byte.- Parameters:
value- the byte value.- Returns:
- a CBOR-serializable value containing the byte.
-
of
Creates a CBOR-serializable value from a short.- Parameters:
value- the short value.- Returns:
- a CBOR-serializable value containing the short.
-
of
Creates a CBOR-serializable value from an int.- Parameters:
value- the int value.- Returns:
- a CBOR-serializable value containing the int.
-
of
Creates a CBOR-serializable value from a long.- Parameters:
value- the long value.- Returns:
- a CBOR-serializable value containing the long.
-
of
Creates a CBOR-serializable value from aBigInteger.- Parameters:
value- the BigInteger value.- Returns:
- a CBOR-serializable value containing the BigInteger.
-
tagged
Creates a tagged CBOR value.
CBOR tags are used to associate semantic information with CBOR data items.
- Parameters:
tag- the numeric tag identifier.value- the CBOR value to tag.- Returns:
- a
DittoCborSerializable.TaggedCBOR value.
-
nullValue
Returns a CBOR null value.- Returns:
- the singleton
DittoCborSerializable.NullValueinstance.
-
buildArray
Creates a new builder for constructing a CBOR array.- Returns:
- a new
DittoCborSerializable.Array.Builderinstance.
-
buildDictionary
Creates a new builder for constructing a CBOR dictionary.- Returns:
- a new
DittoCborSerializable.Dictionary.Builderinstance.
-