Skip to content

Commit 4fc7cb0

Browse files
committed
Implemented #607, #608
1 parent fefe1bd commit 4fc7cb0

File tree

9 files changed

+349
-141
lines changed

9 files changed

+349
-141
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Project: jackson-databind
3737
#597: Improve error messaging for cases where JSON Creator returns null (which
3838
is illegal)
3939
(contributed by Aurelien L)
40+
#607: Allow (re)config of `JsonParser.Feature`s via `ObjectReader`
41+
#608: Allow (re)config of `JsonGenerator.Feature`s via `ObjectWriter`
4042
- Allow use of `Shape.ARRAY` for Enums, as an alias to 'use index'
4143
- Start using `JsonGenerator.writeStartArray(int)` to help data formats
4244
that benefit from knowing number of elements in arrays (and would otherwise

src/main/java/com/fasterxml/jackson/databind/DeserializationConfig.java

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public final class DeserializationConfig
6868
/**
6969
* Bitflag of {@link JsonParser.Feature}s to enable/disable
7070
*/
71-
protected final int _parserFeatureMask;
71+
protected final int _parserFeaturesToChange;
7272

7373
/*
7474
/**********************************************************
@@ -87,9 +87,21 @@ public DeserializationConfig(BaseSettings base,
8787
_nodeFactory = JsonNodeFactory.instance;
8888
_problemHandlers = null;
8989
_parserFeatures = 0;
90-
_parserFeatureMask = 0;
90+
_parserFeaturesToChange = 0;
9191
}
9292

93+
private DeserializationConfig(DeserializationConfig src,
94+
int mapperFeatures, int deserFeatures,
95+
int parserFeatures, int parserFeatureMask)
96+
{
97+
super(src, mapperFeatures);
98+
_deserFeatures = deserFeatures;
99+
_nodeFactory = src._nodeFactory;
100+
_problemHandlers = src._problemHandlers;
101+
_parserFeatures = parserFeatures;
102+
_parserFeaturesToChange = parserFeatureMask;
103+
}
104+
93105
/**
94106
* Copy constructor used to create a non-shared instance with given mix-in
95107
* annotation definitions and subtype resolver.
@@ -101,19 +113,7 @@ private DeserializationConfig(DeserializationConfig src, SubtypeResolver str)
101113
_nodeFactory = src._nodeFactory;
102114
_problemHandlers = src._problemHandlers;
103115
_parserFeatures = src._parserFeatures;
104-
_parserFeatureMask = src._parserFeatureMask;
105-
}
106-
107-
private DeserializationConfig(DeserializationConfig src,
108-
int mapperFeatures, int deserFeatures,
109-
int parserFeatures, int parserFeatureMask)
110-
{
111-
super(src, mapperFeatures);
112-
_deserFeatures = deserFeatures;
113-
_nodeFactory = src._nodeFactory;
114-
_problemHandlers = src._problemHandlers;
115-
_parserFeatures = parserFeatures;
116-
_parserFeatureMask = parserFeatureMask;
116+
_parserFeaturesToChange = src._parserFeaturesToChange;
117117
}
118118

119119
private DeserializationConfig(DeserializationConfig src, BaseSettings base)
@@ -123,7 +123,7 @@ private DeserializationConfig(DeserializationConfig src, BaseSettings base)
123123
_nodeFactory = src._nodeFactory;
124124
_problemHandlers = src._problemHandlers;
125125
_parserFeatures = src._parserFeatures;
126-
_parserFeatureMask = src._parserFeatureMask;
126+
_parserFeaturesToChange = src._parserFeaturesToChange;
127127
}
128128

129129
private DeserializationConfig(DeserializationConfig src, JsonNodeFactory f)
@@ -133,7 +133,7 @@ private DeserializationConfig(DeserializationConfig src, JsonNodeFactory f)
133133
_problemHandlers = src._problemHandlers;
134134
_nodeFactory = f;
135135
_parserFeatures = src._parserFeatures;
136-
_parserFeatureMask = src._parserFeatureMask;
136+
_parserFeaturesToChange = src._parserFeaturesToChange;
137137
}
138138

139139
private DeserializationConfig(DeserializationConfig src,
@@ -144,7 +144,7 @@ private DeserializationConfig(DeserializationConfig src,
144144
_problemHandlers = problemHandlers;
145145
_nodeFactory = src._nodeFactory;
146146
_parserFeatures = src._parserFeatures;
147-
_parserFeatureMask = src._parserFeatureMask;
147+
_parserFeaturesToChange = src._parserFeaturesToChange;
148148
}
149149

150150
private DeserializationConfig(DeserializationConfig src, String rootName)
@@ -154,7 +154,7 @@ private DeserializationConfig(DeserializationConfig src, String rootName)
154154
_problemHandlers = src._problemHandlers;
155155
_nodeFactory = src._nodeFactory;
156156
_parserFeatures = src._parserFeatures;
157-
_parserFeatureMask = src._parserFeatureMask;
157+
_parserFeaturesToChange = src._parserFeaturesToChange;
158158
}
159159

160160
private DeserializationConfig(DeserializationConfig src, Class<?> view)
@@ -164,7 +164,7 @@ private DeserializationConfig(DeserializationConfig src, Class<?> view)
164164
_problemHandlers = src._problemHandlers;
165165
_nodeFactory = src._nodeFactory;
166166
_parserFeatures = src._parserFeatures;
167-
_parserFeatureMask = src._parserFeatureMask;
167+
_parserFeaturesToChange = src._parserFeaturesToChange;
168168
}
169169

170170
/**
@@ -177,7 +177,7 @@ protected DeserializationConfig(DeserializationConfig src, Map<ClassKey,Class<?>
177177
_problemHandlers = src._problemHandlers;
178178
_nodeFactory = src._nodeFactory;
179179
_parserFeatures = src._parserFeatures;
180-
_parserFeatureMask = src._parserFeatureMask;
180+
_parserFeaturesToChange = src._parserFeaturesToChange;
181181
}
182182

183183
/**
@@ -190,7 +190,7 @@ protected DeserializationConfig(DeserializationConfig src, ContextAttributes att
190190
_problemHandlers = src._problemHandlers;
191191
_nodeFactory = src._nodeFactory;
192192
_parserFeatures = src._parserFeatures;
193-
_parserFeatureMask = src._parserFeatureMask;
193+
_parserFeaturesToChange = src._parserFeaturesToChange;
194194
}
195195

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

216216
}
217217

@@ -224,7 +224,7 @@ public DeserializationConfig without(MapperFeature... features)
224224
}
225225
return (newMapperFlags == _mapperFeatures) ? this :
226226
new DeserializationConfig(this, newMapperFlags, _deserFeatures,
227-
_parserFeatures, _parserFeatureMask);
227+
_parserFeatures, _parserFeaturesToChange);
228228
}
229229

230230
@Override
@@ -238,7 +238,7 @@ public DeserializationConfig with(MapperFeature feature, boolean state)
238238
}
239239
return (newMapperFlags == _mapperFeatures) ? this :
240240
new DeserializationConfig(this, newMapperFlags, _deserFeatures,
241-
_parserFeatures, _parserFeatureMask);
241+
_parserFeatures, _parserFeaturesToChange);
242242
}
243243

244244
@Override
@@ -357,7 +357,7 @@ public DeserializationConfig with(DeserializationFeature feature)
357357
int newDeserFeatures = (_deserFeatures | feature.getMask());
358358
return (newDeserFeatures == _deserFeatures) ? this :
359359
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
360-
_parserFeatures, _parserFeatureMask);
360+
_parserFeatures, _parserFeaturesToChange);
361361
}
362362

363363
/**
@@ -373,7 +373,7 @@ public DeserializationConfig with(DeserializationFeature first,
373373
}
374374
return (newDeserFeatures == _deserFeatures) ? this :
375375
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
376-
_parserFeatures, _parserFeatureMask);
376+
_parserFeatures, _parserFeaturesToChange);
377377
}
378378

379379
/**
@@ -388,7 +388,7 @@ public DeserializationConfig withFeatures(DeserializationFeature... features)
388388
}
389389
return (newDeserFeatures == _deserFeatures) ? this :
390390
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
391-
_parserFeatures, _parserFeatureMask);
391+
_parserFeatures, _parserFeaturesToChange);
392392
}
393393

394394
/**
@@ -400,7 +400,7 @@ public DeserializationConfig without(DeserializationFeature feature)
400400
int newDeserFeatures = _deserFeatures & ~feature.getMask();
401401
return (newDeserFeatures == _deserFeatures) ? this :
402402
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
403-
_parserFeatures, _parserFeatureMask);
403+
_parserFeatures, _parserFeaturesToChange);
404404
}
405405

406406
/**
@@ -416,7 +416,7 @@ public DeserializationConfig without(DeserializationFeature first,
416416
}
417417
return (newDeserFeatures == _deserFeatures) ? this :
418418
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
419-
_parserFeatures, _parserFeatureMask);
419+
_parserFeatures, _parserFeaturesToChange);
420420
}
421421

422422
/**
@@ -431,7 +431,7 @@ public DeserializationConfig withoutFeatures(DeserializationFeature... features)
431431
}
432432
return (newDeserFeatures == _deserFeatures) ? this :
433433
new DeserializationConfig(this, _mapperFeatures, newDeserFeatures,
434-
_parserFeatures, _parserFeatureMask);
434+
_parserFeatures, _parserFeaturesToChange);
435435
}
436436

437437
/*
@@ -443,61 +443,69 @@ public DeserializationConfig withoutFeatures(DeserializationFeature... features)
443443
/**
444444
* Fluent factory method that will construct and return a new configuration
445445
* object instance with specified features enabled.
446+
*
447+
* @since 2.5
446448
*/
447449
public DeserializationConfig with(JsonParser.Feature feature)
448450
{
449451
int newSet = _parserFeatures | feature.getMask();
450-
int newMask = _parserFeatureMask | feature.getMask();
451-
return ((_parserFeatures == newSet) && (_parserFeatureMask == newMask)) ? this :
452+
int newMask = _parserFeaturesToChange | feature.getMask();
453+
return ((_parserFeatures == newSet) && (_parserFeaturesToChange == newMask)) ? this :
452454
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
453455
newSet, newMask);
454456
}
455457

