Add support for serializing kotlin.time.Duration in Parcelize plugin.

This commit adds support for the Parcelize plugin to generate default
serialization implementation for the kotlin.time.Duration. As Parcelize
already supports serializing some of the kotlin library types it makes
sense to support more common ones for the user convenience.

https://issuetracker.google.com/issues/264614661
This commit is contained in:
Rafał Galczak
2023-12-22 13:41:54 +01:00
committed by Space Team
parent b828365bb5
commit c7d4a7e1f1
6 changed files with 73 additions and 1 deletions
@@ -0,0 +1,42 @@
// WITH_STDLIB
@file:JvmName("TestKt")
package test
import kotlinx.parcelize.*
import android.os.Parcelable
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.Duration.Companion.nanoseconds
@Parcelize
data class Test(
val basic: Duration,
val negative: Duration,
val nullable: Duration?,
val nullableWithValue: Duration?,
val noNanoseconds: Duration,
val noSeconds: Duration,
val infinite: Duration,
val negativeInfinite: Duration,
) : Parcelable
fun box() = parcelTest { parcel ->
val test = Test(
basic = 10.seconds + 2.nanoseconds,
negative = -10.seconds - 2.nanoseconds,
nullable = null,
nullableWithValue = 10.seconds + 2.nanoseconds,
noNanoseconds = 10.seconds,
noSeconds = 2.nanoseconds,
infinite = Duration.INFINITE,
negativeInfinite = -Duration.INFINITE,
)
test.writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.unmarshall(bytes, 0, bytes.size)
parcel.setDataPosition(0)
val got = parcelableCreator<Test>().createFromParcel(parcel)
assert(test == got)
}