I have an Android app, and I'm trying to transition from ThreeTenABP to the Java 8 "time" APIs that are supported by Android Gradle Plugin 4.0 through desugaring
Here is my custom Moshi adapter
@Keep
internal class DateTimeAdapter {
@ToJson fun toJson(@DateTimeTimestamp dateTime: LocalDateTime): Long {
return dateTime.toEpochSecond(ZoneOffset.UTC)
}
@FromJson @DateTimeTimestamp fun fromJson(dateTimeTimestamp: Long): LocalDateTime {
return localDateTimeOf(dateTimeTimestamp)
}
}
@JsonQualifier
@Retention(AnnotationRetention.RUNTIME)
internal annotation class DateTimeTimestamp
Here's how I supply it while building the Moshi instance
fun provideMoshi(): Moshi {
return Moshi.Builder()
.add(DateTimeAdapter())
.add(KotlinJsonAdapterFactory()).build()
}
And here's how I use it in one of my DTOs
@Keep
data class ErrorDto(
@Json(name = "timestamp") @DateTimeTimestamp val date: LocalDateTime?,
@Json(name = "status") val statusCode: Int,
)
However, at runtime, I get this exception
java.lang.IllegalArgumentException: Platform class java.time.LocalDateTime requires explicit JsonAdapter to be registered
The same exact adapter works fine with the org.threeten.bp.LocalDateTime. That is, the application doesn't crash and the Long timestamp is successfully turned into LocalDateTime