Skip to content

Commit

Permalink
Allow FilteringParserDelegate to skip last elements in array (#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgomulka authored Jan 8, 2023
1 parent 7910a8b commit 9fd899d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,9 @@ protected final JsonToken _nextTokenWithBuffering(final TokenFilterContext buffR
if (returnEnd) {
return t;
}
if (gotEnd) {
return null;
}
}
continue main_loop;
case ID_END_OBJECT:
Expand Down Expand Up @@ -781,6 +784,9 @@ protected final JsonToken _nextTokenWithBuffering(final TokenFilterContext buffR
if (returnEnd) {
return t;
}
if (gotEnd) {
return null;
}
}
continue main_loop;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -719,4 +719,48 @@ public void testIncludeEmptyArrayIfNotFilteredAfterFiltered() throws Exception {
);
assertEquals(a2q("[{'empty_array':[]}]"), readAndWrite(JSON_F, p));
}

public void testExcludeObjectAtTheBeginningOfArray() throws Exception {
JsonParser p0 = JSON_F.createParser(a2q(
"{'parent':[{'exclude':false},{'include':true}]}"));
JsonParser p = new FilteringParserDelegate(p0,
new NameMatchFilter(new String[] { "include" } ),
Inclusion.INCLUDE_ALL_AND_PATH,
false // multipleMatches
);
assertEquals(a2q("{'parent':[{'include':true}]}"), readAndWrite(JSON_F, p));
}

public void testExcludeObjectAtTheEndOfArray() throws Exception {
JsonParser p0 = JSON_F.createParser(a2q(
"{'parent':[{'include':true},{'exclude':false}]}"));
JsonParser p = new FilteringParserDelegate(p0,
new NameMatchFilter(new String[] { "include" } ),
Inclusion.INCLUDE_ALL_AND_PATH,
false // multipleMatches
);
assertEquals(a2q("{'parent':[{'include':true}]}"), readAndWrite(JSON_F, p));
}

public void testExcludeObjectInMiddleOfArray() throws Exception {
JsonParser p0 = JSON_F.createParser(a2q(
"{'parent':[{'include-1':1},{'skip':0},{'include-2':2}]}"));
JsonParser p = new FilteringParserDelegate(p0,
new NameMatchFilter(new String[]{"include-1", "include-2"}),
Inclusion.INCLUDE_ALL_AND_PATH,
true // multipleMatches
);
assertEquals(a2q("{'parent':[{'include-1':1},{'include-2':2}]}"), readAndWrite(JSON_F, p));
}

public void testExcludeLastArrayInsideArray() throws Exception {
JsonParser p0 = JSON_F.createParser(a2q(
"['skipped', [], ['skipped']]"));
JsonParser p = new FilteringParserDelegate(p0,
INCLUDE_EMPTY_IF_NOT_FILTERED,
Inclusion.INCLUDE_ALL_AND_PATH,
true // multipleMatches
);
assertEquals(a2q("[[]]"), readAndWrite(JSON_F, p));
}
}

0 comments on commit 9fd899d

Please sign in to comment.