Jackson JSON Mixin 反序列化 - 不同类型的数组

发布于 2024-11-30 07:05:48 字数 2417 浏览 1 评论 0原文

我有一个 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 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文