MOXy JAXB unable to unmarshall into List

I was able to unmarshall the structure below to List using Oracle JDK 1.8 standard library.

<parent>
    <child>1234 1234 1234</child>
    <child>1231 1313 1331</child>
</parent>

where I have a and XmlAdapter class like below to tokenize String to int[] vice versa and using XMlJavaTypeAdapter on the root XML class like below.

class ChildAdapter extends XmlAdapter<String,int[]> {
    ...
}


@XmlRootElement(name="parent")
class Parent {
    ...
    private List<int[]> children;
    ...
    @XmlElement(name="child")
    @XmlJavaTypeAdapter(ChildAdapter.class)
    public void setChildren(List<int[]> children) {
        ...
    }
    ...
}

but I got an Exception when I switch to use EclipseLink MOXy implementation. Has anyone tried this before?

Exception [EclipseLink-33] (Eclipse Persistence Services - 2.7.3.v20180807-4be1041): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Trying to invoke [setChildren] on the object with the value [[[email protected]].  The number of actual and formal parameters differs, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: argument type mismatch
Mapping: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[childrenList-->child/text()]
Descriptor: XMLDescriptor(mypackage.Parent --> [DatabaseTable(Parent)])
at org.eclipse.persistence.exceptions.DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(DescriptorException.java:714)
at org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor.setAttributeValueInObject(MethodAttributeAccessor.java:286)
at org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor.setAttributeValueInObject(MethodAttributeAccessor.java:239)

Using Moxy, the only way is I need to create a Wrapper class to holds the int[] like below, but it is not what I really want.

class ChildWrapper {
    private int[] childs;
    public void setChilds(int[] childs) {
        this.childs = childs
    }
    public int[] getChilds() {
        return childs;
    }
}

Changed the adapter to class ChildAdapter extends XmlAdapter<String, ChildWrapper>.

Suprizingly the List<int[]> children has now become List<ChildWrapper> children in the resulting object. I didn’t change parent, so the unmarshaller now create a different object through reflection or something? Isn’t this broken?

Answer

I have a workaround. Seems like the problem here Moxy do not like List of List List<List<T>>.

I created an XML Adapter of type <String,Object> and casted the object to List and use this for @XMLJavaTypeAdapter(ChildAdapter.class) annotation.

public class ChildAdapter extends XmlAdapter<String, Object> {

    @Override
    public Object unmarshall(String v) {
         List<Integer> result = new ArrayList<>();
         ...Tokenize your String v and add them to result...
         return result;
    }


    @Override
    public String marshall(Object v) {
         List<Integer> l = (List<Integer)v;
         StringBuilder b = new StringBuilder();
         ...Loop l and append b...
         return b.toString();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *