Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Call the right filterFinishArray()/filterFinishObject() from FilteringParserDelegate #1111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ public JsonToken nextToken() throws IOException
boolean returnEnd = _headContext.isStartHandled();
f = _headContext.getFilter();
if ((f != null) && (f != TokenFilter.INCLUDE_ALL)) {
f.filterFinishArray();
if (t.id() == JsonTokenId.ID_END_ARRAY) {
f.filterFinishArray();
} else {
f.filterFinishObject();
}
}
_headContext = _headContext.getParent();
_itemFilter = _headContext.getFilter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,73 @@ public TokenFilter filterStartObject() {
}
}

static class LoggingFilter extends TokenFilter
{
final TokenFilter _parent;
final ArrayList<String> _log;

LoggingFilter(final TokenFilter parent) {
_parent = parent;
_log = new ArrayList<>();
}

LoggingFilter(final TokenFilter parent, final ArrayList<String> log) {
_parent = parent;
_log = log;
}

TokenFilter rewrap(final TokenFilter filter) {
if (filter == null) {
return null;
} else if (filter == TokenFilter.INCLUDE_ALL) {
return TokenFilter.INCLUDE_ALL;
}
return new LoggingFilter(filter, _log);
}

@Override
public TokenFilter includeElement(final int index) {
_log.add("includeElement: " + index);
return rewrap(_parent.includeElement(index));
}

@Override
public TokenFilter includeProperty(final String name) {
_log.add("includeProperty: " + name);
return rewrap(_parent.includeProperty(name));
}

@Override
public TokenFilter filterStartObject() {
_log.add("filterStartObject");
return rewrap(_parent.filterStartObject());
}

@Override
public TokenFilter filterStartArray() {
_log.add("filterStartArray");
return rewrap(_parent.filterStartArray());
}

@Override
public void filterFinishObject() {
_log.add("filterFinishObject");
_parent.filterFinishObject();
}

@Override
public void filterFinishArray() {
_log.add("filterFinishArray");
_parent.filterFinishArray();
}

@Override
protected boolean _includeScalar() {
_log.add("_includeScalar");
return _parent._includeScalar();
}
}

static final TokenFilter INCLUDE_EMPTY_IF_NOT_FILTERED = new TokenFilter() {
@Override
public boolean includeEmptyArray(boolean contentsFiltered) {
Expand Down Expand Up @@ -763,4 +830,21 @@ public void testExcludeLastArrayInsideArray() throws Exception {
);
assertEquals(a2q("[[]]"), readAndWrite(JSON_F, p));
}

public void testCallbacksFromFilteringParserDelegate1() throws Exception {
LoggingFilter loggingFilter = new LoggingFilter(new JsonPointerBasedFilter("/parent"));

JsonParser p0 = JSON_F.createParser(a2q(
"{'parent':{'child':1}}"));
JsonParser p = new FilteringParserDelegate(p0,
loggingFilter,
Inclusion.ONLY_INCLUDE_ALL,
true
);
assertEquals(a2q("{'child':1}"), readAndWrite(JSON_F, p));

assertEquals(
Arrays.asList("filterStartObject", "includeProperty: parent", "filterFinishObject"),
loggingFilter._log);
}
}