Skip to content

Commit 34723d2

Browse files
committed
Start implementing #1980
1 parent ae38732 commit 34723d2

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/main/java/com/fasterxml/jackson/databind/JsonNode.java

+16
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,22 @@ public <T extends JsonNode> T withObject(String propertyName) {
10901090
+getClass().getName()+"), cannot call with() on it");
10911091
}
10921092

1093+
/**
1094+
* Method that can be called on Object nodes, to access a Object-valued
1095+
* node pointed to by given {@link JsonPointer}, if such a node exists:
1096+
* if not, an attempt is made to create it.
1097+
* If the node method is called on is not Object node,
1098+
* or if property exists and has value that is not Object node,
1099+
* {@link UnsupportedOperationException} is thrown
1100+
*
1101+
* @since 2.14
1102+
*/
1103+
public <T extends JsonNode> T withObject(JsonPointer ptr) {
1104+
// To avoid abstract method, base implementation just fails
1105+
throw new UnsupportedOperationException("`withObject(JsonPointer)` not implemented by "
1106+
+getClass().getName());
1107+
}
1108+
10931109
/**
10941110
* @deprecated Since 2.14 use {@code withObject} instead
10951111
*/

src/main/java/com/fasterxml/jackson/databind/node/BaseJsonNode.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ public JsonParser.NumberType numberType() {
103103
return null;
104104
}
105105

106+
/*
107+
/**********************************************************
108+
/* Other traversal
109+
/**********************************************************
110+
*/
111+
112+
@Override
113+
public <T extends JsonNode> T withObject(JsonPointer ptr) {
114+
if (!isObject()) {
115+
// To avoid abstract method, base implementation just fails
116+
_reportWrongNodeType("Can only call `withObject()` on `ObjectNode`, not `%s`",
117+
getClass().getName());
118+
}
119+
return _withObject(ptr, ptr);
120+
}
121+
122+
protected <T extends JsonNode> T _withObject(JsonPointer origPtr,
123+
JsonPointer currentPTr) {
124+
return null;
125+
}
126+
106127
/*
107128
/**********************************************************
108129
/* JsonSerializable
@@ -140,5 +161,18 @@ public String toString() {
140161
public String toPrettyString() {
141162
return InternalNodeMapper.nodeToPrettyString(this);
142163
}
143-
}
144164

165+
/*
166+
/**********************************************************
167+
/* Other helper methods for subtypes
168+
/**********************************************************
169+
*/
170+
171+
/**
172+
* Helper method that throws {@link UnsupportedOperationException} as a result of
173+
* this node being of wrong type
174+
*/
175+
protected <T> T _reportWrongNodeType(String msgTemplate, Object...args) {
176+
throw new UnsupportedOperationException(String.format(msgTemplate, args));
177+
}
178+
}

0 commit comments

Comments
 (0)