Skip to content

Commit

Permalink
Incremental improvement to mergeability, add per-type overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 22, 2016
1 parent 9cf32e2 commit 9beb512
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 49 deletions.
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.annotation.ObjectIdGenerator;
import com.fasterxml.jackson.annotation.ObjectIdResolver;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.cfg.ConfigOverride;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.*;
import com.fasterxml.jackson.databind.deser.impl.ObjectIdReader;
Expand Down Expand Up @@ -405,6 +406,18 @@ public final JsonNodeFactory getNodeFactory() {
return _config.getNodeFactory();
}

/**
* Convenience method, functionally equivalent to:
*<pre>
* getConfig().findConfigOverride(type);
* </pre>
*
* @since 2.9
*/
public final ConfigOverride findConfigOverride(Class<?> type) {
return _config.findConfigOverride(type);
}

/*
/**********************************************************
/* Public API, pass-through to DeserializerCache
Expand Down
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonSetter;

/**
* Configuration object that is accessed by databinding functionality
Expand Down Expand Up @@ -31,14 +32,21 @@ public abstract class ConfigOverride
*/
protected JsonIgnoreProperties.Value _ignorals;

/**
* Definitions of setter overrides for things like mergeability.
*
* @since 2.9
*/
protected JsonSetter.Value _setterInfo;

/**
* Flag that indicates whether "is ignorable type" is specified for this type;
* and if so, is it to be ignored (true) or not ignored (false); `null` is
* used to indicate "not specified", in which case other configuration (class
* annotation) is used.
*/
protected Boolean _isIgnoredType;

protected ConfigOverride() { }
protected ConfigOverride(ConfigOverride src) {
_format = src._format;
Expand All @@ -47,11 +55,38 @@ protected ConfigOverride(ConfigOverride src) {
_isIgnoredType = src._isIgnoredType;
}

/**
* Accessor for immutable "empty" instance that has no configuration overrides defined.
*
* @since 2.9
*/
public static ConfigOverride empty() {
return Empty.INSTANCE;
}

public JsonFormat.Value getFormat() { return _format; }
public JsonInclude.Value getInclude() { return _include; }

/**
* @since 2.9
*/
public JsonSetter.Value getSetterInfo() { return _setterInfo; }

public JsonIgnoreProperties.Value getIgnorals() { return _ignorals; }

public Boolean getIsIgnoredType() {
return _isIgnoredType;
}

/**
* Implementation used solely for "empty" instance; has no mutators
* and is not changed by core functionality.
*
* @since 2.9
*/
final static class Empty extends ConfigOverride {
final static Empty INSTANCE = new Empty();

private Empty() { }
}
}
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonSetter;

