Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.fasterxml.jackson.databind.SerializerProvider Maven / Gradle / Ivy
package com.fasterxml.jackson.databind;
import java.io.IOException;
import java.text.DateFormat;
import java.util.*;
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.ContextualDeserializer;
import com.fasterxml.jackson.databind.introspect.Annotated;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.*;
import com.fasterxml.jackson.databind.ser.impl.*;
import com.fasterxml.jackson.databind.ser.std.NullSerializer;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.RootNameLookup;
public abstract class SerializerProvider
extends DatabindContext
{
@Deprecated
protected final static JavaType TYPE_OBJECT = TypeFactory.defaultInstance().uncheckedSimpleType(Object.class);
protected final static boolean CACHE_UNKNOWN_MAPPINGS = false ;
public final static JsonSerializer DEFAULT_NULL_KEY_SERIALIZER =
new FailingSerializer("Null key for a Map not allowed in JSON (use a converting NullKeySerializer?)" );
protected final static JsonSerializer DEFAULT_UNKNOWN_SERIALIZER = new UnknownSerializer();
final protected SerializationConfig _config;
final protected Class _serializationView;
final protected SerializerFactory _serializerFactory;
final protected SerializerCache _serializerCache;
final protected RootNameLookup _rootNames;
protected transient ContextAttributes _attributes;
protected JsonSerializer _unknownTypeSerializer = DEFAULT_UNKNOWN_SERIALIZER;
protected JsonSerializer _keySerializer;
protected JsonSerializer _nullValueSerializer = NullSerializer.instance;
protected JsonSerializer _nullKeySerializer = DEFAULT_NULL_KEY_SERIALIZER;
protected final ReadOnlyClassToSerializerMap _knownSerializers;
protected DateFormat _dateFormat;
protected final boolean _stdNullValueSerializer;
public SerializerProvider ()
{
_config = null ;
_serializerFactory = null ;
_serializerCache = new SerializerCache();
_knownSerializers = null ;
_rootNames = new RootNameLookup();
_serializationView = null ;
_attributes = null ;
_stdNullValueSerializer = true ;
}
protected SerializerProvider (SerializerProvider src,
SerializationConfig config, SerializerFactory f)
{
if (config == null ) {
throw new NullPointerException();
}
_serializerFactory = f;
_config = config;
_serializerCache = src._serializerCache;
_unknownTypeSerializer = src._unknownTypeSerializer;
_keySerializer = src._keySerializer;
_nullValueSerializer = src._nullValueSerializer;
_stdNullValueSerializer = (_nullValueSerializer == DEFAULT_NULL_KEY_SERIALIZER);
_nullKeySerializer = src._nullKeySerializer;
_rootNames = src._rootNames;
_knownSerializers = _serializerCache.getReadOnlyLookupMap();
_serializationView = config.getActiveView();
_attributes = config.getAttributes();
}
public void setDefaultKeySerializer (JsonSerializer ks)
{
if (ks == null ) {
throw new IllegalArgumentException("Can not pass null JsonSerializer" );
}
_keySerializer = ks;
}
public void setNullValueSerializer (JsonSerializer nvs)
{
if (nvs == null ) {
throw new IllegalArgumentException("Can not pass null JsonSerializer" );
}
_nullValueSerializer = nvs;
}
public void setNullKeySerializer (JsonSerializer nks)
{
if (nks == null ) {
throw new IllegalArgumentException("Can not pass null JsonSerializer" );
}
_nullKeySerializer = nks;
}
@Override
public final SerializationConfig getConfig () { return _config; }
@Override
public final AnnotationIntrospector getAnnotationIntrospector () {
return _config.getAnnotationIntrospector();
}
@Override
public final TypeFactory getTypeFactory () {
return _config.getTypeFactory();
}
@Override
public final Class getActiveView () { return _serializationView; }
@Deprecated
public final Class getSerializationView () { return _serializationView; }
@Override
public Object getAttribute (Object key) {
return _attributes.getAttribute(key);
}
@Override
public SerializerProvider setAttribute (Object key, Object value)
{
_attributes = _attributes.withPerCallAttribute(key, value);
return this ;
}
public final boolean isEnabled (SerializationFeature feature) {
return _config.isEnabled(feature);
}
public final boolean hasSerializationFeatures (int featureMask) {
return _config.hasSerializationFeatures(featureMask);
}
public final FilterProvider getFilterProvider () {
return _config.getFilterProvider();
}
public Locale getLocale () {
return _config.getLocale();
}
public TimeZone getTimeZone () {
return _config.getTimeZone();
}
public abstract WritableObjectId findObjectId (Object forPojo,
ObjectIdGenerator generatorType) ;
@SuppressWarnings ("unchecked" )
public JsonSerializer findValueSerializer (Class valueType,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer ser = _knownSerializers.untypedValueSerializer(valueType);
if (ser == null ) {
ser = _serializerCache.untypedValueSerializer(valueType);
if (ser == null ) {
ser = _serializerCache.untypedValueSerializer(_config.constructType(valueType));
if (ser == null ) {
ser = _createAndCacheUntypedSerializer(valueType);
if (ser == null ) {
ser = getUnknownTypeSerializer(valueType);
if (CACHE_UNKNOWN_MAPPINGS) {
_serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this );
}
return ser;
}
}
}
}
return (JsonSerializer) handleSecondaryContextualization(ser, property);
}
@SuppressWarnings ("unchecked" )
public JsonSerializer findValueSerializer (JavaType valueType, BeanProperty property)
throws JsonMappingException
{
JsonSerializer ser = _knownSerializers.untypedValueSerializer(valueType);
if (ser == null ) {
ser = _serializerCache.untypedValueSerializer(valueType);
if (ser == null ) {
ser = _createAndCacheUntypedSerializer(valueType);
if (ser == null ) {
ser = getUnknownTypeSerializer(valueType.getRawClass());
if (CACHE_UNKNOWN_MAPPINGS) {
_serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this );
}
return ser;
}
}
}
return (JsonSerializer) handleSecondaryContextualization(ser, property);
}
@SuppressWarnings ("unchecked" )
public JsonSerializer findPrimaryPropertySerializer (JavaType valueType, BeanProperty property)
throws JsonMappingException
{
JsonSerializer ser = _knownSerializers.untypedValueSerializer(valueType);
if (ser == null ) {
ser = _serializerCache.untypedValueSerializer(valueType);
if (ser == null ) {
ser = _createAndCacheUntypedSerializer(valueType);
if (ser == null ) {
ser = getUnknownTypeSerializer(valueType.getRawClass());
if (CACHE_UNKNOWN_MAPPINGS) {
_serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this );
}
return ser;
}
}
}
return (JsonSerializer) handlePrimaryContextualization(ser, property);
}
@SuppressWarnings ("unchecked" )
public JsonSerializer findPrimaryPropertySerializer (Class valueType,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer ser = _knownSerializers.untypedValueSerializer(valueType);
if (ser == null ) {
ser = _serializerCache.untypedValueSerializer(valueType);
if (ser == null ) {
ser = _serializerCache.untypedValueSerializer(_config.constructType(valueType));
if (ser == null ) {
ser = _createAndCacheUntypedSerializer(valueType);
if (ser == null ) {
ser = getUnknownTypeSerializer(valueType);
if (CACHE_UNKNOWN_MAPPINGS) {
_serializerCache.addAndResolveNonTypedSerializer(valueType, ser, this );
}
return ser;
}
}
}
}
return (JsonSerializer) handlePrimaryContextualization(ser, property);
}
public JsonSerializer findTypedValueSerializer (Class valueType,
boolean cache, BeanProperty property)
throws JsonMappingException
{
JsonSerializer ser = _knownSerializers.typedValueSerializer(valueType);
if (ser != null ) {
return ser;
}
ser = _serializerCache.typedValueSerializer(valueType);
if (ser != null ) {
return ser;
}
ser = findValueSerializer(valueType, property);
TypeSerializer typeSer = _serializerFactory.createTypeSerializer(_config,
_config.constructType(valueType));
if (typeSer != null ) {
typeSer = typeSer.forProperty(property);
ser = new TypeWrappedSerializer(typeSer, ser);
}
if (cache) {
_serializerCache.addTypedSerializer(valueType, ser);
}
return ser;
}
public JsonSerializer findTypedValueSerializer (JavaType valueType, boolean cache,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer ser = _knownSerializers.typedValueSerializer(valueType);
if (ser != null ) {
return ser;
}
ser = _serializerCache.typedValueSerializer(valueType);
if (ser != null ) {
return ser;
}
ser = findValueSerializer(valueType, property);
TypeSerializer typeSer = _serializerFactory.createTypeSerializer(_config, valueType);
if (typeSer != null ) {
typeSer = typeSer.forProperty(property);
ser = new TypeWrappedSerializer(typeSer, ser);
}
if (cache) {
_serializerCache.addTypedSerializer(valueType, ser);
}
return ser;
}
public JsonSerializer findKeySerializer (JavaType keyType,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer ser = _serializerFactory.createKeySerializer(_config, keyType, _keySerializer);
return _handleContextualResolvable(ser, property);
}
public JsonSerializer getDefaultNullKeySerializer () {
return _nullKeySerializer;
}
public JsonSerializer getDefaultNullValueSerializer () {
return _nullValueSerializer;
}
public JsonSerializer findNullKeySerializer (JavaType serializationType,
BeanProperty property)
throws JsonMappingException
{
return _nullKeySerializer;
}
public JsonSerializer findNullValueSerializer (BeanProperty property)
throws JsonMappingException {
return _nullValueSerializer;
}
public JsonSerializer getUnknownTypeSerializer (Class unknownType) {
return _unknownTypeSerializer;
}
public abstract JsonSerializer serializerInstance (Annotated annotated,
Object serDef)
throws JsonMappingException ;
@Deprecated
public JsonSerializer handleContextualization (JsonSerializer ser,
BeanProperty property)
throws JsonMappingException
{
return handleSecondaryContextualization(ser, property);
}
public JsonSerializer handlePrimaryContextualization (JsonSerializer ser,
BeanProperty property)
throws JsonMappingException
{
if (ser != null ) {
if (ser instanceof ContextualSerializer) {
ser = ((ContextualSerializer) ser).createContextual(this , property);
}
}
return ser;
}
public JsonSerializer handleSecondaryContextualization (JsonSerializer ser,
BeanProperty property)
throws JsonMappingException
{
if (ser != null ) {
if (ser instanceof ContextualSerializer) {
ser = ((ContextualSerializer) ser).createContextual(this , property);
}
}
return ser;
}
public final void defaultSerializeValue (Object value, JsonGenerator jgen)
throws IOException, JsonProcessingException
{
if (value == null ) {
if (_stdNullValueSerializer) {
jgen.writeNull();
} else {
_nullValueSerializer.serialize(null , jgen, this );
}
} else {
Class cls = value.getClass();
findTypedValueSerializer(cls, true , null ).serialize(value, jgen, this );
}
}
public final void defaultSerializeField (String fieldName, Object value, JsonGenerator jgen)
throws IOException, JsonProcessingException
{
jgen.writeFieldName(fieldName);
if (value == null ) {
if (_stdNullValueSerializer) {
jgen.writeNull();
} else {
_nullValueSerializer.serialize(null , jgen, this );
}
} else {
Class cls = value.getClass();
findTypedValueSerializer(cls, true , null ).serialize(value, jgen, this );
}
}
public final void defaultSerializeDateValue (long timestamp, JsonGenerator jgen)
throws IOException, JsonProcessingException
{
if (isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
jgen.writeNumber(timestamp);
} else {
jgen.writeString(_dateFormat().format(new Date(timestamp)));
}
}
public final void defaultSerializeDateValue (Date date, JsonGenerator jgen)
throws IOException, JsonProcessingException
{
if (isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)) {
jgen.writeNumber(date.getTime());
} else {
jgen.writeString(_dateFormat().format(date));
}
}
public void defaultSerializeDateKey (long timestamp, JsonGenerator jgen)
throws IOException, JsonProcessingException
{
if (isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
jgen.writeFieldName(String.valueOf(timestamp));
} else {
jgen.writeFieldName(_dateFormat().format(new Date(timestamp)));
}
}
public void defaultSerializeDateKey (Date date, JsonGenerator jgen)
throws IOException, JsonProcessingException
{
if (isEnabled(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS)) {
jgen.writeFieldName(String.valueOf(date.getTime()));
} else {
jgen.writeFieldName(_dateFormat().format(date));
}
}
public final void defaultSerializeNull (JsonGenerator jgen)
throws IOException, JsonProcessingException
{
if (_stdNullValueSerializer) {
jgen.writeNull();
} else {
_nullValueSerializer.serialize(null , jgen, this );
}
}
protected void _reportIncompatibleRootType (Object value, JavaType rootType)
throws IOException, JsonProcessingException
{
if (rootType.isPrimitive()) {
Class wrapperType = ClassUtil.wrapperType(rootType.getRawClass());
if (wrapperType.isAssignableFrom(value.getClass())) {
return ;
}
}
throw new JsonMappingException("Incompatible types: declared root type (" +rootType+") vs "
+value.getClass().getName());
}
protected JsonSerializer _findExplicitUntypedSerializer (Class runtimeType)
throws JsonMappingException
{
JsonSerializer ser = _knownSerializers.untypedValueSerializer(runtimeType);
if (ser != null ) {
return ser;
}
ser = _serializerCache.untypedValueSerializer(runtimeType);
if (ser != null ) {
return ser;
}
return _createAndCacheUntypedSerializer(runtimeType);
}
protected JsonSerializer _createAndCacheUntypedSerializer (Class type)
throws JsonMappingException
{
JsonSerializer ser;
try {
ser = _createUntypedSerializer(_config.constructType(type));
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(iae.getMessage(), null , iae);
}
if (ser != null ) {
_serializerCache.addAndResolveNonTypedSerializer(type, ser, this );
}
return ser;
}
protected JsonSerializer _createAndCacheUntypedSerializer (JavaType type)
throws JsonMappingException
{
JsonSerializer ser;
try {
ser = _createUntypedSerializer(type);
} catch (IllegalArgumentException iae) {
throw new JsonMappingException(iae.getMessage(), null , iae);
}
if (ser != null ) {
_serializerCache.addAndResolveNonTypedSerializer(type, ser, this );
}
return ser;
}
protected JsonSerializer _createUntypedSerializer (JavaType type)
throws JsonMappingException
{
return (JsonSerializer)_serializerFactory.createSerializer(this , type);
}
@SuppressWarnings ("unchecked" )
protected JsonSerializer _handleContextualResolvable (JsonSerializer ser,
BeanProperty property)
throws JsonMappingException
{
if (ser instanceof ResolvableSerializer) {
((ResolvableSerializer) ser).resolve(this );
}
return (JsonSerializer) handleSecondaryContextualization(ser, property);
}
@SuppressWarnings ("unchecked" )
protected JsonSerializer _handleResolvable (JsonSerializer ser)
throws JsonMappingException
{
if (ser instanceof ResolvableSerializer) {
((ResolvableSerializer) ser).resolve(this );
}
return (JsonSerializer) ser;
}
protected final DateFormat _dateFormat ()
{
if (_dateFormat != null ) {
return _dateFormat;
}
DateFormat df = _config.getDateFormat();
_dateFormat = df = (DateFormat) df.clone();
return df;
}
}