Class ObjectSwap<T,S>
- Type Parameters:
T- The normal form of the class.S- The swapped form of the class.
- Direct Known Subclasses:
AutoListSwap,AutoMapSwap,AutoNumberSwap,AutoObjectSwap,FunctionalSwap,HalLinkOrArraySwap,HalResourceOrArraySwap,JsonApiLinkOrStringSwap,JsonSchema.BooleanOrSchemaArraySwap,JsonSchema.BooleanOrSchemaSwap,JsonSchema.JsonSchemaOrSchemaArraySwap,JsonSchema.JsonTypeOrJsonTypeArraySwap,MapSwap,MatchResultSwap,OpenApiUI,ParsedReaderSwap,RedocUI,StackTraceElementSwap,StringSwap,SurrogateSwap,SwaggerUI
Description
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.
<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).
Swap Class Type <S>
For normal serialization, 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 -
Collectioncontaining anything on this list. -
Mapcontaining anything on this list. - A java bean with properties of anything on this list.
- An array of anything on this list.
For OpenAPI serialization, the valid swapped types also include and
Normal Class Type <T>
The normal object representation of an object.
See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedConstructor.protectedObjectSwap(Class<T> normalClass, Class<?> swapClass) Constructor for when the normal and transformed classes are already known. -
Method Summary
Modifier and TypeMethodDescriptionReturns the media types that this swap is applicable to.ObjectSwap<T,?> forMediaTypes(MediaType[] mediaTypes) Sets the media types that this swap is associated with.Returns the T class, the normalized form of the class.Returns the G class, the generalized form of the class.ClassMeta<?>getSwapClassMeta(MarshallingSession session) Returns theClassMetaof the transformed class type.booleanChecks if the specified object is an instance of the normal class defined on this swap.booleanChecks if the specified object is an instance of the swap class defined on this swap.intmatch(MarshallingSession session) Returns a number indicating how well this swap matches the specified session.swap(MarshallingSession session, T o) If this transform is to be used to serialize non-serializable objects, it must implement this method.swap(MarshallingSession session, T o, String template) Same asswap(MarshallingSession, Object), but can be used if your swap has a template associated with it.toString()unswap(MarshallingSession session, S f, ClassMeta<?> hint) If this transform is to be used to reconstitute objects that aren't true Java beans, it must implement this method.unswap(MarshallingSession session, S f, ClassMeta<?> hint, String template) Same asunswap(MarshallingSession, Object, ClassMeta), but can be used if your swap has a template associated with it.Returns additional context information for this swap.ObjectSwap<T,?> withTemplate(String template) Sets the template string on this swap.
-
Field Details
-
NULL
Represents a non-existent object swap.
-
-
Constructor Details
-
ObjectSwap
protected ObjectSwap()Constructor. -
ObjectSwap
Constructor for when the normal and transformed classes are already known.- Parameters:
normalClass- The normal class (cannot be serialized).swapClass- The transformed class (serializable).
-
-
Method Details
-
forMediaTypes
Returns the media types that this swap is applicable to.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.See Also:
- Returns:
- The media types that this swap is applicable to, or
null if it's applicable for all media types.
-
forMediaTypes
Sets the media types that this swap is associated with.See Also:
- Parameters:
mediaTypes- The media types that this swap is associated with. Can benull to make this swap applicable to all media types.- Returns:
- This object.
-
getNormalClass
Returns the T class, the normalized form of the class.- Returns:
- The normal form of this class.
-
getSwapClass
Returns the G class, the generalized form of the class.Subclasses must override this method if the generalized class is
Object, meaning it can produce multiple generalized forms.- Returns:
- The transformed form of this class.
-
getSwapClassMeta
Returns theClassMetaof the transformed class type.This value is cached for quick lookup.
- Parameters:
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. Must not benull on the first (uncached) call.- Returns:
- The
ClassMetaof the transformed class type.
-
isNormalObject
Checks if the specified object is an instance of the normal class defined on this swap.- Parameters:
o- The object to check. Can benull (returnsfalse ).- Returns:
true if the specified object is a subclass of the normal class defined on this transform. Anull value always returnsfalse .
-
isSwappedObject
Checks if the specified object is an instance of the swap class defined on this swap.- Parameters:
o- The object to check. Can benull (returnsfalse ).- Returns:
true if the specified object is a subclass of the transformed class defined on this transform. Anull value always returnsfalse .
-
match
Returns a number indicating how well this swap matches the specified 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 object, we want to pick the best one.For example, if the session media type is
"text/json" , then the match values are shown below:"text/json" =100,000 "*/json" =5,100 "*/*" =5,000 - No media types specified on swap =
1 "text/xml" =0
- Parameters:
session- The bean session. Can benull (returns0 , or1 if this swap has no media types configured).- Returns:
- Zero if swap doesn't match the session, or a positive number if it does.
-
swap
If this transform is to be used to serialize non-serializable objects, it must implement this method.The object must be converted into one of the following serializable types:
-
String -
Number -
Boolean -
Collectioncontaining anything on this list. -
Mapcontaining anything on this list. - A java bean with properties of anything on this list.
- An array of anything on this list.
- Parameters:
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.- Returns:
- The transformed object.
- Throws:
Exception- If a problem occurred trying to convert the output.
-
-
swap
Same asswap(MarshallingSession, Object), but can be used if your swap has a template associated with it.- Parameters:
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.- Returns:
- The transformed object.
- Throws:
Exception- If a problem occurred trying to convert the output.
-
toString
-
unswap
If this transform is to be used to reconstitute objects that aren't true Java beans, it must implement this method.- Parameters:
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 typeGregorianCalendar.
This may benull if the parser cannot make this determination.- Returns:
- The narrowed object.
- Throws:
Exception- If this method is not implemented.
-
unswap
public T unswap(MarshallingSession session, S f, ClassMeta<?> hint, String template) throws Exception Same asunswap(MarshallingSession, Object, ClassMeta), but can be used if your swap has a template associated with it.- Parameters:
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 typeGregorianCalendar.
This may benull if the parser cannot make this determination.template- The template string associated with this swap.- Returns:
- The transformed object.
- Throws:
Exception- If a problem occurred trying to convert the output.
-
withTemplate
Returns additional context information for this swap.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.See Also:
- Returns:
- Additional context information, or
null if not specified.
-
withTemplate
Sets the template string on this swap.See Also:
- Parameters:
template- The template string on this swap. Can benull to unset the template.- Returns:
- This object.
-