8
8
import com .fasterxml .jackson .core .*;
9
9
import com .fasterxml .jackson .databind .node .JsonNodeType ;
10
10
import com .fasterxml .jackson .databind .node .MissingNode ;
11
+ import com .fasterxml .jackson .databind .node .ObjectNode ;
11
12
import com .fasterxml .jackson .databind .util .ClassUtil ;
12
13
13
14
/**
@@ -40,6 +41,45 @@ public abstract class JsonNode
40
41
extends JsonSerializable .Base // i.e. implements JsonSerializable
41
42
implements TreeNode , Iterable <JsonNode >
42
43
{
44
+ /**
45
+ * Configuration setting used with {@link JsonNode#withObject(JsonPointer)}
46
+ * method overrides, to indicate which overwrites are acceptable if the
47
+ * path pointer indicates has incompatible nodes (for example, instead
48
+ * of Object node a Null node is encountered).
49
+ * Overwrite means that the existing value is replaced with compatible type,
50
+ * potentially losing existing values or even sub-trees.
51
+ *<p>
52
+ * Default value if {@code NULLS} which only allows Null-value nodes
53
+ * to be replaced but no other types.
54
+ *
55
+ * @since 2.14
56
+ */
57
+ public enum OverwriteMode {
58
+ /**
59
+ * Mode in which no values may be overwritten, not even {@code NullNode}s;
60
+ * only compatible paths may be traversed.
61
+ */
62
+ NONE ,
63
+
64
+ /**
65
+ * Mode in which explicit {@code NullNode}s may be replaced but no other
66
+ * node types.
67
+ */
68
+ NULLS ,
69
+
70
+ /**
71
+ * Mode in which all scalar value nodes may be replaced, but not
72
+ * Array or Object nodes.
73
+ */
74
+ SCALARS ,
75
+
76
+ /**
77
+ * Mode in which all incompatible node types may be replaced, including
78
+ * Array and Object nodes where necessary.
79
+ */
80
+ ALL ;
81
+ }
82
+
43
83
/*
44
84
/**********************************************************
45
85
/* Construction, related
@@ -1090,6 +1130,24 @@ public <T extends JsonNode> T withObject(String propertyName) {
1090
1130
+getClass ().getName ()+"), cannot call withObject() on it" );
1091
1131
}
1092
1132
1133
+ /**
1134
+ * Same as {@link #withObject(JsonPointer, OverwriteMode, boolean)} but
1135
+ * with defaults of {@code OvewriteMode#NULLS} (overwrite mode)
1136
+ * and {@code true} for {@code preferIndex} (that is, will try to
1137
+ * consider {@link JsonPointer} segments index if at all possible
1138
+ * and only secondarily as property name
1139
+ *
1140
+ * @param ptr Pointer that indicates path to use for Object value to return
1141
+ * (potentially creating as necessary)
1142
+ *
1143
+ * @return ObjectNode found or created
1144
+ *
1145
+ * @since 2.14
1146
+ */
1147
+ public final ObjectNode withObject (JsonPointer ptr ) {
1148
+ return withObject (ptr , OverwriteMode .NULLS , true );
1149
+ }
1150
+
1093
1151
/**
1094
1152
* Method that can be called on Object nodes, to access a Object-valued
1095
1153
* node pointed to by given {@link JsonPointer}, if such a node exists:
@@ -1098,9 +1156,16 @@ public <T extends JsonNode> T withObject(String propertyName) {
1098
1156
* or if property exists and has value that is not Object node,
1099
1157
* {@link UnsupportedOperationException} is thrown
1100
1158
*
1159
+ * @param ptr Pointer that indicates path to use for Object value to return
1160
+ * (potentially creating as necessary)
1161
+ * @param overwriteMode Defines w
1162
+ *
1163
+ * @return ObjectNode found or created
1164
+ *
1101
1165
* @since 2.14
1102
1166
*/
1103
- public <T extends JsonNode > T withObject (JsonPointer ptr ) {
1167
+ public ObjectNode withObject (JsonPointer ptr ,
1168
+ OverwriteMode overwriteMode , boolean preferIndex ) {
1104
1169
// To avoid abstract method, base implementation just fails
1105
1170
throw new UnsupportedOperationException ("`withObject(JsonPointer)` not implemented by "
1106
1171
+getClass ().getName ());
0 commit comments