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

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

问题描述

假设我有一个像这样的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"
    }
}

为了帮助我阅读,我使用Jackson的Object Mapper和以下课程:

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; }
}

我的问题是,如何(使用最简单的解释),是对象映射器反序列化Json对象?我认为它匹配变量名称,但用几个字母更改它们不会影响输出。然后,我尝试切换set()函数的顺序,但是没有做任何事情。我也试过了,但这也没用。我猜这里有更复杂的东西,但是什么?

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.

推荐答案

没有注释:



没有任何注释,它执行所谓的 POJO 映射,它只使用反射,并使用一些有关如何将json中的键映射到实例成员名称的规则。 * 注意:它适用于私有成员以及 public 包受保护以及

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

如果它与实例成员的名称不匹配,则它开始尝试匹配 getXXX setXXX 方法,如果它不匹配则放弃。

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.

它使用注释提供的元数据进行映射和转换。

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.

记住显式总是优于隐式!

映射注释

我现在正在为我的所有新项目创建JSON模式定义,以根据模式规则引擎记录什么是有效的JSON。这是记录数据结构和消除解析错误的好方法。

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天全站免登陆