开发者

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

开发者 https://www.devze.com 2023-03-21 22:11 出处:网络
How could I implement a \"variant\" class which would act as an adapter between Object and JAXB-natively-supported types?

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[]开发者_StackOverflow社区, 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 {
        // ...
    }
}


This existes already: See JAXBElement

Or for Collections and Map:

utils-apl-derived project

0

精彩评论

暂无评论...
验证码 换一张
取 消