Class JsonPointer
java.lang.Object
cloud.opencode.base.json.path.JsonPointer
JSON Pointer - RFC 6901 Implementation
JSON Pointer - RFC 6901 实现
This class implements JSON Pointer (RFC 6901), a syntax for identifying specific values within a JSON document.
此类实现 JSON Pointer(RFC 6901),用于标识 JSON 文档中特定值的语法。
Syntax | 语法:
""- Root document - 根文档"/foo"- Property "foo" of root - 根的属性 "foo""/foo/0"- First element of array "foo" - 数组 "foo" 的第一个元素"/a~1b"- Property "a/b" (/ escaped as ~1) - 属性 "a/b"(/ 转义为 ~1)"/m~0n"- Property "m~n" (~ escaped as ~0) - 属性 "m~n"(~ 转义为 ~0)
Example | 示例:
JsonNode root = OpenJson.parse("{\"foo\":{\"bar\":[1,2,3]}}");
// Access nested values
JsonPointer pointer = JsonPointer.parse("/foo/bar/1");
JsonNode value = pointer.evaluate(root); // Returns 2
// Modify values
JsonPointer.parse("/foo/bar/0").set(root, JsonNode.of(10));
Features | 主要功能:
- RFC 6901 JSON Pointer implementation - RFC 6901 JSON Pointer实现
- Path-based navigation with escaping support - 支持转义的路径导航
- Pointer composition and decomposition - 指针组合和分解
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
- Since:
- JDK 25, opencode-base-json V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionappend(int index) Appends an array index to this pointer.Appends a property name to this pointer.intdepth()Returns the depth (number of tokens).booleanEvaluates this pointer against a JSON node.evaluateOrNull(JsonNode root) Evaluates this pointer, returning null if path doesn't exist.booleanChecks if this pointer exists in the given node.Returns the last reference token.Returns the reference tokens.inthashCode()booleanisRoot()Returns whether this is the root pointer.static JsonPointerCreates a JsonPointer from reference tokens.static JsonPointerCreates a JsonPointer from reference tokens.parent()Returns the parent pointer.static JsonPointerParses a JSON Pointer string.toString()Returns the pointer string representation.
-
Field Details
-
ROOT
Empty/root pointer 空/根指针
-
-
Method Details
-
parse
Parses a JSON Pointer string. 解析 JSON Pointer 字符串。- Parameters:
pointer- the pointer string (e.g., "/foo/bar") - 指针字符串- Returns:
- the JsonPointer instance - JsonPointer 实例
- Throws:
OpenJsonProcessingException- if the pointer is invalid - 如果指针无效
-
of
Creates a JsonPointer from reference tokens. 从引用令牌创建 JsonPointer。- Parameters:
tokens- the reference tokens - 引用令牌- Returns:
- the JsonPointer instance - JsonPointer 实例
-
of
Creates a JsonPointer from reference tokens. 从引用令牌创建 JsonPointer。- Parameters:
tokens- the reference tokens - 引用令牌- Returns:
- the JsonPointer instance - JsonPointer 实例
-
evaluate
Evaluates this pointer against a JSON node. 对 JSON 节点求值此指针。- Parameters:
root- the root node - 根节点- Returns:
- the node at this pointer location - 此指针位置的节点
- Throws:
OpenJsonProcessingException- if path is invalid - 如果路径无效
-
evaluateOrNull
-
exists
Checks if this pointer exists in the given node. 检查此指针是否存在于给定节点中。- Parameters:
root- the root node - 根节点- Returns:
- true if the path exists - 如果路径存在则返回 true
-
parent
Returns the parent pointer. 返回父指针。- Returns:
- the parent pointer, or ROOT if this is ROOT - 父指针,如果是 ROOT 则返回 ROOT
-
append
Appends a property name to this pointer. 向此指针追加属性名。- Parameters:
property- the property name - 属性名- Returns:
- a new pointer with the appended property - 追加属性后的新指针
-
append
Appends an array index to this pointer. 向此指针追加数组索引。- Parameters:
index- the array index - 数组索引- Returns:
- a new pointer with the appended index - 追加索引后的新指针
-
getLastToken
Returns the last reference token. 返回最后一个引用令牌。- Returns:
- the last token, or null for ROOT - 最后一个令牌,ROOT 返回 null
-
getTokens
-
depth
public int depth()Returns the depth (number of tokens). 返回深度(令牌数量)。- Returns:
- the depth - 深度
-
isRoot
public boolean isRoot()Returns whether this is the root pointer. 返回此指针是否为根指针。- Returns:
- true if root - 如果是根则返回 true
-
toString
-
equals
-
hashCode
-