Skip to content

Serialization

Island Time provides an artifact containing custom serializers for use with Kotlin Serialization. These can be used to convert dates, times, durations, and intervals into ISO-compatible strings, which work well with JSON.

Gradle Setup

Add the following dependency to your project's Gradle script:

dependencies {
    implementation "io.islandtime:serialization-extensions:0.6.3"
}
dependencies {
    implementation("io.islandtime:serialization-extensions:0.6.3")
}

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,
    @Serializable(with = DateRangeSerializer::class) val dateRange: DateRange,
    @Serializable(with = InstantSerializer::class) 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 are not serializable out of the box, so we explicitly specify the serializers provided by Island Time.

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.