12
12
* Specialized {@link JsonGeneratorDelegate} that allows use of
13
13
* {@link TokenFilter} for outputting a subset of content that
14
14
* caller tries to generate.
15
- *
15
+ *
16
16
* @since 2.6
17
17
*/
18
18
public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
@@ -22,7 +22,7 @@ public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
22
22
/* Configuration
23
23
/**********************************************************
24
24
*/
25
-
25
+
26
26
/**
27
27
* Object consulted to determine whether to write parts of content generator
28
28
* is asked to write or not.
@@ -51,7 +51,7 @@ public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
51
51
* actual use case. But it seemed possible need could arise, which
52
52
* is feature has not yet been removed. If no use is found within
53
53
* first version or two, just remove.
54
- *
54
+ *
55
55
* Marked as deprecated since its status is uncertain.
56
56
*/
57
57
@ Deprecated
@@ -76,7 +76,7 @@ public class FilteringGeneratorDelegate extends JsonGeneratorDelegate
76
76
* property, and also used for array elements.
77
77
*/
78
78
protected TokenFilter _itemFilter ;
79
-
79
+
80
80
/**
81
81
* Number of tokens for which {@link TokenFilter#INCLUDE_ALL}
82
82
* has been returned
@@ -113,7 +113,7 @@ public FilteringGeneratorDelegate(JsonGenerator d, TokenFilter f,
113
113
public JsonStreamContext getFilterContext () {
114
114
return _filterContext ;
115
115
}
116
-
116
+
117
117
/**
118
118
* Accessor for finding number of matches, where specific token and sub-tree
119
119
* starting (if structured type) are passed.
@@ -127,7 +127,7 @@ public int getMatchCount() {
127
127
/* Public API, accessors
128
128
/**********************************************************
129
129
*/
130
-
130
+
131
131
@ Override
132
132
public JsonStreamContext getOutputContext () {
133
133
/* 11-Apr-2015, tatu: Choice is between pre- and post-filter context;
@@ -136,13 +136,13 @@ public JsonStreamContext getOutputContext() {
136
136
*/
137
137
return _filterContext ;
138
138
}
139
-
139
+
140
140
/*
141
141
/**********************************************************
142
142
/* Public API, write methods, structural
143
143
/**********************************************************
144
144
*/
145
-
145
+
146
146
@ Override
147
147
public void writeStartArray () throws IOException
148
148
{
@@ -173,7 +173,7 @@ public void writeStartArray() throws IOException
173
173
_filterContext = _filterContext .createChildArrayContext (_itemFilter , false );
174
174
}
175
175
}
176
-
176
+
177
177
@ Override
178
178
public void writeStartArray (int size ) throws IOException
179
179
{
@@ -202,7 +202,7 @@ public void writeStartArray(int size) throws IOException
202
202
_filterContext = _filterContext .createChildArrayContext (_itemFilter , false );
203
203
}
204
204
}
205
-
205
+
206
206
@ Override
207
207
public void writeEndArray () throws IOException
208
208
{
@@ -230,7 +230,7 @@ public void writeStartObject() throws IOException
230
230
if (f == null ) {
231
231
return ;
232
232
}
233
-
233
+
234
234
if (f != TokenFilter .INCLUDE_ALL ) {
235
235
f = f .filterStartObject ();
236
236
}
@@ -242,7 +242,37 @@ public void writeStartObject() throws IOException
242
242
_filterContext = _filterContext .createChildObjectContext (f , false );
243
243
}
244
244
}
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
+
246
276
@ Override
247
277
public void writeEndObject () throws IOException
248
278
{
@@ -315,7 +345,7 @@ public void writeString(String value) throws IOException
315
345
}
316
346
}
317
347
_checkParentPath ();
318
- }
348
+ }
319
349
delegate .writeString (value );
320
350
}
321
351
@@ -337,7 +367,7 @@ public void writeString(char[] text, int offset, int len) throws IOException
337
367
}
338
368
}
339
369
_checkParentPath ();
340
- }
370
+ }
341
371
delegate .writeString (text , offset , len );
342
372
}
343
373
@@ -358,7 +388,7 @@ public void writeString(SerializableString value) throws IOException
358
388
}
359
389
}
360
390
_checkParentPath ();
361
- }
391
+ }
362
392
delegate .writeString (value );
363
393
}
364
394
@@ -489,7 +519,7 @@ public void writeNumber(short v) throws IOException
489
519
}
490
520
}
491
521
_checkParentPath ();
492
- }
522
+ }
493
523
delegate .writeNumber (v );
494
524
}
495
525
@@ -510,7 +540,7 @@ public void writeNumber(int v) throws IOException
510
540
}
511
541
}
512
542
_checkParentPath ();
513
- }
543
+ }
514
544
delegate .writeNumber (v );
515
545
}
516
546
@@ -531,7 +561,7 @@ public void writeNumber(long v) throws IOException
531
561
}
532
562
}
533
563
_checkParentPath ();
534
- }
564
+ }
535
565
delegate .writeNumber (v );
536
566
}
537
567
@@ -552,7 +582,7 @@ public void writeNumber(BigInteger v) throws IOException
552
582
}
553
583
}
554
584
_checkParentPath ();
555
- }
585
+ }
556
586
delegate .writeNumber (v );
557
587
}
558
588
@@ -573,7 +603,7 @@ public void writeNumber(double v) throws IOException
573
603
}
574
604
}
575
605
_checkParentPath ();
576
- }
606
+ }
577
607
delegate .writeNumber (v );
578
608
}
579
609
@@ -594,7 +624,7 @@ public void writeNumber(float v) throws IOException
594
624
}
595
625
}
596
626
_checkParentPath ();
597
- }
627
+ }
598
628
delegate .writeNumber (v );
599
629
}
600
630
@@ -615,7 +645,7 @@ public void writeNumber(BigDecimal v) throws IOException
615
645
}
616
646
}
617
647
_checkParentPath ();
618
- }
648
+ }
619
649
delegate .writeNumber (v );
620
650
}
621
651
@@ -657,7 +687,7 @@ public void writeBoolean(boolean v) throws IOException
657
687
}
658
688
}
659
689
_checkParentPath ();
660
- }
690
+ }
661
691
delegate .writeBoolean (v );
662
692
}
663
693
@@ -678,7 +708,7 @@ public void writeNull() throws IOException
678
708
}
679
709
}
680
710
_checkParentPath ();
681
- }
711
+ }
682
712
delegate .writeNull ();
683
713
}
684
714
@@ -695,7 +725,7 @@ public void writeOmittedField(String fieldName) throws IOException {
695
725
delegate .writeOmittedField (fieldName );
696
726
}
697
727
}
698
-
728
+
699
729
/*
700
730
/**********************************************************
701
731
/* Public API, write methods, Native Ids
@@ -704,7 +734,7 @@ public void writeOmittedField(String fieldName) throws IOException {
704
734
705
735
// 25-Mar-2015, tatu: These are tricky as they sort of predate actual filtering calls.
706
736
// Let's try to use current state as a clue at least...
707
-
737
+
708
738
@ Override
709
739
public void writeObjectId (Object id ) throws IOException {
710
740
if (_itemFilter != null ) {
@@ -718,7 +748,7 @@ public void writeObjectRef(Object id) throws IOException {
718
748
delegate .writeObjectRef (id );
719
749
}
720
750
}
721
-
751
+
722
752
@ Override
723
753
public void writeTypeId (Object id ) throws IOException {
724
754
if (_itemFilter != null ) {
@@ -741,7 +771,7 @@ public void writeObject(Object pojo) throws IOException,JsonProcessingException
741
771
delegate.writeObject(pojo);
742
772
return;
743
773
}
744
- // NOTE: copied from
774
+ // NOTE: copied from
745
775
if (pojo == null) {
746
776
writeNull();
747
777
} else {
@@ -752,7 +782,7 @@ public void writeObject(Object pojo) throws IOException,JsonProcessingException
752
782
_writeSimpleObject(pojo);
753
783
}
754
784
}
755
-
785
+
756
786
@Override
757
787
public void writeTree(TreeNode rootNode) throws IOException {
758
788
if (delegateCopyMethods) {
@@ -835,7 +865,7 @@ protected void _checkPropertyParentPath() throws IOException
835
865
_filterContext .skipParentChecks ();
836
866
}
837
867
}
838
-
868
+
839
869
protected boolean _checkBinaryWrite () throws IOException
840
870
{
841
871
if (_itemFilter == null ) {
@@ -850,7 +880,7 @@ protected boolean _checkBinaryWrite() throws IOException
850
880
}
851
881
return false ;
852
882
}
853
-
883
+
854
884
protected boolean _checkRawValueWrite () throws IOException
855
885
{
856
886
if (_itemFilter == null ) {
0 commit comments