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 |
---|---|
ObjectMap |
asMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
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 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.
|
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.
|
boolean |
isUseWhitespace()
Configuration property: Use whitespace.
|
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 String |
relativizeUri(Object relativeTo,
Object uri)
Opposite of
resolveUri(Object) . |
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
String or
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 static List<Object> |
toList(Class<?> type,
Object array)
Converts the contents of the specified object array to a list.
|
String |
toString(Object o)
Converts the specified object to a
String . |
String |
trim(Object o)
Trims the specified string if
isTrimStrings() returns |
getInitialDepth, getLastLocation, getMaxDepth, getStack, isDetectRecursions, isIgnoreRecursions, pop, push, setCurrentClass, setCurrentProperty, willRecurse
_class, checkForWarnings, convertToMemberType, convertToMemberType, convertToType, convertToType, convertToType, getArgsClassMeta, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionaryClasses, getBeanFieldVisibility, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanTypePropertyName, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getLocale, getMediaType, getPropertyNamer, getSchema, getStringBuilder, getTimeZone, isBean, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isDebug, isFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnorePropertiesWithoutSetters, isIgnoreUnknownBeanProperties, isIgnoreUnknownNullBeanProperties, isSortProperties, isUseEnumNames, isUseInterfaceProxies, isUseJavaBeanIntrospector, newBean, newBean, newBeanMap, newBeanMap, newInstance, newInstance, newInstanceFromOuter, 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 ObjectMap asMap()
Session
asMap
in class BeanSession
protected 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.protected abstract void doSerialize(SerializerPipe pipe, Object o) throws Exception
This method should NOT close the context object.
pipe
- Where to send the output from the serializer.o
- The object to serialize.Exception
- If thrown from underlying stream, or if the input contains a syntax error or is malformed.public abstract Object serialize(Object o) throws SerializeException
String
or byte []
depending on the serializer type.o
- The object to serialize.String
.
byte []
.SerializeException
- If a problem occurred trying to convert the output.public abstract String serializeToString(Object o) throws SerializeException
o
- The object to serialize.String
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
.public final void serialize(Object o, Object out) throws SerializeException
out
- Where to send the output from the serializer.o
- The object to serialize.SerializeException
- If a problem occurred trying to convert the output.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 relativeTo
parameter.
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)
String
.
Also has the following effects:
Class
object is converted to a readable name. See ClassUtils.getReadableClassName(Class)
.
o
- The object to convert to a String
.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 SOAPAction
header.
This method is typically meaningless if the serializer is being used stand-alone (i.e. outside of a REST server or client).
public SerializerListener getListener()
public <T extends SerializerListener> T getListener(Class<T> c)
c
- The listener class to cast to.public boolean isUseWhitespace()
Serializer.SERIALIZER_useWhitespace
protected boolean isAddBeanTypes()
Serializer.SERIALIZER_addBeanTypes
protected final boolean isTrimNullProperties()
Serializer.SERIALIZER_trimNullProperties
protected final boolean isTrimEmptyCollections()
Serializer.SERIALIZER_trimEmptyCollections
protected final boolean isTrimEmptyMaps()
Serializer.SERIALIZER_trimEmptyMaps
protected boolean isTrimStrings()
String.trim()
before being serialized.Serializer.SERIALIZER_trimStrings
protected final boolean isSortCollections()
Serializer.SERIALIZER_sortCollections
protected final boolean isSortMaps()
Serializer.SERIALIZER_sortMaps
protected final boolean isAddRootType()
Serializer.SERIALIZER_addRootType
protected final UriContext getUriContext()
Serializer.SERIALIZER_uriContext
protected final UriResolution getUriResolution()
Serializer.SERIALIZER_uriResolution
protected final UriRelativity getUriRelativity()
Serializer.SERIALIZER_uriRelativity
Copyright © 2018 Apache. All rights reserved.