-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ObjectNode.java
1079 lines (977 loc) · 33.8 KB
/
ObjectNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.fasterxml.jackson.databind.node;
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.WritableTypeId;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.cfg.JsonNodeFeature;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.util.RawValue;
/**
* Node that maps to JSON Object structures in JSON content.
*<p>
* Note: class was <code>final</code> temporarily for Jackson 2.2.
*/
public class ObjectNode
extends ContainerNode<ObjectNode>
implements java.io.Serializable
{
private static final long serialVersionUID = 1L; // since 2.10
// Note: LinkedHashMap for backwards compatibility
protected final Map<String, JsonNode> _children;
public ObjectNode(JsonNodeFactory nc) {
super(nc);
_children = new LinkedHashMap<String, JsonNode>();
}
/**
* @since 2.4
*/
public ObjectNode(JsonNodeFactory nc, Map<String, JsonNode> kids) {
super(nc);
_children = kids;
}
@Override
protected JsonNode _at(JsonPointer ptr) {
return get(ptr.getMatchingProperty());
}
/* Question: should this delegate to `JsonNodeFactory`? It does not absolutely
* have to, as long as sub-types override the method but...
*/
// note: co-variant for type safety
@SuppressWarnings("unchecked")
@Override
public ObjectNode deepCopy()
{
ObjectNode ret = new ObjectNode(_nodeFactory);
for (Map.Entry<String, JsonNode> entry: _children.entrySet())
ret._children.put(entry.getKey(), entry.getValue().deepCopy());
return ret;
}
/*
/**********************************************************
/* Support for withArray()/withObject()
/**********************************************************
*/
@SuppressWarnings("unchecked")
@Deprecated
@Override
public ObjectNode with(String exprOrProperty) {
JsonPointer ptr = _jsonPointerIfValid(exprOrProperty);
if (ptr != null) {
return withObject(ptr);
}
JsonNode n = _children.get(exprOrProperty);
if (n != null) {
if (n instanceof ObjectNode) {
return (ObjectNode) n;
}
throw new UnsupportedOperationException("Property '" + exprOrProperty
+ "' has value that is not of type `ObjectNode` (but `" + n
.getClass().getName() + "`)");
}
ObjectNode result = objectNode();
_children.put(exprOrProperty, result);
return result;
}
@SuppressWarnings("unchecked")
@Override
public ArrayNode withArray(String exprOrProperty)
{
JsonPointer ptr = _jsonPointerIfValid(exprOrProperty);
if (ptr != null) {
return withArray(ptr);
}
JsonNode n = _children.get(exprOrProperty);
if (n != null) {
if (n instanceof ArrayNode) {
return (ArrayNode) n;
}
throw new UnsupportedOperationException("Property '" + exprOrProperty
+ "' has value that is not of type `ArrayNode` (but `" + n
.getClass().getName() + "`)");
}
ArrayNode result = arrayNode();
_children.put(exprOrProperty, result);
return result;
}
@Override
protected ObjectNode _withObject(JsonPointer origPtr,
JsonPointer currentPtr,
OverwriteMode overwriteMode, boolean preferIndex)
{
if (currentPtr.matches()) {
return this;
}
JsonNode n = _at(currentPtr);
// If there's a path, follow it
if ((n != null) && (n instanceof BaseJsonNode)) {
ObjectNode found = ((BaseJsonNode) n)._withObject(origPtr, currentPtr.tail(),
overwriteMode, preferIndex);
if (found != null) {
return found;
}
// Ok no; must replace if allowed to
_withXxxVerifyReplace(origPtr, currentPtr, overwriteMode, preferIndex, n);
}
// Either way; must replace or add a new property
return _withObjectAddTailProperty(currentPtr, preferIndex);
}
@Override
protected ArrayNode _withArray(JsonPointer origPtr,
JsonPointer currentPtr,
OverwriteMode overwriteMode, boolean preferIndex)
{
if (currentPtr.matches()) {
// Cannot return, not an ArrayNode so:
return null;
}
JsonNode n = _at(currentPtr);
// If there's a path, follow it
if ((n != null) && (n instanceof BaseJsonNode)) {
ArrayNode found = ((BaseJsonNode) n)._withArray(origPtr, currentPtr.tail(),
overwriteMode, preferIndex);
if (found != null) {
return found;
}
// Ok no; must replace if allowed to
_withXxxVerifyReplace(origPtr, currentPtr, overwriteMode, preferIndex, n);
}
// Either way; must replace or add a new property
return _withArrayAddTailProperty(currentPtr, preferIndex);
}
protected ObjectNode _withObjectAddTailProperty(JsonPointer tail, boolean preferIndex)
{
final String propName = tail.getMatchingProperty();
tail = tail.tail();
// First: did we complete traversal? If so, easy, we got our result
if (tail.matches()) {
return putObject(propName);
}
// Otherwise, do we want Array or Object
if (preferIndex && tail.mayMatchElement()) { // array!
return putArray(propName)._withObjectAddTailElement(tail, preferIndex);
}
return putObject(propName)._withObjectAddTailProperty(tail, preferIndex);
}
protected ArrayNode _withArrayAddTailProperty(JsonPointer tail, boolean preferIndex)
{
final String propName = tail.getMatchingProperty();
tail = tail.tail();
// First: did we complete traversal? If so, easy, we got our result
if (tail.matches()) {
return putArray(propName);
}
// Otherwise, do we want Array or Object
if (preferIndex && tail.mayMatchElement()) { // array!
return putArray(propName)._withArrayAddTailElement(tail, preferIndex);
}
return putObject(propName)._withArrayAddTailProperty(tail, preferIndex);
}
/*
/**********************************************************
/* Overrides for JsonSerializable.Base
/**********************************************************
*/
@Override
public boolean isEmpty(SerializerProvider serializers) {
return _children.isEmpty();
}
/*
/**********************************************************
/* Implementation of core JsonNode API
/**********************************************************
*/
@Override
public JsonNodeType getNodeType() {
return JsonNodeType.OBJECT;
}
@Override
public final boolean isObject() {
return true;
}
@Override public JsonToken asToken() { return JsonToken.START_OBJECT; }
@Override
public int size() {
return _children.size();
}
@Override // since 2.10
public boolean isEmpty() { return _children.isEmpty(); }
@Override
public Iterator<JsonNode> elements() {
return _children.values().iterator();
}
@Override
public JsonNode get(int index) { return null; }
@Override
public JsonNode get(String propertyName) {
return _children.get(propertyName);
}
@Override
public Iterator<String> fieldNames() {
return _children.keySet().iterator();
}
@Override
public JsonNode path(int index) {
return MissingNode.getInstance();
}
@Override
public JsonNode path(String propertyName)
{
JsonNode n = _children.get(propertyName);
if (n != null) {
return n;
}
return MissingNode.getInstance();
}
@Override
public JsonNode required(String propertyName) {
JsonNode n = _children.get(propertyName);
if (n != null) {
return n;
}
return _reportRequiredViolation("No value for property '%s' of `ObjectNode`", propertyName);
}
/**
* Method to use for accessing all properties (with both names
* and values) of this JSON Object.
*/
@Override
public Iterator<Map.Entry<String, JsonNode>> fields() {
return _children.entrySet().iterator();
}
@Override
public boolean equals(Comparator<JsonNode> comparator, JsonNode o)
{
if (!(o instanceof ObjectNode)) {
return false;
}
ObjectNode other = (ObjectNode) o;
Map<String, JsonNode> m1 = _children;
Map<String, JsonNode> m2 = other._children;
final int len = m1.size();
if (m2.size() != len) {
return false;
}
for (Map.Entry<String, JsonNode> entry : m1.entrySet()) {
JsonNode v2 = m2.get(entry.getKey());
if ((v2 == null) || !entry.getValue().equals(comparator, v2)) {
return false;
}
}
return true;
}
/*
/**********************************************************
/* Public API, finding value nodes
/**********************************************************
*/
@Override
public JsonNode findValue(String propertyName)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
return entry.getValue();
}
JsonNode value = entry.getValue().findValue(propertyName);
if (value != null) {
return value;
}
}
return null;
}
@Override
public List<JsonNode> findValues(String propertyName, List<JsonNode> foundSoFar)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<JsonNode>();
}
foundSoFar.add(entry.getValue());
} else { // only add children if parent not added
foundSoFar = entry.getValue().findValues(propertyName, foundSoFar);
}
}
return foundSoFar;
}
@Override
public List<String> findValuesAsText(String propertyName, List<String> foundSoFar)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<String>();
}
foundSoFar.add(entry.getValue().asText());
} else { // only add children if parent not added
foundSoFar = entry.getValue().findValuesAsText(propertyName,
foundSoFar);
}
}
return foundSoFar;
}
@Override
public ObjectNode findParent(String propertyName)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
return this;
}
JsonNode value = entry.getValue().findParent(propertyName);
if (value != null) {
return (ObjectNode) value;
}
}
return null;
}
@Override
public List<JsonNode> findParents(String propertyName, List<JsonNode> foundSoFar)
{
for (Map.Entry<String, JsonNode> entry : _children.entrySet()) {
if (propertyName.equals(entry.getKey())) {
if (foundSoFar == null) {
foundSoFar = new ArrayList<JsonNode>();
}
foundSoFar.add(this);
} else { // only add children if parent not added
foundSoFar = entry.getValue()
.findParents(propertyName, foundSoFar);
}
}
return foundSoFar;
}
/*
/**********************************************************
/* Public API, serialization
/**********************************************************
*/
/**
* Method that can be called to serialize this node and
* all of its descendants using specified JSON generator.
*/
@SuppressWarnings("deprecation")
@Override
public void serialize(JsonGenerator g, SerializerProvider provider)
throws IOException
{
if (provider != null) {
boolean trimEmptyArray = !provider.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
boolean skipNulls = !provider.isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES);
if (trimEmptyArray || skipNulls) {
g.writeStartObject(this);
serializeFilteredContents(g, provider, trimEmptyArray, skipNulls);
g.writeEndObject();
return;
}
}
g.writeStartObject(this);
for (Map.Entry<String, JsonNode> en : _children.entrySet()) {
JsonNode value = en.getValue();
g.writeFieldName(en.getKey());
value.serialize(g, provider);
}
g.writeEndObject();
}
@SuppressWarnings("deprecation")
@Override
public void serializeWithType(JsonGenerator g, SerializerProvider provider,
TypeSerializer typeSer)
throws IOException
{
boolean trimEmptyArray = false;
boolean skipNulls = false;
if (provider != null) {
trimEmptyArray = !provider.isEnabled(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS);
skipNulls = !provider.isEnabled(JsonNodeFeature.WRITE_NULL_PROPERTIES);
}
WritableTypeId typeIdDef = typeSer.writeTypePrefix(g,
typeSer.typeId(this, JsonToken.START_OBJECT));
if (trimEmptyArray || skipNulls) {
serializeFilteredContents(g, provider, trimEmptyArray, skipNulls);
} else {
for (Map.Entry<String, JsonNode> en : _children.entrySet()) {
JsonNode value = en.getValue();
g.writeFieldName(en.getKey());
value.serialize(g, provider);
}
}
typeSer.writeTypeSuffix(g, typeIdDef);
}
/**
* Helper method shared and called by {@link #serialize} and {@link #serializeWithType}
* in cases where actual filtering is needed based on configuration.
*
* @since 2.14
*/
protected void serializeFilteredContents(final JsonGenerator g, final SerializerProvider provider,
final boolean trimEmptyArray, final boolean skipNulls)
throws IOException
{
for (Map.Entry<String, JsonNode> en : _children.entrySet()) {
// 17-Feb-2009, tatu: Can we trust that all nodes will always
// extend BaseJsonNode? Or if not, at least implement
// JsonSerializable? Let's start with former, change if
// we must.
BaseJsonNode value = (BaseJsonNode) en.getValue();
// as per [databind#867], see if WRITE_EMPTY_JSON_ARRAYS feature is disabled,
// if the feature is disabled, then should not write an empty array
// to the output, so continue to the next element in the iteration
if (trimEmptyArray && value.isArray() && value.isEmpty(provider)) {
continue;
}
if (skipNulls && value.isNull()) {
continue;
}
g.writeFieldName(en.getKey());
value.serialize(g, provider);
}
}
/*
/**********************************************************
/* Extended ObjectNode API, mutators, since 2.1
/**********************************************************
*/
/**
* Method that will set specified property, replacing old value, if any.
* Note that this is identical to {@link #replace(String, JsonNode)},
* except for return value.
*<p>
* NOTE: added to replace those uses of {@link #put(String, JsonNode)}
* where chaining with 'this' is desired.
*<p>
* NOTE: co-variant return type since 2.10
*
* @param propertyName Name of property to set
* @param value Value to set property to; if null, will be converted
* to a {@link NullNode} first (to remove a property, call
* {@link #remove} instead)
*
* @return This node after adding/replacing property value (to allow chaining)
*
* @since 2.1
*/
@SuppressWarnings("unchecked")
public <T extends JsonNode> T set(String propertyName, JsonNode value)
{
if (value == null) {
value = nullNode();
}
_children.put(propertyName, value);
return (T) this;
}
/**
* Method for adding given properties to this object node, overriding
* any existing values for those properties.
*<p>
* NOTE: co-variant return type since 2.10
*
* @param properties Properties to add
*
* @return This node after adding/replacing property values (to allow chaining)
*
* @since 2.1
*/
@SuppressWarnings("unchecked")
public <T extends JsonNode> T setAll(Map<String,? extends JsonNode> properties)
{
for (Map.Entry<String,? extends JsonNode> en : properties.entrySet()) {
JsonNode n = en.getValue();
if (n == null) {
n = nullNode();
}
_children.put(en.getKey(), n);
}
return (T) this;
}
/**
* Method for adding all properties of the given Object, overriding
* any existing values for those properties.
*<p>
* NOTE: co-variant return type since 2.10
*
* @param other Object of which properties to add to this object
*
* @return This node after addition (to allow chaining)
*
* @since 2.1
*/
@SuppressWarnings("unchecked")
public <T extends JsonNode> T setAll(ObjectNode other)
{
_children.putAll(other._children);
return (T) this;
}
/**
* Method for replacing value of specific property with passed
* value, and returning value (or null if none).
*
* @param propertyName Property of which value to replace
* @param value Value to set property to, replacing old value if any
*
* @return Old value of the property; null if there was no such property
* with value
*
* @since 2.1
*/
public JsonNode replace(String propertyName, JsonNode value)
{
if (value == null) { // let's not store 'raw' nulls but nodes
value = nullNode();
}
return _children.put(propertyName, value);
}
/**
* Method for removing property from this ObjectNode, and
* returning instance after removal.
*<p>
* NOTE: co-variant return type since 2.10
*
* @return This node after removing property (if any)
*
* @since 2.1
*/
@SuppressWarnings("unchecked")
public <T extends JsonNode> T without(String propertyName)
{
_children.remove(propertyName);
return (T) this;
}
/**
* Method for removing specified field properties out of
* this ObjectNode.
*<p>
* NOTE: co-variant return type since 2.10
*
* @param propertyNames Names of properties to remove
*
* @return This node after removing entries
*
* @since 2.1
*/
@SuppressWarnings("unchecked")
public <T extends JsonNode> T without(Collection<String> propertyNames)
{
_children.keySet().removeAll(propertyNames);
return (T) this;
}
/*
/**********************************************************
/* Extended ObjectNode API, mutators, generic
/**********************************************************
*/
/**
* Method that will set specified property, replacing old value, if any.
*
* @param propertyName Name of property to set
* @param value Value to set to property; if null, will be converted
* to a {@link NullNode} first (to remove a property, call
* {@link #remove} instead).
*
* @return Old value of the property, if any; {@code null} if there was no
* old value.
*
* @deprecated Since 2.4 use either {@link #set(String,JsonNode)} or {@link #replace(String,JsonNode)},
*/
@Deprecated
public JsonNode put(String propertyName, JsonNode value)
{
if (value == null) { // let's not store 'raw' nulls but nodes
value = nullNode();
}
return _children.put(propertyName, value);
}
/**
* Method that will set value of specified property if (and only if)
* it had no set value previously.
* Note that explicitly set {@code null} is a value.
* Functionally equivalent to:
*<code>
* if (get(propertyName) == null) {
* set(propertyName, value);
* return null;
* } else {
* return get(propertyName);
* }
*</code>
*
* @param propertyName Name of property to set
* @param value Value to set to property (if and only if it had no value previously);
* if null, will be converted to a {@link NullNode} first.
*
* @return Old value of the property, if any (in which case value was not changed);
* null if there was no old value (in which case value is now set)
*
* @since 2.13
*/
public JsonNode putIfAbsent(String propertyName, JsonNode value)
{
if (value == null) { // let's not store 'raw' nulls but nodes
value = nullNode();
}
return _children.putIfAbsent(propertyName, value);
}
/**
* Method for removing a property from this {@code ObjectNode}.
* Will return previous value of the property, if such property existed;
* null if not.
*
* @return Value of specified property, if it existed; null if not
*/
public JsonNode remove(String propertyName) {
return _children.remove(propertyName);
}
/**
* Method for removing specified field properties out of
* this ObjectNode.
*
* @param propertyNames Names of fields to remove
*
* @return This node after removing entries
*/
public ObjectNode remove(Collection<String> propertyNames)
{
_children.keySet().removeAll(propertyNames);
return this;
}
/**
* Method for removing all properties, such that this
* ObjectNode will contain no properties after call.
*
* @return This node after removing all entries
*/
@Override
public ObjectNode removeAll()
{
_children.clear();
return this;
}
/**
* Method for adding given properties to this object node, overriding
* any existing values for those properties.
*
* @param properties Properties to add
*
* @return This node after adding/replacing property values (to allow chaining)
*
* @deprecated Since 2.4 use {@link #setAll(Map)},
*/
@Deprecated
public JsonNode putAll(Map<String,? extends JsonNode> properties) {
return setAll(properties);
}
/**
* Method for adding all properties of the given Object, overriding
* any existing values for those properties.
*
* @param other Object of which properties to add to this object
*
* @return This node (to allow chaining)
*
* @deprecated Since 2.4 use {@link #setAll(ObjectNode)},
*/
@Deprecated
public JsonNode putAll(ObjectNode other) {
return setAll(other);
}
/**
* Method for removing all properties out of this ObjectNode
* <b>except</b> for ones specified in argument.
*
* @param propertyNames Fields to <b>retain</b> in this ObjectNode
*
* @return This node (to allow call chaining)
*/
public ObjectNode retain(Collection<String> propertyNames)
{
_children.keySet().retainAll(propertyNames);
return this;
}
/**
* Method for removing all properties out of this ObjectNode
* <b>except</b> for ones specified in argument.
*
* @param propertyNames Fields to <b>retain</b> in this ObjectNode
*
* @return This node (to allow call chaining)
*/
public ObjectNode retain(String... propertyNames) {
return retain(Arrays.asList(propertyNames));
}
/*
/**********************************************************
/* Extended ObjectNode API, mutators, typed
/**********************************************************
*/
/**
* Method that will construct an ArrayNode and add it as a
* property of this {@code ObjectNode}, replacing old value, if any.
*<p>
* <b>NOTE</b>: Unlike all <b>put(...)</b> methods, return value
* is <b>NOT</b> this <code>ObjectNode</code>, but the
* <b>newly created</b> <code>ArrayNode</code> instance.
*
* @return Newly constructed ArrayNode (NOT the old value,
* which could be of any type)
*/
public ArrayNode putArray(String propertyName)
{
ArrayNode n = arrayNode();
_put(propertyName, n);
return n;
}
/**
* Method that will construct an ObjectNode and add it as a
* property of this {@code ObjectNode}, replacing old value, if any.
*<p>
* <b>NOTE</b>: Unlike all <b>put(...)</b> methods, return value
* is <b>NOT</b> this <code>ObjectNode</code>, but the
* <b>newly created</b> <code>ObjectNode</code> instance.
*
* @return Newly constructed ObjectNode (NOT the old value,
* which could be of any type)
*/
public ObjectNode putObject(String propertyName)
{
ObjectNode n = objectNode();
_put(propertyName, n);
return n;
}
/**
* Method for adding an opaque Java value as the value of specified property.
* Value can be serialized like any other property, as long as Jackson can
* serialize it. Despite term "POJO" this allows use of about any Java type, including
* {@link java.util.Map}s, {@link java.util.Collection}s, as well as Beans (POJOs),
* primitives/wrappers and even {@link JsonNode}s.
* Method is most commonly useful when composing content to serialize from heterogenous
* sources.
*<p>
* NOTE: if using {@link JsonNode#toString()} (or {@link JsonNode#toPrettyString()}
* support for serialization may be more limited, compared to serializing node
* with specifically configured {@link ObjectMapper}.
*
* @param propertyName Name of property to set.
* @param pojo Java value to set as the property value
*
* @return This {@code ObjectNode} (to allow chaining)
*/
public ObjectNode putPOJO(String propertyName, Object pojo) {
return _put(propertyName, pojoNode(pojo));
}
/**
* @since 2.6
*/
public ObjectNode putRawValue(String propertyName, RawValue raw) {
return _put(propertyName, rawValueNode(raw));
}
/**
* Method for setting value of a property to explicit {@code null} value.
*
* @param propertyName Name of property to set.
*
* @return This {@code ObjectNode} (to allow chaining)
*/
public ObjectNode putNull(String propertyName)
{
_children.put(propertyName, nullNode());
return this;
}
/**
* Method for setting value of a property to specified numeric value.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String propertyName, short v) {
return _put(propertyName, numberNode(v));
}
/**
* Alternative method that we need to avoid bumping into NPE issues
* with auto-unboxing.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, Short v) {
return _put(fieldName, (v == null) ? nullNode()
: numberNode(v.shortValue()));
}
/**
* Method for setting value of a field to specified numeric value.
* The underlying {@link JsonNode} that will be added is constructed
* using {@link JsonNodeFactory#numberNode(int)}, and may be
* "smaller" (like {@link ShortNode}) in cases where value fits within
* range of a smaller integral numeric value.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, int v) {
return _put(fieldName, numberNode(v));
}
/**
* Alternative method that we need to avoid bumping into NPE issues
* with auto-unboxing.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, Integer v) {
return _put(fieldName, (v == null) ? nullNode()
: numberNode(v.intValue()));
}
/**
* Method for setting value of a field to specified numeric value.
* The underlying {@link JsonNode} that will be added is constructed
* using {@link JsonNodeFactory#numberNode(long)}, and may be
* "smaller" (like {@link IntNode}) in cases where value fits within
* range of a smaller integral numeric value.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, long v) {
return _put(fieldName, numberNode(v));
}
/**
* Method for setting value of a field to specified numeric value.
* The underlying {@link JsonNode} that will be added is constructed
* using {@link JsonNodeFactory#numberNode(Long)}, and may be
* "smaller" (like {@link IntNode}) in cases where value fits within
* range of a smaller integral numeric value.
* <p>
* Note that this is alternative to {@link #put(String, long)} needed to avoid
* bumping into NPE issues with auto-unboxing.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, Long v) {
return _put(fieldName, (v == null) ? nullNode()
: numberNode(v.longValue()));
}
/**
* Method for setting value of a field to specified numeric value.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, float v) {
return _put(fieldName, numberNode(v));
}
/**
* Alternative method that we need to avoid bumping into NPE issues
* with auto-unboxing.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, Float v) {
return _put(fieldName, (v == null) ? nullNode()
: numberNode(v.floatValue()));
}
/**
* Method for setting value of a field to specified numeric value.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, double v) {
return _put(fieldName, numberNode(v));
}
/**
* Alternative method that we need to avoid bumping into NPE issues
* with auto-unboxing.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, Double v) {
return _put(fieldName, (v == null) ? nullNode()
: numberNode(v.doubleValue()));
}
/**
* Method for setting value of a field to specified numeric value.
*
* @return This node (to allow chaining)
*/
public ObjectNode put(String fieldName, BigDecimal v) {
return _put(fieldName, (v == null) ? nullNode()
: numberNode(v));
}
/**
* Method for setting value of a field to specified numeric value.
*
* @return This node (to allow chaining)
*
* @since 2.9
*/
public ObjectNode put(String fieldName, BigInteger v) {
return _put(fieldName, (v == null) ? nullNode()
: numberNode(v));
}
/**
* Method for setting value of a field to specified String value.
*
* @return This node (to allow chaining)