/** * Method called to serialize fields, when the value type is statically known, so that value * serializer is passed and does not need to be fetched from provider. */ public void serializeFieldsUsing( Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, JsonSerializer<Object> ser) throws IOException { final JsonSerializer<Object> keySerializer = _keySerializer; final Set<String> ignored = _ignoredEntries; final TypeSerializer typeSer = _valueTypeSerializer; for (Map.Entry<?, ?> entry : value.entrySet()) { Object keyElem = entry.getKey(); if (ignored != null && ignored.contains(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)); } } } }
/** * Helper method used when we have a JSON Filter to use for potentially filtering out Map entries. * * @since 2.5 */ public void serializeFilteredFields( Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, PropertyFilter filter, Object suppressableValue) // since 2.5 throws IOException { final Set<String> ignored = _ignoredEntries; final MapProperty prop = new MapProperty(_valueTypeSerializer, _property); final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue); for (Map.Entry<?, ?> entry : value.entrySet()) { // First, serialize key; unless ignorable by key final Object keyElem = entry.getKey(); if (ignored != null && ignored.contains(keyElem)) continue; JsonSerializer<Object> keySerializer; if (keyElem == null) { keySerializer = provider.findNullKeySerializer(_keyType, _property); } else { keySerializer = _keySerializer; } // or by value; nulls often suppressed final Object valueElem = entry.getValue(); JsonSerializer<Object> valueSer; // And then value if (valueElem == null) { if (_suppressNulls) { continue; } valueSer = provider.getDefaultNullValueSerializer(); } else { valueSer = _valueSerializer; if (valueSer == null) { valueSer = _findSerializer(provider, valueElem); } // also may need to skip non-empty values: if (checkEmpty) { if (valueSer.isEmpty(provider, valueElem)) { continue; } } else if (suppressableValue != null) { if (suppressableValue.equals(valueElem)) { continue; } } } // and with that, ask filter to handle it prop.reset(keyElem, keySerializer, valueSer); try { filter.serializeAsField(valueElem, gen, provider, prop); } catch (Exception e) { wrapAndThrow(provider, e, value, String.valueOf(keyElem)); } } }
/** Serialization method called when exclusion filtering needs to be applied. */ public void serializeOptionalFields( Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, Object suppressableValue) throws IOException { // If value type needs polymorphic type handling, some more work needed: if (_valueTypeSerializer != null) { serializeTypedFields(value, gen, provider, suppressableValue); return; } final Set<String> ignored = _ignoredEntries; final boolean checkEmpty = (MARKER_FOR_EMPTY == suppressableValue); for (Map.Entry<?, ?> entry : value.entrySet()) { // First find key serializer final Object keyElem = entry.getKey(); JsonSerializer<Object> keySerializer; if (keyElem == null) { keySerializer = provider.findNullKeySerializer(_keyType, _property); } else { if (ignored != null && ignored.contains(keyElem)) continue; keySerializer = _keySerializer; } // Then value serializer final Object valueElem = entry.getValue(); JsonSerializer<Object> valueSer; if (valueElem == null) { if (_suppressNulls) { // all suppressions include null-suppression continue; } valueSer = provider.getDefaultNullValueSerializer(); } else { valueSer = _valueSerializer; if (valueSer == null) { valueSer = _findSerializer(provider, valueElem); } // also may need to skip non-empty values: if (checkEmpty) { if (valueSer.isEmpty(provider, valueElem)) { continue; } } else if (suppressableValue != null) { if (suppressableValue.equals(valueElem)) { continue; } } } // and then serialize, if all went well try { keySerializer.serialize(keyElem, gen, provider); valueSer.serialize(valueElem, gen, provider); } catch (Exception e) { wrapAndThrow(provider, e, value, String.valueOf(keyElem)); } } }
/** @since 2.5 */ public void serializeTypedFields( Map<?, ?> value, JsonGenerator gen, SerializerProvider provider, Object suppressableValue) // since 2.5 throws IOException { final Set<String> ignored = _ignoredEntries; 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 { // One twist: is entry ignorable? If so, skip if (ignored != null && ignored.contains(keyElem)) continue; keySerializer = _keySerializer; } final Object valueElem = entry.getValue(); // And then value JsonSerializer<Object> valueSer; if (valueElem == null) { if (_suppressNulls) { // all suppression include null suppression continue; } valueSer = provider.getDefaultNullValueSerializer(); } else { valueSer = _valueSerializer; if (valueSer == null) { valueSer = _findSerializer(provider, valueElem); } // also may need to skip non-empty values: 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)); } } }
/** @since 2.5 */ @SuppressWarnings("unchecked") protected MapSerializer( Set<String> ignoredEntries, JavaType keyType, JavaType valueType, boolean valueTypeIsStatic, TypeSerializer vts, JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer) { super(Map.class, false); _ignoredEntries = ((ignoredEntries == null) || ignoredEntries.isEmpty()) ? null : ignoredEntries; _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; }
/** * General-purpose serialization for contents, where we do not necessarily know the value * serialization, but we do know that no value suppression is needed (which simplifies processing * a bit) */ public void serializeFields(Map<?, ?> value, JsonGenerator gen, SerializerProvider provider) throws IOException { // If value type needs polymorphic type handling, some more work needed: if (_valueTypeSerializer != null) { serializeTypedFields(value, gen, provider, null); return; } final JsonSerializer<Object> keySerializer = _keySerializer; final Set<String> ignored = _ignoredEntries; Object keyElem = null; try { for (Map.Entry<?, ?> entry : value.entrySet()) { Object valueElem = entry.getValue(); // First, serialize key keyElem = entry.getKey(); if (keyElem == null) { provider.findNullKeySerializer(_keyType, _property).serialize(null, gen, provider); } else { // One twist: is entry ignorable? If so, skip if ((ignored != null) && ignored.contains(keyElem)) { continue; } keySerializer.serialize(keyElem, gen, provider); } // And then value 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) { // Add reference information wrapAndThrow(provider, e, value, String.valueOf(keyElem)); } }
@SuppressWarnings("unchecked") protected MapSerializer( MapSerializer src, BeanProperty property, JsonSerializer<?> keySerializer, JsonSerializer<?> valueSerializer, Set<String> ignoredEntries) { super(Map.class, false); _ignoredEntries = ((ignoredEntries == null) || ignoredEntries.isEmpty()) ? null : ignoredEntries; _keyType = src._keyType; _valueType = src._valueType; _valueTypeIsStatic = src._valueTypeIsStatic; _valueTypeSerializer = src._valueTypeSerializer; _keySerializer = (JsonSerializer<Object>) keySerializer; _valueSerializer = (JsonSerializer<Object>) valueSerializer; _dynamicValueSerializers = src._dynamicValueSerializers; _property = property; _filterId = src._filterId; _sortKeys = src._sortKeys; _suppressableValue = src._suppressableValue; _suppressNulls = src._suppressNulls; }
@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(); // First: if we have a property, may have property-annotation overrides if ((propertyAcc != null) && (intr != null)) { 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; } // [databind#124]: May have a content converter ser = findContextualConvertingSerializer(provider, 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. // 20-Aug-2013, tatu: Need to avoid trying to access serializer for java.lang.Object tho if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) { ser = provider.findValueSerializer(_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; boolean sortKeys = false; if ((intr != null) && (propertyAcc != null)) { JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnorals(propertyAcc); if (ignorals != null) { Set<String> newIgnored = ignorals.findIgnoredForSerialization(); if ((newIgnored != null) && !newIgnored.isEmpty()) { ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored); for (String str : newIgnored) { ignored.add(str); } } } Boolean b = intr.findSerializationSortAlphabetically(propertyAcc); sortKeys = (b != null) && b.booleanValue(); } 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, sortKeys); // [databind#307]: allow filtering if (property != null) { AnnotatedMember m = property.getMember(); if (m != null) { Object filterId = intr.findFilterId(m); if (filterId != null) { mser = mser.withFilterId(filterId); } } JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), null); 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) { // 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; // 30-Sep-2016, tatu: Should not need to check global flags here, // if inclusion forced to be ALWAYS suppressNulls = false; break; } mser = mser.withContentInclusion(valueToSuppress, suppressNulls); } } } return mser; }