@Swap Annotation
{@link oaj.annotation.Swap @Swap} can be used to associate a swap class using an
annotation.
This is often cleaner than using the builder pojoSwaps() method since you can keep
your swap class near your POJO class.
@Swap(MyPojoSwap.class)
public class MyPojo {
...
}
// Sample swap for converting MyPojo classes to a simple string.
public class MyPojoSwap extends PojoSwap<MyPojo,String> {
@Override /* PojoSwap */
public String swap(BeanSession session, MyPojo o) {
return o.toSomeSerializableForm();
}
}
Multiple swaps can be associated with a POJO by using the {@link oaj.annotation.Swaps @Swaps} annotation:
@Swaps(
{
@Swap(MyJsonSwap.class),
@Swap(MyXmlSwap.class),
@Swap(MyOtherSwap.class)
}
)
public class MyPojo {}
Readers get serialized directly to the output of a serializer.
Therefore it's possible to implement a swap that provides fully-customized output.
public class MyJsonSwap extends PojoSwap<MyPojo,Reader> {
public MediaType[] forMediaTypes() {
return MediaType.forStrings("*/json");
}
public Reader swap(BeanSession session, MyPojo o) throws Exception {
return new StringReader("{message:'Custom JSON!'}");
}
}
The @Swap annotation can also be used on getters and setters as well to apply a swap
to individual property values
@BeanProperty(swap=MyPojoSwap.class)
public MyPojo getMyPojo();