456458
/**
457459
* Fluent factory method that will construct and return a new configuration
458460
* object instance with specified features enabled.
461+
*
462+
* @since 2.5
459463
*/
460464
public DeserializationConfig withFeatures(JsonParser.Feature... features)
461465
{
462466
int newSet = _parserFeatures;
463-
int newMask = _parserFeatureMask;
467+
int newMask = _parserFeaturesToChange;
464468
for (JsonParser.Feature f : features) {
465469
int mask = f.getMask();
466470
newSet |= mask;
467471
newMask |= mask;
468472
}
469-
return ((_parserFeatures == newSet) && (_parserFeatureMask == newMask)) ? this :
473+
return ((_parserFeatures == newSet) && (_parserFeaturesToChange == newMask)) ? this :
470474
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
471475
newSet, newMask);
472476
}
473477

474478
/**
475479
* Fluent factory method that will construct and return a new configuration
476480
* object instance with specified feature disabled.
481+
*
482+
* @since 2.5
477483
*/
478484
public DeserializationConfig without(JsonParser.Feature feature)
479485
{
480-
int newSet = _parserFeatureMask & ~feature.getMask();
486+
int newSet = _parserFeaturesToChange & ~feature.getMask();
481487
int newMask = _parserFeatures | feature.getMask();
482-
return ((_parserFeatures == newSet) && (_parserFeatureMask == newMask)) ? this :
488+
return ((_parserFeatures == newSet) && (_parserFeaturesToChange == newMask)) ? this :
483489
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
484490
newSet, newMask);
485491
}
486492

