下面列出了com.fasterxml.jackson.databind.jsontype.TypeSerializer#forProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException {
JavaType containedType = _type.containedTypeOrUnknown(0);
TypeSerializer vts = (_valueTypeSerializer == null)
? prov.findTypeSerializer(containedType) : _valueTypeSerializer;
if (vts != null) {
vts = vts.forProperty(prov, property);
}
JsonSerializer<Object> vs = ((_valueSerializer == null) && containedType.useStaticType())
? prov.findValueSerializer(containedType) : _valueSerializer;
//noinspection ObjectEqualit
if (vts == _valueTypeSerializer && vs == _valueSerializer) {
return this;
}
return withResolved(vts, property, vs);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property) throws JsonMappingException {
TypeSerializer vts = valueTypeSerializer;
if (vts != null) {
vts = vts.forProperty(property);
}
JsonSerializer<?> ser = valueSerializer;
if (ser == null) {
// A few conditions needed to be able to fetch serializer here:
if (useStatic(provider, property, valueType)) {
ser = provider.findTypedValueSerializer(valueType, true, property);
}
} else {
ser = provider.handlePrimaryContextualization(ser, property);
}
return withResolved(fullType, vts, ser);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property) throws JsonMappingException {
TypeSerializer vts = valueTypeSerializer;
if (vts != null) {
vts = vts.forProperty(property);
}
JsonSerializer<?> ser = valueSerializer;
if (ser == null) {
// A few conditions needed to be able to fetch serializer here:
if (useStatic(provider, property, valueType)) {
ser = provider.findTypedValueSerializer(valueType, true, property);
}
} else {
ser = provider.handlePrimaryContextualization(ser, property);
}
return new LazySerializer(fullType, valueType, vts, ser);
}
/**
* 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;
}
/**
* 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 Declared type of value being serialized (which may not
* be actual runtime type); used for finding both value serializer and
* type serializer to use for adding polymorphic type (if any)
* @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(JavaType 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, valueType);
if (typeSer != null) {
typeSer = typeSer.forProperty(property);
ser = new TypeWrappedSerializer(typeSer, ser);
}
if (cache) {
_serializerCache.addTypedSerializer(valueType, ser);
}
return ser;
}
@Override
public JsonSerializer<?> createContextual(final SerializerProvider provider,
final BeanProperty property )
throws JsonMappingException
{
JsonSerializer<?> valueSer = _valueSerializer;
if (valueSer == null) { // if type is final, can actually resolve:
final JavaType valueType = _type.containedTypeOrUnknown(2);
if (valueType.isFinal()) {
valueSer = provider.findContentValueSerializer(valueType, property);
}
} else {
valueSer = provider.handleSecondaryContextualization(valueSer, property);
}
JsonSerializer<?> rowKeySer = _rowSerializer;
if (rowKeySer == null) {
rowKeySer = provider.findKeySerializer(_type.containedTypeOrUnknown(0), property);
} else {
rowKeySer = provider.handleSecondaryContextualization(rowKeySer, property);
}
JsonSerializer<?> columnKeySer = _columnSerializer;
if (columnKeySer == null) {
columnKeySer = provider.findKeySerializer(_type.containedTypeOrUnknown(1), property);
} else {
columnKeySer = provider.handleSecondaryContextualization(columnKeySer, property);
}
// finally, TypeSerializers may need contextualization as well
TypeSerializer typeSer = _valueTypeSerializer;
if (typeSer != null) {
typeSer = typeSer.forProperty(provider, property);
}
return withResolved(property, provider.getTypeFactory(), rowKeySer, columnKeySer, typeSer, valueSer);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property)
throws JsonMappingException
{
TypeSerializer vts = _valueTypeSerializer;
if (vts != null) {
vts = vts.forProperty(property);
}
JsonSerializer<?> ser = null;
Boolean unwrapSingle = null;
// First: if we have a property, may have property-annotation overrides
if (property != null) {
AnnotatedMember m = property.getMember();
final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
if (m != null) {
Object serDef = intr.findContentSerializer(m);
if (serDef != null) {
ser = serializers.serializerInstance(m, serDef);
}
}
}
JsonFormat.Value format = findFormatOverrides(serializers, property, handledType());
if (format != null) {
unwrapSingle = format.getFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
}
if (ser == null) {
ser = _elementSerializer;
}
// [databind#124]: May have a content converter
ser = findContextualConvertingSerializer(serializers, property, ser);
if (ser == null) {
// 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
// we can consider it a static case as well.
if (_elementType != null) {
if (_staticTyping && !_elementType.isJavaLangObject()) {
ser = serializers.findValueSerializer(_elementType, property);
}
}
}
return withResolved(property, vts, ser, unwrapSingle);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property) throws JsonMappingException
{
TypeSerializer typeSer = _valueTypeSerializer;
if (typeSer != null) {
typeSer = typeSer.forProperty(property);
}
// First: do we have an annotation override from property?
JsonSerializer<?> ser = findAnnotatedContentSerializer(provider, property);
if (ser == null) {
// If not, use whatever was configured by type
ser = _valueSerializer;
if (ser == null) {
// A few conditions needed to be able to fetch serializer here:
if (_useStatic(provider, property, _referredType)) {
ser = _findSerializer(provider, _referredType, property);
}
} else {
ser = provider.handlePrimaryContextualization(ser, property);
}
}
// First, resolve wrt property, resolved serializers
ReferenceTypeSerializer<?> refSer;
if ((_property == property)
&& (_valueTypeSerializer == typeSer) && (_valueSerializer == ser)) {
refSer = this;
} else {
refSer = withResolved(property, typeSer, ser, _unwrapper);
}
// and then see if we have property-inclusion overrides
if (property != null) {
JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), handledType());
if (inclV != null) {
JsonInclude.Include incl = inclV.getContentInclusion();
if (incl != JsonInclude.Include.USE_DEFAULTS) {
Object valueToSuppress;
boolean suppressNulls;
switch (incl) {
case NON_DEFAULT:
valueToSuppress = BeanUtil.getDefaultValue(_referredType);
suppressNulls = true;
if (valueToSuppress != null) {
if (valueToSuppress.getClass().isArray()) {
valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
}
}
break;
case NON_ABSENT:
suppressNulls = true;
valueToSuppress = _referredType.isReferenceType() ? MARKER_FOR_EMPTY : null;
break;
case NON_EMPTY:
suppressNulls = true;
valueToSuppress = MARKER_FOR_EMPTY;
break;
case CUSTOM:
valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
if (valueToSuppress == null) { // is this legal?
suppressNulls = true;
} else {
suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
}
break;
case NON_NULL:
valueToSuppress = null;
suppressNulls = true;
break;
case ALWAYS: // default
default:
valueToSuppress = null;
suppressNulls = false;
break;
}
if ((_suppressableValue != valueToSuppress)
|| (_suppressNulls != suppressNulls)) {
refSer = refSer.withContentInclusion(valueToSuppress, suppressNulls);
}
}
}
}
return refSer;
}
/**
* This method is needed to resolve contextual annotations like
* per-property overrides, as well as do recursive call
* to <code>createContextual</code> of content serializer, if
* known statically.
*/
@Override
public JsonSerializer<?> createContextual(SerializerProvider serializers,
BeanProperty property)
throws JsonMappingException
{
TypeSerializer typeSer = _valueTypeSerializer;
if (typeSer != null) {
typeSer = typeSer.forProperty(property);
}
JsonSerializer<?> ser = null;
Boolean unwrapSingle = null;
// First: if we have a property, may have property-annotation overrides
if (property != null) {
final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
AnnotatedMember m = property.getMember();
if (m != null) {
Object serDef = intr.findContentSerializer(m);
if (serDef != null) {
ser = serializers.serializerInstance(m, serDef);
}
}
}
JsonFormat.Value format = findFormatOverrides(serializers, property, handledType());
if (format != null) {
unwrapSingle = format.getFeature(JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
}
if (ser == null) {
ser = _elementSerializer;
}
// 18-Feb-2013, tatu: May have a content converter:
ser = findContextualConvertingSerializer(serializers, property, ser);
if (ser == null) {
// 30-Sep-2012, tatu: One more thing -- if explicit content type is annotated,
// we can consider it a static case as well.
if (_elementType != null) {
if (_staticTyping && !_elementType.isJavaLangObject()) {
ser = serializers.findValueSerializer(_elementType, property);
}
}
}
if ((ser != _elementSerializer)
|| (property != _property)
|| (_valueTypeSerializer != typeSer)
|| (_unwrapSingle != unwrapSingle)) {
return withResolved(property, typeSer, ser, unwrapSingle);
}
return this;
}