:white_check_mark: FIXED - used the wrong Body.aut...
# http4k
p
FIXED - used the wrong Body.auto import.. quite a pain point tbh :/ +++++ Hey! I’m writing a lambda function with Kotlin, using Kotshi for the json handling. It feels like a super blatant question, but how do I register a JsonAdapter for a platform type?
Copy code
@JsonSerializable
data class Reminder(
  val createdAt: LocalDateTime, // < this is kotlinx.datetime.LocalDateTime
  val task: String,
  val dueAt: LocalDateTime, // < this is kotlinx.datetime.LocalDateTime
)
I’m not sure how to indicate to Kotshi that it should also generate a Factory for this type. I also tried creating my own adapter, but without luck:
Copy code
object LocalDateTimeAdapter : JsonAdapter<LocalDateTime>() {
  override fun fromJson(reader: JsonReader): LocalDateTime =
    LocalDateTime.parse(reader.readJsonValue().toString())

  override fun toJson(writer: JsonWriter, value: LocalDateTime?) {
    writer.jsonValue(value.toString())
  }

}

@OptIn(ExperimentalStdlibApi::class)
@Suppress("unused")
object MoshiWithKotshi : ConfigurableMoshi(
  Moshi.Builder()
    .add(LocalDateTimeAdapter) //<<<
    .add(ApplicationJsonAdapterFactory)
    .asConfigurable()
    .done()
)
Error:
Copy code
Platform class kotlinx.datetime.LocalDateTime requires explicit JsonAdapter to be registered
for class kotlinx.datetime.LocalDateTime createdAt
for class com.p10r.Reminder
Thanks in advance!
I could partially resolve it. Another error was that I was declaring a Lens on a list:
Copy code
val reminderLens = Body.auto<List<Reminder>>().toLens()
Moving this to a wrapper object
Copy code
val reminderLens = Body.auto<Reminders>().toLens()
helped here.
a
With Moshi, you need to use
Array
rather than
List
. But your wrapper should work too. The issue is documented, but not always obvious.
If selecting the right
Body.auto
import is annoying, you can do
Moshi.autoBody
or
MyMoshi.autoBody
instead
d
Qq - what was the reason behind using the kotlinx date times? The library looks pretty bereft of contributions for the last 8 months or so. I'm wondering if it's going to go the same way as kotlinx.io (ie abandoned - which is interesting in itself considering the only alternative is okio afaik - and it would seem weird to fall back on another vendor lib for MP)
p
With Moshi, you need to use Array rather than List. But your wrapper should work too. The issue is documented, but not always obvious.
Thanks, I also found a Github issue that mentioned this. I’m fine with having that in mind, but it might be hard to grasp when onboarding other coworkers. 🙂
Qq - what was the reason behind using the kotlinx date times?
Fair question! We’re running a hackathon at work and built a Kotlin Multiplatform project which shares the Request model. We just reached for kotlinx.datetime for its multiplatform support. Besides that I agree though - it feels like one can’t really count on Jetbrains when it comes to libraries. Ktor and kotlinx.serialization feel really active, but datetime, io and the others feel kind of abandoned.