Specifying an adapter for wrapped values

Question . Given a common wrapper class, how can you specify an XmlAdapter in a field of this type that should then be used to serialize the wrapped value?

Situation: Property Bag

I have a class with several typically typed members (which contain some business logic, but it comes down to typed wrapper classes for this question), which I would like to serialize by specifying an XmlAdapter wrapped value using field annotation.

Let's say we have a bag of properties:

class Bag {
   public Property<TypeA> propertyA = new Property<TypeA>();
   public Property<TypeB> propertyB = new Property<TypeB>();
}

class Property<T extends Object> {
   T value;

   @XmlElement
   public T getValue()
   public T setValue(T value)
   // ...
}

This gets serialized / deserialized just fine until TypeA or TypeB knows the XmlAdapter or can be serialized by definition.

<Bag>
   <propertyA>myAValue</propertyA>
   <property>myBValue</propertyB>
</Bag>

How do I specify an adapter for a property value?

A () XmlAdapter, . ist runtime value XML.

XmlAdapter , @XmlJavaTypeAdapter Annotation.

, , . "propertyA" , Propertys (, propertyA.getValue()).

.

class Bag {
   @PropertyValueAdapter(TypeAdapterForA.class)
   public Property<TypeA> propertyA = new Property<TypeA>();
   //...
} 

// This generically adapts TypeA
class TypeAdapterForA extends XmlAdapter<Object, TypeA> {
  public TypeA unmarshal(Object element){
  // ...
}

. , , Property, Marshalling, . .

XmlAdapters, (Un) Marshaller.Listener .., XmlAdapter Property, ( ) beforeUnmarshal Bag, . , , JAXB ( XML).

. , , ; unmarshalling (, Bag, ).

, , -, XmlAdapter , ?

+4

Source: https://habr.com/ru/post/1599524/


All Articles