487493
/**
488494
* Fluent factory method that will construct and return a new configuration
489495
* object instance with specified features disabled.
496+
*
497+
* @since 2.5
490498
*/
491499
public DeserializationConfig withoutFeatures(JsonParser.Feature... features)
492500
{
493501
int newSet = _parserFeatures;
494-
int newMask = _parserFeatureMask;
502+
int newMask = _parserFeaturesToChange;
495503
for (JsonParser.Feature f : features) {
496504
int mask = f.getMask();
497505
newSet &= ~mask;
498506
newMask |= mask;
499507
}
500-
return ((_parserFeatures == newSet) && (_parserFeatureMask == newMask)) ? this :
508+
return ((_parserFeatures == newSet) && (_parserFeaturesToChange == newMask)) ? this :
501509
new DeserializationConfig(this, _mapperFeatures, _deserFeatures,
502510
newSet, newMask);
503511
}
@@ -552,16 +560,16 @@ public DeserializationConfig withNoProblemHandlers() {
552560
*/
553561

554562
/**
555-
* Method called by {@link ObjectMapper} and {@link ObjectWriter}
556-
* to modify those {@link com.fasterxml.jackson.core.JsonGenerator.Feature} settings
563+
* Method called by {@link ObjectMapper} and {@link ObjectReader}
564+
* to modify those {@link com.fasterxml.jackson.core.JsonParser.Feature} settings
557565
* that have been configured via this config instance.
558566
*
559567
* @since 2.5
560568
*/
561569
public void initialize(JsonParser p) {
562-
if (_parserFeatureMask != 0) {
570+
if (_parserFeaturesToChange != 0) {
563571
int orig = p.getFeatureMask();
564-
int newFlags = (orig & ~_parserFeatureMask) | _parserFeatures;
572+
int newFlags = (orig & ~_parserFeaturesToChange) | _parserFeatures;
565573
if (orig != newFlags) {
566574
p.setFeatureMask(newFlags);
567575
}

src/main/java/com/fasterxml/jackson/databind/DeserializationFeature.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,21 @@ public enum DeserializationFeature implements ConfigFeature
334334
;
335335

336336
private final boolean _defaultState;
337+
private final int _mask;
337338

338339
private DeserializationFeature(boolean defaultState) {
339340
_defaultState = defaultState;
341+
_mask = (1 << ordinal());
340342
}
341343

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

345347
@Override
346-
public int getMask() { return (1 << ordinal()); }
348+
public int getMask() { return _mask; }
349+
350+
/**
351+
* @since 2.5
352+
*/
353+
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
347354
}

src/main/java/com/fasterxml/jackson/databind/MapperFeature.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,21 @@ public enum MapperFeature implements ConfigFeature
278278
;
279279

280280
private final boolean _defaultState;
281+
private final int _mask;
281282

282283
private MapperFeature(boolean defaultState) {
283284
_defaultState = defaultState;
285+
_mask = (1 << ordinal());
284286
}
285287

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

289291
@Override
290-
public int getMask() { return (1 << ordinal()); }
292+
public int getMask() { return _mask; }
293+
294+
/**
295+
* @since 2.5
296+
*/
297+
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
291298
}

0 commit comments

Comments
 (0)