Skip to content

Commit

Permalink
Implemented #607, #608
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 8, 2014
1 parent fefe1bd commit 4fc7cb0
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 141 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ Project: jackson-databind
#597: Improve error messaging for cases where JSON Creator returns null (which
is illegal)
(contributed by Aurelien L)
#607: Allow (re)config of `JsonParser.Feature`s via `ObjectReader`
#608: Allow (re)config of `JsonGenerator.Feature`s via `ObjectWriter`
- Allow use of `Shape.ARRAY` for Enums, as an alias to 'use index'
- Start using `JsonGenerator.writeStartArray(int)` to help data formats
that benefit from knowing number of elements in arrays (and would otherwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public final class DeserializationConfig
/**
* Bitflag of {@link JsonParser.Feature}s to enable/disable
*/
protected final int _parserFeatureMask;
protected final int _parserFeaturesToChange;

/*
/**********************************************************
Expand All @@ -87,9 +87,21 @@ public DeserializationConfig(BaseSettings base,
_nodeFactory = JsonNodeFactory.instance;
_problemHandlers = null;
_parserFeatures = 0;
_parserFeatureMask = 0;
_parserFeaturesToChange = 0;
}

private DeserializationConfig(DeserializationConfig src,
int mapperFeatures, int deserFeatures,
int parserFeatures, int parserFeatureMask)
{
super(src, mapperFeatures);
_deserFeatures = deserFeatures;
_nodeFactory = src._nodeFactory;
_problemHandlers = src._problemHandlers;
_parserFeatures = parserFeatures;
_parserFeaturesToChange = parserFeatureMask;
}

/**
* Copy constructor used to create a non-shared instance with given mix-in
* annotation definitions and subtype resolver.
Expand All @@ -101,19 +113,7 @@ private DeserializationConfig(DeserializationConfig src, SubtypeResolver str)
_nodeFactory = src._nodeFactory;
_problemHandlers = src._problemHandlers;
_parserFeatures = src._parserFeatures;
_parserFeatureMask = src._parserFeatureMask;
}

private DeserializationConfig(DeserializationConfig src,
int mapperFeatures, int deserFeatures,
int parserFeatures, int parserFeatureMask)
{
super(src, mapperFeatures);
_deserFeatures = deserFeatures;
_nodeFactory = src._nodeFactory;
_problemHandlers = src._problemHandlers;
_parserFeatures = parserFeatures;
_parserFeatureMask = parserFeatureMask;
_parserFeaturesToChange = src._parserFeaturesToChange;
}

private DeserializationConfig(DeserializationConfig src, BaseSettings base)
Expand All @@ -123,7 +123,7 @@ private DeserializationConfig(DeserializationConfig src, BaseSettings base)
_nodeFactory = src._nodeFactory;
_problemHandlers = src._problemHandlers;
_parserFeatures = src._parserFeatures;
_parserFeatureMask = src._parserFeatureMask;
_parserFeaturesToChange = src._parserFeaturesToChange;
}

private DeserializationConfig(DeserializationConfig src, JsonNodeFactory f)
Expand All @@ -133,7 +133,7 @@ private DeserializationConfig(DeserializationConfig src, JsonNodeFactory f)
_problemHandlers = src._problemHandlers;
_nodeFactory = f;
_parserFeatures = src._parserFeatures;
_parserFeatureMask = src._parserFeatureMask;
_parserFeaturesToChange = src._parserFeaturesToChange;
}

private DeserializationConfig(DeserializationConfig src,
Expand All @@ -144,7 +144,7 @@ private DeserializationConfig(DeserializationConfig src,
_problemHandlers = problemHandlers;
_nodeFactory = src._nodeFactory;
_parserFeatures = src._parserFeatures;
_parserFeatureMask = src._parserFeatureMask;
_parserFeaturesToChange = src._parserFeaturesToChange;
}

private DeserializationConfig(DeserializationConfig src, String rootName)
Expand All @@ -154,7 +154,7 @@ private DeserializationConfig(DeserializationConfig src, String rootName)
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
_parserFeatures = src._parserFeatures;
_parserFeatureMask = src._parserFeatureMask;
_parserFeaturesToChange = src._parserFeaturesToChange;
}

private DeserializationConfig(DeserializationConfig src, Class<?> view)
Expand All @@ -164,7 +164,7 @@ private DeserializationConfig(DeserializationConfig src, Class<?> view)
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
_parserFeatures = src._parserFeatures;
_parserFeatureMask = src._parserFeatureMask;
_parserFeaturesToChange = src._parserFeaturesToChange;
}

/**
Expand All @@ -177,7 +177,7 @@ protected DeserializationConfig(DeserializationConfig src, Map<ClassKey,Class<?>
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
_parserFeatures = src._parserFeatures;
_parserFeatureMask = src._parserFeatureMask;
_parserFeaturesToChange = src._parserFeaturesToChange;
}

/**
Expand All @@ -190,7 +190,7 @@ protected DeserializationConfig(DeserializationConfig src, ContextAttributes att
_problemHandlers = src._problemHandlers;
_nodeFactory = src._nodeFactory;
_parserFeatures = src._parserFeatures;
_parserFeatureMask = src._parserFeatureMask;
_parserFeaturesToChange = src._parserFeaturesToChange;
}

// for unit tests only:
Expand All @@ -211,7 +211,7 @@ public DeserializationConfig with(MapperFeature... features)
}
return (newMapperFlags == _mapperFeatures) ? this :
new DeserializationConfig(this, newMapperFlags, _deserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);

}

Expand All @@ -224,7 +224,7 @@ public DeserializationConfig without(MapperFeature... features)
}
return (newMapperFlags == _mapperFeatures) ? this :
new DeserializationConfig(this, newMapperFlags, _deserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);
}

@Override
Expand All @@ -238,7 +238,7 @@ public DeserializationConfig with(MapperFeature feature, boolean state)
}
return (newMapperFlags == _mapperFeatures) ? this :
new DeserializationConfig(this, newMapperFlags, _deserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);
}

@Override
Expand Down Expand Up @@ -357,7 +357,7 @@ public DeserializationConfig with(DeserializationFeature feature)
int newDeserFeatures = (_deserFeatures | feature.getMask());
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);
}

/**
Expand All @@ -373,7 +373,7 @@ public DeserializationConfig with(DeserializationFeature first,
}
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);
}

/**
Expand All @@ -388,7 +388,7 @@ public DeserializationConfig withFeatures(DeserializationFeature... features)
}
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);
}

/**
Expand All @@ -400,7 +400,7 @@ public DeserializationConfig without(DeserializationFeature feature)
int newDeserFeatures = _deserFeatures & ~feature.getMask();
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);
}

/**
Expand All @@ -416,7 +416,7 @@ public DeserializationConfig without(DeserializationFeature first,
}
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);
}

/**
Expand All @@ -431,7 +431,7 @@ public DeserializationConfig withoutFeatures(DeserializationFeature... features)
}
return (newDeserFeatures == _deserFeatures) ? this :
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
_parserFeatures, _parserFeatureMask);
_parserFeatures, _parserFeaturesToChange);
}

/*
Expand All @@ -443,61 +443,69 @@ public DeserializationConfig withoutFeatures(DeserializationFeature... features)
/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified features enabled.
*
* @since 2.5
*/
public DeserializationConfig with(JsonParser.Feature feature)
{
int newSet = _parserFeatures | feature.getMask();
int newMask = _parserFeatureMask | feature.getMask();
return ((_parserFeatures == newSet) && (_parserFeatureMask == newMask)) ? this :
int newMask = _parserFeaturesToChange | feature.getMask();
return ((_parserFeatures == newSet) && (_parserFeaturesToChange == newMask)) ? this :
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
newSet, newMask);
}

