简体   繁体   中英

Jackson: should serialization change when class name changes and class inherits from another class?

Suppose I have the following class:

class OrigClass {
    private int fieldOne;
    private int field Two;
    // getters and setters
}

Let's say that I serialize an object of this class, and pass it read it by the class NewClass:

class NewClass extends BaseClass {
    private int fieldOne;
    // getters and setters
}

class BaseClass {
    private int field Two;
    // getters and setters
}

As you can see, an object of NewClass has exactly the same properties as the properties of OrigClass. The difference is that class names differ and NewClass inherits fieldOne from BaseClass. In this case, will an error occur in deserialization by NewClass?

If the fields are inherited or not doesn't matter to Jackson, it will check for the setters.

If it can deserialize, it will. You should encounter no issues to serialize OrigClass and deserialize NewClass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM