public abstract class Serializer extends BeanContext
The purpose of this class is:
SerializerSession
).
Subclasses should extend directly from OutputStreamSerializer
or WriterSerializer
depending on
whether it's a stream or character based serializer.
Modifier and Type | Field and Description |
---|---|
static String |
SERIALIZER_abridged
Configuration property: Abridged output.
|
static String |
SERIALIZER_addBeanTypeProperties
Configuration property: Add
|
static String |
SERIALIZER_detectRecursions
Configuration property: Automatically detect POJO recursions.
|
static String |
SERIALIZER_ignoreRecursions
Configuration property: Ignore recursion errors.
|
static String |
SERIALIZER_initialDepth
Configuration property: Initial depth.
|
static String |
SERIALIZER_listener
Configuration property: Serializer listener.
|
static String |
SERIALIZER_maxDepth
Configuration property: Max serialization depth.
|
static String |
SERIALIZER_maxIndent
Configuration property: Maximum indentation.
|
static String |
SERIALIZER_quoteChar
Configuration property: Quote character.
|
static String |
SERIALIZER_sortCollections
Configuration property: Sort arrays and collections alphabetically.
|
static String |
SERIALIZER_sortMaps
Configuration property: Sort maps alphabetically.
|
static String |
SERIALIZER_trimEmptyCollections
Configuration property: Trim empty lists and arrays.
|
static String |
SERIALIZER_trimEmptyMaps
Configuration property: Trim empty maps.
|
static String |
SERIALIZER_trimNullProperties
Configuration property: Trim null bean property values.
|
static String |
SERIALIZER_trimStrings
Configuration property: Trim strings.
|
static String |
SERIALIZER_uriContext
Configuration property: URI context bean.
|
static String |
SERIALIZER_uriRelativity
Configuration property: URI relativity.
|
static String |
SERIALIZER_uriResolution
Configuration property: URI resolution.
|
static String |
SERIALIZER_useWhitespace
Configuration property: Use whitespace.
|
BEAN_beanClassVisibility, BEAN_beanConstructorVisibility, BEAN_beanDictionary, BEAN_beanDictionary_add, BEAN_beanDictionary_remove, BEAN_beanFieldVisibility, BEAN_beanFilters, BEAN_beanFilters_add, BEAN_beanFilters_remove, BEAN_beanMapPutReturnsOldValue, BEAN_beanMethodVisibility, BEAN_beansRequireDefaultConstructor, BEAN_beansRequireSerializable, BEAN_beansRequireSettersForGetters, BEAN_beansRequireSomeProperties, BEAN_beanTypePropertyName, BEAN_debug, BEAN_excludeProperties, BEAN_ignoreInvocationExceptionsOnGetters, BEAN_ignoreInvocationExceptionsOnSetters, BEAN_ignorePropertiesWithoutSetters, BEAN_ignoreUnknownBeanProperties, BEAN_ignoreUnknownNullBeanProperties, BEAN_implClasses, BEAN_includeProperties, BEAN_locale, BEAN_mediaType, BEAN_notBeanClasses, BEAN_notBeanClasses_add, BEAN_notBeanClasses_remove, BEAN_notBeanPackages, BEAN_notBeanPackages_add, BEAN_notBeanPackages_remove, BEAN_pojoSwaps, BEAN_pojoSwaps_add, BEAN_pojoSwaps_remove, BEAN_propertyNamer, BEAN_sortProperties, BEAN_timeZone, BEAN_useInterfaceProxies, BEAN_useJavaBeanIntrospector, DEFAULT_SORTED
Modifier and Type | Method and Description |
---|---|
ObjectMap |
asMap()
Returns the properties defined on this bean context as a simple map for debugging purposes.
|
SerializerBuilder |
builder()
Creates a builder from this context object.
|
SerializerSessionArgs |
createDefaultSessionArgs()
Defines default session arguments used when calling the
Context.createSession() method. |
SerializerSession |
createSession()
Create a new bean session based on the properties defined on this context.
|
abstract SerializerSession |
createSession(SerializerSessionArgs args)
Create the session object used for actual serialization of objects.
|
MediaType[] |
getMediaTypes()
Returns the media types handled based on the value of the
accept parameter passed into the constructor. |
MediaType |
getResponseContentType()
Optional method that returns the response
Content-Type for this serializer if it is different from
the matched media type. |
boolean |
isWriterSerializer()
Returns
WriterSerializer . |
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 output)
Serializes a POJO to the specified output stream or writer.
|
_class, create, createBeanSession, createBeanSession, createDefaultBeanSessionArgs, createSession, createSession, dumpCacheStats, getBeanMeta, getBeanRegistry, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getExcludeProperties, getImplClassConstructor, getIncludeProperties, hasSameCache, isBean, isNotABean, newInstance, newInstance, newInstanceFromOuter, object, resolveClassMeta, string
equals, getArrayProperty, getArrayProperty, getBooleanProperty, getClassArrayProperty, getClassArrayProperty, getClassArrayProperty, getClassListProperty, getClassListProperty, getClassMapProperty, getClassMapProperty, getClassProperty, getClassSetProperty, getClassSetProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceArrayProperty, getInstanceProperty, getInstanceProperty, getInstanceProperty, getIntegerProperty, getListProperty, getListProperty, getLongProperty, getMapProperty, getProperty, getProperty, getPropertyKeys, getPropertyStore, getSetProperty, getSetProperty, getStringProperty, hashCode, toString
public static final String SERIALIZER_abridged
Boolean
When enabled, it is assumed that the parser knows the exact Java POJO type being parsed, and therefore top-level type information that might normally be included to determine the data type will not be serialized.
For example, when serializing a top-level POJO with a @Bean.typeName()
value, a
Note the differences between the following settings:
SERIALIZER_abridged
- Affects whether SERIALIZER_addBeanTypeProperties
- Affects whether
public static final String SERIALIZER_addBeanTypeProperties
Boolean
If
This is used to recreate the correct objects during parsing if the object types cannot be inferred.
For example, when serializing a Map<String,Object>
field where the bean class cannot be determined from
the type of the values.
Note the differences between the following settings:
SERIALIZER_abridged
- Affects whether SERIALIZER_addBeanTypeProperties
- Affects whether
public static final String SERIALIZER_detectRecursions
Boolean
Specifies that recursions should be checked for during serialization.
Recursions can occur when serializing models that aren't true trees but rather contain loops.
In general, unchecked recursions cause stack-overflow-errors.
These show up as ParseExceptions
with the message
The behavior when recursions are detected depends on the value for SERIALIZER_ignoreRecursions
.
For example, if a model contains the links A->B->C->A, then the JSON generated will look like
the following when
{A:{B:{C:
public static final String SERIALIZER_ignoreRecursions
Boolean
Used in conjunction with SERIALIZER_detectRecursions
.
Setting is ignored if
If
Otherwise, a SerializeException
is thrown with the message
public static final String SERIALIZER_initialDepth
Integer
0
The initial indentation level at the root.
Useful when constructing document fragments that need to be indented at a certain level.
public static final String SERIALIZER_listener
Class<? extends SerializerListener>
Class used to listen for errors and warnings that occur during serialization.
public static final String SERIALIZER_maxDepth
Integer
100
Abort serialization if specified depth is reached in the POJO tree.
If this depth is exceeded, an exception is thrown.
public static final String SERIALIZER_maxIndent
Integer
100
Specifies the maximum indentation level in the serialized document.
This setting does not apply to the MessagePack or RDF serializers.
public static final String SERIALIZER_quoteChar
String
This is the character used for quoting attributes and values.
This setting does not apply to the MessagePack or RDF serializers.
public static final String SERIALIZER_sortCollections
Boolean
Copies and sorts the contents of arrays and collections before serializing them.
Note that this introduces a performance penalty.
public static final String SERIALIZER_sortMaps
Boolean
Copies and sorts the contents of maps by their keys before serializing them.
Note that this introduces a performance penalty.
public static final String SERIALIZER_trimEmptyCollections
Boolean
If
Note that enabling this setting has the following effects on parsing:
public static final String SERIALIZER_trimEmptyMaps
Boolean
If
Note that enabling this setting has the following effects on parsing:
public static final String SERIALIZER_trimNullProperties
Boolean
If
Note that enabling this setting has the following effects on parsing:
public static final String SERIALIZER_trimStrings
Boolean
If String.trim()
before being serialized.
public static final String SERIALIZER_uriContext
String
(JSON object representing a UriContext
)
Bean used for resolution of URIs to absolute or root-relative form.
public static final String SERIALIZER_uriRelativity
String
(UriRelativity
)
Defines what relative URIs are relative to when serializing any of the following:
Possible values are:
UriRelativity.RESOURCE
- Relative URIs should be considered relative to the servlet URI.
UriRelativity.PATH_INFO
- Relative URIs should be considered relative to the request URI.
public static final String SERIALIZER_uriResolution
String
(UriResolution
)
Defines the resolution level for URIs when serializing any of the following:
Possible values are:
UriResolution.ABSOLUTE
- Resolve to an absolute URL (e.g. UriResolution.ROOT_RELATIVE
- Resolve to a root-relative URL (e.g. UriResolution.NONE
- Don't do any URL resolution.
public static final String SERIALIZER_useWhitespace
Boolean
If
This setting does not apply to the MessagePack serializer.
public SerializerBuilder builder()
Context
Builders are used to define new contexts (e.g. serializers, parsers) based on existing configurations.
builder
in class BeanContext
public boolean isWriterSerializer()
WriterSerializer
.WriterSerializer
.public abstract SerializerSession createSession(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 final SerializerSession createSession()
BeanContext
Use this method for creating sessions if you don't need to override any properties or locale/timezone currently set on this context.
createSession
in class BeanContext
public final SerializerSessionArgs createDefaultSessionArgs()
Context
Context.createSession()
method.createDefaultSessionArgs
in class BeanContext
public final void serialize(Object o, Object output) throws SerializeException
Equivalent to calling serializer.createSession().serialize(o, output);
o
- The object to serialize.output
- The output object.
Writer
OutputStream
- Output will be written as UTF-8 encoded stream.
File
- Output will be written as system-default encoded stream.
StringBuilder
- Output will be written to the specified string builder.
SerializeException
- If a problem occurred trying to convert the output.public 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 final MediaType[] getMediaTypes()
accept
parameter passed into the constructor.public final MediaType getResponseContentType()
Content-Type
for this serializer if it is different from
the matched media type.
This method is specified to override the content type for this serializer.
For example, the JsonSerializer.Simple
class returns that it handles media type
Accept
header values.
This method is typically meaningless if the serializer is being used stand-alone (i.e. outside of a REST server or client).
public ObjectMap asMap()
Context
asMap
in class BeanContext
Copyright © 2018 Apache. All rights reserved.