JAXB: How to implement a JAXB-compatible variant wrapper class?

こ雲淡風輕ζ 提交于 2019-12-24 09:49:40

问题


How could I implement a "variant" class which would act as an adapter between Object and JAXB-natively-supported types?

I could then use Object in JAXB-annotated classes.

Therefor, I guess, I would need to store a type ID inside that adapter.

Any ideas?

NOTE: With "JAXB-natively-supported types" I mean types such as:
all primitive types, String, Date, byte[], List<any-JAXB-supported-type>.

Usage Scenario

@XmlType
class SomeClass {
    @XmlJavaTypeAdapter(VariantAdapter.class) // WITH OR WITHOUT?
    @XmlElement
    private Object somePrimitive = null;

    // ...
}

Variant Class Idea Pseudocode

@XmlRootElement
@XmlType
class Variant {
    @XmlAttribute
    private final String typeID;
    @XmlAttribute
    private final String rawXML;

    // ...
}

Adapter Class (Trivial)

class VariantAdapter extends XmlAdapter<Object, Variant> {
    @Override
    public Object marshal(VariantObject arg0) throws Exception {
        // ...
    }

    @Override
    public VariantObject unmarshal(Object arg0) throws Exception {
        // ...
    }
}

回答1:


This existes already: See JAXBElement

Or for Collections and Map:

utils-apl-derived project



来源:https://stackoverflow.com/questions/6762115/jaxb-how-to-implement-a-jaxb-compatible-variant-wrapper-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!