Kotlin,  Ktor

Kotlin FeatureRequest: Serialize null string to empty string

Table of Content

Kotlin FeatureRequest: Serialize null string to empty string

fun customize(mapper: ObjectMapper) {
    val serializerProvider = DefaultSerializerProvider.Impl()
    serializerProvider.setNullValueSerializer(NullValueSerializer()) // NullをStringEmptyに変換する
    mapper.setSerializerProvider(serializerProvider)
}

class NullValueSerializer : JsonSerializer<Any?>() {
    override fun serialize(t: Any?, generator: JsonGenerator, provider: SerializerProvider?) = generator.writeString("")
}