Skip to content

Commit 9076909

Browse files
committed
FasterXML#359 override writeStartObject(Object forValue)
1 parent d89facc commit 9076909

File tree

2 files changed

+101
-40
lines changed

2 files changed

+101
-40
lines changed

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

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Specialized {@link JsonGeneratorDelegate} that allows use of
1313
* {@link TokenFilter} for outputting a subset of content that
1414
* caller tries to generate.
15-
*
15+
*
1616
* @since 2.6
1717
*/
1818
public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
@@ -22,7 +22,7 @@ public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
2222
/* Configuration
2323
/**********************************************************
2424
*/
25-
25+
2626
/**
2727
* Object consulted to determine whether to write parts of content generator
2828
* is asked to write or not.
@@ -51,7 +51,7 @@ public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
5151
* actual use case. But it seemed possible need could arise, which
5252
* is feature has not yet been removed. If no use is found within
5353
* first version or two, just remove.
54-
*
54+
*
5555
* Marked as deprecated since its status is uncertain.
5656
*/
5757
@Deprecated
@@ -76,7 +76,7 @@ public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
7676
* property, and also used for array elements.
7777
*/
7878
protected TokenFilter _itemFilter;
79-
79+
8080
/**
8181
* Number of tokens for which {@link TokenFilter#INCLUDE_ALL}
8282
* has been returned
@@ -113,7 +113,7 @@ public FilteringGeneratorDelegate(JsonGenerator d, TokenFilter f,
113113
public JsonStreamContext getFilterContext() {
114114
return _filterContext;
115115
}
116-
116+
117117
/**
118118
* Accessor for finding number of matches, where specific token and sub-tree
119119
* starting (if structured type) are passed.
@@ -127,7 +127,7 @@ public int getMatchCount() {
127127
/* Public API, accessors
128128
/**********************************************************
129129
*/
130-
130+
131131
@Override
132132
public JsonStreamContext getOutputContext() {
133133
/* 11-Apr-2015, tatu: Choice is between pre- and post-filter context;
@@ -136,13 +136,13 @@ public JsonStreamContext getOutputContext() {
136136
*/
137137
return _filterContext;
138138
}
139-
139+
140140
/*
141141
/**********************************************************
142142
/* Public API, write methods, structural
143143
/**********************************************************
144144
*/
145-
145+
146146
@Override
147147
public void writeStartArray() throws IOException
148148
{
@@ -173,7 +173,7 @@ public void writeStartArray() throws IOException
173173
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
174174
}
175175
}
176-
176+
177177
@Override
178178
public void writeStartArray(int size) throws IOException
179179
{
@@ -202,7 +202,7 @@ public void writeStartArray(int size) throws IOException
202202
_filterContext = _filterContext.createChildArrayContext(_itemFilter, false);
203203
}
204204
}
205-
205+
206206
@Override
207207
public void writeEndArray() throws IOException
208208
{
@@ -230,7 +230,7 @@ public void writeStartObject() throws IOException
230230
if (f == null) {
231231
return;
232232
}
233-
233+
234234
if (f != TokenFilter.INCLUDE_ALL) {
235235
f = f.filterStartObject();
236236
}
@@ -242,7 +242,37 @@ public void writeStartObject() throws IOException
242242
_filterContext = _filterContext.createChildObjectContext(f, false);
243243
}
244244
}
245-
245+
246+
@Override
247+
public void writeStartObject(Object forValue) throws IOException
248+
{
249+
if (_itemFilter == null) {
250+
_filterContext = _filterContext.createChildObjectContext(_itemFilter, false);
251+
return;
252+
}
253+
if (_itemFilter == TokenFilter.INCLUDE_ALL) {
254+
_filterContext = _filterContext.createChildObjectContext(_itemFilter, true);
255+
delegate.writeStartObject(forValue);
256+
return;
257+
}
258+
259+
TokenFilter f = _filterContext.checkValue(_itemFilter);
260+
if (f == null) {
261+
return;
262+
}
263+
264+
if (f != TokenFilter.INCLUDE_ALL) {
265+
f = f.filterStartObject();
266+
}
267+
if (f == TokenFilter.INCLUDE_ALL) {
268+
_checkParentPath();
269+
_filterContext = _filterContext.createChildObjectContext(f, true);
270+
delegate.writeStartObject(forValue);
271+
} else { // filter out
272+
_filterContext = _filterContext.createChildObjectContext(f, false);
273+
}
274+
}
275+
246276
@Override
247277
public void writeEndObject() throws IOException
248278
{
@@ -315,7 +345,7 @@ public void writeString(String value) throws IOException
315345
}
316346
}
317347
_checkParentPath();
318-
}
348+
}
319349
delegate.writeString(value);
320350
}
321351

