@Test public void serializingThenDeserializingWithConfiguredObjectMapperShouldWork() throws IOException { // given this.mapper.setDefaultPropertyInclusion(construct(ALWAYS, NON_NULL)).setSerializationInclusion(NON_ABSENT); UsernamePasswordAuthenticationToken original = new UsernamePasswordAuthenticationToken("Frodo", null); // when String serialized = this.mapper.writeValueAsString(original); UsernamePasswordAuthenticationToken deserialized = this.mapper.readValue(serialized, UsernamePasswordAuthenticationToken.class); // then assertThat(deserialized).isEqualTo(original); }
return prov.reportBadPropertyDefinition(_beanDesc, propDef, ClassUtil.exceptionMessage(e)); JavaType ct = serializationType.getContentType(); prov.reportBadPropertyDefinition(_beanDesc, propDef, "serialization type "+serializationType+" has no content"); serializationType = serializationType.withContentTypeHandler(contentTypeSer); ct = serializationType.getContentType(); JsonInclude.Value inclV = _config.getDefaultInclusion(actualType.getRawClass(), rawPropertyType, _defaultInclusion); inclV = inclV.withOverrides(propDef.findInclusion()); JsonInclude.Include inclusion = inclV.getValueInclusion(); if (inclusion == JsonInclude.Include.USE_DEFAULTS) { // should not occur but... inclusion = JsonInclude.Include.ALWAYS; am.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); break; case CUSTOM: // new with 2.9 valueToSuppress = prov.includeFilterInstance(propDef, inclV.getValueFilter()); if (valueToSuppress == null) { // is this legal? suppressNulls = true; Object serDef = _annotationIntrospector.findNullSerializer(am);
public PropertyBuilder(SerializationConfig config, BeanDescription beanDesc) { _config = config; _beanDesc = beanDesc; // 08-Sep-2016, tatu: This gets tricky, with 3 levels of definitions: // (a) global default inclusion // (b) per-type default inclusion (from annotation or config overrides; // config override having precedence) // (c) per-property override (from annotation on specific property or // config overrides per type of property; // annotation having precedence) // // and not only requiring merging, but also considering special handling // for NON_DEFAULT in case of (b) (vs (a) or (c)) JsonInclude.Value inclPerType = JsonInclude.Value.merge( beanDesc.findPropertyInclusion(JsonInclude.Value.empty()), config.getDefaultPropertyInclusion(beanDesc.getBeanClass(), JsonInclude.Value.empty())); _defaultInclusion = JsonInclude.Value.merge(config.getDefaultPropertyInclusion(), inclPerType); _useRealPropertyDefaults = inclPerType.getValueInclusion() == JsonInclude.Include.NON_DEFAULT; _annotationIntrospector = _config.getAnnotationIntrospector(); }
final AnnotationIntrospector intr = provider.getAnnotationIntrospector(); final AnnotatedMember propertyAcc = (property == null) ? null : property.getMember(); 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 (_valueTypeIsStatic && !_valueType.isJavaLangObject()) { ser = provider.findValueSerializer(_valueType, property); JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), null); if (inclV != null) { JsonInclude.Include incl = inclV.getContentInclusion(); if (incl != JsonInclude.Include.USE_DEFAULTS) { switch (incl) { break; case CUSTOM: valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter()); if (valueToSuppress == null) { // is this legal? suppressNulls = true;
serializationType = findSerializationType(am, defaultUseStaticTyping, declaredType); } catch (JsonMappingException e) { return prov.reportBadPropertyDefinition(_beanDesc, propDef, e.getMessage()); JavaType ct = serializationType.getContentType(); prov.reportBadPropertyDefinition(_beanDesc, propDef, "serialization type "+serializationType+" has no content"); serializationType = serializationType.withContentTypeHandler(contentTypeSer); ct = serializationType.getContentType(); JsonInclude.Value inclV = _config.getDefaultPropertyInclusion(actualType.getRawClass(), _defaultInclusion); inclV = inclV.withOverrides(propDef.findInclusion()); JsonInclude.Include inclusion = inclV.getValueInclusion(); if (prov.isEnabled(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS)) { am.fixAccess(_config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS)); } else { if (valueToSuppress.getClass().isArray()) { valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress); if (actualType.isContainerType() && !_config.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)) { valueToSuppress = BeanPropertyWriter.MARKER_FOR_EMPTY;
throws JsonMappingException final SerializationConfig config = prov.getConfig(); JsonInclude.Value inclV = beanDesc.findPropertyInclusion(config.getDefaultPropertyInclusion()); inclV = config.getDefaultPropertyInclusion(configType, inclV); JsonInclude.Value valueIncl = config.getDefaultPropertyInclusion(contentType.getRawClass(), null); switch (valueIncl.getValueInclusion()) { case USE_DEFAULTS: break; case CUSTOM: inclV = inclV.withContentFilter(valueIncl.getContentFilter()); break; default: inclV = inclV.withContentInclusion(valueIncl.getValueInclusion());
@Override public JsonInclude.Value findPropertyInclusion(MapperConfig<?> config, Class<?> baseType) { JsonInclude.Value v0 = config.getDefaultInclusion(baseType, _type.getRawClass()); AnnotationIntrospector intr = config.getAnnotationIntrospector(); if ((intr == null) || (_member == null)) { return v0; } JsonInclude.Value v = intr.findPropertyInclusion(_member); if (v == null) { return v0; } return v0.withOverrides(v); }
final AnnotationIntrospector intr = provider.getAnnotationIntrospector(); final AnnotatedMember propertyAcc = (property == null) ? null : property.getMember(); Object suppressableValue = _suppressableValue; 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); JsonInclude.Include incl = inclV.getContentInclusion(); if ((incl != null) && (incl != JsonInclude.Include.USE_DEFAULTS)) { suppressableValue = incl; if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) { ser = provider.findValueSerializer(_valueType, property); boolean sortKeys = false; if ((intr != null) && (propertyAcc != null)) { JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnorals(propertyAcc); if (ignorals != null){ Set<String> newIgnored = ignorals.findIgnoredForSerialization();
ser = provider.handlePrimaryContextualization(ser, property); JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), handledType()); if (inclV != null) { JsonInclude.Include incl = inclV.getContentInclusion(); if (valueToSuppress != null) { if (valueToSuppress.getClass().isArray()) { valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress); case NON_ABSENT: suppressNulls = true; valueToSuppress = _referredType.isReferenceType() ? MARKER_FOR_EMPTY : null; break; case NON_EMPTY: break; case CUSTOM: valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter()); if (valueToSuppress == null) { // is this legal? suppressNulls = true;
void writeField(JsonGenerator generator, SerializerProvider provider, String name, String value) throws IOException { switch (provider.getConfig().getDefaultPropertyInclusion().getValueInclusion()) { case NON_NULL: case NON_ABSENT: if (value == null) return; break; case NON_EMPTY: if (value == null || value.isEmpty()) return; break; default: case ALWAYS: // Allow. break; } generator.writeStringField(name, value); } }
JsonInclude.Include incl = (inclV == null) ? JsonInclude.Include.USE_DEFAULTS : inclV.getContentInclusion(); if (incl == JsonInclude.Include.USE_DEFAULTS || incl == JsonInclude.Include.ALWAYS) { if (!prov.isEnabled(SerializationFeature.WRITE_NULL_MAP_VALUES)) { return mapSer.withContentInclusion(null, true); if (valueToSuppress != null) { if (valueToSuppress.getClass().isArray()) { valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress); case NON_ABSENT: // new with 2.6, to support Guava/JDK8 Optionals valueToSuppress = contentType.isReferenceType() ? MapSerializer.MARKER_FOR_EMPTY : null; break; break; case CUSTOM: // new with 2.9 valueToSuppress = prov.includeFilterInstance(null, inclV.getContentFilter()); if (valueToSuppress == null) { // is this legal? suppressNulls = true; } else { suppressNulls = prov.includeFilterSuppressNulls(valueToSuppress);
JsonFormat.Value formatOverride = prov.getDefaultPropertyFormat(Map.Entry.class); JsonFormat.Value formatFromAnnotation = beanDesc.findExpectedFormat(null); JsonFormat.Value format = JsonFormat.Value.merge(formatFromAnnotation, formatOverride); staticTyping, createTypeSerializer(prov.getConfig(), valueType), null); JsonInclude.Include incl = (inclV == null) ? JsonInclude.Include.USE_DEFAULTS : inclV.getContentInclusion(); if (incl == JsonInclude.Include.USE_DEFAULTS || incl == JsonInclude.Include.ALWAYS) { if (valueToSuppress != null) { if (valueToSuppress.getClass().isArray()) { valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress); valueToSuppress = contentType.isReferenceType() ? MapSerializer.MARKER_FOR_EMPTY : null; break; break; case CUSTOM: valueToSuppress = prov.includeFilterInstance(null, inclV.getContentFilter()); if (valueToSuppress == null) { // is this legal? suppressNulls = true;
ser = provider.handlePrimaryContextualization(ser, property); JsonInclude.Value incl = property.findPropertyInclusion(provider.getConfig(), Optional.class); JsonInclude.Include newIncl = incl.getContentInclusion(); if ((newIncl != contentIncl) && (newIncl != JsonInclude.Include.USE_DEFAULTS)) { contentIncl = newIncl;
static boolean mustIncludeNull(SerializationConfig config, Class<? extends Enumerable> enumerableType) { try { // getDefaultPropertyInclusion exists since Jackson 2.7 JsonInclude.Value inclusion = config.getDefaultPropertyInclusion(enumerableType); if (inclusion != null) return !SKIP_NULL_INCLUSIONS.contains(inclusion.getValueInclusion().name()); } catch (LinkageError le) { LOGGER.log(Level.FINEST, "SerializationConfig.getDefaultPropertyInclusion() unavailable. Using Jackson < 2.7?", le); } try { JsonInclude.Include inclusion = config.getSerializationInclusion(); if (inclusion != null) return !SKIP_NULL_INCLUSIONS.contains(inclusion.name()); } catch (LinkageError le) { LOGGER.log(Level.FINEST, "SerializationConfig.getSerializationInclusion() unavailable. Deprecation removed?", le); } return true; }
/** * @deprecated Since 2.7 use {@link #getDefaultPropertyInclusion} instead */ @Deprecated public JsonInclude.Include getSerializationInclusion() { JsonInclude.Include incl = getDefaultPropertyInclusion().getValueInclusion(); return (incl == JsonInclude.Include.USE_DEFAULTS) ? JsonInclude.Include.ALWAYS : incl; }
@Override public JsonInclude.Value findPropertyInclusion(Annotated a) { JsonInclude.Value v2 = _secondary.findPropertyInclusion(a); JsonInclude.Value v1 = _primary.findPropertyInclusion(a); if (v2 == null) { // shouldn't occur but return v1; } return v2.withOverrides(v1); }
ser = provider.handlePrimaryContextualization(ser, property); JsonInclude.Include newIncl = incl.getContentInclusion(); if ((newIncl != contentIncl) && (newIncl != JsonInclude.Include.USE_DEFAULTS)) { contentIncl = newIncl;
/** * Factory method to use for constructing an instance for components */ public static Value construct(Include valueIncl, Include contentIncl) { if (((valueIncl == Include.USE_DEFAULTS) || (valueIncl == null)) && ((contentIncl == Include.USE_DEFAULTS) || (contentIncl == null))) { return EMPTY; } return new Value(valueIncl, contentIncl, null, null); }
@Override public final JsonInclude.Value getDefaultPropertyInclusion(Class<?> baseType) { JsonInclude.Value v = getConfigOverride(baseType).getInclude(); JsonInclude.Value def = getDefaultPropertyInclusion(); if (def == null) { return v; } return def.withOverrides(v); }
@Override public JsonInclude.Value findPropertyInclusion(Annotated a) { JsonInclude inc = _findAnnotation(a, JsonInclude.class); JsonInclude.Value value = (inc == null) ? JsonInclude.Value.empty() : JsonInclude.Value.from(inc); // only consider deprecated variant if we didn't have non-deprecated one: if (value.getValueInclusion() == JsonInclude.Include.USE_DEFAULTS) { value = _refinePropertyInclusion(a, value); } return value; }