0

I want to serialize with XMLEncoder classes that are generated by wsimport tool. There are many ArrayOfXXXXXX classes that have the following structure:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ArrayOfSitejournal", propOrder = {
    "item"
})
public class ArrayOfSitejournal
    implements Serializable
{

    protected List<Journal> item;

    public List<Journal> getItem() {
        if (item == null) {
            item = new ArrayList<Journal>();
        }
        return this.item;
    }
}

When I serialize ArrayOfSitejournal class I get this output:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_45" class="java.beans.XMLDecoder">
 <object class="ArrayOfSitejournal"/>
</java>

There is no setItem() method so item property is not serialized. My question is: how I can serialize whole ArrayOfSitejournal object together with item member?

I can't change code of this class because it is generated.

I know that I can write PersistenceDelegate to customize serialization. But to deserialize it invocation like this is necessary:

arrayOfSitejournal.getItem().add(journal)

Can you please help me to write such PersistenceDelegate.

Hobbes
  • 1
  • 1

1 Answers1

0

I found the solution myself.

encoder.setPersistenceDelegate(ArrayOfSitejournal.class, new DefaultPersistenceDelegate() {
    protected void initialize(Class type, Object oldInstance, Object newInstance, Encoder out) {
        super.initialize(type, oldInstance,  newInstance, out);

        ArrayOfSitejournal m = (ArrayOfSitejournal)oldInstance;

        Expression expression = new Expression(oldInstance, "getItem", new Object[] {});
        out.writeExpression(expression);
    }
});

I'm not sure whether it is correct, but it works. It generates following output:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_45" class="java.beans.XMLDecoder">
 <object class="ArrayOfSitejournal">
  <void property="item">
   <void method="add">
    <object class="Journal">
     <void property="changeType">
      <string>start</string>
     </void>
     <void property="itemId">
      <long>4239463180</long>
     </void>
    </object>
   </void>
   <void method="add">
    <object class="Journal">
     <void property="changeType">
      <string>now</string>
     </void>
     <void property="itemId">
      <long>4239463180</long>
     </void>
    </object>
   </void>
  </void>
 </object>
</java>

and deserializes correctly.

Hobbes
  • 1
  • 1