Skip to content

Commit 4a89815

Browse files
committed
FasterXML#208 FilteringParserDelegate match count support
1 parent c66d62b commit 4a89815

File tree

3 files changed

+171
-35
lines changed

3 files changed

+171
-35
lines changed

src/main/java/com/fasterxml/jackson/core/filter/FilteringParserDelegate.java

+25-11
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public JsonToken nextToken() throws IOException
242242
}
243243
} else if (_currToken.isScalarValue()) {
244244
//else if scalar, and scalar not present in obj/array and !includePath and INCLUDE_ALL matched once
245-
// then return null
245+
// then return null
246246
if (!_headContext.isStartHandled() && (_itemFilter == TokenFilter.INCLUDE_ALL)) {
247247
return (_currToken = null);
248248
}
@@ -381,7 +381,7 @@ public JsonToken nextToken() throws IOException
381381
boolean returnEnd = _headContext.isStartHandled();
382382
f = _headContext.getFilter();
383383
if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) {
384-
f.filterFinishArray();
384+
f.filterFinishObject();
385385
}
386386
_headContext = _headContext.getParent();
387387
_itemFilter = _headContext.getFilter();
@@ -421,7 +421,7 @@ public JsonToken nextToken() throws IOException
421421
}
422422
_itemFilter = f;
423423
if (f == TokenFilter.INCLUDE_ALL) {
424-
if (_includePath) {
424+
if (_verifyAllowedMatches() && _includePath) {
425425
return (_currToken = t);
426426
}
427427
}
@@ -444,7 +444,9 @@ public JsonToken nextToken() throws IOException
444444
f = _headContext.checkValue(f);
445445
if ((f == TokenFilter.INCLUDE_ALL)
446446
|| ((f != null) && f.includeValue(delegate))) {
447-
return (_currToken = t);
447+
if (_verifyAllowedMatches()) {
448+
return (_currToken = t);
449+
}
448450
}
449451
}
450452
// Otherwise not included (leaves must be explicitly included)
@@ -548,7 +550,7 @@ protected final JsonToken _nextToken2() throws IOException
548550
boolean returnEnd = _headContext.isStartHandled();
549551
f = _headContext.getFilter();
550552
if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) {
551-
f.filterFinishArray();
553+
f.filterFinishObject();
552554
}
553555
_headContext = _headContext.getParent();
554556
_itemFilter = _headContext.getFilter();
@@ -579,7 +581,7 @@ protected final JsonToken _nextToken2() throws IOException
579581
}
580582
_itemFilter = f;
581583
if (f == TokenFilter.INCLUDE_ALL) {
582-
if (_includePath) {
584+
if (_verifyAllowedMatches() && _includePath) {
583585
return (_currToken = t);
584586
}
585587
// if (_includeImmediateParent) { ...
@@ -604,7 +606,9 @@ protected final JsonToken _nextToken2() throws IOException
604606
f = _headContext.checkValue(f);
605607
if ((f == TokenFilter.INCLUDE_ALL)
606608
|| ((f != null) && f.includeValue(delegate))) {
607-
return (_currToken = t);
609+
if (_verifyAllowedMatches()) {
610+
return (_currToken = t);
611+
}
608612
}
609613
}
610614
// Otherwise not included (leaves must be explicitly included)
@@ -683,7 +687,7 @@ protected final JsonToken _nextTokenWithBuffering(final TokenFilterContext buffR
683687
// included (won't get this far otherwise)
684688
f = _headContext.getFilter();
685689
if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) {
686-
f.filterFinishArray();
690+
f.filterFinishObject();
687691
}
688692
boolean gotEnd = (_headContext == buffRoot);
689693
boolean returnEnd = gotEnd && _headContext.isStartHandled();
@@ -721,7 +725,7 @@ protected final JsonToken _nextTokenWithBuffering(final TokenFilterContext buffR
721725
continue main_loop;
722726
}
723727
_itemFilter = f;
724-
if (f == TokenFilter.INCLUDE_ALL) {
728+
if (f == TokenFilter.INCLUDE_ALL && _verifyAllowedMatches()) {
725729
return _nextBuffered(buffRoot);
726730
}
727731
}
@@ -736,7 +740,9 @@ protected final JsonToken _nextTokenWithBuffering(final TokenFilterContext buffR
736740
f = _headContext.checkValue(f);
737741
if ((f == TokenFilter.INCLUDE_ALL)
738742
|| ((f != null) && f.includeValue(delegate))) {
739-
return _nextBuffered(buffRoot);
743+
if (_verifyAllowedMatches()) {
744+
return _nextBuffered(buffRoot);
745+
}
740746
}
741747
}
742748
// Otherwise not included (leaves must be explicitly included)
@@ -774,7 +780,15 @@ private JsonToken _nextBuffered(TokenFilterContext buffRoot) throws IOException
774780
}
775781
}
776782
}
777-
783+
784+
private final boolean _verifyAllowedMatches() throws IOException {
785+
if (_matchCount == 0 || _allowMultipleMatches) {
786+
++_matchCount;
787+
return true;
788+
}
789+
return false;
790+
}
791+
778792
@Override
779793
public JsonToken nextValue() throws IOException {
780794
// Re-implemented same as ParserMinimalBase:

src/test/java/com/fasterxml/jackson/core/filter/BasicGeneratorFilteringTest.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ public void testSingleMatchFilteringWithPathAlternate1() throws Exception
190190
gen.close();
191191

192192
assertEquals(aposToQuotes("{'ob':{'value':['x']}}"), w.toString());
193+
assertEquals(1, gen.getMatchCount());
193194
}
194195

