Class SerializerSession
- Direct Known Subclasses:
- OutputStreamSerializerSession,- WriterSerializerSession
Serializer.
 Used by serializers for the following purposes:
- Keeping track of how deep it is in a model for indentation purposes.
- Ensuring infinite loops don't occur by setting a limit on how deep to traverse a model.
- Ensuring infinite loops don't occur from loops in the model (when detectRecursions is enabled.
- Allowing serializer properties to be overridden on method calls.
Notes:
- This class is not thread safe and is typically discarded after one use.
See Also:
- 
Nested Class SummaryNested Classes
- 
Field SummaryFields inherited from class org.apache.juneau.BeanTraverseSessionindent
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprotectedConstructor.
- 
Method SummaryModifier and TypeMethodDescriptionaddVarBean(Class<T> c, T value) Adds a session object to theVarResolverSessionin this session.final booleancanIgnoreValue(ClassMeta<?> cm, String attrName, Object value) Returnstrue if the specified value should not be serialized.static SerializerSession.Buildercreate(Serializer ctx) Creates a new builder for this object.protected static final BeanPropertyValuecreateBeanTypeNameProperty(BeanMap<?> m, String typeName) Create a "_type" property that contains the dictionary name of the bean.protected VarResolverSessionAdds a session object to theVarResolverSessionin this session.protected SerializerPipecreatePipe(Object output) Wraps the specified input object into aParserPipeobject so that it can be easily converted into a stream or reader.protected voiddoSerialize(SerializerPipe pipe, Object o) Serializes a POJO to the specified pipe.final <E> voidforEachEntry(Collection<E> c, Consumer<E> consumer) Consumes each entry in the list.final <K,V> void forEachEntry(Map<K, V> m, Consumer<Map.Entry<K, V>> consumer) Consumes each map entry in the map.protected final Objectgeneralize(Object o, ClassMeta<?> type) Generalize the specified object if a POJO swap is associated with it.protected final StringgetBeanTypeName(SerializerSession session, ClassMeta<?> eType, ClassMeta<?> aType, BeanPropertyMeta pMeta) Resolves the dictionary name for the actual type.protected final ClassMeta<?>Returns the parser-side expected type for the object.protected final MethodReturns the Java method that invoked this serializer.Returns the listener associated with this session.<T extends SerializerListener>
 TgetListener(Class<T> c) Returns the listener associated with this session.Optional method that specifies HTTP request headers for this serializer.final HttpPartSchemaHTTP part schema of object being serialized.protected final UriContextURI context bean.protected final UriRelativityURI relativity.protected final UriResolutionURI resolution.protected final UriResolverReturns the URI resolver.Returns the variable resolver session.protected static <T extends Throwable>
 voidhandleThrown(T causedBy) Converts the specified throwable to either aRuntimeExceptionorSerializeException.protected booleanAdd"_type" properties when needed.protected final booleanAdd type attribute to root nodes.protected final booleanDon't trim null bean property values.protected final booleanSort arrays and collections alphabetically.protected final booleanSort maps alphabetically.protected final booleanTrim empty lists and arrays.protected final booleanTrim empty maps.protected booleanTrim strings.booleanReturnstrue if this serializer subclasses fromWriterSerializer.protected final voidSpecialized warning when an exception is thrown while executing a bean getter.protected voidLogs a warning message.protected JsonMapReturns the properties on this bean as a map for debugging.protected final ClassMeta<?>Same asBeanTraverseSession.push(String, Object, ClassMeta)but wrapsBeanRecursionExceptioninsideSerializeException.protected final StringrelativizeUri(Object relativeTo, Object uri) Opposite ofresolveUri(Object).Resolves any variables in the specified string.final StringresolveUri(Object uri) Converts a String to an absolute URI based on theUriContexton this session.Shortcut method for serializing objects directly to either aString orbyte []final voidSerialize the specified object using the specified session.Shortcut method for serializing an object to a String.final <E> Collection<E>sort(Collection<E> c) Sorts the specified collection ifisSortCollections()returnstrue .final <E> List<E>Sorts the specified collection ifisSortCollections()returnstrue .final <K,V> Map<K, V> Sorts the specified map ifisSortMaps()returnstrue .protected Objectswap(ObjectSwap swap, Object o) Invokes the specified swap on the specified object if the swap is not null.Converts the contents of the specified object array to a list.final StringConverts the specified object to aString .final StringTrims the specified string ifisTrimStrings()returnstrue .Methods inherited from class org.apache.juneau.BeanTraverseSessiongetInitialDepth, getLastLocation, getMaxDepth, getOptionalType, getOptionalValue, getStack, isDetectRecursions, isIgnoreRecursions, isOptional, isRoot, pop, push, setCurrentClass, setCurrentProperty, willExceedDepth, willRecurseMethods inherited from class org.apache.juneau.BeanSession_class, addWarning, convertToMemberType, convertToMemberType, convertToType, convertToType, convertToType, create, getArgsClassMeta, getBeanClassVisibility, getBeanConstructorVisibility, getBeanDictionary, getBeanFieldVisibility, getBeanMeta, getBeanMethodVisibility, getBeanRegistry, getBeanTypePropertyName, getBeanTypePropertyName, getClassMeta, getClassMeta, getClassMetaForObject, getClassMetaForObject, getLocale, getMediaType, getNamePropertyName, getNotBeanClasses, getNotBeanPackagesNames, getNotBeanPackagesPrefixes, getPropertyNamer, getSwaps, getTimeZone, getTimeZoneId, isBean, isBean, isBeanMapPutReturnsOldValue, isBeansRequireDefaultConstructor, isBeansRequireSerializable, isBeansRequireSettersForGetters, isBeansRequireSomeProperties, isFindFluentSetters, isIgnoreInvocationExceptionsOnGetters, isIgnoreInvocationExceptionsOnSetters, isIgnoreMissingSetters, isIgnoreUnknownBeanProperties, isIgnoreUnknownNullBeanProperties, isSortProperties, isUseEnumNames, isUseInterfaceProxies, isUseJavaBeanIntrospector, newBean, newBean, newBeanMap, newBeanMap, newGenericMap, object, string, toArray, toBeanMap, toBeanMap, toBeanMapMethods inherited from class org.apache.juneau.ContextSessioncheckForWarnings, getContext, getSessionProperties, getWarnings, isDebug, toString