@@ -337,7 +367,7 @@ public void writeString(char[] text, int offset, int len) throws IOException
337367
}
338368
}
339369
_checkParentPath();
340-
}
370+
}
341371
delegate.writeString(text, offset, len);
342372
}
343373

@@ -358,7 +388,7 @@ public void writeString(SerializableString value) throws IOException
358388
}
359389
}
360390
_checkParentPath();
361-
}
391+
}
362392
delegate.writeString(value);
363393
}
364394

@@ -489,7 +519,7 @@ public void writeNumber(short v) throws IOException
489519
}
490520
}
491521
_checkParentPath();
492-
}
522+
}
493523
delegate.writeNumber(v);
494524
}
495525

@@ -510,7 +540,7 @@ public void writeNumber(int v) throws IOException
510540
}
511541
}
512542
_checkParentPath();
513-
}
543+
}
514544
delegate.writeNumber(v);
515545
}
516546

@@ -531,7 +561,7 @@ public void writeNumber(long v) throws IOException
531561
}
532562
}
533563
_checkParentPath();
534-
}
564+
}
535565
delegate.writeNumber(v);
536566
}
537567

@@ -552,7 +582,7 @@ public void writeNumber(BigInteger v) throws IOException
552582
}
553583
}
554584
_checkParentPath();
555-
}
585+
}
556586
delegate.writeNumber(v);
557587
}
558588

@@ -573,7 +603,7 @@ public void writeNumber(double v) throws IOException
573603
}
574604
}
575605
_checkParentPath();
576-
}
606+
}
577607
delegate.writeNumber(v);
578608
}
579609

@@ -594,7 +624,7 @@ public void writeNumber(float v) throws IOException
594624
}
595625
}
596626
_checkParentPath();
597-
}
627+
}
598628
delegate.writeNumber(v);
599629
}
600630

@@ -615,7 +645,7 @@ public void writeNumber(BigDecimal v) throws IOException
615645
}
616646
}
617647
_checkParentPath();
618-
}
648+
}
619649
delegate.writeNumber(v);
620650
}
621651

@@ -657,7 +687,7 @@ public void writeBoolean(boolean v) throws IOException
657687
}
658688
}
659689
_checkParentPath();
660-
}
690+
}
661691
delegate.writeBoolean(v);
662692
}
663693

@@ -678,7 +708,7 @@ public void writeNull() throws IOException
678708
}
679709
}
680710
_checkParentPath();
681-
}
711+
}
682712
delegate.writeNull();
683713
}
684714

@@ -695,7 +725,7 @@ public void writeOmittedField(String fieldName) throws IOException {
695725
delegate.writeOmittedField(fieldName);
696726
}
697727
}
698-
728+
699729
/*
700730
/**********************************************************
701731
/* Public API, write methods, Native Ids
@@ -704,7 +734,7 @@ public void writeOmittedField(String fieldName) throws IOException {
704734

705735
// 25-Mar-2015, tatu: These are tricky as they sort of predate actual filtering calls.
706736
// Let's try to use current state as a clue at least...
707-
737+
708738
@Override
709739
public void writeObjectId(Object id) throws IOException {
710740
if (_itemFilter != null) {
@@ -718,7 +748,7 @@ public void writeObjectRef(Object id) throws IOException {
718748
delegate.writeObjectRef(id);
719749
}
720750
}
721-
751+
722752
@Override
723753
public void writeTypeId(Object id) throws IOException {
724754
if (_itemFilter != null) {
@@ -741,7 +771,7 @@ public void writeObject(Object pojo) throws IOException,JsonProcessingException
741771
delegate.writeObject(pojo);
742772
return;
743773
}
744-
// NOTE: copied from
774+
// NOTE: copied from
745775
if (pojo == null) {
746776
writeNull();
747777
} else {
@@ -752,7 +782,7 @@ public void writeObject(Object pojo) throws IOException,JsonProcessingException
752782
_writeSimpleObject(pojo);
753783
}
754784
}
755-
785+
756786
@Override
757787
public void writeTree(TreeNode rootNode) throws IOException {
758788
if (delegateCopyMethods) {
@@ -835,7 +865,7 @@ protected void _checkPropertyParentPath() throws IOException
835865
_filterContext.skipParentChecks();
836866
}
837867
}
838-
868+
839869
protected boolean _checkBinaryWrite() throws IOException
840870
{
841871
if (_itemFilter == null) {
@@ -850,7 +880,7 @@ protected boolean _checkBinaryWrite() throws IOException
850880
}
851881
return false;
852882
}
853-
883+
854884
protected boolean _checkRawValueWrite() throws IOException
855885
{
856886
if (_itemFilter == null) {

0 commit comments

Comments
 (0)