Jackson 映射器如何知道每个 Json 对象中的哪个字段分配给类对象? [英] How does the Jackson mapper know what field in each Json object to assign to a class object?

查看:17
本文介绍了Jackson 映射器如何知道每个 Json 对象中的哪个字段分配给类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Let's say I have a Json object like this:

{
    "name": "Bob Dole",
    "company": "Bob Dole Industries",
    "phone": {
        "work": "123-456-7890",
        "home": "234-567-8901",
        "mobile": "345-678-9012"
    }
}

And to help me read it, I use Jackson's Object Mapper with the following class:

public class Contact {
        public static class Phone {
        private String work;
        private String home;
        private String mobile;

        public String getWork() { return work; }
        public String getHome() { return home; }
        public String getMobile() { return mobile; }

        public void setWork(String s) { work = s; }
        public void setHome(String s) { home = s; }
        public void setMobile(String s) { mobile = s; }
    }

    private String name;
    private String company;
    private Phone phone;

    public String getName() { return name; }
    public String getCompany() { return company; }
    public Phone getPhone() { return phone; }

    public void setName(String s) { name = s; }
    public void setCompany(String s) { company = s; }
    public void setPhone(Phone p) { phone = p; }
}

My question is, how (using the simplest explanation possible), does the Object mapper "deserialize" the Json object? I thought it was matching variable names, but changing them by a few letters didn't affect the output. Then, I tried switching the order of the set() functions, but that didn't do anything. I also tried both, but that was also useless. I'm guessing there's something more sophisticated at work here, but what?

I tried to look in the documentation and past code, but I didn't see an explanation that made sense to me.

解决方案

Without Annotations:

Without any annotations, it does what is called POJO mapping, it just uses reflection on the instance members and uses some rules about how to map the keys in the json to the names of the instance members. *note: it works on private members as well as public or package protected as well

If it doesn't match the names of the instance members, then it starts trying to match the getXXX and setXXX methods, if it doesn't match anything then it gives up.

With Annotations:

It uses the metadata supplied by the annotations to do the mapping and conversions.

It is always better to explicitly use the annotations when you have the source to add them to, then there is no guess work on what gets mapped to what.

Remember explicit is always better than implicit!

This is all well documented on the WIKI:

Mapping and Annotations

JSON Schema:

I am creating JSON Schema definitions for all my new projects now to document what is and isn't valid JSON according to the schema rules engine. It is a great way to document your data structures and eliminate parsing errors.

这篇关于Jackson 映射器如何知道每个 Json 对象中的哪个字段分配给类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