- 
Constructor Details- 
SerializerSessionConstructor.- Parameters:
- builder- The builder for this object.
 
 
- 
- 
Method Details- 
createCreates a new builder for this object.- Parameters:
- ctx- The context creating this session.
- Returns:
- A new builder.
 
- 
addVarBeanAdds a session object to theVarResolverSessionin this session.- Type Parameters:
- T- The bean type.
- Parameters:
- c- The bean type being added.
- value- The bean being added.
- Returns:
- This object.
 
- 
createDefaultVarResolverSessionAdds a session object to theVarResolverSessionin this session.- Returns:
- This object.
 
- 
getVarResolverReturns the variable resolver session.- Returns:
- The variable resolver session.
 
- 
getSchemaHTTP part schema of object being serialized.- Returns:
- HTTP part schema of object being serialized, or null if not specified.
 
- 
doSerializeSerializes a POJO to the specified pipe.This method should NOT close the context object. The default implementation of this method simply calls Serializer.doSerialize(SerializerSession,SerializerPipe,Object).- Parameters:
- pipe- Where to send the output from the serializer.
- o- The object to serialize.
- Throws:
- IOException- Thrown by underlying stream.
- SerializeException- Problem occurred trying to serialize object.
 
- 
serializeShortcut method for serializing objects directly to either aString orbyte []- Parameters:
- o- The object to serialize.
- Returns:
- The serialized object.
   
 Character-based serializers will return aString .
 Stream-based serializers will return abyte []
- Throws:
- SerializeException- If a problem occurred trying to convert the output.
 
- 
serializeToStringShortcut method for serializing an object to a String.- Parameters:
- o- The object to serialize.
- Returns:
- The serialized object.
   
 Character-based serializers will return aString 
 Stream-based serializers will return abyte []OutputStreamSerializer.Builder.binaryFormat(BinaryFormat)setting.
- Throws:
- SerializeException- If a problem occurred trying to convert the output.
 
- 
isWriterSerializerReturnstrue if this serializer subclasses fromWriterSerializer.- Returns:
- true if this serializer subclasses from- WriterSerializer.
 
- 
createPipeWraps the specified input object into aParserPipeobject so that it can be easily converted into a stream or reader.- Parameters:
- output- The output location.
 For character-based serializers, this can be any of the following types:- Writer
- OutputStream- Output will be written as UTF-8 encoded stream.
- File- Output will be written as system-default encoded stream.
- StringBuilder
 
 For byte-based serializers, this can be any of the following types:
- Returns:
- A new ParserPipewrapper around the specified input object.
 
- 
serializeSerialize the specified object using the specified session.- Parameters:
- out- Where to send the output from the serializer.
- o- The object to serialize.
- Throws:
- SerializeException- If a problem occurred trying to convert the output.
- IOException- Thrown by the underlying stream.
 
- 
getJavaMethodReturns the Java method that invoked this serializer.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. - Returns:
- The Java method that invoked this serializer.
 
