Attribute not being serialized on custom ArrayList extending class

161 views Asked by At

Here's my problem, I have a class A like this :

class A {
    public A(MyList myList) {
        this.myList = myList;
    }

    @ElementList(name = "MyList", entry = "MyListElement", type = MyListElement.class)
    private MyList myList;

    // getters, setters ...
}

And I have my MyList class which is a particular ArrayList :

class MyList extends ArrayList<MyListElement> {

    public MyList(Long attribute) {
        this.myListAttribute = attribute;
    }

    @Attribute(name = "MyListAttribute")
    private Long myListAttribute;

    // getters, setters ...
}

So here, my element list needs an attribute to be provided. I found out that it was the solution (extending ArrayList<> class), or maybe I'm wrong ?

The problem is that, everything works well and is serialized the way I want, even the MyListElement which contains attributes, and many elements, except the MyList attribute

I get something like this when I try to serialize :

<A>
    <MyList>  <!-- Here the attribute is missing... -->
         <MyListElement Att1="X" Att2="Something" Att3="Blabla">
             <AnElement>Test</AnElement>
             <AnotherElement>Test2</AnotherElement>
         </MyListElement>
    </MyList>
</A>

I think I've missed something in the documentation, maybe I'm doing something wrong.

Thanks in advance!

1

There are 1 answers

0
Lebowski On BEST ANSWER

I just found another way to do what I want. Maybe I was doing wrong or maybe the framework does not look the attributes set on an @ElementList property.

So I transformed my MyList class like this :

class MyList { // notice it does not extend ArrayList<MyListElement> anymore

    // Now I set this list in inline mode
    @ElementList(entry = "MyListElement", type = MyListElement.class, inline = true)
    private ArrayList<MyListElement> elementList;

    @Attribute(name = "MyListAttribute")
    private Long attribute;
}

and in my class A:

class A {
   @Element(name = "MyList")
   private MyList myList;
}

That way, I get what I expect :

<A>
    <MyList MyListAttribute="...">
        <MyListElement></MyListElement>
    </MyList>
</A>