Skip to content

Commit

Permalink
Add global default settings for setter info (JsonSetter.Value) to all…
Browse files Browse the repository at this point in the history
…ow for default merging. Also MapperFeature to avoid exceptions.
  • Loading branch information
cowtowncoder committed Oct 23, 2016
1 parent 6faae87 commit 4f3f4ff
Show file tree
Hide file tree
Showing 19 changed files with 438 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ public final class DeserializationConfig

/*
/**********************************************************
/* Life-cycle, constructors
/* Life-cycle, primary constructors for new instances
/**********************************************************
*/

/**
* Constructor used by ObjectMapper to create default configuration object instance.
*/
public DeserializationConfig(BaseSettings base,
SubtypeResolver str, SimpleMixInResolver mixins,
RootNameLookup rootNames, ConfigOverrides configOverrides)
SubtypeResolver str, SimpleMixInResolver mixins, RootNameLookup rootNames,
ConfigOverrides configOverrides)
{
super(base, str, mixins, rootNames, configOverrides);
_deserFeatures = collectFeatureDefaults(DeserializationFeature.class);
Expand All @@ -114,6 +114,32 @@ public DeserializationConfig(BaseSettings base,
_formatReadFeaturesToChange = 0;
}

/**
* Copy-constructor used for making a copy used by new {@link ObjectMapper}.
*
* @since 2.9
*/
protected DeserializationConfig(DeserializationConfig src,
SimpleMixInResolver mixins, RootNameLookup rootNames,
ConfigOverrides configOverrides)
{
super(src, mixins, rootNames, configOverrides);
_deserFeatures = src._deserFeatures;
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
_parserFeatures = src._parserFeatures;
_parserFeaturesToChange = src._parserFeaturesToChange;
_formatReadFeatures = src._formatReadFeatures;
_formatReadFeaturesToChange = src._formatReadFeaturesToChange;
}

/*
/**********************************************************
/* Life-cycle, secondary constructors to support
/* "mutant factories", with single property changes
/**********************************************************
*/

private DeserializationConfig(DeserializationConfig src,
int mapperFeatures, int deserFeatures,
int parserFeatures, int parserFeatureMask,
Expand Down Expand Up @@ -230,24 +256,6 @@ protected DeserializationConfig(DeserializationConfig src, SimpleMixInResolver m
_formatReadFeaturesToChange = src._formatReadFeaturesToChange;
}

/**
* Copy-constructor used for making a copy used by new {@link ObjectMapper}.
*
* @since 2.8
*/
protected DeserializationConfig(DeserializationConfig src, SimpleMixInResolver mixins,
RootNameLookup rootNames, ConfigOverrides configOverrides)
{
super(src, mixins, rootNames, configOverrides);
_deserFeatures = src._deserFeatures;
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
_parserFeatures = src._parserFeatures;
_parserFeaturesToChange = src._parserFeaturesToChange;
_formatReadFeatures = src._formatReadFeatures;
_formatReadFeaturesToChange = src._formatReadFeaturesToChange;
}

// for unit tests only:
protected BaseSettings getBaseSettings() { return _base; }

Expand Down Expand Up @@ -723,17 +731,6 @@ public void initialize(JsonParser p) {
}
}

/*
/**********************************************************
/* Configuration: default settings with per-type overrides
/**********************************************************
*/

@Override
public JsonInclude.Value getDefaultPropertyInclusion() {
return EMPTY_INCLUDE;
}

/*
/**********************************************************
/* MapperConfig implementation/overrides: other
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public abstract T deserialize(JsonParser p, DeserializationContext ctxt)
public T deserialize(JsonParser p, DeserializationContext ctxt, T intoValue)
throws IOException, JsonProcessingException
{
if (ctxt.isEnabled(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)) {
return deserialize(p, ctxt);
}
throw new UnsupportedOperationException("Can not update object of type "
+intoValue.getClass().getName()+" (by deserializer of type "+getClass().getName()+")");
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/com/fasterxml/jackson/databind/MapperFeature.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,21 @@ public enum MapperFeature implements ConfigFeature
*
* @since 2.5
*/
IGNORE_DUPLICATE_MODULE_REGISTRATIONS(true)
IGNORE_DUPLICATE_MODULE_REGISTRATIONS(true),

/**
* Setting that determines what happens if an attempt is made to "merge"
* value of a property, where value does not support merging; either
* merging is skipped and new value is created (<code>true</code>) or
* an exception is thrown (false).
* This feature is most relevant when default setter info has been changed
* with {@link com.fasterxml.jackson.databind.ObjectMapper#setDefaultSetterInfo}.
*<p>
* Feature is enabled by default.
*
* @since 2.9
*/
IGNORE_MERGE_FOR_UNMERGEABLE(true)

;

Expand Down
82 changes: 56 additions & 26 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class ObjectMapper
implements Versioned,
java.io.Serializable // as of 2.1
{
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L; // as of 2.9

/*
/**********************************************************
Expand Down Expand Up @@ -341,9 +341,9 @@ public boolean useForType(JavaType t)
* Currently active per-type configuration overrides, accessed by
* declared type of property.
*
* @since 2.8
* @since 2.9
*/
protected ConfigOverrides _propertyOverrides;
protected final ConfigOverrides _configOverrides;

/*
/**********************************************************
Expand Down Expand Up @@ -504,12 +504,14 @@ protected ObjectMapper(ObjectMapper src)
_subtypeResolver = src._subtypeResolver;
_typeFactory = src._typeFactory;
_injectableValues = src._injectableValues;
_propertyOverrides = src._propertyOverrides.copy();
_configOverrides = src._configOverrides.copy();
_mixIns = src._mixIns.copy();

RootNameLookup rootNames = new RootNameLookup();
_serializationConfig = new SerializationConfig(src._serializationConfig, _mixIns, rootNames, _propertyOverrides);
_deserializationConfig = new DeserializationConfig(src._deserializationConfig, _mixIns, rootNames, _propertyOverrides);
_serializationConfig = new SerializationConfig(src._serializationConfig,
_mixIns, rootNames, _configOverrides);
_deserializationConfig = new DeserializationConfig(src._deserializationConfig,
_mixIns, rootNames, _configOverrides);
_serializerProvider = src._serializerProvider.copy();
_deserializationContext = src._deserializationContext.copy();

Expand Down Expand Up @@ -562,12 +564,11 @@ public ObjectMapper(JsonFactory jf,
SimpleMixInResolver mixins = new SimpleMixInResolver(null);
_mixIns = mixins;
BaseSettings base = DEFAULT_BASE.withClassIntrospector(defaultClassIntrospector());
ConfigOverrides propOverrides = new ConfigOverrides();
_propertyOverrides = propOverrides;
_configOverrides = new ConfigOverrides();
_serializationConfig = new SerializationConfig(base,
_subtypeResolver, mixins, rootNames, propOverrides);
_subtypeResolver, mixins, rootNames, _configOverrides);
_deserializationConfig = new DeserializationConfig(base,
_subtypeResolver, mixins, rootNames, propOverrides);
_subtypeResolver, mixins, rootNames, _configOverrides);

// Some overrides we may need
final boolean needOrder = _jsonFactory.requiresPropertyOrdering();
Expand Down Expand Up @@ -1190,7 +1191,7 @@ public void setMixInAnnotations(Map<Class<?>, Class<?>> sourceMixins) {
public final void addMixInAnnotations(Class<?> target, Class<?> mixinSource) {
addMixIn(target, mixinSource);
}

/*
/**********************************************************
/* Configuration, introspection
Expand Down Expand Up @@ -1332,6 +1333,27 @@ public PropertyNamingStrategy getPropertyNamingStrategy() {
return _serializationConfig.getPropertyNamingStrategy();
}

/**
* Method for specifying {@link PrettyPrinter} to use when "default pretty-printing"
* is enabled (by enabling {@link SerializationFeature#INDENT_OUTPUT})
*
* @param pp Pretty printer to use by default.
*
* @return This mapper, useful for call-chaining
*
* @since 2.6
*/
public ObjectMapper setDefaultPrettyPrinter(PrettyPrinter pp) {
_serializationConfig = _serializationConfig.withDefaultPrettyPrinter(pp);
return this;
}

/*
/**********************************************************
/* Configuration: global-default/per-type override settings
/**********************************************************
*/

/**
* Convenience method, equivalent to calling:
*<pre>
Expand All @@ -1344,27 +1366,35 @@ public ObjectMapper setSerializationInclusion(JsonInclude.Include incl) {
}

/**
* Method for setting default POJO property inclusion strategy for serialization.
*
* @since 2.7
* @deprecated Since 2.9 use {@link #setDefaultPropertyInclusion}
*/
@Deprecated
public ObjectMapper setPropertyInclusion(JsonInclude.Value incl) {
_serializationConfig = _serializationConfig.withPropertyInclusion(incl);
return this;
return setDefaultPropertyInclusion(incl);
}

/**
* Method for specifying {@link PrettyPrinter} to use when "default pretty-printing"
* is enabled (by enabling {@link SerializationFeature#INDENT_OUTPUT})
*
* @param pp Pretty printer to use by default.
*
* @return This mapper, useful for call-chaining
*
* @since 2.6
* Method for setting default POJO property inclusion strategy for serialization,
* applied for all properties for which there are no per-type or per-property
* overrides (via annotations or config overrides).
*
* @since 2.9 (basically rename of <code>setPropertyInclusion</code>)
*/
public ObjectMapper setDefaultPrettyPrinter(PrettyPrinter pp) {
_serializationConfig = _serializationConfig.withDefaultPrettyPrinter(pp);
public ObjectMapper setDefaultPropertyInclusion(JsonInclude.Value incl) {
_configOverrides.setDefaultInclusion(incl);
return this;
}

/**
* Method for setting default Setter configuration, regarding things like
* merging, null-handling; used for properties for which there are
* no per-type or per-property overrides (via annotations or config overrides).
*
* @since 2.9
*/
public ObjectMapper setDefaultSetterInfo(JsonSetter.Value v) {
_configOverrides.setDefaultSetterInfo(v);
return this;
}

Expand Down Expand Up @@ -1510,7 +1540,7 @@ public void registerSubtypes(NamedType... types) {
* @since 2.8
*/
public MutableConfigOverride configOverride(Class<?> type) {
return _propertyOverrides.findOrCreateOverride(type);
return _configOverrides.findOrCreateOverride(type);
}

/*
Expand Down
Loading

0 comments on commit 4f3f4ff

Please sign in to comment.