使用 ObjectMapper 进行 JSON 序列化和反序列化的工作方式不同
我有一个像这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
CommonObject
类中有两个问题哪些需要修复。
visible = true
(因此默认为
visible = false
)。这意味着 JSON 输入中的
type
未反序列化到 Java
type
属性。私有字符串类型;
这是因为在 JSON 输出中您需要
type
仅根据您的
@JsonTypeInfo
和@JsonSubTypes
,但也不是从 Java
type
属性序列化的。There are two issues in your
CommonObject
classwhich need to be fixed.
visible = true
(thus defaulting to
visible = false
).This means the
type
from JSON input is not deserializedto a Java
type
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.