T
- The normal form of the class.S
- The swapped form of the class.@BeanIgnore public abstract class PojoSwap<T,S> extends Object
PojoSwaps
are used to extend the functionality of the serializers and parsers to be able to handle
POJOs that aren't automatically handled by the serializers or parsers.
For example, JSON does not have a standard representation for rendering dates.
By defining a special Date
swap and associating it with a serializer and parser, you can convert a
Date
object to a String
during serialization, and convert that String
object back into a
Date
object during parsing.
Swaps MUST declare a public no-arg constructor so that the bean context can instantiate them.
PojoSwaps
are associated with serializers and parsers through the following:
@Swap
@Swaps
BeanContextBuilder.pojoSwaps(Object...)
BeanContextBuilder.pojoSwaps(Class...)
BeanContextBuilder.pojoSwaps(boolean, Object...)
BeanContextBuilder.pojoSwapsRemove(Object...)
PojoSwaps
have two parameters:
<T>
- The normal representation of an object.
<S>
- The swapped representation of an object.
Serializers
use swaps to convert objects of type T into objects of type S, and on calls to
BeanMap.get(Object)
.
Parsers
use swaps to convert objects of type S into objects of type T, and on calls to
BeanMap.put(String,Object)
.
<S>
The swapped object representation of an object must be an object type that the serializers can natively convert to
JSON (or language-specific equivalent).
The list of valid transformed types are as follows...
String
Number
Boolean
Collection
containing anything on this list.
Map
containing anything on this list.
<T>
The normal object representation of an object.
Modifier and Type | Field and Description |
---|---|
static PojoSwap |
NULL
Represents a non-existent pojo swap.
|
Modifier | Constructor and Description |
---|---|
protected |
PojoSwap()
Constructor.
|
protected |
PojoSwap(Class<T> normalClass,
Class<?> swapClass)
Constructor for when the normal and transformed classes are already known.
|
Modifier and Type | Method and Description |
---|---|
MediaType[] |
forMediaTypes()
Returns the media types that this swap is applicable to.
|
PojoSwap<T,?> |
forMediaTypes(MediaType[] mediaTypes)
Sets the media types that this swap is associated with.
|
Class<T> |
getNormalClass()
Returns the T class, the normalized form of the class.
|
Class<?> |
getSwapClass()
Returns the G class, the generalized form of the class.
|
ClassMeta<?> |
getSwapClassMeta(BeanSession session)
Returns the
ClassMeta of the transformed class type. |
boolean |
isNormalObject(Object o)
Checks if the specified object is an instance of the normal class defined on this swap.
|
boolean |
isSwappedObject(Object o)
Checks if the specified object is an instance of the swap class defined on this swap.
|
int |
match(BeanSession session)
Returns a number indicating how well this swap matches the specified session.
|
S |
swap(BeanSession session,
T o)
If this transform is to be used to serialize non-serializable POJOs, it must implement this method.
|
S |
swap(BeanSession session,
T o,
String template)
Same as
swap(BeanSession, Object) , but can be used if your swap has a template associated with it. |
String |
toString() |
T |
unswap(BeanSession session,
S f,
ClassMeta<?> hint)
If this transform is to be used to reconstitute POJOs that aren't true Java beans, it must implement this method.
|
T |
unswap(BeanSession session,
S f,
ClassMeta<?> hint,
String template)
Same as
unswap(BeanSession, Object, ClassMeta) , but can be used if your swap has a template associated with it. |
String |
withTemplate()
Returns additional context information for this swap.
|
PojoSwap<T,?> |
withTemplate(String template)
Sets the template string on this swap.
|
protected PojoSwap()
public MediaType[] forMediaTypes()
This method can be overridden to programmatically specify what media types it applies to.
This method is the programmatic equivalent to the @Swap.mediaTypes()
annotation.
public String withTemplate()
Typically this is going to be used to specify a template name, such as a FreeMarker template file name.
This method can be overridden to programmatically specify a template value.
This method is the programmatic equivalent to the @Swap.template()
annotation.
public PojoSwap<T,?> forMediaTypes(MediaType[] mediaTypes)
mediaTypes
- The media types that this swap is associated with.public PojoSwap<T,?> withTemplate(String template)
template
- The template string on this swap.public int match(BeanSession session)
Uses the MediaType.match(MediaType, boolean)
method algorithm to produce a number whereby a
larger value indicates a "better match".
The idea being that if multiple swaps are associated with a given POJO, we want to pick the best one.
For example, if the session media type is
100,000
5,100
5,000
1
0
session
- The bean session.public S swap(BeanSession session, T o) throws Exception
The object must be converted into one of the following serializable types:
String
Number
Boolean
Collection
containing anything on this list.
Map
containing anything on this list.
session
- The bean session to use to get the class meta.
This is always going to be the same bean context that created this swap.o
- The object to be transformed.Exception
- If a problem occurred trying to convert the output.public S swap(BeanSession session, T o, String template) throws Exception
swap(BeanSession, Object)
, but can be used if your swap has a template associated with it.session
- The bean session to use to get the class meta.
This is always going to be the same bean context that created this swap.o
- The object to be transformed.template
- The template string associated with this swap.Exception
- If a problem occurred trying to convert the output.public T unswap(BeanSession session, S f, ClassMeta<?> hint) throws Exception
session
- The bean session to use to get the class meta.
This is always going to be the same bean context that created this swap.f
- The transformed object.hint
- If possible, the parser will try to tell you the object type being created.
For example, on a serialized date, this may tell you that the object being created must be of type
GregorianCalendar
.
Exception
- If this method is not implemented.public T unswap(BeanSession session, S f, ClassMeta<?> hint, String template) throws Exception
unswap(BeanSession, Object, ClassMeta)
, but can be used if your swap has a template associated with it.session
- The bean session to use to get the class meta.
This is always going to be the same bean context that created this swap.f
- The transformed object.hint
- If possible, the parser will try to tell you the object type being created.
For example, on a serialized date, this may tell you that the object being created must be of type
GregorianCalendar
.
template
- The template string associated with this swap.Exception
- If a problem occurred trying to convert the output.public Class<T> getNormalClass()
public Class<?> getSwapClass()
Subclasses must override this method if the generalized class is Object
, meaning it can produce multiple
generalized forms.
public ClassMeta<?> getSwapClassMeta(BeanSession session)
ClassMeta
of the transformed class type.
This value is cached for quick lookup.
session
- The bean context to use to get the class meta.
This is always going to be the same bean context that created this swap.ClassMeta
of the transformed class type.public boolean isNormalObject(Object o)
o
- The object to check.public boolean isSwappedObject(Object o)
o
- The object to check.Copyright © 2018 Apache. All rights reserved.