Skip to content

Serialization

Island Time includes built-in support for Kotlin Serialization. By default, dates, times, durations, and intervals are serialized as ISO-compatible strings.

Serializing to JSON

For example purposes, let's assume we have a data structure describing an event that we'd like to serialize.

@Serializable
data class EventDto(
    val name: String,
    val dateRange: DateRange,
    val createdAt: Instant
)

By using the @Serializable annotation, we instruct the Kotlin Serialization plugin to generate a serializer for the EventDto class. Island Time's DateRange and Instant classes will be automatically serialized as ISO-8601 strings.

Now, we can serialize the EventDto class to JSON with the following code:

fun writeToJson(val event: EventDto): String {
    val json = Json { prettyPrint = true }
    return json.encodeToString(EventDto.serializer(), event)
}

Example output might look something like this:

{
    "name": "KotlinConf 2019",
    "dateRange": "2019-12-04/2012-12-06",
    "createdAt": "2020-03-14T14:19:03.478Z"
}

For more information on how to use Kotlin Serialization, consult the GitHub page.

Binary Formats

At the present time, there are no serializers tuned specifically for binary formats. If you have a use case that requires that, feel free to raise an issue.