Class DittoJsonSerializable
java.lang.Object
com.ditto.java.serialization.DittoJsonSerializable
- Direct Known Subclasses:
DittoJsonSerializable.ArrayValue,DittoJsonSerializable.NullValue,DittoJsonSerializable.ObjectValue,DittoJsonSerializable.PrimitiveValue,DittoJsonSerializable.StringValue
public abstract sealed class DittoJsonSerializable
extends Object
permits DittoJsonSerializable.StringValue, DittoJsonSerializable.ObjectValue, DittoJsonSerializable.ArrayValue, DittoJsonSerializable.PrimitiveValue, DittoJsonSerializable.NullValue
Represents a value that can be serialized to JSON format.
This class provides methods for creating and manipulating JSON-serializable values used by Ditto for document data. JSON serialization is simpler than CBOR and is commonly used for interoperability with web services and REST APIs.
Values can be created using the static factory methods:
- [#of(String)] - for string values
- [#of(Map)] - for JSON objects
- [#of(List)] - for JSON arrays
- [#of(boolean)], [#of(int)], [#of(long)], [#of(float)], [#of(double)] - for primitives
- [#nullValue()] - for null values
Values can be converted to Java types using the various get* methods,
such as getBoolean(), getInt(), getDouble(), etc.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a JSON array value.static final classRepresents a JSON null value.static final classRepresents a JSON object value.static final classRepresents a JSON primitive value (boolean, number).static final classRepresents a JSON string value. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDittoJsonSerializable(@NonNull com.ditto.internal.serialization.InternalDittoJsonSerializable implementation) Protected constructor for internal use. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanbooleanConverts this value to a boolean.@Nullable BooleanConverts this value to a boolean, or returnsnullif conversion fails.bytegetByte()Converts this value to a byte.doubleConverts this value to a double.floatgetFloat()Converts this value to a float.protected @NonNull com.ditto.internal.serialization.InternalDittoJsonSerializableReturns the internal JSON serializable implementation.intgetInt()Converts this value to an int.longgetLong()Converts this value to a long.shortgetShort()Converts this value to a short.inthashCode()booleanisNull()Checks if this value represents null.static @NonNull DittoJsonSerializable.NullValueReturns a JSON null value.static @NonNull DittoJsonSerializable.PrimitiveValueof(boolean value) Creates a JSON-serializable value from a boolean.static @NonNull DittoJsonSerializable.PrimitiveValueof(double value) Creates a JSON-serializable value from a double.static @NonNull DittoJsonSerializable.PrimitiveValueof(float value) Creates a JSON-serializable value from a float.static @NonNull DittoJsonSerializable.PrimitiveValueof(int value) Creates a JSON-serializable value from an int.static @NonNull DittoJsonSerializable.PrimitiveValueof(long value) Creates a JSON-serializable value from a long.static @NonNull DittoJsonSerializable.StringValueCreates a JSON-serializable value from a string.static @NonNull DittoJsonSerializable.ArrayValueof(@NonNull List<@NonNull DittoJsonSerializable> value) Creates a JSON-serializable value from a list (JSON array).static @NonNull DittoJsonSerializable.ObjectValueof(@NonNull Map<@NonNull String, @NonNull DittoJsonSerializable> value) Creates a JSON-serializable value from a map (JSON object).toString()
-
Constructor Details
-
DittoJsonSerializable
protected DittoJsonSerializable(@NonNull com.ditto.internal.serialization.InternalDittoJsonSerializable implementation) Protected constructor for internal use.- Parameters:
implementation- the internal JSON serializable implementation.
-
-
Method Details
-
getImplementation
protected @NonNull com.ditto.internal.serialization.InternalDittoJsonSerializable getImplementation()Returns the internal JSON serializable implementation.- Returns:
- the internal implementation.
-
isNull
public boolean isNull()Checks if this value represents null.- Returns:
trueif this is a null value,falseotherwise.
-
getBoolean
Converts this value to a boolean.- Returns:
- the boolean value.
- Throws:
DittoException- if this value is not a boolean.
-
getBooleanOrNull
Converts this value to a boolean, or returnsnullif conversion fails.- Returns:
- the boolean value, or
nullif this value cannot be converted.
-
getByte
Converts this value to a byte.- Returns:
- the byte value.
- Throws:
DittoException- if this value is not a byte.
-
getShort
Converts this value to a short.- Returns:
- the short value.
- Throws:
DittoException- if this value is not a short.
-
getInt
Converts this value to an int.- Returns:
- the int value.
- Throws:
DittoException- if this value is not an int.
-
getLong
Converts this value to a long.- Returns:
- the long value.
- Throws:
DittoException- if this value is not a long.
-
getFloat
Converts this value to a float.- Returns:
- the float value.
- Throws:
DittoException- if this value is not a float.
-
getDouble
Converts this value to a double.- Returns:
- the double value.
- Throws:
DittoException- if this value is not a double.
-
equals
-
hashCode
-
toString
-
of
Creates a JSON-serializable value from a string.- Parameters:
value- the string value.- Returns:
- a
DittoJsonSerializable.StringValuecontaining the value.
-
of
public static @NonNull DittoJsonSerializable.ObjectValue of(@NonNull Map<@NonNull String, @NonNull DittoJsonSerializable> value) Creates a JSON-serializable value from a map (JSON object).- Parameters:
value- the map with string keys and JSON-serializable values.- Returns:
- an
DittoJsonSerializable.ObjectValuecontaining the map.
-
of
public static @NonNull DittoJsonSerializable.ArrayValue of(@NonNull List<@NonNull DittoJsonSerializable> value) Creates a JSON-serializable value from a list (JSON array).- Parameters:
value- the list of JSON-serializable values.- Returns:
- an
DittoJsonSerializable.ArrayValuecontaining the list.
-
of
Creates a JSON-serializable value from a boolean.- Parameters:
value- the boolean value.- Returns:
- a
DittoJsonSerializable.PrimitiveValuecontaining the value.
-
of
Creates a JSON-serializable value from an int.- Parameters:
value- the int value.- Returns:
- a
DittoJsonSerializable.PrimitiveValuecontaining the value.
-
of
Creates a JSON-serializable value from a long.- Parameters:
value- the long value.- Returns:
- a
DittoJsonSerializable.PrimitiveValuecontaining the value.
-
of
Creates a JSON-serializable value from a float.- Parameters:
value- the float value.- Returns:
- a
DittoJsonSerializable.PrimitiveValuecontaining the value.
-
of
Creates a JSON-serializable value from a double.- Parameters:
value- the double value.- Returns:
- a
DittoJsonSerializable.PrimitiveValuecontaining the value.
-
nullValue
Returns a JSON null value.- Returns:
- the singleton
DittoJsonSerializable.NullValueinstance.
-