使用 ObjectMapper 进行 JSON 序列化和反序列化的工作方式不同

发布于 2025-01-16 19:21:04 字数 1580 浏览 1 评论 0原文

我有一个像这样的 JSON:

{
  "commonObjects": [
    {
      "type": "C",
      "C": "1234567890"
    },
    {
      "type": "C",
      "c": "0987654321"
    },
    {
      "type": "B",
      "b": "ABCDEFGH"
    },
    {
      "type": "A",
      "A": "11111111",
      "AA": "22222222"
    }
  ]
}

所有类型都可以包含可变次数,或者只能有一种类型一次。

在 SpringBoot 应用程序中,我有三个类,扩展公共类。

public class AObject extends CommonObject {
    @JsonProperty("A")
    private String a;
    @JsonProperty("AA")
    private String aa;
}
public class BObject extends CommonObject {
    @JsonProperty("B")
    private String b;
}
public class CObject extends CommonObject {
    @JsonProperty("C")
    private String c;
}
public class CommonObject {
    private String type;
}
public class CommonObjects {
    List<CommonObject> commonObjects;
}

我有两种方法来序列化和反序列化它。

return objectMapper.writeValueAsString(commonObjects);
...
return objectMapper.readValue(jsonValue, CommonObjects.class);

当测试序列化是可以的。但反序列化默认情况下不能以这种方式工作。所以我修改了 CommonObject 如下:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    property = "type",
    visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = AObject.class, name = "A"),
    @JsonSubTypes.Type(value = BObject.class, name = "B"),
    @JsonSubTypes.Type(value = CObject.class, name = "C")
})
public class CommonObject {
  private String type;
}

现在,反序列化工作正常,但序列化不行。属性 type 在 json 值中包含两次。

有什么方法可以处理这个特定问题,还是我必须自己编写自定义反序列化器?

I have a JSON like this:

{
  "commonObjects": [
    {
      "type": "C",
      "C": "1234567890"
    },
    {
      "type": "C",
      "c": "0987654321"
    },
    {
      "type": "B",
      "b": "ABCDEFGH"
    },
    {
      "type": "A",
      "A": "11111111",
      "AA": "22222222"
    }
  ]
}

All types can be included variable times or there can be only one type only once.

In SpringBoot application I have three classes, extending common class.

public class AObject extends CommonObject {
    @JsonProperty("A")
    private String a;
    @JsonProperty("AA")
    private String aa;
}
public class BObject extends CommonObject {
    @JsonProperty("B")
    private String b;
}
public class CObject extends CommonObject {
    @JsonProperty("C")
    private String c;
}
public class CommonObject {
    private String type;
}
public class CommonObjects {
    List<CommonObject> commonObjects;
}

Than I have two methods to serialize and deserialize it.

return objectMapper.writeValueAsString(commonObjects);
...
return objectMapper.readValue(jsonValue, CommonObjects.class);

While testing serialization is OK. But deserialization doesn't work in default this way. So I modified CommonObject like this:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    property = "type",
    visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = AObject.class, name = "A"),
    @JsonSubTypes.Type(value = BObject.class, name = "B"),
    @JsonSubTypes.Type(value = CObject.class, name = "C")
})
public class CommonObject {
  private String type;
}

Now, deserialization works OK, but serialization doesn't. Property type is included twice in json value.

Is there any way to handle this particular problem or do I have to write myself custom deserializer?

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

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

发布评论

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

评论(1

乜一 2025-01-23 19:21:04

您的 CommonObject 类中有两个问题
哪些需要修复。

  • 删除 visible = true
    (因此默认为 visible = false)。
    这意味着 JSON 输入中的 type 未反序列化
    到 Java type 属性。
  • 删除属性私有字符串类型;
    这是因为在 JSON 输出中您需要 type
    仅根据您的 @JsonTypeInfo@JsonSubTypes
    但也不是从 Java type 属性序列化的。

There are two issues in your CommonObjectclass
which need to be fixed.

  • Remove the visible = true
    (thus defaulting to visible = false).
    This means the type from JSON input is not deserialized
    to a Java type property.
  • Remove the property private String type;
    This is because in JSON output you want the type
    only according to your @JsonTypeInfo and @JsonSubTypes,
    but not also serialized from a Java type property.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文