org.apache.juneau.serializer.Serializer Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.serializer;
import java.io.*;
import org.apache.juneau.*;
import org.apache.juneau.annotation.*;
import org.apache.juneau.http.*;
import org.apache.juneau.internal.*;
/**
* Parent class for all Juneau serializers.
*
* Description
*
* Base serializer class that serves as the parent class for all serializers.
*
*
* The purpose of this class is:
*
* - Maintain a read-only configuration state of a serializer.
*
- Create session objects used for serializing POJOs (i.e. {@link SerializerSession}).
*
- Provide convenience methods for serializing POJOs without having to construct session objects.
*
*
*
* Subclasses should extend directly from {@link OutputStreamSerializer} or {@link WriterSerializer} depending on
* whether it's a stream or character based serializer.
*/
public abstract class Serializer extends BeanTraverseContext {
//-------------------------------------------------------------------------------------------------------------------
// Configurable properties
//-------------------------------------------------------------------------------------------------------------------
private static final String PREFIX = "Serializer.";
/**
* Configuration property: Add "_type" properties when needed.
*
*
Property:
*
* - Name:
"Serializer.addBeanTypes.b"
* - Data type:
Boolean
* - Default:
false
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#addBeanTypes()}
*
- {@link SerializerBuilder#addBeanTypes(boolean)}
*
*
*
* Description:
*
* If true , then "_type" properties will be added to beans if their type cannot be inferred
* through reflection.
*
*
* 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:
*
* - {@link #SERIALIZER_addRootType} - Affects whether
'_type' is added to root node.
* - {@link #SERIALIZER_addBeanTypes} - Affects whether
'_type' is added to any nodes.
*
*
* Example:
*
* // Create a serializer that adds _type to nodes.
* WriterSerializer s = JsonSerializer
* .create ()
* .addBeanTypes()
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_addBeanTypes , true )
* .build();
*
* // A map of objects we want to serialize.
* @Bean (typeName="mybean" )
* public class MyBean {...}
*
* Map<String,Object> m = new HashMap<>();
* m.put("foo" , new MyBean());
*
* // Will contain '_type' attribute.
* String json = s.serialize(m);
*
*/
public static final String SERIALIZER_addBeanTypes = PREFIX + "addBeanTypes.b";
/**
* Configuration property: Add type attribute to root nodes.
*
* Property:
*
* - Name:
"Serializer.addRootType.b"
* - Data type:
Boolean
* - Default:
false
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#addRootType(boolean)}
*
- {@link SerializerBuilder#addRootType()}
*
*
*
* Description:
*
* When disabled, 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 {@link Bean#typeName() @Bean(typeName)} value, a
* '_type' attribute will only be added when this setting is enabled.
*
*
* Note the differences between the following settings:
*
* - {@link #SERIALIZER_addRootType} - Affects whether
'_type' is added to root node.
* - {@link #SERIALIZER_addBeanTypes} - Affects whether
'_type' is added to any nodes.
*
*
* Example:
*
* // Create a serializer that adds _type to root node.
* WriterSerializer s = JsonSerializer
* .create ()
* .addRootType()
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_addRootType , true )
* .build();
*
* // The bean we want to serialize.
* @Bean (typeName="mybean" )
* public class MyBean {...}
*
* // Will contain '_type' attribute.
* String json = s.serialize(new MyBean());
*
*/
public static final String SERIALIZER_addRootType = PREFIX + "addRootType.b";
/**
* Configuration property: Serializer listener.
*
* Property:
*
* - Name:
"Serializer.listener.c"
* - Data type:
Class<? extends SerializerListener>
* - Default:
null
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#listener(Class)}
*
*
*
* Description:
*
* Class used to listen for errors and warnings that occur during serialization.
*
*
Example:
*
* // Define our serializer listener.
* // Simply captures all errors.
* public class MySerializerListener extends SerializerListener {
*
* // A simple property to store our events.
* public List<String> events = new LinkedList<>();
*
* @Override
* public <T> void onError(SerializerSession session, Throwable t, String msg) {
* events .add(session.getLastLocation() + "," + msg + "," + t);
* }
* }
*
* // Create a serializer using our listener.
* WriterSerializer s = JsonSerializer.
* .create ()
* .listener(MySerializerListener.class )
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer.
* .create ()
* .set(SERIALIZER_listener , MySerializerListener.class )
* .build();
*
* // Create a session object.
* // Needed because listeners are created per-session.
* try (WriterSerializerSession ss = s.createSession()) {
*
* // Serialize a bean.
* String json = ss.serialize(new MyBean());
*
* // Get the listener.
* MySerializerListener l = ss.getListener(MySerializerListener.class );
*
* // Dump the results to the console.
* SimpleJsonSerializer.DEFAULT .println(l.events );
* }
*
*/
public static final String SERIALIZER_listener = PREFIX + "listener.c";
/**
* Configuration property: Sort arrays and collections alphabetically.
*
* Property:
*
* - Name:
"Serializer.sortCollections.b"
* - Data type:
Boolean
* - Default:
false
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#sortCollections(boolean)}
*
- {@link SerializerBuilder#sortCollections()}
*
*
*
* Description:
*
*
* Copies and sorts the contents of arrays and collections before serializing them.
*
*
* Note that this introduces a performance penalty.
*
*
Example:
*
* // Create a serializer that sorts arrays and collections before serialization.
* WriterSerializer s = JsonSerializer
* .create ()
* .sortCollections()
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_sortCollections , true )
* .build();
*
*/
public static final String SERIALIZER_sortCollections = PREFIX + "sortCollections.b";
/**
* Configuration property: Sort maps alphabetically.
*
* Property:
*
* - Name:
"Serializer.sortMaps.b"
* - Data type:
Boolean
* - Default:
false
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#sortMaps(boolean)}
*
- {@link SerializerBuilder#sortMaps()}
*
*
*
* Description:
*
*
* Copies and sorts the contents of maps by their keys before serializing them.
*
*
* Note that this introduces a performance penalty.
*
*
Example:
*
* // Create a serializer that sorts maps before serialization.
* WriterSerializer s = JsonSerializer
* .create ()
* .sortMaps()
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_sortMaps , true )
* .build();
*
*/
public static final String SERIALIZER_sortMaps = PREFIX + "sortMaps.b";
/**
* Configuration property: Trim empty lists and arrays.
*
* Property:
*
* - Name:
"Serializer.trimEmptyCollections.b"
* - Data type:
Boolean
* - Default:
false
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#trimEmptyCollections(boolean)}
*
- {@link SerializerBuilder#trimEmptyCollections()}
*
*
*
* Description:
*
*
* If true , empty lists and arrays will not be serialized.
*
*
* Note that enabling this setting has the following effects on parsing:
*
* -
* Map entries with empty list values will be lost.
*
-
* Bean properties with empty list values will not be set.
*
*
* Example:
*
* // Create a serializer that skips empty arrays and collections.
* WriterSerializer s = JsonSerializer
* .create ()
* .trimEmptyCollections()
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_trimEmptyCollections , true )
* .build();
*
*/
public static final String SERIALIZER_trimEmptyCollections = PREFIX + "trimEmptyCollections.b";
/**
* Configuration property: Trim empty maps.
*
* Property:
*
* - Name:
"Serializer.trimEmptyMaps.b"
* - Data type:
Boolean
* - Default:
false
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#trimEmptyMaps(boolean)}
*
- {@link SerializerBuilder#trimEmptyMaps()}
*
*
*
* Description:
*
* If true , empty map values will not be serialized to the output.
*
*
* Note that enabling this setting has the following effects on parsing:
*
* -
* Bean properties with empty map values will not be set.
*
*
* Example:
*
* // Create a serializer that skips empty maps.
* WriterSerializer s = JsonSerializer
* .create ()
* .trimEmptyMaps()
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_trimEmptyMaps , true )
* .build();
*
*/
public static final String SERIALIZER_trimEmptyMaps = PREFIX + "trimEmptyMaps.b";
/**
* Configuration property: Trim null bean property values.
*
* Property:
*
* - Name:
"Serializer.trimNullProperties.b"
* - Data type:
Boolean
* - Default:
true
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#trimNullProperties(boolean)}
*
*
*
* Description:
*
* If true , null bean values will not be serialized to the output.
*
*
* Note that enabling this setting has the following effects on parsing:
*
* -
* Map entries with
null values will be lost.
*
*
* Example:
*
* // Create a serializer that serializes null properties.
* WriterSerializer s = JsonSerializer
* .create ()
* .trimNullProperties(false )
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_trimNullProperties , false )
* .build();
*
*/
public static final String SERIALIZER_trimNullProperties = PREFIX + "trimNullProperties.b";
/**
* Configuration property: Trim strings.
*
* Property:
*
* - Name:
"Serializer.trimStrings.b"
* - Data type:
Boolean
* - Default:
false
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#trimStrings(boolean)}
*
- {@link SerializerBuilder#trimStrings()}
*
*
*
* Description:
*
* If true , string values will be trimmed of whitespace using {@link String#trim()} before being serialized.
*
*
Example:
*
* // Create a serializer that trims strings before serialization.
* WriterSerializer s = JsonSerializer
* .create ()
* .trimStrings()
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_trimStrings , true )
* .build();
*
* Map<String,String> m = new HashMap<>();
* m.put(" foo " , " bar " );
*
* // Produces "{foo:'bar'}"
* String json = SimpleJsonSerializer.DEFAULT .toString(m);
*
*/
public static final String SERIALIZER_trimStrings = PREFIX + "trimStrings.b";
/**
* Configuration property: URI context bean.
*
* Property:
*
* - Name:
"Serializer.uriContext.s"
* - Data type:
String
(JSON object representing a {@link UriContext})
* - Default:
"{}"
* - Session property:
true
* - Methods:
*
* - {@link SerializerBuilder#uriContext(UriContext)}
*
- {@link SerializerBuilder#uriContext(String)}
*
*
*
* Description:
*
* Bean used for resolution of URIs to absolute or root-relative form.
*
*
Example:
*
* // Our URI contextual information.
* String authority = "http://localhost:10000" ;
* String contextRoot = "/myContext" ;
* String servletPath = "/myServlet" ;
* String pathInfo = "/foo" ;
*
* // Create a UriContext object.
* UriContext uriContext = new UriContext(authority, contextRoot, servletPath, pathInfo);
*
* // Associate it with our serializer.
* WriterSerializer s = JsonSerializer
* .create ()
* .uriContext(uriContext)
* .build();
*
* // Same, but specify as a JSON string.
* WriterSerializer s = JsonSerializer
* .create ()
* .uriContext("{authority:'http://localhost:10000',contextRoot:'/myContext',servletPath:'/myServlet',pathInfo:'/foo'}" )
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_uriContext , uriContext)
* .build();
*
* // Same, but define it on the session args instead.
* SerializerSessionArgs sessionArgs = new SerializerSessionArgs().uriContext(uriContext);
* try (WriterSerializerSession session = s.createSession(sessionArgs)) {
* ...
* }
*
*/
public static final String SERIALIZER_uriContext = PREFIX + "uriContext.s";
/**
* Configuration property: URI relativity.
*
* Property:
*
* - Name:
"Serializer.uriRelativity.s"
* - Data type:
String
({@link UriRelativity})
* - Default:
"RESOURCE"
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#uriRelativity(UriRelativity)}
*
- {@link SerializerBuilder#uriRelativity(String)}
*
*
*
* Description:
*
* Defines what relative URIs are relative to when serializing any of the following:
*
* - {@link java.net.URI}
*
- {@link java.net.URL}
*
- Properties and classes annotated with {@link org.apache.juneau.annotation.URI @URI}
*
*
*
* Possible values are:
*
* - {@link UriRelativity#RESOURCE}
* - Relative URIs should be considered relative to the servlet URI.
*
- {@link UriRelativity#PATH_INFO}
* - Relative URIs should be considered relative to the request URI.
*
*
* Example:
*
* // Define a serializer that converts resource-relative URIs to absolute form.
* WriterSerializer s = JsonSerializer
* .create ()
* .uriContext("{authority:'http://localhost:10000',contextRoot:'/myContext',servletPath:'/myServlet',pathInfo:'/foo'}" )
* .uriResolution(ABSOLUTE )
* .uriRelativity(RESOURCE )
* .build();
*
*
* See Also:
*
* - {@doc juneau-marshall.URIs}
*
*/
public static final String SERIALIZER_uriRelativity = PREFIX + "uriRelativity.s";
/**
* Configuration property: URI resolution.
*
* Property:
*
* - Name:
"Serializer.uriResolution.s"
* - Data type:
String
({@link UriResolution})
* - Default:
"NONE"
* - Session property:
false
* - Methods:
*
* - {@link SerializerBuilder#uriResolution(UriResolution)}
*
- {@link SerializerBuilder#uriResolution(String)}
*
*
*
* Description:
*
* Defines the resolution level for URIs when serializing any of the following:
*
* - {@link java.net.URI}
*
- {@link java.net.URL}
*
- Properties and classes annotated with {@link org.apache.juneau.annotation.URI @URI}
*
*
*
* Possible values are:
*
* - {@link UriResolution#ABSOLUTE}
* - Resolve to an absolute URL (e.g.
"http://host:port/context-root/servlet-path/path-info" ).
* - {@link UriResolution#ROOT_RELATIVE}
* - Resolve to a root-relative URL (e.g.
"/context-root/servlet-path/path-info" ).
* - {@link UriResolution#NONE}
* - Don't do any URL resolution.
*
*
* Example:
*
* // Define a serializer that converts resource-relative URIs to absolute form.
* WriterSerializer s = JsonSerializer
* .create ()
* .uriContext("{authority:'http://localhost:10000',contextRoot:'/myContext',servletPath:'/myServlet',pathInfo:'/foo'}" )
* .uriResolution(ABSOLUTE )
* .uriRelativity(RESOURCE )
* .build();
*
*
* See Also:
*
* - {@doc juneau-marshall.URIs}
*
*/
public static final String SERIALIZER_uriResolution = PREFIX + "uriResolution.s";
/**
* Configuration property: Use whitespace.
*
* Property:
*
* - Name:
"Serializer.useWhitespace.b"
* - Data type:
Boolean
* - Default:
false
* - Session property:
true
* - Methods:
*
* - {@link SerializerBuilder#useWhitespace(boolean)}
*
- {@link SerializerBuilder#useWhitespace()}
*
- {@link SerializerBuilder#ws()}
*
- {@link SerializerSessionArgs#useWhitespace(Boolean)}
*
*
*
* Description:
*
* If true , whitespace is added to the output to improve readability.
*
*
Example:
*
* // Create a serializer with whitespace enabled.
* WriterSerializer s = JsonSerializer
* .create ()
* .ws()
* .build();
*
* // Same, but use property.
* WriterSerializer s = JsonSerializer
* .create ()
* .set(SERIALIZER_useWhitespace , true )
* .build();
*
* // Produces "\{\n\t'foo': 'bar'\n\}\n"
* String json = s.serialize(new MyBean());
*
*/
public static final String SERIALIZER_useWhitespace = PREFIX + "useWhitespace.b";
static final Serializer DEFAULT = new Serializer(PropertyStore.create().build(), "", "") {
@Override
public SerializerSession createSession(SerializerSessionArgs args) {
throw new NoSuchMethodError();
}
};
//-------------------------------------------------------------------------------------------------------------------
// Instance
//-------------------------------------------------------------------------------------------------------------------
private final boolean
addBeanTypes,
trimNullProperties,
trimEmptyCollections,
trimEmptyMaps,
trimStrings,
sortCollections,
sortMaps,
addRootType,
useWhitespace;
private final UriContext uriContext;
private final UriResolution uriResolution;
private final UriRelativity uriRelativity;
private final Class extends SerializerListener> listener;
private final MediaTypeRange[] accept;
private final MediaType[] accepts;
private final MediaType produces;
/**
* Constructor
*
* @param ps
* The property store containing all the settings for this object.
* @param produces
* The media type that this serializer produces.
* @param accept
* The accept media types that the serializer can handle.
*
* Can contain meta-characters per the media-type
specification of {@doc RFC2616.section14.1}
*
* If empty, then assumes the only media type supported is produces
.
*
* For example, if this serializer produces "application/json" but should handle media types of
* "application/json" and "text/json" , then the arguments should be:
*
* super (ps, "application/json" , "application/json,text/json" );
*
*
...or...
*
* super (ps, "application/json" , "*/json" );
*
*
* The accept value can also contain q-values.
*/
protected Serializer(PropertyStore ps, String produces, String accept) {
super(ps);
addBeanTypes = getBooleanProperty(SERIALIZER_addBeanTypes, false);
trimNullProperties = getBooleanProperty(SERIALIZER_trimNullProperties, true);
trimEmptyCollections = getBooleanProperty(SERIALIZER_trimEmptyCollections, false);
trimEmptyMaps = getBooleanProperty(SERIALIZER_trimEmptyMaps, false);
trimStrings = getBooleanProperty(SERIALIZER_trimStrings, false);
sortCollections = getBooleanProperty(SERIALIZER_sortCollections, false);
sortMaps = getBooleanProperty(SERIALIZER_sortMaps, false);
addRootType = getBooleanProperty(SERIALIZER_addRootType, false);
uriContext = getProperty(SERIALIZER_uriContext, UriContext.class, UriContext.DEFAULT);
uriResolution = getProperty(SERIALIZER_uriResolution, UriResolution.class, UriResolution.NONE);
uriRelativity = getProperty(SERIALIZER_uriRelativity, UriRelativity.class, UriRelativity.RESOURCE);
useWhitespace = getBooleanProperty(SERIALIZER_useWhitespace, false);
listener = getClassProperty(SERIALIZER_listener, SerializerListener.class, null);
this.produces = MediaType.forString(produces);
this.accept = accept == null ? MediaTypeRange.parse(produces) : MediaTypeRange.parse(accept);
this.accepts = accept == null ? new MediaType[] {this.produces} : MediaType.forStrings(StringUtils.split(accept, ','));
}
@Override /* Context */
public SerializerBuilder builder() {
return null;
}
//-----------------------------------------------------------------------------------------------------------------
// Abstract methods
//-----------------------------------------------------------------------------------------------------------------
/**
* Returns true if this serializer subclasses from {@link WriterSerializer}.
*
* @return true if this serializer subclasses from {@link WriterSerializer}.
*/
public boolean isWriterSerializer() {
return true;
}
/**
* Create the session object used for actual serialization of objects.
*
* @param 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.
* @return
* The new session object.
*/
public abstract SerializerSession createSession(SerializerSessionArgs args);
//-----------------------------------------------------------------------------------------------------------------
// Convenience methods
//-----------------------------------------------------------------------------------------------------------------
@Override /* Context */
public SerializerSession createSession() {
return createSession(createDefaultSessionArgs());
}
@Override /* Context */
public final SerializerSessionArgs createDefaultSessionArgs() {
return new SerializerSessionArgs().mediaType(getResponseContentType());
}
/**
* Serializes a POJO to the specified output stream or writer.
*
*
* Equivalent to calling serializer.createSession().serialize(o, output);
*
* @param o The object to serialize.
* @param output
* The output object.
*
Character-based serializers can handle the following output class types:
*
* - {@link Writer}
*
- {@link OutputStream} - Output will be written as UTF-8 encoded stream.
*
- {@link File} - Output will be written as system-default encoded stream.
*
- {@link StringBuilder} - Output will be written to the specified string builder.
*
*
Stream-based serializers can handle the following output class types:
*
* - {@link OutputStream}
*
- {@link File}
*
* @throws SerializeException If a problem occurred trying to convert the output.
*/
public final void serialize(Object o, Object output) throws SerializeException {
createSession().serialize(o, output);
}
/**
* Shortcut method for serializing objects directly to either a String
or byte []
* depending on the serializer type.
*
* @param o The object to serialize.
* @return
* The serialized object.
*
Character-based serializers will return a String
*
Stream-based serializers will return a byte []
* @throws SerializeException If a problem occurred trying to convert the output.
*/
public Object serialize(Object o) throws SerializeException {
return createSession().serialize(o);
}
/**
* Convenience method for serializing an object to a String.
*
*
* For writer-based serializers, this is identical to calling {@link #serialize(Object)}.
*
For stream-based serializers, this converts the returned byte array to a string based on
* the {@link OutputStreamSerializer#OSSERIALIZER_binaryFormat} setting.
*
* @param o The object to serialize.
* @return The output serialized to a string.
* @throws SerializeException If a problem occurred trying to convert the output.
*/
public final String serializeToString(Object o) throws SerializeException {
return createSession().serializeToString(o);
}
//-----------------------------------------------------------------------------------------------------------------
// Other methods
//-----------------------------------------------------------------------------------------------------------------
/**
* Returns the media types handled based on the value of the accept
parameter passed into the constructor.
*
*
* Note that the order of these ranges are from high to low q-value.
*
* @return The list of media types. Never null .
*/
public final MediaTypeRange[] getMediaTypeRanges() {
return accept;
}
/**
* Returns the first entry in the accept
parameter passed into the constructor.
*
*
* This signifies the 'primary' media type for this serializer.
*
* @return The media type. Never null .
*/
public final MediaType getPrimaryMediaType() {
return accepts[0];
}
/**
* Returns the media types handled based on the value of the accept
parameter passed into the constructor.
*
*
* The order of the media types are the same as those in the accept
parameter.
*
* @return The list of media types. Never null .
*/
public final MediaType[] getAcceptMediaTypes() {
return accepts;
}
/**
* Optional method that returns the response 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 {@link org.apache.juneau.json.SimpleJsonSerializer} class returns that it handles media type
* "text/json+simple" , but returns "text/json" as the actual content type.
* This allows clients to request specific 'flavors' of content using specialized 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).
*
* @return The response content type. If null , then the matched media type is used.
*/
public final MediaType getResponseContentType() {
return produces;
}
//-----------------------------------------------------------------------------------------------------------------
// Properties
//-----------------------------------------------------------------------------------------------------------------
/**
* Configuration property: Add "_type" properties when needed.
*
* @see #SERIALIZER_addBeanTypes
* @return
* true if "_type" properties added to beans if their type cannot be inferred
* through reflection.
*/
protected boolean isAddBeanTypes() {
return addBeanTypes;
}
/**
* Configuration property: Trim null bean property values.
*
* @see #SERIALIZER_trimNullProperties
* @return
* true if null bean values are not serialized to the output.
*/
protected final boolean isTrimNullProperties() {
return trimNullProperties;
}
/**
* Configuration property: Trim empty lists and arrays.
*
* @see #SERIALIZER_trimEmptyCollections
* @return
* true if empty lists and arrays are not serialized to the output.
*/
protected final boolean isTrimEmptyCollections() {
return trimEmptyCollections;
}
/**
* Configuration property: Trim empty maps.
*
* @see #SERIALIZER_trimEmptyMaps
* @return
* true if empty map values are not serialized to the output.
*/
protected final boolean isTrimEmptyMaps() {
return trimEmptyMaps;
}
/**
* Configuration property: Trim strings.
*
* @see #SERIALIZER_trimStrings
* @return
* true if string values will be trimmed of whitespace using {@link String#trim()} before being serialized.
*/
protected final boolean isTrimStrings() {
return trimStrings;
}
/**
* Configuration property: Sort arrays and collections alphabetically.
*
* @see #SERIALIZER_sortCollections
* @return
* true if arrays and collections are copied and sorted before serialization.
*/
protected final boolean isSortCollections() {
return sortCollections;
}
/**
* Configuration property: Sort maps alphabetically.
*
* @see #SERIALIZER_sortMaps
* @return
* true if maps are copied and sorted before serialization.
*/
protected final boolean isSortMaps() {
return sortMaps;
}
/**
* Configuration property: Add type attribute to root nodes.
*
* @see #SERIALIZER_addRootType
* @return
* true if type property should be added to root node.
*/
protected final boolean isAddRootType() {
return addRootType;
}
/**
* Configuration property: URI context bean.
*
* @see #SERIALIZER_uriContext
* @return
* Bean used for resolution of URIs to absolute or root-relative form.
*/
protected final UriContext getUriContext() {
return uriContext;
}
/**
* Configuration property: URI resolution.
*
* @see #SERIALIZER_uriResolution
* @return
* Defines the resolution level for URIs when serializing URIs.
*/
protected final UriResolution getUriResolution() {
return uriResolution;
}
/**
* Configuration property: URI relativity.
*
* @see #SERIALIZER_uriRelativity
* @return
* Defines what relative URIs are relative to when serializing any of the following:
*/
protected final UriRelativity getUriRelativity() {
return uriRelativity;
}
/**
* Configuration property: Trim strings.
*
* @see #SERIALIZER_trimStrings
* @return
* true if string values will be trimmed of whitespace using {@link String#trim()} before being serialized.
*/
protected final boolean isUseWhitespace() {
return useWhitespace;
}
/**
* Configuration property: Serializer listener.
*
* @see #SERIALIZER_listener
* @return
* Class used to listen for errors and warnings that occur during serialization.
*/
protected final Class extends SerializerListener> getListener() {
return listener;
}
@Override /* Context */
public ObjectMap asMap() {
return super.asMap()
.append("Serializer", new ObjectMap()
.append("addBeanTypes", addBeanTypes)
.append("trimNullProperties", trimNullProperties)
.append("trimEmptyCollections", trimEmptyCollections)
.append("trimEmptyMaps", trimEmptyMaps)
.append("trimStrings", trimStrings)
.append("sortCollections", sortCollections)
.append("sortMaps", sortMaps)
.append("addRootType", addRootType)
.append("uriContext", uriContext)
.append("uriResolution", uriResolution)
.append("uriRelativity", uriRelativity)
.append("listener", listener)
);
}
}