有没有办法在 Kotlinx-Serialization 中序列化地图

jch*_*htd 7 java serialization dictionary kotlin

我正在使用 Kotlin 1.3.10(并绑定到此版本)和 Kotlinx-Serialization 0.13,但在 Kotlinx-Serialization 中序列化地图时遇到问题。

我有以下代码:

@Serializer(forClass = LocalDate::class)
object LocalDateSerializer : KSerializer<LocalDate> {
    private val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
    override val descriptor: SerialDescriptor
        get() = StringDescriptor.withName("LocalDate")
    override fun serialize(encoder: Encoder, obj: LocalDate) {
        encoder.encodeString(obj.format(formatter))
    }
    override fun deserialize(decoder: Decoder): LocalDate {
        return LocalDate.parse(decoder.decodeString(), formatter)
    }
}

@Serializable
data class MyClass (
    val students: Map<String,LocalDate>
)

@UnstableDefault
@Test
fun decodeEncodeSerialization() {
    val jsonParser = Json(
        JsonConfiguration(
            allowStructuredMapKeys = true
        )
    )
    val mc = MyClass(
        mapOf("Alex" to LocalDate.of(1997,2,23))
        )

    val mcJson = jsonParser.stringify(MyClass.serializer(), mc)
    val mcObject = jsonParser.parse(MyClass.serializer(), mcJson)

    assert(true)
}
Run Code Online (Sandbox Code Playgroud)

检查代码时有一条红线,上面写着“未找到 'LocalDate' 的序列化程序。要使用上下文序列化程序作为后备,请使用 @ContextualSerialization 显式注释类型或属性。”

对于其他类型的字段,添加 @Serialization 就足够了。

@Serializable
data class Student (
    val name: String,
    @Serializable(with = LocalDateSerializer::class)
    val dob: LocalDate
)
Run Code Online (Sandbox Code Playgroud)

但是有了地图,我似乎无法弄清楚如何。我把它放在上面,或者放在物体旁边......

@Serializable
data class MyClass (
    val students: Map<String,@Serializable(with = LocalDateSerializer::class) LocalDate> //here
    //or 
    //@Serializable(with = LocalDateSerializer::class)
    //val students2: Map<String, LocalDate> //here 
)
Run Code Online (Sandbox Code Playgroud)

...但测试仍然失败

kotlinx.serialization.SerializationException:无法找到类 java.time.LocalDate 的无参数序列化程序(Kotlin 反射不可用)。对于泛型类,例如列表,请明确提供序列化程序。

我的解决方法是

@Serializable
data class MyClass (
    val students: List<Student>
)
@Serializable
data class Student (
    val name: String,
    @Serializable(with = LocalDateSerializer::class)
    val dob: LocalDate
)
Run Code Online (Sandbox Code Playgroud)

有没有办法我不会求助于解决方法?谢谢!

Len*_*Bru 1

@file:UseSerializers(LocalDateSerializer::class)
Run Code Online (Sandbox Code Playgroud)

将其放在声明对象的文件中,每次看到本地日期时都应该使用 LocalDateSerializer