- 
getUriResolverReturns the URI resolver.- Returns:
- The URI resolver.
 
- 
onBeanGetterExceptionprotected final void onBeanGetterException(BeanPropertyMeta p, Throwable t) throws SerializeException Specialized warning when an exception is thrown while executing a bean getter.- Parameters:
- p- The bean map entry representing the bean property.
- t- The throwable that the bean getter threw.
- Throws:
- SerializeException- Thrown if ignoreInvocationExceptionOnGetters is false.
 
- 
onErrorLogs a warning message.- Overrides:
- onErrorin class- BeanTraverseSession
- Parameters:
- t- The throwable that was thrown (if there was one).
- msg- The warning message.
- args- Optional- MessageFormat-style arguments.
 
- 
trimTrims the specified string ifisTrimStrings()returnstrue .- Parameters:
- o- The input string to trim.
- Returns:
- The trimmed string, or null if the input wasnull .
 
- 
generalizeGeneralize the specified object if a POJO swap is associated with it.- Parameters:
- o- The object to generalize.
- type- The type of object.
- Returns:
- The generalized object, or null if the object isnull .
- Throws:
- SerializeException- If a problem occurred trying to convert the output.
 
- 
canIgnoreValuepublic final boolean canIgnoreValue(ClassMeta<?> cm, String attrName, Object value) throws SerializeException Returnstrue if the specified value should not be serialized.- Parameters:
- cm- The class type of the object being serialized.
- attrName- The bean attribute name, or- null if this isn't a bean attribute.
- value- The object being serialized.
- Returns:
- true if the specified value should not be serialized.
- Throws:
- SerializeException- If recursion occurred.
 
- 
sortSorts the specified map ifisSortMaps()returnstrue .- Type Parameters:
- K- The key type.
- V- The value type.
- Parameters:
- m- The map being sorted.
- Returns:
- A new sorted TreeMap.
 
- 
forEachEntryConsumes each map entry in the map.- Type Parameters:
- K- The key type.
- V- The value type.
- Parameters:
- m- The map being consumed.
- consumer- The map entry consumer.
 
- 
sortSorts the specified collection ifisSortCollections()returnstrue .- Type Parameters:
- E- The element type.
- Parameters:
- c- The collection being sorted.
- Returns:
- A new sorted TreeSet.
 
- 
forEachEntryConsumes each entry in the list.- Type Parameters:
- E- The element type.
- Parameters:
- c- The collection being sorted.
- consumer- The entry consumer.
 
- 
sortSorts the specified collection ifisSortCollections()returnstrue .- Type Parameters:
- E- The element type.
- Parameters:
- c- The collection being sorted.
- Returns:
- A new sorted TreeSet.
 
- 
toListConverts the contents of the specified object array to a list.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 typeisint [][]listwill have entries of typeint []- Parameters:
- type- The type of array.
- array- The array being converted.
- Returns:
- The array as a list.
 
- 
resolveUriConverts a String to an absolute URI based on theUriContexton this session.- Parameters:
- uri- The input URI. Can be any of the following: URI can be any of the following forms:- "foo://foo" - Absolute URI.
- "/foo" - Root-relative URI.
- "/" - Root URI.
- "context:/foo" - Context-root-relative URI.
- "context:/" - Context-root URI.
- "servlet:/foo" - Servlet-path-relative URI.
- "servlet:/" - Servlet-path URI.
- "request:/foo" - Request-path-relative URI.
- "request:/" - Request-path URI.
- "foo" - Path-info-relative URI.
- "" - Path-info URI.
 
- Returns:
- The resolved URI.
 
