1

我正在尝试将kotlinx serialization与 Algolia 一起用于类型ArrayList<String>Date但我无法弄清楚该serialization技术。任何指针,因为我采用了Algolia.

4

1 回答 1

1

检查这个

kotlinx.序列化

import kotlinx.serialization.*
import kotlinx.serialization.internal.*
import java.util.*

@Serializable
class DateWrapper(val date: Date)

@Serializer(forClass = Date::class)
object DateSerializer: KSerializer<Date> {
    private val df: DateFormat = SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS")

    override fun save(output: KOutput, obj: Date) {
        output.writeStringValue(df.format(obj))
    }

    override fun load(input: KInput): Date {
        return df.parse(input.readStringValue())
    }

    override val serialClassDesc: KSerialClassDesc = SerialClassDescImpl("Date")
}

在您的应用程序启动时注册它:

kotlinx.serialization.registerSerializer("java.util.Date", DateSerializer)

JSON.stringify(DateWrapper(Date()))会给你 {"date":["Date","10/11/2019 12:50:10.665"]}

于 2020-01-17T05:06:51.807 回答