Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.math.BigInteger;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.filter.TokenFilter.Inclusion;
import com.fasterxml.jackson.core.util.JsonGeneratorDelegate;

/**
Expand All @@ -17,6 +18,7 @@
*/
public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
{

/*
/**********************************************************
/* Configuration
Expand Down Expand Up @@ -44,18 +46,7 @@ public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
* done and only explicitly included entries are output; if `true` then
* path from main level down to match is also included as necessary.
*/
protected boolean _includePath;

/* NOTE: this feature is included in the first version (2.6), but
* there is no public API to enable it, yet, since there isn't an
* actual use case. But it seemed possible need could arise, which
* is feature has not yet been removed. If no use is found within
* first version or two, just remove.
*
* Marked as deprecated since its status is uncertain.
*/
@Deprecated
protected boolean _includeImmediateParent;
protected TokenFilter.Inclusion _inclusion;

/*
/**********************************************************
Expand Down Expand Up @@ -89,16 +80,23 @@ public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
/**********************************************************
*/

@Deprecated
public FilteringGeneratorDelegate(JsonGenerator d, TokenFilter f,
boolean includePath, boolean allowMultipleMatches)
{
this(d, f, includePath ? Inclusion.INCLUDE_ALL_AND_PATH : Inclusion.ONLY_INCLUDE_ALL, allowMultipleMatches);
}

public FilteringGeneratorDelegate(JsonGenerator d, TokenFilter f,
TokenFilter.Inclusion inclusion, boolean allowMultipleMatches)
{
// By default, do NOT delegate copy methods
super(d, false);
rootFilter = f;
// and this is the currently active filter for root values
_itemFilter = f;
_filterContext = TokenFilterContext.createRootContext(f);
_includePath = includePath;
_inclusion = inclusion;
_allowMultipleMatches = allowMultipleMatches;
}

Expand Down Expand Up @@ -169,6 +167,10 @@ public void writeStartArray() throws IOException
_checkParentPath();
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
delegate.writeStartArray();
} else if (_itemFilter != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
_checkParentPath(false /* isMatch */);
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
delegate.writeStartArray();
} else {
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
}
Expand Down Expand Up @@ -198,6 +200,10 @@ public void writeStartArray(int size) throws IOException
_checkParentPath();
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
delegate.writeStartArray(size);
} else if (_itemFilter != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
_checkParentPath(false /* isMatch */);
_filterContext = _filterContext.createChildArrayContext(_itemFilter, true);
delegate.writeStartArray(size);
} else {
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
}
Expand Down Expand Up @@ -296,6 +302,10 @@ public void writeStartObject() throws IOException
_checkParentPath();
_filterContext = _filterContext.createChildObjectContext(f, true);
delegate.writeStartObject();
} else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
_checkParentPath(false /* isMatch */);
_filterContext = _filterContext.createChildObjectContext(f, true);
delegate.writeStartObject();
} else { // filter out
_filterContext = _filterContext.createChildObjectContext(f, false);
}
Expand Down Expand Up @@ -326,6 +336,10 @@ public void writeStartObject(Object forValue) throws IOException
_checkParentPath();
_filterContext = _filterContext.createChildObjectContext(f, true);
delegate.writeStartObject(forValue);
} else if (f != null && _inclusion == Inclusion.INCLUDE_NON_NULL) {
_checkParentPath(false /* isMatch */);
_filterContext = _filterContext.createChildObjectContext(f, true);
delegate.writeStartObject(forValue);
} else { // filter out
_filterContext = _filterContext.createChildObjectContext(f, false);
}
Expand Down Expand Up @@ -439,7 +453,7 @@ public void writeString(String value) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeString(value);
}

Expand All @@ -461,7 +475,7 @@ public void writeString(char[] text, int offset, int len) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeString(text, offset, len);
}

Expand All @@ -482,7 +496,7 @@ public void writeString(SerializableString value) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeString(value);
}

Expand Down Expand Up @@ -613,7 +627,7 @@ public void writeNumber(short v) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeNumber(v);
}

Expand All @@ -634,7 +648,7 @@ public void writeNumber(int v) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeNumber(v);
}

Expand All @@ -655,7 +669,7 @@ public void writeNumber(long v) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeNumber(v);
}

Expand All @@ -676,7 +690,7 @@ public void writeNumber(BigInteger v) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeNumber(v);
}

Expand All @@ -697,7 +711,7 @@ public void writeNumber(double v) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeNumber(v);
}

Expand All @@ -718,7 +732,7 @@ public void writeNumber(float v) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeNumber(v);
}

Expand All @@ -739,7 +753,7 @@ public void writeNumber(BigDecimal v) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeNumber(v);
}

Expand Down Expand Up @@ -781,7 +795,7 @@ public void writeBoolean(boolean v) throws IOException
}
}
_checkParentPath();
}
}
delegate.writeBoolean(v);
}

Expand All @@ -802,7 +816,7 @@ public void writeNull() throws IOException
}
}
_checkParentPath();
}
}
delegate.writeNull();
}

Expand Down Expand Up @@ -925,13 +939,23 @@ public void copyCurrentStructure(JsonParser jp) throws IOException {

protected void _checkParentPath() throws IOException
{
++_matchCount;
_checkParentPath(true);
}

protected void _checkParentPath(boolean isMatch) throws IOException
{
if (isMatch) {
++_matchCount;
}
// only need to construct path if parent wasn't written
if (_includePath) {
if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
_filterContext.writePath(delegate);
} else if (_inclusion == Inclusion.INCLUDE_NON_NULL) {
// path has already been written, except for maybe field name
_filterContext.ensureFieldNameWritten(delegate);
}
// also: if no multiple matches desired, short-cut checks
if (!_allowMultipleMatches) {
if (isMatch && !_allowMultipleMatches) {
// Mark parents as "skip" so that further check calls are not made
_filterContext.skipParentChecks();
}
Expand All @@ -945,12 +969,11 @@ protected void _checkParentPath() throws IOException
protected void _checkPropertyParentPath() throws IOException
{
++_matchCount;
if (_includePath) {
if (_inclusion == Inclusion.INCLUDE_ALL_AND_PATH) {
_filterContext.writePath(delegate);
} else if (_includeImmediateParent) {
// 21-Apr-2015, tatu: Note that there is no API to enable this currently...
// retained for speculative future use
_filterContext.writeImmediatePath(delegate);
} else if (_inclusion == Inclusion.INCLUDE_NON_NULL) {
// path has already been written, except for maybe field name
_filterContext.ensureFieldNameWritten(delegate);
}

// also: if no multiple matches desired, short-cut checks
Expand Down
Loading