Does XStream support inheritance?

I am trying to serialize some entities, and it seems that XStream does not support inheritance at all. XStream Version: 1.4.5

My test code is:

public class SOverflow {
    public static void main(String[] args) {
        XStream xStream = new XStream(new StaxDriver());
        xStream.processAnnotations(Animal.class);
        xStream.processAnnotations(Dog.class);

        String s = xStream.toXML(new Dog());
        System.out.println(s);
    }
}


@XStreamAlias("animal")
class Animal {
    @XStreamAlias("weight")
    private String weight = "10";

    public Animal() {
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }
}

@XStreamAlias("dog")
class Dog extends Animal {
    @XStreamAlias("name")
    private String name = "Yo-yo";

    public Dog() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

And as a result, I:

<?xml version="1.0" ?><dog><name>Yo-yo</name></dog>

The default behavior without attacks is one and the same.

Maybe something is missing or is it not supported at all?

+4
source share

Source: https://habr.com/ru/post/1628507/


All Articles