gpt4 book ai didi

json - 棘手的(?)JSON,多态反序列化

转载 作者:行者123 更新时间:2023-12-04 21:42:59 34 4
gpt4 key购买 nike

我想使用 Jackson 2.x 反序列化一段 JSON。

有一个看起来像这样的json:

{
"response":[
230,
{
"id":1627,
"from_id":4534,
"attachments":[
{
"type":"audio",
"audio":{
"aid":25918,
"owner_id":20000,
}
},
{
"type":"link",
"link":{
"url":"http://lenta.ru",
"title":"bla"
}
}
]
}
...
]
}

所以我想使用多态类型处理来反序列化附件
进入附件列表。
我有以下带有 Mixins 的 POJO:
public class Post {
private final String id;
private List<? extends Attachment> attachments;
// ...
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class PostMixin {
@JsonProperty("id")
String id;

@JsonProperty("attachments")
List<? extends Attachment> attachments;
// ...
}

public abstract class Attachment {
public static enum AttachmentType {
AUDIO, LINK
}

private AttachmentType type;
public AttachmentType getType() {
return type;
}
}

// Not sure here if using these annotations propper way
@JsonTypeInfo(use=JsonTypeInfo.Id.NAME,
include=JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(name="link", value=LinkAttachment.class),
@JsonSubTypes.Type(name="audio", value=AudioAttachment.class)
})
@JsonIgnoreProperties(ignoreUnknown = true)
public class AttachmentMixin {
@JsonProperty("type")
@JsonDeserialize(using = AttachmentTypeDeserializer.class)
Attachment.AttachmentType type;

// parse type into enum
private static class AttachmentTypeDeserializer extends
JsonDeserializer<Attachment.AttachmentType> {
@Override
public Attachment.AttachmentType deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
return Attachment.AttachmentType.valueOf(jp.getText().toUpperCase());
}
}
}

public class LinkAttachment extends Attachment {
private String url;
private String title;

public String getUrl() {
return url;
}
public String getTitle() {
return title;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class LinkAttachmentMixin extends AttachmentMixin {
@JsonProperty("url")
String url;

@JsonProperty("title")
String title;
}

public class AudioAttachment extends Attachment {
private String id;
private String ownerId;

public String getId() {
return id;
}
public String getOwnerId() {
return ownerId;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class AudioAttachmentMixin extends AttachmentMixin {
@JsonProperty("aid")
private String id;

@JsonProperty("owner_id")
private String ownerId;
}

创建了注册 Mixins 的模块:
public class MyModule extends SimpleModule {
public MyModule() {
super("MyModule");
}

@Override
public void setupModule(SetupContext context) {
context.setMixInAnnotations(Post.class, PostMixin.class);
context.setMixInAnnotations(Attachment.class, AttachmentMixin.class);
context.setMixInAnnotations(LinkAttachment.class, LinkAttachmentMixin.class);
context.setMixInAnnotations(AudioAttachment.class,AudioAttachmentMixin.class);
}
}

初始化 ObjectMapper:
objectMapper = new ObjectMapper();
objectMapper.registerModule(new MyModule());

当我尝试反序列化 JSON 时,出现以下异常:
java.lang.IllegalArgumentException: Class my.package.LinkAttachmentMixin is not assignable to my.package.Attachment
(through reference chain: my.package.Post["attachments"])

是否可以使用 jackson 多态类型反序列化此 JSON
处理支持?
我需要编写自己的反序列化器吗?谁能给个好
sample 开始?
是否有可能仅使用注释来解析这个 JSON?

最佳答案

试着把 @JsonTypeInfo@JsonSubTypes您的 Attachment 上的注释类而不是在 mixin 上。您甚至可以通过正确注释其他类来完全消除混入。如果你走那条路,那么你可能必须用 @JsonCreator 注释你的 VO 类/属性和 AttachmentType。 .

Here's an article这描述了类似于您正在尝试做的反序列化。我发现它过去对做这种事情很有用。

关于json - 棘手的(?)JSON,多态反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691074/

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