mabuzer
mabuzer

Reputation: 6717

XmlAdaptar in JAXB

I have the following Enum Class:

@XmlJavaTypeAdapter(value = PastMedicalHistoryAdapter.class)
public enum PastMedicalHistory {
    Diabetes, Obesity, Smoking, COPD, CAD, PVD, Other
}

and Generic Adapter:

public abstract class GenericEnumAdapter<T extends Enum> extends XmlAdapter<String, Enum> {

    @Override
    public Enum unmarshal(String v) throws Exception {
        log.info("unmarshal: {}", v);
        return convert(v + "");
    }

    public abstract T convert(String value);

    @Override
    public String marshal(Enum v) throws Exception {
        log.info("marshal: {}", v.name());
        String s = "{\"" + v.name() + "\":" + true + "}";
        return s;
    }
}

and basic implementation with

public class PastMedicalHistoryAdapter extends GenericEnumAdapter<PastMedicalHistory> {
    @Override
    public PastMedicalHistory convert(String value) {
        return PastMedicalHistory.valueOf(value);
    }
}

and I used it like this:

@Data
@XmlRootElement(name = "Patient")
public class Test {

    private List<PastMedicalHistory> history;


    public static void main(String[] args) throws Exception {
        JAXBContext cxt = JAXBContext.newInstance(Test.class);
        Marshaller mar = cxt.createMarshaller();
        mar.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        mar.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json");
        mar.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, Boolean.FALSE);
        Test t = new Test();
        t.setHistory(Arrays.asList(PastMedicalHistory.CAD, PastMedicalHistory.Diabetes));
        mar.marshal(t, System.out);
    }
}

the problem, is Output for history is always null like this:

[exec:exec]
2013-09-29 12:13:18:511 INFO marshal: CAD
2013-09-29 12:13:18:522 INFO marshal: Diabetes
{
   "history" : [ null, null ]
}

I'm using Moxy 2.5.1 as JAXB Provider, so What I'm missing, or what I'm doing wrong?

Upvotes: 2

Views: 158

Answers (1)

bdoughan
bdoughan

Reputation: 149047

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

I have been able to reproduce the bug that you are hitting. You can use the following link to track our progress on this issue:

Upvotes: 2

Related Questions