Idealist
Idealist

Reputation: 802

Jackson serialization includes subclass's fields

I had a problem when using Jackson to serialize an object. The fields in the subclass are missing in the file. I tried Gson but have the same problem as well. Can anyone help me with this? Thank you.

public class A extends ArrayList<B>{

    public String name;

    public A(){
    }
    //getter and setter
}

A a = new A();
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(file, a);

file includes all the fields in B, but it does not contains fields in A.

Upvotes: 0

Views: 471

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280030

By making your custom type a List subtype, Jackson uses a special List specific serializer to generate the JSON. It will simply iterate the elements of the List and write those.

Instead of using inheritance, use composition.

Upvotes: 2

Related Questions