use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.
the class MapEntrySerializer method serializeDynamic.
protected void serializeDynamic(Map.Entry<?, ?> value, JsonGenerator gen, SerializerProvider provider) throws IOException {
final TypeSerializer vts = _valueTypeSerializer;
final Object keyElem = value.getKey();
JsonSerializer<Object> keySerializer;
if (keyElem == null) {
keySerializer = provider.findNullKeySerializer(_keyType, _property);
} else {
keySerializer = _keySerializer;
}
// or by value; nulls often suppressed
final Object valueElem = value.getValue();
JsonSerializer<Object> valueSer;
// And then value
if (valueElem == null) {
if (_suppressNulls) {
return;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
Class<?> cc = valueElem.getClass();
valueSer = _dynamicValueSerializers.serializerFor(cc);
if (valueSer == null) {
if (_valueType.hasGenericTypes()) {
valueSer = _findAndAddDynamic(_dynamicValueSerializers, provider.constructSpecializedType(_valueType, cc), provider);
} else {
valueSer = _findAndAddDynamic(_dynamicValueSerializers, cc, provider);
}
}
}
// also may need to skip non-empty values:
if (_suppressableValue != null) {
if (_suppressableValue == MARKER_FOR_EMPTY) {
if (valueSer.isEmpty(provider, valueElem)) {
return;
}
}
if (_suppressableValue.equals(valueElem)) {
return;
}
}
}
keySerializer.serialize(keyElem, gen, provider);
try {
if (vts == null) {
valueSer.serialize(valueElem, gen, provider);
} else {
valueSer.serializeWithType(valueElem, gen, provider, vts);
}
} catch (Exception e) {
String keyDesc = "" + keyElem;
wrapAndThrow(provider, e, value, keyDesc);
}
}
use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.
the class BeanSerializerFactory method findPropertyTypeSerializer.
/**
* Method called to create a type information serializer for values of given
* non-container property
* if one is needed. If not needed (no polymorphic handling configured), should
* return null.
*
* @param baseType Declared type to use as the base type for type information serializer
*
* @return Type serializer to use for property values, if one is needed; null if not.
*/
public TypeSerializer findPropertyTypeSerializer(JavaType baseType, SerializationConfig config, AnnotatedMember accessor) throws JsonMappingException {
AnnotationIntrospector ai = config.getAnnotationIntrospector();
TypeResolverBuilder<?> b = ai.findPropertyTypeResolver(config, accessor, baseType);
TypeSerializer typeSer;
// Defaulting: if no annotations on member, check value class
if (b == null) {
typeSer = createTypeSerializer(config, baseType);
} else {
Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByClass(config, accessor, baseType);
typeSer = b.buildTypeSerializer(config, baseType, subtypes);
}
return typeSer;
}
use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.
the class BeanSerializerFactory method findPropertyContentTypeSerializer.
/**
* Method called to create a type information serializer for values of given
* container property
* if one is needed. If not needed (no polymorphic handling configured), should
* return null.
*
* @param containerType Declared type of the container to use as the base type for type information serializer
*
* @return Type serializer to use for property value contents, if one is needed; null if not.
*/
public TypeSerializer findPropertyContentTypeSerializer(JavaType containerType, SerializationConfig config, AnnotatedMember accessor) throws JsonMappingException {
JavaType contentType = containerType.getContentType();
AnnotationIntrospector ai = config.getAnnotationIntrospector();
TypeResolverBuilder<?> b = ai.findPropertyContentTypeResolver(config, accessor, containerType);
TypeSerializer typeSer;
// Defaulting: if no annotations on member, check value class
if (b == null) {
typeSer = createTypeSerializer(config, contentType);
} else {
Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByClass(config, accessor, contentType);
typeSer = b.buildTypeSerializer(config, contentType, subtypes);
}
return typeSer;
}
use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.
the class BeanSerializerFactory method _constructWriter.
/*
/**********************************************************
/* Internal helper methods
/**********************************************************
*/
/**
* Secondary helper method for constructing {@link BeanPropertyWriter} for
* given member (field or method).
*/
protected BeanPropertyWriter _constructWriter(SerializerProvider prov, BeanPropertyDefinition propDef, PropertyBuilder pb, boolean staticTyping, AnnotatedMember accessor) throws JsonMappingException {
final PropertyName name = propDef.getFullName();
JavaType type = accessor.getType();
BeanProperty.Std property = new BeanProperty.Std(name, type, propDef.getWrapperName(), accessor, propDef.getMetadata());
// Does member specify a serializer? If so, let's use it.
JsonSerializer<?> annotatedSerializer = findSerializerFromAnnotation(prov, accessor);
// here will NOT be resolved or contextualized, unless done here, so:
if (annotatedSerializer instanceof ResolvableSerializer) {
((ResolvableSerializer) annotatedSerializer).resolve(prov);
}
// 05-Sep-2013, tatu: should be primary property serializer so:
annotatedSerializer = prov.handlePrimaryContextualization(annotatedSerializer, property);
// And how about polymorphic typing? First special to cover JAXB per-field settings:
TypeSerializer contentTypeSer = null;
// 16-Feb-2014, cgc: contentType serializers for collection-like and map-like types
if (type.isContainerType() || type.isReferenceType()) {
contentTypeSer = findPropertyContentTypeSerializer(type, prov.getConfig(), accessor);
}
// and if not JAXB collection/array with annotations, maybe regular type info?
TypeSerializer typeSer = findPropertyTypeSerializer(type, prov.getConfig(), accessor);
return pb.buildWriter(prov, propDef, type, annotatedSerializer, typeSer, contentTypeSer, accessor, staticTyping);
}
use of com.fasterxml.jackson.databind.jsontype.TypeSerializer in project jackson-databind by FasterXML.
the class SerializerProvider method findTypedValueSerializer.
/**
* Method called to locate regular serializer, matching type serializer,
* and if both found, wrap them in a serializer that calls both in correct
* sequence. This method is currently only used for root-level serializer
* handling to allow for simpler caching. A call can always be replaced
* by equivalent calls to access serializer and type serializer separately.
*
* @param valueType Type for purpose of locating a serializer; usually dynamic
* runtime type, but can also be static declared type, depending on configuration
* @param cache Whether resulting value serializer should be cached or not; this is just
* a hint
* @param property When creating secondary serializers, property for which
* serializer is needed: annotations of the property (or bean that contains it)
* may be checked to create contextual serializers.
*/
public JsonSerializer<Object> findTypedValueSerializer(Class<?> valueType, boolean cache, BeanProperty property) throws JsonMappingException {
// Two-phase lookups; local non-shared cache, then shared:
JsonSerializer<Object> ser = _knownSerializers.typedValueSerializer(valueType);
if (ser != null) {
return ser;
}
// If not, maybe shared map already has it?
ser = _serializerCache.typedValueSerializer(valueType);
if (ser != null) {
return ser;
}
// Well, let's just compose from pieces:
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;
}
Aggregations