package com.fasterxml.jackson.databind.ser.std;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.introspect.AnnotatedMember;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatVisitorWrapper;
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonMapFormatVisitor;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.ContainerSerializer;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.databind.util.ArrayBuilders;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.IgnorePropertiesUtil;
@JacksonStdImpl
public class MapSerializer
extends ContainerSerializer<Map<?,?>>
implements ContextualSerializer
{
private static final long serialVersionUID = 1L;
protected final static JavaType UNSPECIFIED_TYPE = TypeFactory.unknownType();
public final static Object MARKER_FOR_EMPTY = JsonInclude.Include.NON_EMPTY;
protected final BeanProperty _property;
protected final boolean _valueTypeIsStatic;
protected final JavaType _keyType;
protected final JavaType _valueType;
protected JsonSerializer<Object> _keySerializer;
protected JsonSerializer<Object> _valueSerializer;
protected final TypeSerializer _valueTypeSerializer;
protected PropertySerializerMap _dynamicValueSerializers;
protected final Set<String> _ignoredEntries;
protected final Set<String> _includedEntries;
protected final Object _filterId;
protected final Object _suppressableValue;
protected final boolean _suppressNulls;
protected final IgnorePropertiesUtil.Checker _inclusionChecker;
protected final boolean _sortKeys;
@SuppressWarnings("unchecked")
protected MapSerializer(Set<String> ignoredEntries, Set<String> includedEntries,
JavaType keyType, JavaType valueType, boolean valueTypeIsStatic,
TypeSerializer vts,
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer)
{
super(Map.class, false);
_ignoredEntries = ((ignoredEntries == null) || ignoredEntries.isEmpty())
? null : ignoredEntries;
_includedEntries = includedEntries;
_keyType = keyType;
_valueType = valueType;
_valueTypeIsStatic = valueTypeIsStatic;
_valueTypeSerializer = vts;
_keySerializer = (JsonSerializer<Object>) keySerializer;
_valueSerializer = (JsonSerializer<Object>) valueSerializer;
_dynamicValueSerializers = PropertySerializerMap.emptyForProperties();
_property = null;
_filterId = null;
_sortKeys = false;
_suppressableValue = null;
_suppressNulls = false;
_inclusionChecker = IgnorePropertiesUtil.buildCheckerIfNeeded(_ignoredEntries, _includedEntries);
}
@Deprecated
protected MapSerializer(Set<String> ignoredEntries,
JavaType keyType, JavaType valueType, boolean valueTypeIsStatic,
TypeSerializer vts,
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer)
{
this(ignoredEntries, null,
keyType, valueType, valueTypeIsStatic,
vts,
keySerializer, valueSerializer);
}
@SuppressWarnings("unchecked")
protected MapSerializer(MapSerializer src, BeanProperty property,
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer,
Set<String> ignoredEntries, Set<String> includedEntries)
{
super(Map.class, false);
_ignoredEntries = ((ignoredEntries == null) || ignoredEntries.isEmpty())
? null : ignoredEntries;
_includedEntries = includedEntries;
_keyType = src._keyType;
_valueType = src._valueType;
_valueTypeIsStatic = src._valueTypeIsStatic;
_valueTypeSerializer = src._valueTypeSerializer;
_keySerializer = (JsonSerializer<Object>) keySerializer;
_valueSerializer = (JsonSerializer<Object>) valueSerializer;
_dynamicValueSerializers = PropertySerializerMap.emptyForProperties();
_property = property;
_filterId = src._filterId;
_sortKeys = src._sortKeys;
_suppressableValue = src._suppressableValue;
_suppressNulls = src._suppressNulls;
_inclusionChecker = IgnorePropertiesUtil.buildCheckerIfNeeded(_ignoredEntries, _includedEntries);
}
@Deprecated
protected MapSerializer(MapSerializer src, BeanProperty property,
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer,
Set<String> ignoredEntries)
{
this(src, property, keySerializer, valueSerializer, ignoredEntries, null);
}
protected MapSerializer(MapSerializer src, TypeSerializer vts,
Object suppressableValue, boolean suppressNulls)
{
super(Map.class, false);
_ignoredEntries = src._ignoredEntries;
_includedEntries = src._includedEntries;
_keyType = src._keyType;
_valueType = src._valueType;
_valueTypeIsStatic = src._valueTypeIsStatic;
_valueTypeSerializer = vts;
_keySerializer = src._keySerializer;
_valueSerializer = src._valueSerializer;
_dynamicValueSerializers = src._dynamicValueSerializers;
_property = src._property;
_filterId = src._filterId;
_sortKeys = src._sortKeys;
_suppressableValue = suppressableValue;
_suppressNulls = suppressNulls;
_inclusionChecker = src._inclusionChecker;
}
protected MapSerializer(MapSerializer src, Object filterId, boolean sortKeys)
{
super(Map.class, false);
_ignoredEntries = src._ignoredEntries;
_includedEntries = src._includedEntries;
_keyType = src._keyType;
_valueType = src._valueType;
_valueTypeIsStatic = src._valueTypeIsStatic;
_valueTypeSerializer = src._valueTypeSerializer;
_keySerializer = src._keySerializer;
_valueSerializer = src._valueSerializer;
_dynamicValueSerializers = PropertySerializerMap.emptyForProperties();
_property = src._property;
_filterId = filterId;
_sortKeys = sortKeys;
_suppressableValue = src._suppressableValue;
_suppressNulls = src._suppressNulls;
_inclusionChecker = src._inclusionChecker;
}
@Override
public MapSerializer _withValueTypeSerializer(TypeSerializer vts) {
if (_valueTypeSerializer == vts) {
return this;
}
_ensureOverride("_withValueTypeSerializer");
return new MapSerializer(this, vts, _suppressableValue, _suppressNulls);
}
public MapSerializer withResolved(BeanProperty property,
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer,
Set<String> ignored, Set<String> included, boolean sortKeys)
{
_ensureOverride("withResolved");
MapSerializer ser = new MapSerializer(this, property, keySerializer, valueSerializer, ignored, included);
if (sortKeys != ser._sortKeys) {
ser = new MapSerializer(ser, _filterId, sortKeys);
}
return ser;
}
public MapSerializer withResolved(BeanProperty property,
JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer,
Set<String> ignored, boolean sortKeys)
{
return withResolved(property, keySerializer, valueSerializer, ignored, null, sortKeys);
}
@Override
public MapSerializer withFilterId(Object filterId) {
if (_filterId == filterId) {
return this;
}
_ensureOverride("withFilterId");
return new MapSerializer(this, filterId, _sortKeys);
}
public MapSerializer withContentInclusion(Object suppressableValue, boolean suppressNulls) {
if ((suppressableValue == _suppressableValue) && (suppressNulls == _suppressNulls)) {
return this;
}
_ensureOverride("withContentInclusion");
return new MapSerializer(this, _valueTypeSerializer, suppressableValue, suppressNulls);
}
public static MapSerializer construct(Set<String> ignoredEntries, Set<String> includedEntries, JavaType mapType,
boolean staticValueType, TypeSerializer vts,
JsonSerializer<Object> keySerializer, JsonSerializer<Object> valueSerializer,
Object filterId)
{
JavaType keyType, valueType;
if (mapType == null) {
keyType = valueType = UNSPECIFIED_TYPE;
} else {
keyType = mapType.getKeyType();
if (mapType.hasRawClass(java.util.Properties.class)) {
valueType = TypeFactory.unknownType();
} else {
valueType = mapType.getContentType();
}
}
if (!staticValueType) {
staticValueType = (valueType != null && valueType.isFinal());
} else {
if (valueType.getRawClass() == Object.class) {
staticValueType = false;
}
}
MapSerializer ser = new MapSerializer(ignoredEntries, includedEntries, keyType, valueType, staticValueType, vts,
keySerializer, valueSerializer);
if (filterId != null) {
ser = ser.withFilterId(filterId);
}
return ser;
}
public static MapSerializer construct(Set<String> ignoredEntries, JavaType mapType,
boolean staticValueType, TypeSerializer vts,
JsonSerializer<Object> keySerializer, JsonSerializer<Object> valueSerializer,
Object filterId)
{
return construct(ignoredEntries, null, mapType, staticValueType, vts, keySerializer, valueSerializer, filterId);
}
protected void _ensureOverride(String method) {
ClassUtil.verifyMustOverride(MapSerializer.class, this, method);
}
@Deprecated
protected void _ensureOverride() {
_ensureOverride("N/A");
}
@Deprecated
protected MapSerializer(MapSerializer src, TypeSerializer vts,
Object suppressableValue)
{
this(src, vts, suppressableValue, false);
}
@Deprecated
public MapSerializer withContentInclusion(Object suppressableValue) {
return new MapSerializer(this, _valueTypeSerializer, suppressableValue, _suppressNulls);
}
@Deprecated
public static MapSerializer construct(String[] ignoredList, JavaType mapType,
boolean staticValueType, TypeSerializer vts,
JsonSerializer<Object> keySerializer, JsonSerializer<Object> valueSerializer,
Object filterId)
{
Set<String> ignoredEntries = ArrayBuilders.arrayToSet(ignoredList);
return construct(ignoredEntries, mapType, staticValueType, vts,
keySerializer, valueSerializer, filterId);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer<?> ser = null;
JsonSerializer<?> keySer = null;
final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
final AnnotatedMember propertyAcc = (property == null) ? null : property.getMember();
if (_neitherNull(propertyAcc, intr)) {
Object serDef = intr.findKeySerializer(propertyAcc);
if (serDef != null) {
keySer = provider.serializerInstance(propertyAcc, serDef);
}
serDef = intr.findContentSerializer(propertyAcc);
if (serDef != null) {
ser = provider.serializerInstance(propertyAcc, serDef);
}
}
if (ser == null) {
ser = _valueSerializer;
}
ser = findContextualConvertingSerializer(provider, property, ser);
if (ser == null) {
if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) {
ser = provider.findContentValueSerializer(_valueType, property);
}
}
if (keySer == null) {
keySer = _keySerializer;
}
if (keySer == null) {
keySer = provider.findKeySerializer(_keyType, property);
} else {
keySer = provider.handleSecondaryContextualization(keySer, property);
}
Set<String> ignored = _ignoredEntries;
Set<String> included = _includedEntries;
boolean sortKeys = false;
if (_neitherNull(propertyAcc, intr)) {
final SerializationConfig config = provider.getConfig();
Set<String> newIgnored = intr.findPropertyIgnoralByName(config, propertyAcc).findIgnoredForSerialization();
if (_nonEmpty(newIgnored)) {
ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
for (String str : newIgnored) {
ignored.add(str);
}
}
Set<String> newIncluded = intr.findPropertyInclusionByName(config, propertyAcc).getIncluded();
if (newIncluded != null) {
included = (included == null) ? new HashSet<String>() : new HashSet<String>(included);
for (String str : newIncluded) {
included.add(str);
}
}
Boolean b = intr.findSerializationSortAlphabetically(propertyAcc);
sortKeys = Boolean.TRUE.equals(b);
}
JsonFormat.Value format = findFormatOverrides(provider, property, Map.class);
if (format != null) {
Boolean B = format.getFeature(JsonFormat.Feature.WRITE_SORTED_MAP_ENTRIES);
if (B != null) {
sortKeys = B.booleanValue();
}
}
MapSerializer mser = withResolved(property, keySer, ser, ignored, included, sortKeys);
if (propertyAcc != null) {
Object filterId = intr.findFilterId(propertyAcc);
if (filterId != null) {
mser = mser.withFilterId(filterId);
}
}
JsonInclude.Value inclV = findIncludeOverrides(provider, property, Map.class);
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(_valueType);
suppressNulls = true;
if (valueToSuppress != null) {
if (valueToSuppress.getClass().isArray()) {
valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
}
}
break;
case NON_ABSENT:
suppressNulls = true;
valueToSuppress = _valueType.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) {
suppressNulls = true;
} else {
suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
}
break;
case NON_NULL:
valueToSuppress = null;
suppressNulls = true;
break;
case ALWAYS:
default:
valueToSuppress = null;
suppressNulls = false;
break;
}
mser = mser.withContentInclusion(valueToSuppress, suppressNulls);
}
}
return mser;
}
@Override
public JavaType getContentType() {
return _valueType;
}
@Override
public JsonSerializer<?> getContentSerializer() {
return _valueSerializer;
}
@Override
public boolean isEmpty(SerializerProvider prov, Map<?,?> value)
{
if (value.isEmpty()) {
return true;
}
Object supp = _suppressableValue;
if ((supp == null) && !_suppressNulls) {
return false;
}
JsonSerializer<Object> valueSer = _valueSerializer;
final boolean checkEmpty = (MARKER_FOR_EMPTY == supp);
if (valueSer != null) {
for (Object elemValue : value.values()) {
if (elemValue == null) {
if (_suppressNulls) {
continue;
}
return false;
}
if (checkEmpty) {
if (!valueSer.isEmpty(prov, elemValue)) {
return false;
}
} else if ((supp == null) || !supp.equals(value)) {
return false;
}
}
return true;
}
for (Object elemValue : value.values()) {
if (elemValue == null) {
if (_suppressNulls) {
continue;
}
return false;
}
try {
valueSer = _findSerializer(prov, elemValue);
} catch (DatabindException e) {
return false;
}
if (checkEmpty) {
if (!valueSer.isEmpty(prov, elemValue)) {
return false;
}
} else if ((supp == null) || !supp.equals(value)) {
return false;
}
}
return true;
}
@Override
public boolean hasSingleElement(Map<?,?> value) {
return (value.size() == 1);
}
public JsonSerializer<?> getKeySerializer() {
return _keySerializer;
}
@Override
public void serialize(Map<?,?> value, JsonGenerator gen, SerializerProvider provider)
throws IOException
{
gen.writeStartObject(value);
serializeWithoutTypeInfo(value, gen, provider);
gen.writeEndObject();
}
@Override
public void serializeWithType(Map<?,?> value, JsonGenerator gen, SerializerProvider provider,
TypeSerializer typeSer)
throws IOException
{
gen.setCurrentValue(value);
WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen,
typeSer.typeId(value, JsonToken.START_OBJECT));
serializeWithoutTypeInfo(value, gen, provider);
typeSer.writeTypeSuffix(gen, typeIdDef);
}
public void serializeWithoutTypeInfo(Map<?, ?> value, JsonGenerator gen, SerializerProvider provider) throws IOException {
if (!value.isEmpty()) {
if (_sortKeys || provider.isEnabled(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)) {
value = _orderEntries(value, gen, provider);
}
PropertyFilter pf;
if ((_filterId != null) && (pf = findPropertyFilter(provider, _filterId, value)) != null) {
serializeFilteredFields(value, gen, provider, pf, _suppressableValue);
} else if ((_suppressableValue != null) || _suppressNulls) {
serializeOptionalFields(value, gen, provider, _suppressableValue);
} else if (_valueSerializer != null) {
serializeFieldsUsing(value, gen, provider, _valueSerializer);
} else {
serializeFields(value, gen, provider);
}
}
}
public void serializeFields(Map<?,?> value, JsonGenerator gen, SerializerProvider provider)
throws IOException
{
if (_valueTypeSerializer != null) {
serializeTypedFields(value, gen, provider, null);
return;
}
final JsonSerializer<Object> keySerializer = _keySerializer;
Object keyElem = null;
try {
for (Map.Entry<?,?> entry : value.entrySet()) {
Object valueElem = entry.getValue();
keyElem = entry.getKey();
if (keyElem == null) {
provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
} else {
if ((_inclusionChecker != null) && _inclusionChecker.shouldIgnore(keyElem)) {
continue;
}
keySerializer.serialize(keyElem, gen, provider);
}
if (valueElem == null) {
provider.defaultSerializeNull(gen);
continue;
}
JsonSerializer<Object> serializer = _valueSerializer;
if (serializer == null) {
serializer = _findSerializer(provider, valueElem);
}
serializer.serialize(valueElem, gen, provider);
}
} catch (Exception e) {
wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
public void serializeOptionalFields(Map<?,?> value, JsonGenerator gen, SerializerProvider provider,
Object suppressableValue)
throws IOException
{
if (_valueTypeSerializer != null) {
serializeTypedFields(value, gen, provider, suppressableValue);
return;
}
final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);
for (Map.Entry<?,?> entry : value.entrySet()) {
final Object keyElem = entry.getKey();
JsonSerializer<Object> keySerializer;
if (keyElem == null) {
keySerializer = provider.findNullKeySerializer(_keyType, _property);
} else {
if ((_inclusionChecker != null) && _inclusionChecker.shouldIgnore(keyElem)) {
continue;
}
keySerializer = _keySerializer;
}
final Object valueElem = entry.getValue();
JsonSerializer<Object> valueSer;
if (valueElem == null) {
if (_suppressNulls) {
continue;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
valueSer = _findSerializer(provider, valueElem);
}
if (checkEmpty) {
if (valueSer.isEmpty(provider, valueElem)) {
continue;
}
} else if (suppressableValue != null) {
if (suppressableValue.equals(valueElem)) {
continue;
}
}
}
try {
keySerializer.serialize(keyElem, gen, provider);
valueSer.serialize(valueElem, gen, provider);
} catch (Exception e) {
wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
public void serializeFieldsUsing(Map<?,?> value, JsonGenerator gen, SerializerProvider provider,
JsonSerializer<Object> ser)
throws IOException
{
final JsonSerializer<Object> keySerializer = _keySerializer;
final TypeSerializer typeSer = _valueTypeSerializer;
for (Map.Entry<?,?> entry : value.entrySet()) {
Object keyElem = entry.getKey();
if ((_inclusionChecker != null) && _inclusionChecker.shouldIgnore(keyElem)) {
continue;
}
if (keyElem == null) {
provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider);
} else {
keySerializer.serialize(keyElem, gen, provider);
}
final Object valueElem = entry.getValue();
if (valueElem == null) {
provider.defaultSerializeNull(gen);
} else {
try {
if (typeSer == null) {
ser.serialize(valueElem, gen, provider);
} else {
ser.serializeWithType(valueElem, gen, provider, typeSer);
}
} catch (Exception e) {
wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
}
public void serializeFilteredFields(Map<?,?> value, JsonGenerator gen, SerializerProvider provider,
PropertyFilter filter,
Object suppressableValue)
throws IOException
{
final MapProperty prop = new MapProperty(_valueTypeSerializer, _property);
final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);
for (Map.Entry<?,?> entry : value.entrySet()) {
final Object keyElem = entry.getKey();
if ((_inclusionChecker != null) && _inclusionChecker.shouldIgnore(keyElem)) {
continue;
}
JsonSerializer<Object> keySerializer;
if (keyElem == null) {
keySerializer = provider.findNullKeySerializer(_keyType, _property);
} else {
keySerializer = _keySerializer;
}
final Object valueElem = entry.getValue();
JsonSerializer<Object> valueSer;
if (valueElem == null) {
if (_suppressNulls) {
continue;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
valueSer = _findSerializer(provider, valueElem);
}
if (checkEmpty) {
if (valueSer.isEmpty(provider, valueElem)) {
continue;
}
} else if (suppressableValue != null) {
if (suppressableValue.equals(valueElem)) {
continue;
}
}
}
prop.reset(keyElem, valueElem, keySerializer, valueSer);
try {
filter.serializeAsField(value, gen, provider, prop);
} catch (Exception e) {
wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
public void serializeTypedFields(Map<?,?> value, JsonGenerator gen, SerializerProvider provider,
Object suppressableValue)
throws IOException
{
final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);
for (Map.Entry<?,?> entry : value.entrySet()) {
Object keyElem = entry.getKey();
JsonSerializer<Object> keySerializer;
if (keyElem == null) {
keySerializer = provider.findNullKeySerializer(_keyType, _property);
} else {
if ((_inclusionChecker != null) && _inclusionChecker.shouldIgnore(keyElem)) {
continue;
}
keySerializer = _keySerializer;
}
final Object valueElem = entry.getValue();
JsonSerializer<Object> valueSer;
if (valueElem == null) {
if (_suppressNulls) {
continue;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
valueSer = _findSerializer(provider, valueElem);
}
if (checkEmpty) {
if (valueSer.isEmpty(provider, valueElem)) {
continue;
}
} else if (suppressableValue != null) {
if (suppressableValue.equals(valueElem)) {
continue;
}
}
}
keySerializer.serialize(keyElem, gen, provider);
try {
valueSer.serializeWithType(valueElem, gen, provider, _valueTypeSerializer);
} catch (Exception e) {
wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
public void serializeFilteredAnyProperties(SerializerProvider provider, JsonGenerator gen,
Object bean, Map<?,?> value, PropertyFilter filter,
Object suppressableValue)
throws IOException
{
final MapProperty prop = new MapProperty(_valueTypeSerializer, _property);
final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue);
for (Map.Entry<?,?> entry : value.entrySet()) {
final Object keyElem = entry.getKey();
if ((_inclusionChecker != null) && _inclusionChecker.shouldIgnore(keyElem)) {
continue;
}
JsonSerializer<Object> keySerializer;
if (keyElem == null) {
keySerializer = provider.findNullKeySerializer(_keyType, _property);
} else {
keySerializer = _keySerializer;
}
final Object valueElem = entry.getValue();
JsonSerializer<Object> valueSer;
if (valueElem == null) {
if (_suppressNulls) {
continue;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
valueSer = _findSerializer(provider, valueElem);
}
if (checkEmpty) {
if (valueSer.isEmpty(provider, valueElem)) {
continue;
}
} else if (suppressableValue != null) {
if (suppressableValue.equals(valueElem)) {
continue;
}
}
}
prop.reset(keyElem, valueElem, keySerializer, valueSer);
try {
filter.serializeAsField(bean, gen, provider, prop);
} catch (Exception e) {
wrapAndThrow(provider, e, value, String.valueOf(keyElem));
}
}
}
@Override
public JsonNode getSchema(SerializerProvider provider, Type typeHint)
{
return createSchemaNode("object", true);
}
@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint)
throws JsonMappingException
{
JsonMapFormatVisitor v2 = visitor.expectMapFormat(typeHint);
if (v2 != null) {
v2.keyFormat(_keySerializer, _keyType);
JsonSerializer<?> valueSer = _valueSerializer;
if (valueSer == null) {
valueSer = _findAndAddDynamic(_dynamicValueSerializers,
_valueType, visitor.getProvider());
}
v2.valueFormat(valueSer, _valueType);
}
}
protected final JsonSerializer<Object> _findAndAddDynamic(PropertySerializerMap map,
Class<?> type, SerializerProvider provider)
throws JsonMappingException
{
PropertySerializerMap.SerializerAndMapResult result = map.findAndAddSecondarySerializer(type, provider, _property);
if (map != result.map) {
_dynamicValueSerializers = result.map;
}
return result.serializer;
}
protected final JsonSerializer<Object> _findAndAddDynamic(PropertySerializerMap map,
JavaType type, SerializerProvider provider)
throws JsonMappingException
{
PropertySerializerMap.SerializerAndMapResult result = map.findAndAddSecondarySerializer(type, provider, _property);
if (map != result.map) {
_dynamicValueSerializers = result.map;
}
return result.serializer;
}
protected Map<?,?> _orderEntries(Map<?,?> input, JsonGenerator gen,
SerializerProvider provider)
throws IOException
{
if (input instanceof SortedMap<?,?>) {
return input;
}
if (_hasNullKey(input)) {
TreeMap<Object,Object> result = new TreeMap<Object,Object>();
for (Map.Entry<?,?> entry : input.entrySet()) {
Object key = entry.getKey();
if (key == null) {
_writeNullKeyedEntry(gen, provider, entry.getValue());
continue;
}
result.put(key, entry.getValue());
}
return result;
}
return new TreeMap<Object,Object>(input);
}
protected boolean _hasNullKey(Map<?,?> input) {
return (input instanceof HashMap) && input.containsKey(null);
}
protected void _writeNullKeyedEntry(JsonGenerator gen, SerializerProvider provider,
Object value)
throws IOException
{
JsonSerializer<Object> keySerializer = provider.findNullKeySerializer(_keyType, _property);
JsonSerializer<Object> valueSer;
if (value == null) {
if (_suppressNulls) {
return;
}
valueSer = provider.getDefaultNullValueSerializer();
} else {
valueSer = _valueSerializer;
if (valueSer == null) {
valueSer = _findSerializer(provider, value);
}
if (_suppressableValue == MARKER_FOR_EMPTY) {
if (valueSer.isEmpty(provider, value)) {
return;
}
} else if ((_suppressableValue != null)
&& (_suppressableValue.equals(value))) {
return;
}
}
try {
keySerializer.serialize(null, gen, provider);
valueSer.serialize(value, gen, provider);
} catch (Exception e) {
wrapAndThrow(provider, e, value, "");
}
}
private final JsonSerializer<Object> _findSerializer(SerializerProvider provider,
Object value)
throws JsonMappingException
{
final Class<?> cc = value.getClass();
JsonSerializer<Object> valueSer = _dynamicValueSerializers.serializerFor(cc);
if (valueSer != null) {
return valueSer;
}
if (_valueType.hasGenericTypes()) {
return _findAndAddDynamic(_dynamicValueSerializers,
provider.constructSpecializedType(_valueType, cc), provider);
}
return _findAndAddDynamic(_dynamicValueSerializers, cc, provider);
}
}