Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlinx.Serialization deserializing dates

I'm having a hard time finding documentation on how to deserialize date fields. How do i achieve this? Most solutions i found on SO don't work or they use classes that are no longer available

@Serializable
data class Dashboard(
    val someNumber: Int,
    val someDate: Date? // Custom Deserialization from unix timestamp
)

EDIT: Most solution i found use PrimitiveSerialDescriptor which seems to be not available

like image 769
aryaxt Avatar asked Mar 03 '26 12:03

aryaxt


1 Answers

Do not use Date, but use Instant or LocalDateTime instead, you need to add this in build.gradle file

implementation "org.jetbrains.kotlinx:kotlinx-datetime:0.3.2"

read more about it in this link: https://github.com/Kotlin/kotlinx-datetime

This will automatically Serializable and also work in KMM šŸ˜€

like image 65
Tom William Avatar answered Mar 06 '26 02:03

Tom William



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!