Optional
Represents a value that encapsulates all three possible states of a value in the Discord API. Specifically:
Missing - a field that was not present in the serialized entity
Null - a field that was assigned null in the serialized entity
Value - a field that was assigned a non-null value in the serialized entity.
The base class is (de)serializable with kotlinx.serialization and should be used as follows:
Optional<T>
- a field that is only optional but not nullable.Optional<T?>
- A field that is both optional and nullable.A field that is only nullable should be represented as
T?
instead.
Trying to deserialize null
as Optional<T>
will result in a SerializationException being thrown.
Note that kotlinx.serialization does not call serializers for values that are not present in the serialized format. Optional
fields should have a default value of Optional.Missing
:
@Serializable
class DiscordUser(
val id: Long,
val username: String,
val bot: Optional<Boolean?> = Optional.Missing()
)