/**
* Extension of {@link ConfigOverride} that allows changing of
Expand All @@ -24,7 +25,7 @@ public class MutableConfigOverride
protected MutableConfigOverride(MutableConfigOverride src) {
super(src);
}

protected MutableConfigOverride copy() {
return new MutableConfigOverride(this);
}
Expand All @@ -33,18 +34,24 @@ public MutableConfigOverride setFormat(JsonFormat.Value v) {
_format = v;
return this;
}

public MutableConfigOverride setInclude(JsonInclude.Value v) {
_include = v;
return this;
}

public MutableConfigOverride setSetterInfo(JsonSetter.Value v) {
_setterInfo = v;
return this;
}

public MutableConfigOverride setIgnorals(JsonIgnoreProperties.Value v) {
_ignorals = v;
return this;
}

public MutableConfigOverride setIsIgnoredType(Boolean v) {

_isIgnoredType = v;
return this;
}
Expand Down
Expand Up @@ -140,15 +140,14 @@ public JsonDeserializer<Object> createBeanDeserializer(DeserializationContext ct
}

@Override
public JsonDeserializer<Object> createBuilderBasedDeserializer(
DeserializationContext ctxt, JavaType valueType, BeanDescription beanDesc,
Class<?> builderClass)
throws JsonMappingException
public JsonDeserializer<Object> createBuilderBasedDeserializer(DeserializationContext ctxt,
JavaType valueType, BeanDescription beanDesc, Class<?> builderClass)
throws JsonMappingException
{
// First: need a BeanDescription for builder class
JavaType builderType = ctxt.constructType(builderClass);
BeanDescription builderDesc = ctxt.getConfig().introspectForBuilder(builderType);
return buildBuilderBasedDeserializer(ctxt, valueType, builderDesc);
// First: need a BeanDescription for builder class
JavaType builderType = ctxt.constructType(builderClass);
BeanDescription builderDesc = ctxt.getConfig().introspectForBuilder(builderType);
return buildBuilderBasedDeserializer(ctxt, valueType, builderDesc);
}

/**
Expand Down Expand Up @@ -512,7 +511,7 @@ protected void addBeanProps(DeserializationContext ctxt,
AnnotatedMethod setter = propDef.getSetter();
JavaType propertyType = setter.getParameterType(0);
prop = constructSettableProperty(ctxt, beanDesc, propDef, propertyType);
if (_isMergeableProperty(ctxt, setter, propertyType)) {
if (_isMergeableProperty(ctxt, setter, propertyType, propDef)) {
AnnotatedMember accessor = propDef.getAccessor();
if (accessor != null) {
accessor.fixAccess(ctxt.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
Expand All @@ -523,7 +522,7 @@ protected void addBeanProps(DeserializationContext ctxt,
AnnotatedField field = propDef.getField();
JavaType propertyType = field.getType();
prop = constructSettableProperty(ctxt, beanDesc, propDef, propertyType);
if (_isMergeableProperty(ctxt, field, propertyType)) {
if (_isMergeableProperty(ctxt, field, propertyType, propDef)) {
AnnotatedMember accessor = propDef.getAccessor();
if (accessor != null) {
accessor.fixAccess(ctxt.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
Expand Down Expand Up @@ -587,7 +586,7 @@ protected void addBeanProps(DeserializationContext ctxt,
}

protected boolean _isMergeableProperty(DeserializationContext ctxt,
AnnotatedMember accessor, JavaType type)
AnnotatedMember accessor, JavaType type, BeanPropertyDefinition propDef)
{
AnnotationIntrospector ai = ctxt.getAnnotationIntrospector();
if (ai != null) {
Expand All @@ -599,6 +598,14 @@ protected boolean _isMergeableProperty(DeserializationContext ctxt,
}
}
}
ConfigOverride override = propDef.findConfigOverride(type.getRawClass());
JsonSetter.Value v = override.getSetterInfo();
if (v != null) {
Boolean b = v.getMerge();
if (b != null) {
return b.booleanValue();
}
}
return false;
}

Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.ConfigOverride;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.Named;

Expand All @@ -22,6 +23,8 @@ public abstract class BeanPropertyDefinition
{
protected final static JsonInclude.Value EMPTY_INCLUDE = JsonInclude.Value.empty();

protected final static ConfigOverride EMPTY_OVERRIDES = ConfigOverride.empty();

/*
/**********************************************************
/* Fluent factory methods for creating modified copies
Expand Down Expand Up @@ -238,4 +241,13 @@ public boolean isRequired() {
public JsonInclude.Value findInclusion() {
return EMPTY_INCLUDE;
}

/**
* Accessor for finding per-type overrides applicable to this property.
*
* @since 2.9
*/
public ConfigOverride findConfigOverride(Class<?> propType) {
return EMPTY_OVERRIDES;
}
}
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.ConfigOverride;
import com.fasterxml.jackson.databind.cfg.MapperConfig;
import com.fasterxml.jackson.databind.util.ClassUtil;

Expand Down Expand Up @@ -47,6 +48,11 @@ public class POJOPropertyBuilder

protected Linked<AnnotatedMethod> _setters;

/**
* @since 2.9
*/
protected transient ConfigOverride _configOverride;

public POJOPropertyBuilder(MapperConfig<?> config, AnnotationIntrospector ai,
boolean forSerialization, PropertyName internalName) {
this(config, ai, forSerialization, internalName, internalName);
Expand Down Expand Up @@ -561,6 +567,17 @@ public JsonInclude.Value findInclusion() {
return (v == null) ? JsonInclude.Value.empty() : v;
}

@Override // since 2.9
public ConfigOverride findConfigOverride(Class<?> propType) {
if (_configOverride == null) {
_configOverride = _config.findConfigOverride(propType);
if (_configOverride == null) {
_configOverride = ConfigOverride.empty();
}
}
return _configOverride;
}

public JsonProperty.Access findAccess() {
return fromMemberAnnotationsExcept(new WithMember<JsonProperty.Access>() {
@Override
Expand Down

0 comments on commit 9beb512

Please sign in to comment.