Jackson JSON Mixin 反序列化 - 不同类型的数组
我有一个 JSON 字符串,看起来像这样
{"response":[125,{"id":219},{"id":212}]}
,正如你所看到的,“响应”是一个由 Number 和 2 个 POJO (id:Number) 组成的数组。
您能给我一些如何使用 Jackson Mixins 反序列化此类字符串的建议吗? 假设每个条目都有 POJO 的类型是行不通的 - Jackson 会抛出异常,因为 Number 不是 POJO 的实例。 实际上,可以跳过这个令人讨厌的数字,我只需要 POJO 列表。
Test.java
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new SimpleModule("VKontakteModule", new Version(1, 0, 0, null)) {
@Override
public void setupModule(SetupContext context) {
context.setMixInAnnotations(VKontaktePost.class, VKontaktePostMixin.class);
context.setMixInAnnotations(VKontaktePosts.class, VKontaktePostsMixin.class);
}
});
String str;
VKontaktePosts posts;
str = "{\"response\":[{\"id\":282},{\"id\":283}]}";
posts = objectMapper.readValue(deserStr, VKontaktePosts.class); //Works
str = "{\"response\":[125,{\"id\":282},{\"id\":283}]}";
posts = objectMapper.readValue(deserStr, VKontaktePosts.class); //Throws JsonMappingException: Can not construct instance of VKontaktePost, problem: no suitable creator method found to deserialize from JSON Number
VKontaktePosts.java
public class VKontaktePosts implements Serializable {
private final List<VKontaktePost> posts;
public VKontaktePosts(List<VKontaktePost> posts) {
this.posts = posts;
}
public List<VKontaktePost> getPosts() {
return posts;
}
}
VKontaktePost.java
public class VKontaktePost {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
VKontaktePostsMixin.java
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class VKontaktePostsMixin {
@JsonCreator
VKontaktePostsMixin(@JsonProperty("response") List<VKontaktePostMixin> posts) {
}
@JsonProperty("response")
abstract List<VKontaktePostMixin> getPosts();
}
VKontaktePostMixin.java
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class VKontaktePostMixin {
@JsonProperty("id")
abstract String getId();
}
I have JSON string that looks like
{"response":[125,{"id":219},{"id":212}]}
So as you can see "response" is an array consist of Number and 2 POJOs (id:Number).
Could you please give me some advice how to deserialize such strings using Jackson Mixins?
Assumption every entry has POJO's type does not work - Jackson throws an Exception because Number is not an instance of POJO's.
Actually it's possible to skip this nasty Number I need only List of POJOs.
Test.java
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new SimpleModule("VKontakteModule", new Version(1, 0, 0, null)) {
@Override
public void setupModule(SetupContext context) {
context.setMixInAnnotations(VKontaktePost.class, VKontaktePostMixin.class);
context.setMixInAnnotations(VKontaktePosts.class, VKontaktePostsMixin.class);
}
});
String str;
VKontaktePosts posts;
str = "{\"response\":[{\"id\":282},{\"id\":283}]}";
posts = objectMapper.readValue(deserStr, VKontaktePosts.class); //Works
str = "{\"response\":[125,{\"id\":282},{\"id\":283}]}";
posts = objectMapper.readValue(deserStr, VKontaktePosts.class); //Throws JsonMappingException: Can not construct instance of VKontaktePost, problem: no suitable creator method found to deserialize from JSON Number
VKontaktePosts.java
public class VKontaktePosts implements Serializable {
private final List<VKontaktePost> posts;
public VKontaktePosts(List<VKontaktePost> posts) {
this.posts = posts;
}
public List<VKontaktePost> getPosts() {
return posts;
}
}
VKontaktePost.java
public class VKontaktePost {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
VKontaktePostsMixin.java
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class VKontaktePostsMixin {
@JsonCreator
VKontaktePostsMixin(@JsonProperty("response") List<VKontaktePostMixin> posts) {
}
@JsonProperty("response")
abstract List<VKontaktePostMixin> getPosts();
}
VKontaktePostMixin.java
@JsonIgnoreProperties(ignoreUnknown = true)
public abstract class VKontaktePostMixin {
@JsonProperty("id")
abstract String getId();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论