public abstract class SerializerSession extends BeanTraverseSession
Serializer
.
Used by serializers for the following purposes:
This class is NOT thread safe. It is typically discarded after one-time use although it can be reused within the same thread.
indent
Modifier | Constructor and Description |
---|---|
protected |
SerializerSession(Serializer ctx,
SerializerSessionArgs args)
Create a new session using properties specified in the context.
|
protected |
SerializerSession(SerializerSessionArgs args)
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
canIgnoreValue(ClassMeta<?> cm,
String attrName,
Object value)
Returns
|
protected static BeanPropertyValue |
createBeanTypeNameProperty(BeanMap<?> m,
String typeName)
Create a "_type" property that contains the dictionary name of the bean.
|
protected VarResolverSession |
createDefaultVarResolverSession()
Adds a session object to the
VarResolverSession in this session. |
protected abstract SerializerPipe |
createPipe(Object output)
Wraps the specified input object into a
ParserPipe object so that it can be easily converted into
a stream or reader. |
protected abstract void |
doSerialize(SerializerPipe pipe,
Object o)
Serializes a POJO to the specified output stream or writer.
|
protected Object |
generalize(Object o,
ClassMeta<?> type)
Generalize the specified object if a POJO swap is associated with it.
|
protected String |
getBeanTypeName(ClassMeta<?> eType,
ClassMeta<?> aType,
BeanPropertyMeta pMeta)
Resolves the dictionary name for the actual type.
|
protected ClassMeta<?> |
getExpectedRootType(Object o)
Returns the parser-side expected type for the object.
|
protected Method |
getJavaMethod()
Returns the Java method that invoked this serializer.
|
SerializerListener |
getListener()
Returns the listener associated with this session.
|
<T extends SerializerListener> |
getListener(Class<T> c)
Returns the listener associated with this session.
|
Map<String,String> |
getResponseHeaders()
Optional method that specifies HTTP request headers for this serializer.
|
protected UriContext |
getUriContext()
Configuration property: URI context bean.
|
protected UriRelativity |
getUriRelativity()
Configuration property: URI relativity.
|
protected UriResolution |
getUriResolution()
Configuration property: URI resolution.
|
protected UriResolver |
getUriResolver()
Returns the URI resolver.
|
VarResolverSession |
getVarResolver()
Returns the variable resolver session.
|
protected boolean |
isAddBeanTypes()
Configuration property: Add
|
protected boolean |
isAddRootType()
Configuration property: Add type attribute to root nodes.
|
protected boolean |
isSortCollections()
Configuration property: Sort arrays and collections alphabetically.
|
protected boolean |
isSortMaps()
Configuration property: Sort maps alphabetically.
|
protected boolean |
isTrimEmptyCollections()
Configuration property: Trim empty lists and arrays.
|
protected boolean |
isTrimEmptyMaps()
Configuration property: Trim empty maps.
|
protected boolean |
isTrimNullProperties()
Configuration property: Trim null bean property values.
|
protected boolean |
isTrimStrings()
Configuration property: Trim strings.
|
abstract boolean |
isWriterSerializer()
Returns
WriterSerializer . |
protected void |
onBeanGetterException(BeanPropertyMeta p,
Throwable t)
Specialized warning when an exception is thrown while executing a bean getter.
|
protected void |
onError(Throwable t,
String msg,
Object... args)
Logs a warning message.
|
protected ClassMeta<?> |
push2(String attrName,
Object o,
ClassMeta<?> eType)
Same as
BeanTraverseSession.push(String, Object, ClassMeta) but wraps BeanRecursionException inside SerializeException . |
protected String |
relativizeUri(Object relativeTo,
Object uri)
Opposite of
resolveUri(Object) . |
String |
resolve(String string)
Resolves any variables in the specified string.
|
String |
resolveUri(Object uri)
Converts a String to an absolute URI based on the
UriContext on this session. |
abstract Object |
serialize(Object o)
Shortcut method for serializing objects directly to either a
depending on the serializer type. |
void |
serialize(Object o,
Object out)
Serialize the specified object using the specified session.
|
abstract String |
serializeToString(Object o)
Shortcut method for serializing an object to a String.
|
<E> Collection<E> |
sort(Collection<E> c)
Sorts the specified collection if
isSortCollections() returns |
<K,V> Map<K,V> |
sort(Map<K,V> m)
Sorts the specified map if
isSortMaps() returns |
protected Object |
swap(PojoSwap swap,
Object o)
Invokes the specified swap on the specified object.
|
protected static List<Object> |
toList(Class<?> type,
Object array)
Converts the contents of the specified object array to a list.
|
ObjectMap |
toMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
String |
toString(Object o)
Converts the specified object to a
|
String |
trim(Object o)
Trims the specified string if
isTrimStrings() returns |
SerializerSession |
varSessionObject(String name,
Object value)
Adds a session object to the
VarResolverSession in this session. |
getInitialDepth, getLastLocation, getMaxDepth, getOptionalType, getOptionalValue, getStack, isDetectRecursions, isIgnoreRecursions, isOptional, pop, push, setCurrentClass, setCurrentProperty, willRecurse
_class, checkForWarnings, convertToMemberType, convertToMemberType, convertToType, convertToType, convertToType, getArgsClassMeta, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionaryClasses, getBeanFieldVisibility, getBeanFilters, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanTypePropertyName, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getClassMetaForObject, getExamples, getExcludeProperties, getImplClasses, getIncludeProperties, getLocale, getMediaType, getNotBeanClasses, getNotBeanPackagesNames, getNotBeanPackagesPrefixes, getPojoSwaps, getPropertyNamer, getSchema, getStringBuilder, getTimeZone, getTimeZoneId, isBean, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isDebug, isFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnorePropertiesWithoutSetters, isIgnoreUnknownBeanProperties, isIgnoreUnknownNullBeanProperties, isSortProperties, isUseEnumNames, isUseInterfaceProxies, isUseJavaBeanIntrospector, newBean, newBean, newBeanMap, newBeanMap, object, returnStringBuilder, string, toArray, toBeanMap, toBeanMap
addToCache, addToCache, addWarning, getClassProperty, getFromCache, getInstanceArrayProperty, getInstanceProperty, getLogger, getProperties, getProperty, getProperty, getProperty, getPropertyKeys, getWarnings, hasProperty, hasWarnings, toString
protected SerializerSession(Serializer ctx, SerializerSessionArgs args)
ctx
- The context creating this session object.
The context contains all the configuration settings for this object.
Can be args
- Runtime arguments.
These specify session-level information such as locale and URI context.
It also include session-level properties that override the properties defined on the bean and
serializer contexts.protected SerializerSession(SerializerSessionArgs args)
args
- Runtime arguments.
These specify session-level information such as locale and URI context.
It also include session-level properties that override the properties defined on the bean and
serializer contexts.public SerializerSession varSessionObject(String name, Object value)
VarResolverSession
in this session.name
- The session object key.value
- The session object.protected VarResolverSession createDefaultVarResolverSession()
VarResolverSession
in this session.public VarResolverSession getVarResolver()
protected abstract void doSerialize(SerializerPipe pipe, Object o) throws IOException, SerializeException
This method should NOT close the context object.
pipe
- Where to send the output from the serializer.o
- The object to serialize.IOException
- Thrown by underlying stream.SerializeException
- Problem occurred trying to serialize object.public abstract Object serialize(Object o) throws SerializeException
byte []
depending on the serializer type.o
- The object to serialize.byte []
.SerializeException
- If a problem occurred trying to convert the output.public abstract String serializeToString(Object o) throws SerializeException
o
- The object to serialize.byte []
converted to a string based on the OutputStreamSerializer.OSSERIALIZER_binaryFormat
setting.SerializeException
- If a problem occurred trying to convert the output.public abstract boolean isWriterSerializer()
WriterSerializer
.WriterSerializer
.protected abstract SerializerPipe createPipe(Object output)
ParserPipe
object so that it can be easily converted into
a stream or reader.output
- The output location.
Writer
OutputStream
- Output will be written as UTF-8 encoded stream.
File
- Output will be written as system-default encoded stream.
StringBuilder
ParserPipe
wrapper around the specified input object.public final void serialize(Object o, Object out) throws SerializeException, IOException
out
- Where to send the output from the serializer.o
- The object to serialize.SerializeException
- If a problem occurred trying to convert the output.IOException
- Thrown by the underlying stream.protected final Method getJavaMethod()
When using the REST API, this is the Java method invoked by the REST call. Can be used to access annotations defined on the method or class.
protected final UriResolver getUriResolver()
protected final void onBeanGetterException(BeanPropertyMeta p, Throwable t)
p
- The bean map entry representing the bean property.t
- The throwable that the bean getter threw.protected void onError(Throwable t, String msg, Object... args)
onError
in class BeanTraverseSession
t
- The throwable that was thrown (if there was one).msg
- The warning message.args
- Optional MessageFormat
-style arguments.public final String trim(Object o)
isTrimStrings()
returns o
- The input string to trim.protected final Object generalize(Object o, ClassMeta<?> type) throws SerializeException
o
- The object to generalize.type
- The type of object.SerializeException
- If a problem occurred trying to convert the output.public final boolean canIgnoreValue(ClassMeta<?> cm, String attrName, Object value) throws SerializeException
cm
- The class type of the object being serialized.attrName
- The bean attribute name, or value
- The object being serialized.SerializeException
- If recursion occurred.public final <K,V> Map<K,V> sort(Map<K,V> m)
isSortMaps()
returns m
- The map being sorted.TreeMap
.public final <E> Collection<E> sort(Collection<E> c)
isSortCollections()
returns c
- The collection being sorted.TreeSet
.protected static final List<Object> toList(Class<?> type, Object array)
Works on both object and primitive arrays.
In the case of multi-dimensional arrays, the outgoing list will contain elements of type n-1 dimension.
i.e. if type
is
then list
will have entries of type
.
type
- The type of array.array
- The array being converted.public final String resolveUri(Object uri)
UriContext
on this session.uri
- The input URI.
Can be any of the following:
URI can be any of the following forms:
protected final String relativizeUri(Object relativeTo, Object uri)
resolveUri(Object)
.
Converts the URI to a value relative to the specified
Both parameters can be any of the following:
Both URIs can be any of the following forms:
relativeTo
- The URI to relativize against.uri
- The URI to relativize.public final String toString(Object o)
Also has the following effects:
ClassInfo.getFullName()
.
o
- The object to convert to a protected static final BeanPropertyValue createBeanTypeNameProperty(BeanMap<?> m, String typeName)
m
- The bean map to create a class property on.typeName
- The type name of the bean.protected final String getBeanTypeName(ClassMeta<?> eType, ClassMeta<?> aType, BeanPropertyMeta pMeta)
eType
- The expected type of the bean property.aType
- The actual type of the bean property.pMeta
- The current bean property being serialized.protected final ClassMeta<?> getExpectedRootType(Object o)
The return value depends on the Serializer.SERIALIZER_addRootType
setting.
When disabled, the parser already knows the Java POJO type being parsed, so there is
no reason to add
o
- The object to get the expected type on.public Map<String,String> getResponseHeaders()
For example, SoapXmlSerializer
needs to set a
This method is typically meaningless if the serializer is being used stand-alone (i.e. outside of a REST server or client).
public <T extends SerializerListener> T getListener(Class<T> c)
c
- The listener class to cast to.public String resolve(String string)
string
- The string to resolve values in.protected final ClassMeta<?> push2(String attrName, Object o, ClassMeta<?> eType) throws SerializeException
BeanTraverseSession.push(String, Object, ClassMeta)
but wraps BeanRecursionException
inside SerializeException
.attrName
- The attribute name.o
- The current object being traversed.eType
- The expected class type.ClassMeta
of the object so that SerializeException
- If recursion occurred.protected Object swap(PojoSwap swap, Object o) throws SerializeException
swap
- The swap to invoke.o
- The input object.SerializeException
- If swap method threw an exception.protected boolean isAddBeanTypes()
Serializer.SERIALIZER_addBeanTypes
protected final boolean isAddRootType()
Serializer.SERIALIZER_addRootType
public SerializerListener getListener()
protected final boolean isSortCollections()
Serializer.SERIALIZER_sortCollections
protected final boolean isSortMaps()
Serializer.SERIALIZER_sortMaps
protected final boolean isTrimEmptyCollections()
Serializer.SERIALIZER_trimEmptyCollections
protected final boolean isTrimEmptyMaps()
Serializer.SERIALIZER_trimEmptyMaps
protected final boolean isTrimNullProperties()
Serializer.SERIALIZER_trimNullProperties
protected boolean isTrimStrings()
String.trim()
before being serialized.Serializer.SERIALIZER_trimStrings
protected final UriContext getUriContext()
Serializer.SERIALIZER_uriContext
protected final UriRelativity getUriRelativity()
Serializer.SERIALIZER_uriRelativity
protected final UriResolution getUriResolution()
Serializer.SERIALIZER_uriResolution
public ObjectMap toMap()
Session
toMap
in class BeanTraverseSession
Copyright © 2016–2019 The Apache Software Foundation. All rights reserved.