- 
relativizeUriOpposite ofresolveUri(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: - "foo://foo" - Absolute URI.
- "/foo" - Root-relative URI.
- "/" - Root URI.
- "context:/foo" - Context-root-relative URI.
- "context:/" - Context-root URI.
- "servlet:/foo" - Servlet-path-relative URI.
- "servlet:/" - Servlet-path URI.
- "request:/foo" - Request-path-relative URI.
- "request:/" - Request-path URI.
- "foo" - Path-info-relative URI.
- "" - Path-info URI.
 - Parameters:
- relativeTo- The URI to relativize against.
- uri- The URI to relativize.
- Returns:
- The relativized URI.
 
- 
toStringConverts the specified object to aString .Also has the following effects: - Class object is converted to a readable name. See- ClassInfo.getFullName().
- Whitespace is trimmed if the trim-strings setting is enabled.
 - Parameters:
- o- The object to convert to a- String .
- Returns:
- The object converted to a String, or null if the input wasnull .
 
- 
createBeanTypeNamePropertyCreate a "_type" property that contains the dictionary name of the bean.- Parameters:
- m- The bean map to create a class property on.
- typeName- The type name of the bean.
- Returns:
- A new bean property value.
 
- 
getBeanTypeNameprotected final String getBeanTypeName(SerializerSession session, ClassMeta<?> eType, ClassMeta<?> aType, BeanPropertyMeta pMeta) Resolves the dictionary name for the actual type.- Parameters:
- session- The current serializer session.
- eType- The expected type of the bean property.
- aType- The actual type of the bean property.
- pMeta- The current bean property being serialized.
- Returns:
- The bean dictionary name, or null if a name could not be found.
 
- 
getExpectedRootTypeReturns the parser-side expected type for the object.The return value depends on the Serializer.Builder.addRootType()setting. When disabled, the parser already knows the Java POJO type being parsed, so there is no reason to add"_type" attributes to the root-level object.- Parameters:
- o- The object to get the expected type on.
- Returns:
- The expected type.
 
- 
getResponseHeadersOptional method that specifies HTTP request headers for this serializer.For example, SoapXmlSerializerneeds to set aSOAPAction header.This method is typically meaningless if the serializer is being used stand-alone (i.e. outside of a REST server or client). The default implementation of this method simply calls Serializer.getResponseHeaders(SerializerSession).- Returns:
- The HTTP headers to set on HTTP requests.
   Never null .
 
- 
getListenerReturns the listener associated with this session.- Type Parameters:
- T- The listener type.
- Parameters:
- c- The listener class to cast to.
- Returns:
- The listener associated with this session, or null if there is no listener.
 
- 
resolveResolves any variables in the specified string.- Parameters:
- string- The string to resolve values in.
- Returns:
- The string with variables resolved.
 
- 
push2protected final ClassMeta<?> push2(String attrName, Object o, ClassMeta<?> eType) throws SerializeException Same asBeanTraverseSession.push(String, Object, ClassMeta)but wrapsBeanRecursionExceptioninsideSerializeException.- Parameters:
- attrName- The attribute name.
- o- The current object being traversed.
- eType- The expected class type.
- Returns:
- The ClassMetaof the object so thatinstanceof operations only need to be performed once (since they can be expensive).
- Throws:
- SerializeException- If recursion occurred.
 
- 
swapInvokes the specified swap on the specified object if the swap is not null.- Parameters:
- swap- The swap to invoke. Can be- null .
- o- The input object.
- Returns:
- The swapped object.
- Throws:
- SerializeException- If swap method threw an exception.
 
- 
isAddBeanTypesAdd"_type" properties when needed.- Returns:
- true if- "_type" properties added to beans if their type cannot be inferred through reflection.
- See Also:
 
- 
isAddRootTypeAdd type attribute to root nodes.- Returns:
- true if type property should be added to root node.
- See Also:
 
- 
getListenerReturns the listener associated with this session.- Returns:
- The listener associated with this session, or null if there is no listener.
 
- 
isSortCollectionsSort arrays and collections alphabetically.- Returns:
- true if arrays and collections are copied and sorted before serialization.
- See Also:
 
- 
isSortMapsSort maps alphabetically.- Returns:
- true if maps are copied and sorted before serialization.
- See Also:
 
- 
isTrimEmptyCollectionsTrim empty lists and arrays.- Returns:
- true if empty lists and arrays are not serialized to the output.
- See Also:
 
- 
isTrimEmptyMapsTrim empty maps.- Returns:
- true if empty map values are not serialized to the output.
- See Also:
 
- 
isKeepNullPropertiesDon't trim null bean property values.- Returns:
- true if null bean values are serialized to the output.
- See Also:
 
- 
isTrimStringsTrim strings.- Returns:
- true if string values will be trimmed of whitespace using- String.trim()before being serialized.
- See Also:
 
- 
getUriContextURI context bean.- Returns:
- Bean used for resolution of URIs to absolute or root-relative form.
- See Also:
 
- 
getUriRelativityURI relativity.- Returns:
- Defines what relative URIs are relative to when serializing any of the following:
- See Also:
 
- 
getUriResolutionURI resolution.- Returns:
- Defines the resolution level for URIs when serializing URIs.
- See Also:
 
- 
handleThrownConverts the specified throwable to either aRuntimeExceptionorSerializeException.- Type Parameters:
- T- The throwable type.
- Parameters:
- causedBy- The exception to cast or wrap.
 
- 
propertiesDescription copied from class:ContextSessionReturns the properties on this bean as a map for debugging.- Overrides:
- propertiesin class- BeanTraverseSession
- Returns:
- The properties on this bean as a map for debugging.
 
 
-