public abstract class SerializerSession extends BeanSession
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.
Modifier and Type | Field and Description |
---|---|
int |
indent
The current indentation depth into the model.
|
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.
|
protected 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 int |
getInitialDepth()
Returns the
Serializer.SERIALIZER_initialDepth setting value for this session. |
protected Method |
getJavaMethod()
Returns the Java method that invoked this serializer.
|
protected ObjectMap |
getLastLocation()
Returns information used to determine at what location in the parse a failure occurred.
|
SerializerListener |
getListener()
Returns the listener associated with this session.
|
<T extends SerializerListener> |
getListener(Class<T> c)
Returns the listener associated with this session.
|
protected int |
getMaxDepth()
Returns the
Serializer.SERIALIZER_maxDepth setting value for this session. |
protected int |
getMaxIndent()
Returns the
Serializer.SERIALIZER_maxIndent setting value for this session. |
protected char |
getQuoteChar()
Returns the
Serializer.SERIALIZER_quoteChar setting value for this session. |
Map<String,String> |
getResponseHeaders()
Optional method that specifies HTTP request headers for this serializer.
|
protected UriResolver |
getUriResolver()
Returns the URI resolver.
|
protected boolean |
isAddBeanTypeProperties()
Returns the
Serializer.SERIALIZER_addBeanTypeProperties setting value for this session. |
protected boolean |
isDetectRecursions()
Returns the
Serializer.SERIALIZER_detectRecursions setting value for this session. |
protected boolean |
isIgnoreRecursions()
Returns the
Serializer.SERIALIZER_ignoreRecursions setting value for this session. |
protected boolean |
isSortCollections()
Returns the
Serializer.SERIALIZER_sortCollections setting value for this session. |
protected boolean |
isSortMaps()
Returns the
Serializer.SERIALIZER_sortMaps setting value for this session. |
protected boolean |
isTrimEmptyCollections()
Returns the
Serializer.SERIALIZER_trimEmptyCollections setting value for this session. |
protected boolean |
isTrimEmptyMaps()
Returns the
Serializer.SERIALIZER_trimEmptyMaps setting value for this session. |
protected boolean |
isTrimNulls()
Returns the
Serializer.SERIALIZER_trimNullProperties setting value for this session. |
protected boolean |
isTrimStrings()
Returns the
Serializer.SERIALIZER_trimStrings setting value for this session. |
protected boolean |
isUseWhitespace()
Returns the
Serializer.SERIALIZER_useWhitespace setting value for this session. |
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 void |
pop()
Pop an object off the stack.
|
protected ClassMeta<?> |
push(String attrName,
Object o,
ClassMeta<?> eType)
Push the specified object onto the stack.
|
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.
|
protected void |
setCurrentClass(ClassMeta<?> currentClass)
Sets the current class being serialized for proper error messages.
|
protected void |
setCurrentProperty(BeanPropertyMeta currentProperty)
Sets the current bean property being serialized for proper error messages.
|
protected <E> Collection<E> |
sort(Collection<E> c)
Sorts the specified collection if
isSortCollections() returns |
protected <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 . |
protected String |
trim(Object o)
Trims the specified string if
isTrimStrings() returns |
protected boolean |
willRecurse(String attrName,
Object o,
ClassMeta<?> cm)
Returns
Serializer.SERIALIZER_detectRecursions is enabled, and the specified
object is already higher up in the serialization chain. |
_class, checkForWarnings, convertToMemberType, convertToMemberType, convertToType, convertToType, convertToType, getArgsClassMeta, getBeanMeta, getBeanRegistry, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getLocale, getMediaType, getStringBuilder, getTimeZone, isBean, isBean, isDebug, isIgnoreUnknownBeanProperties, 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, hasWarnings, toString
public int indent
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 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 void setCurrentProperty(BeanPropertyMeta currentProperty)
currentProperty
- The current property being serialized.protected final void setCurrentClass(ClassMeta<?> currentClass)
currentClass
- The current class being serialized.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 int getMaxDepth()
Serializer.SERIALIZER_maxDepth
setting value for this session.Serializer.SERIALIZER_maxDepth
setting value for this session.protected final int getInitialDepth()
Serializer.SERIALIZER_initialDepth
setting value for this session.Serializer.SERIALIZER_initialDepth
setting value for this session.protected final boolean isDetectRecursions()
Serializer.SERIALIZER_detectRecursions
setting value for this session.Serializer.SERIALIZER_detectRecursions
setting value for this session.protected final boolean isIgnoreRecursions()
Serializer.SERIALIZER_ignoreRecursions
setting value for this session.Serializer.SERIALIZER_ignoreRecursions
setting value for this session.protected boolean isUseWhitespace()
Serializer.SERIALIZER_useWhitespace
setting value for this session.Serializer.SERIALIZER_useWhitespace
setting value for this session.protected int getMaxIndent()
Serializer.SERIALIZER_maxIndent
setting value for this session.Serializer.SERIALIZER_maxIndent
setting value for this session.protected boolean isAddBeanTypeProperties()
Serializer.SERIALIZER_addBeanTypeProperties
setting value for this session.Serializer.SERIALIZER_addBeanTypeProperties
setting value for this session.protected char getQuoteChar()
Serializer.SERIALIZER_quoteChar
setting value for this session.Serializer.SERIALIZER_quoteChar
setting value for this session.protected final boolean isTrimNulls()
Serializer.SERIALIZER_trimNullProperties
setting value for this session.Serializer.SERIALIZER_trimNullProperties
setting value for this session.protected final boolean isTrimEmptyCollections()
Serializer.SERIALIZER_trimEmptyCollections
setting value for this session.Serializer.SERIALIZER_trimEmptyCollections
setting value for this session.protected final boolean isTrimEmptyMaps()
Serializer.SERIALIZER_trimEmptyMaps
setting value for this session.Serializer.SERIALIZER_trimEmptyMaps
setting value for this session.protected boolean isTrimStrings()
Serializer.SERIALIZER_trimStrings
setting value for this session.Serializer.SERIALIZER_trimStrings
setting value for this session.protected final boolean isSortCollections()
Serializer.SERIALIZER_sortCollections
setting value for this session.Serializer.SERIALIZER_sortCollections
setting value for this session.protected final boolean isSortMaps()
Serializer.SERIALIZER_sortMaps
setting value for this session.Serializer.SERIALIZER_sortMaps
setting value for this session.protected final ClassMeta<?> push(String attrName, Object o, ClassMeta<?> eType) throws SerializeException
attrName
- The attribute name.o
- The current object being serialized.eType
- The expected class type.ClassMeta
of the object so that instanceof
operations only need to be performed
once (since they can be expensive).SerializeException
- If recursion occurred.protected final boolean willRecurse(String attrName, Object o, ClassMeta<?> cm) throws SerializeException
Serializer.SERIALIZER_detectRecursions
is enabled, and the specified
object is already higher up in the serialization chain.attrName
- The bean property attribute name, or some other identifier.o
- The object to check for recursion.cm
- The metadata on the object class.SerializeException
- If recursion occurred.protected final void pop()
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 final void onError(Throwable t, String msg, Object... args)
t
- The throwable that was thrown (if there was one).msg
- The warning message.args
- Optional MessageFormat
-style arguments.protected 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.protected 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.protected final <K,V> Map<K,V> sort(Map<K,V> m)
isSortMaps()
returns m
- The map being sorted.TreeMap
.protected 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 final ObjectMap getLastLocation()
{line:123,column:456,currentProperty:"foobar"}
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_abridged
setting.
When enabled, 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.Copyright © 2018 Apache. All rights reserved.