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, ""); } }
/** * 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)); } } }