gpt4 book ai didi

java - Jackson Yaml 类型信息在序列化时是错误的

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:16:21 24 4
gpt4 key购买 nike

通过 Jackson 将对象序列化为 yml 时,我得到以下输出:

---
commands:
dev: !<foo.bar.baz.DevCommand>

然而,我想要的是:

---
commands:
dev:
type: foo.bar.baz.DevCommand

我能够很好地反序列化。也就是说,反序列化部分按预期工作。我在我能想到的任何地方都添加了以下注释:

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="type")

包括 DevCommand 接口(interface)、DevCommand 具体类、具有 commands 映射(字段和 getter/setter)的类型。

我需要做什么来强制 Jackson 使用我想要的类型格式?

最佳答案

Yaml 已经内置了类型信息,所以 Jackson 默认使用它。来自 this issue ,解决方法是禁用使用 native 类型 ID。

YAML has native Type Ids and Object Ids, so by default those are used (assuming this is what users prefer). But you can disable this with:

YAMLGenerator.Feature.USE_NATIVE_TYPE_ID

and specifically disabling that; something like:

YAMLFactory f = new YAMLFactory();
f.disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID);
ObjectMapper m = new ObjectMapper(f);

or, for convenience

YAMLMapper m = new YAMLMapper()
disable(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID);

关于java - Jackson Yaml 类型信息在序列化时是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40272165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com