195196
public void testSingleMatchFilteringWithPathRawBinary() throws Exception
@@ -240,47 +241,51 @@ public void testSingleMatchFilteringWithPathRawBinary() throws Exception
240241
gen.close();
241242

242243
assertEquals(aposToQuotes("{'array':['AQ==',1,2,3,4 ,5.0 /*x*/,6.25,7.5]}"), w.toString());
244+
assertEquals(1, gen.getMatchCount());
243245
}
244246

245247
public void testMultipleMatchFilteringWithPath1() throws Exception
246248
{
247249
StringWriter w = new StringWriter();
248-
JsonGenerator gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
250+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
249251
new NameMatchFilter("value0", "value2"),
250252
true, /* includePath */ true /* multipleMatches */ );
251253
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
252254
writeJsonDoc(JSON_F, JSON, gen);
253255
assertEquals(aposToQuotes("{'ob':{'value0':2,'value2':4}}"), w.toString());
256+
assertEquals(2, gen.getMatchCount());
254257
}
255258

256259
public void testMultipleMatchFilteringWithPath2() throws Exception
257260
{
258261
StringWriter w = new StringWriter();
259-
260-
JsonGenerator gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
262+
263+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
261264
new NameMatchFilter("array", "b", "value"),
262265
true, true);
263266
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
264267
writeJsonDoc(JSON_F, JSON, gen);
265268
assertEquals(aposToQuotes("{'array':[1,2],'ob':{'value':3},'b':true}"), w.toString());
269+
assertEquals(3, gen.getMatchCount());
266270
}
267271

268272
public void testMultipleMatchFilteringWithPath3() throws Exception
269273
{
270274
StringWriter w = new StringWriter();
271-
272-
JsonGenerator gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
275+
276+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
273277
new NameMatchFilter("value"),
274278
true, true);
275279
final String JSON = "{'root':{'a0':true,'a':{'value':3},'b':{'value':4}},'b0':false}";
276280
writeJsonDoc(JSON_F, JSON, gen);
277281
assertEquals(aposToQuotes("{'root':{'a':{'value':3},'b':{'value':4}}}"), w.toString());
282+
assertEquals(2, gen.getMatchCount());
278283
}
279284

280285
public void testIndexMatchWithPath1() throws Exception
281286
{
282287
StringWriter w = new StringWriter();
283-
JsonGenerator gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
288+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
284289
new IndexMatchFilter(1),
285290
true, true);
286291
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
@@ -293,24 +298,26 @@ public void testIndexMatchWithPath1() throws Exception
293298
true, true);
294299
writeJsonDoc(JSON_F, JSON, gen);
295300
assertEquals(aposToQuotes("{'array':[1]}"), w.toString());
301+
assertEquals(1, gen.getMatchCount());
296302
}
297303

298304
public void testIndexMatchWithPath2() throws Exception
299305
{
300306
StringWriter w = new StringWriter();
301-
JsonGenerator gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
307+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
302308
new IndexMatchFilter(0,1),
303309
true, true);
304310
final String JSON = "{'a':123,'array':[1,2],'ob':{'value0':2,'value':3,'value2':4},'b':true}";
305311
writeJsonDoc(JSON_F, JSON, gen);
306312
assertEquals(aposToQuotes("{'array':[1,2]}"), w.toString());
313+
assertEquals(2, gen.getMatchCount());
307314
}
308315

309316
public void testWriteStartObjectWithObject() throws Exception
310317
{
311318
StringWriter w = new StringWriter();
312319

313-
JsonGenerator gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
320+
FilteringGeneratorDelegate gen = new FilteringGeneratorDelegate(JSON_F.createGenerator(w),
314321
TokenFilter.INCLUDE_ALL,
315322
true, true);
316323

0 commit comments

Comments
 (0)