/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified features enabled.
*
* @since 2.5
*/
public DeserializationConfig withFeatures(JsonParser.Feature... features)
{
int newSet = _parserFeatures;
int newMask = _parserFeatureMask;
int newMask = _parserFeaturesToChange;
for (JsonParser.Feature f : features) {
int mask = f.getMask();
newSet |= mask;
newMask |= mask;
}
return ((_parserFeatures == newSet) && (_parserFeatureMask == newMask)) ? this :
return ((_parserFeatures == newSet) && (_parserFeaturesToChange == newMask)) ? this :
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
newSet, newMask);
}

/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified feature disabled.
*
* @since 2.5
*/
public DeserializationConfig without(JsonParser.Feature feature)
{
int newSet = _parserFeatureMask & ~feature.getMask();
int newSet = _parserFeaturesToChange & ~feature.getMask();
int newMask = _parserFeatures | feature.getMask();
return ((_parserFeatures == newSet) && (_parserFeatureMask == newMask)) ? this :
return ((_parserFeatures == newSet) && (_parserFeaturesToChange == newMask)) ? this :
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
newSet, newMask);
}

/**
* Fluent factory method that will construct and return a new configuration
* object instance with specified features disabled.
*
* @since 2.5
*/
public DeserializationConfig withoutFeatures(JsonParser.Feature... features)
{
int newSet = _parserFeatures;
int newMask = _parserFeatureMask;
int newMask = _parserFeaturesToChange;
for (JsonParser.Feature f : features) {
int mask = f.getMask();
newSet &= ~mask;
newMask |= mask;
}
return ((_parserFeatures == newSet) && (_parserFeatureMask == newMask)) ? this :
return ((_parserFeatures == newSet) && (_parserFeaturesToChange == newMask)) ? this :
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
newSet, newMask);
}
Expand Down Expand Up @@ -552,16 +560,16 @@ public DeserializationConfig withNoProblemHandlers() {
*/

/**
* Method called by {@link ObjectMapper} and {@link ObjectWriter}
* to modify those {@link com.fasterxml.jackson.core.JsonGenerator.Feature} settings
* Method called by {@link ObjectMapper} and {@link ObjectReader}
* to modify those {@link com.fasterxml.jackson.core.JsonParser.Feature} settings
* that have been configured via this config instance.
*
* @since 2.5
*/
public void initialize(JsonParser p) {
if (_parserFeatureMask != 0) {
if (_parserFeaturesToChange != 0) {
int orig = p.getFeatureMask();
int newFlags = (orig & ~_parserFeatureMask) | _parserFeatures;
int newFlags = (orig & ~_parserFeaturesToChange) | _parserFeatures;
if (orig != newFlags) {
p.setFeatureMask(newFlags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,21 @@ public enum DeserializationFeature implements ConfigFeature
;

private final boolean _defaultState;
private final int _mask;

private DeserializationFeature(boolean defaultState) {
_defaultState = defaultState;
_mask = (1 << ordinal());
}

@Override
public boolean enabledByDefault() { return _defaultState; }

@Override
public int getMask() { return (1 << ordinal()); }
public int getMask() { return _mask; }

/**
* @since 2.5
*/
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,21 @@ public enum MapperFeature implements ConfigFeature
;

private final boolean _defaultState;
private final int _mask;

private MapperFeature(boolean defaultState) {
_defaultState = defaultState;
_mask = (1 << ordinal());
}

@Override
public boolean enabledByDefault() { return _defaultState; }

@Override
public int getMask() { return (1 << ordinal()); }
public int getMask() { return _mask; }

/**
* @since 2.5
*/
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
}
Loading

0 comments on commit 4fc7cb0

Please sign in to comment.