Box

Box is generic container for type T, which can be marked as absent (empty). Behaves similarly to Java's Optional, but allows nullable types as well.

Primary usage is to mark properties in kotlinx.serialization as nullable and optional at the same time, to be able to serialize nulls when schema requires it (for example in many request bodies for patch/edit requests) and still have some "default" value (in this case Box.absent) to omit property in serialization process.

@Serializable
data class SomePojo(
// Now we can omit property as it is optional,
// but serialize nulls as-well.
val property: Box<String?> = Box.absent
)

Constructors

Link copied to clipboard
fun Box()

Creates absent (empty) Box

Types

Link copied to clipboard
object Companion
Link copied to clipboard
open class Serializer<T>(valueSerializer: KSerializer<T>) : KSerializer<Box<T>>

Functions

Link copied to clipboard
open override fun toString(): String

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val value: T

Returns underlying value of type T or throws NoSuchElementException if this box is absent.

Extensions

Link copied to clipboard
infix inline fun <T : Any> Box<T?>.computeIfNull(another: () -> T): T
Link copied to clipboard
infix inline fun <T> Box<T>.getOr(another: T): T
infix inline fun <T> Box<T>.getOr(another: () -> T): T
Link copied to clipboard
infix inline fun <T, R> Box<T>.map(block: (T) -> R): Box<R>
Link copied to clipboard
infix inline fun <T> Box<T>.or(another: T): Box<T>
infix inline fun <T> Box<T>.or(another: Box<T>): Box<T>
infix inline fun <T> Box<T>.or(another: () -> T): Box<T>
Link copied to clipboard
val <T> Box<T>.orNull: T?