科特林与Jackson:为数据类字段指定自定义序列化时出现类型错误

发布于 2025-01-15 01:27:27 字数 1018 浏览 4 评论 0原文

我有一个在 Spring Boot 项目中序列化为 JSON 的 Kotlin 数据类。我想自定义序列化为 JSON 时日期的格式。字段的名称应使用默认规则进行序列化。这表达了我想做的事情:

class ZonedDateTimeSerialiser : JsonSerializer<ZonedDateTime>() {
    @Throws(IOException::class)
    fun serialize(value: ZonedDateTime, gen: JsonGenerator, serializers: SerializerProvider?) {
        val parseDate: String? = value.withZoneSameInstant(ZoneId.of("Europe/Warsaw"))
        .withZoneSameLocal(ZoneOffset.UTC)
            .format(DateTimeFormatter.ISO_DATE_TIME)
        gen.writeString(parseDate)
    }
}

data class OrderNotesRequest(
    @JsonSerialize(using = ZonedDateTimeSerialiser::class)
    val date: ZonedDateTime = ZonedDateTime.now()
)

但我收到类型错误:

Type mismatch.
Required:
KClass<out (JsonSerializer<Any!>..JsonSerializer<*>?)>
Found:
KClass<ZonedDateTimeSerialiser>

我确实尝试将参数注释切换为 contentUsing 但类型错误保持不变。

I have a Kotlin data class that is serialised to JSON in a Spring Boot project. I'd like to customise how date is formatted when serialising to JSON. The name of the field should be serialised using default rules. That expresses what I'd like to do:

class ZonedDateTimeSerialiser : JsonSerializer<ZonedDateTime>() {
    @Throws(IOException::class)
    fun serialize(value: ZonedDateTime, gen: JsonGenerator, serializers: SerializerProvider?) {
        val parseDate: String? = value.withZoneSameInstant(ZoneId.of("Europe/Warsaw"))
        .withZoneSameLocal(ZoneOffset.UTC)
            .format(DateTimeFormatter.ISO_DATE_TIME)
        gen.writeString(parseDate)
    }
}

data class OrderNotesRequest(
    @JsonSerialize(using = ZonedDateTimeSerialiser::class)
    val date: ZonedDateTime = ZonedDateTime.now()
)

But I get a type error:

Type mismatch.
Required:
KClass<out (JsonSerializer<Any!>..JsonSerializer<*>?)>
Found:
KClass<ZonedDateTimeSerialiser>

I did try switching the parameter to annotation to contentUsing but the type error remained the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2025-01-22 01:27:27

以下对我有用

object JacksonRun {
    @JvmStatic
    fun main(args: Array<String>) {
        val objMapper = ObjectMapper().apply {
            registerModule(KotlinModule())
        }
        val order = OrderNotesRequest()
        println(objMapper.writeValueAsString(order))
    }

}

data class OrderNotesRequest(
    @JsonSerialize(using = ZonedDateTimeSerialiser::class)
    val date: ZonedDateTime = ZonedDateTime.now()
)

class ZonedDateTimeSerialiser : JsonSerializer<ZonedDateTime>() {
    @Throws(IOException::class)
    override fun serialize(value: ZonedDateTime, gen: JsonGenerator, serializers: SerializerProvider?) {
        val parseDate: String = value.withZoneSameInstant(ZoneId.of("Europe/Warsaw"))
            .withZoneSameLocal(ZoneOffset.UTC)
            .format(DateTimeFormatter.ISO_DATE_TIME)
        gen.writeString(parseDate)
    }
}

build.gradle.kts:

dependencies {
    implementation("com.fasterxml.jackson.core:jackson-core:2.13.2")
    implementation("com.fasterxml.jackson.core:jackson-annotations:2.13.2")
    implementation("com.fasterxml.jackson.core:jackson-databind:2.13.2")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.0")
}

给我输出:

{"date":"2022-03-21T10:29:19.381498Z"}

请确保您正确导入 JsonSerializer

import com.fasterxml.jackson.databind.JsonSerializer

并将 override 标记添加到 serialize方法

Following is working for me

object JacksonRun {
    @JvmStatic
    fun main(args: Array<String>) {
        val objMapper = ObjectMapper().apply {
            registerModule(KotlinModule())
        }
        val order = OrderNotesRequest()
        println(objMapper.writeValueAsString(order))
    }

}

data class OrderNotesRequest(
    @JsonSerialize(using = ZonedDateTimeSerialiser::class)
    val date: ZonedDateTime = ZonedDateTime.now()
)

class ZonedDateTimeSerialiser : JsonSerializer<ZonedDateTime>() {
    @Throws(IOException::class)
    override fun serialize(value: ZonedDateTime, gen: JsonGenerator, serializers: SerializerProvider?) {
        val parseDate: String = value.withZoneSameInstant(ZoneId.of("Europe/Warsaw"))
            .withZoneSameLocal(ZoneOffset.UTC)
            .format(DateTimeFormatter.ISO_DATE_TIME)
        gen.writeString(parseDate)
    }
}

build.gradle.kts:

dependencies {
    implementation("com.fasterxml.jackson.core:jackson-core:2.13.2")
    implementation("com.fasterxml.jackson.core:jackson-annotations:2.13.2")
    implementation("com.fasterxml.jackson.core:jackson-databind:2.13.2")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.0")
}

Gives me output:

{"date":"2022-03-21T10:29:19.381498Z"}

Do make sure you have the correct import for JsonSerializer

import com.fasterxml.jackson.databind.JsonSerializer

and add override marker to serialize method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文