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("")
}
Last modified: 2021年8月19日

Author