Interface JsonNode
- All Known Implementing Classes:
JsonNode.ArrayNode, JsonNode.BooleanNode, JsonNode.NullNode, JsonNode.NumberNode, JsonNode.ObjectNode, JsonNode.StringNode
public sealed interface JsonNode
permits JsonNode.ObjectNode, JsonNode.ArrayNode, JsonNode.StringNode, JsonNode.NumberNode, JsonNode.BooleanNode, JsonNode.NullNode
JSON Node - Universal JSON Tree Representation
JSON 节点 - 通用 JSON 树表示
This class represents a JSON value in a tree model. It can hold any JSON type: object, array, string, number, boolean, or null.
此类表示树模型中的 JSON 值。它可以保存任何 JSON 类型: 对象、数组、字符串、数字、布尔值或 null。
Example | 示例:
// Create nodes
JsonNode obj = JsonNode.object()
.put("name", "John")
.put("age", 30)
.put("active", true);
JsonNode arr = JsonNode.array()
.add("one")
.add(2)
.add(true);
// Access values
String name = obj.get("name").asString();
int age = obj.get("age").asInt();
// Navigate with path
JsonNode value = obj.at("/name");
List<JsonNode> results = obj.select("$.name");
Features | 主要功能:
- Sealed interface with six concrete types - 密封接口包含六种具体类型
- Tree model for JSON objects, arrays, strings, numbers, booleans, and nulls - JSON对象、数组、字符串、数字、布尔值和null的树模型
- Navigation via JSON Pointer and JSONPath - 通过JSON Pointer和JSONPath导航
Security | 安全性:
- Thread-safe: scalar implementations only (
JsonNode.StringNode/JsonNode.NumberNode/JsonNode.BooleanNode/JsonNode.NullNode). The container typesJsonNode.ObjectNodeandJsonNode.ArrayNodeare mutable after construction (viaput/add/remove) and are NOT thread-safe — callers sharing a container across threads must provide their own synchronisation, or perform a deep copy at the publication boundary. - 线程安全:仅标量实现(
JsonNode.StringNode/JsonNode.NumberNode/JsonNode.BooleanNode/JsonNode.NullNode)。容器类型JsonNode.ObjectNode与JsonNode.ArrayNode构造后仍可变(通过put/add/remove),非 线程安全;跨线程共享容器的调用方需自行同步,或在发布边界做深拷贝。 - Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
- Since:
- JDK 25, opencode-base-json V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classArray Node - Represents a JSON array.static final recordBoolean Node - Represents a JSON boolean.static final recordNull Node - Represents a JSON null.static final recordNumber Node - Represents a JSON number.static final classObject Node - Represents a JSON object.static final recordString Node - Represents a JSON string. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum recursion depth forof(Object)smart-box of List/Map. -
Method Summary
Modifier and TypeMethodDescriptionstatic JsonNode.ArrayNodearray()Creates an empty array node.default BigDecimalReturns this node's value as a BigDecimal.default BigIntegerReturns this node's value as a BigInteger.default booleanReturns this node's value as a boolean.default booleanasBoolean(boolean defaultValue) Returns this node's value as a boolean with default.default doubleasDouble()Returns this node's value as a double.default doubleasDouble(double defaultValue) Returns this node's value as a double with default.default intasInt()Returns this node's value as an int.default intasInt(int defaultValue) Returns this node's value as an int with default.default longasLong()Returns this node's value as a long.default longasLong(long defaultValue) Returns this node's value as a long with default.default StringasString()Returns this node's value as a string.default JsonNodeat(JsonPointer pointer) Navigates to a node using JsonPointer.default JsonNodeNavigates to a node using JSON Pointer (RFC 6901).default JsonNodeget(int index) Gets a child node by index (for arrays).default JsonNodeGets a child node by property name (for objects).default booleanReturns whether this node has a property (for objects).default booleanisArray()Returns whether this node is an array.default booleanReturns whether this node is a boolean.default booleanReturns whether this node is a container (object or array).default booleanisEmpty()Returns whether this node is empty (for objects/arrays).default booleanisNull()Returns whether this node is null.default booleanisNumber()Returns whether this node is a number.default booleanisObject()Returns whether this node is an object.default booleanisString()Returns whether this node is a string.default booleanisValue()Returns whether this node represents a value (not container).keys()Returns the property names (for objects).static JsonNodenullNode()Creates a null node.static JsonNode.ObjectNodeobject()Creates an empty object node.static JsonNodeof(boolean value) Creates a boolean node.static JsonNodeCreates a number node.static JsonNodeCreates a node from a value.static JsonNodeCreates a string node.Selects nodes using JSONPath expression.default intsize()Returns the number of children (for objects/arrays).
-
Field Details
-
OF_MAX_DEPTH
static final int OF_MAX_DEPTHMaximum recursion depth forof(Object)smart-box of List/Map. Interface field is implicitlypublic static finalper Java spec.of(Object)List/Map 智能装箱的最大递归深度(接口字段隐式 public static final)。- See Also:
-
-
Method Details
-
isObject
default boolean isObject()Returns whether this node is an object. 返回此节点是否为对象。- Returns:
- true if object - 如果是对象则返回 true
-
isArray
default boolean isArray()Returns whether this node is an array. 返回此节点是否为数组。- Returns:
- true if array - 如果是数组则返回 true
-
isString
default boolean isString()Returns whether this node is a string. 返回此节点是否为字符串。- Returns:
- true if string - 如果是字符串则返回 true
-
isNumber
default boolean isNumber()Returns whether this node is a number. 返回此节点是否为数字。- Returns:
- true if number - 如果是数字则返回 true
-
isBoolean
default boolean isBoolean()Returns whether this node is a boolean. 返回此节点是否为布尔值。- Returns:
- true if boolean - 如果是布尔值则返回 true
-
isNull
default boolean isNull()Returns whether this node is null. 返回此节点是否为 null。- Returns:
- true if null - 如果是 null 则返回 true
-
isValue
default boolean isValue()Returns whether this node represents a value (not container). 返回此节点是否表示值(非容器)。- Returns:
- true if value - 如果是值则返回 true
-
isContainer
default boolean isContainer()Returns whether this node is a container (object or array). 返回此节点是否为容器(对象或数组)。- Returns:
- true if container - 如果是容器则返回 true
-
asString
Returns this node's value as a string. 返回此节点的值作为字符串。- Returns:
- the string value - 字符串值
-
asInt
default int asInt()Returns this node's value as an int. 返回此节点的值作为 int。- Returns:
- the int value - int 值
-
asInt
default int asInt(int defaultValue) Returns this node's value as an int with default. 返回此节点的值作为 int,带默认值。- Parameters:
defaultValue- the default value - 默认值- Returns:
- the int value - int 值
-
asLong
default long asLong()Returns this node's value as a long. 返回此节点的值作为 long。- Returns:
- the long value - long 值
-
asLong
default long asLong(long defaultValue) Returns this node's value as a long with default. 返回此节点的值作为 long,带默认值。- Parameters:
defaultValue- the default value - 默认值- Returns:
- the long value - long 值
-
asDouble
default double asDouble()Returns this node's value as a double. 返回此节点的值作为 double。- Returns:
- the double value - double 值
-
asDouble
default double asDouble(double defaultValue) Returns this node's value as a double with default. 返回此节点的值作为 double,带默认值。- Parameters:
defaultValue- the default value - 默认值- Returns:
- the double value - double 值
-
asBoolean
default boolean asBoolean()Returns this node's value as a boolean. 返回此节点的值作为布尔值。- Returns:
- the boolean value - 布尔值
-
asBoolean
default boolean asBoolean(boolean defaultValue) Returns this node's value as a boolean with default. 返回此节点的值作为布尔值,带默认值。- Parameters:
defaultValue- the default value - 默认值- Returns:
- the boolean value - 布尔值
-
asBigDecimal
Returns this node's value as a BigDecimal. 返回此节点的值作为 BigDecimal。- Returns:
- the BigDecimal value - BigDecimal 值
-
asBigInteger
Returns this node's value as a BigInteger. 返回此节点的值作为 BigInteger。- Returns:
- the BigInteger value - BigInteger 值
-
get
-
get
Gets a child node by index (for arrays). 按索引获取子节点(用于数组)。- Parameters:
index- the array index - 数组索引- Returns:
- the child node, or null - 子节点,或 null
-
has
Returns whether this node has a property (for objects). 返回此节点是否有属性(用于对象)。- Parameters:
key- the property name - 属性名- Returns:
- true if has property - 如果有属性则返回 true
-
keys
Returns the property names (for objects). ForJsonNode.ObjectNodethe returned set is a live unmodifiable view backed by the node's underlying map: subsequentput/removeon the sameJsonNode.ObjectNodeare visible through this view, and iterating it while another thread mutates the node will raiseConcurrentModificationException. If you need a stable snapshot or you intend to share the result across threads, copy it:Set.copyOf(node.keys()). Scalar nodes return an empty immutable set. 返回属性名(仅对对象有意义)。JsonNode.ObjectNode返回的集合是底层 map 的不可变活视图: 后续在同一JsonNode.ObjectNode上put/remove会通过该视图体现; 在迭代过程中另一线程改动节点会抛ConcurrentModificationException。 需要稳定快照或跨线程共享时,请用Set.copyOf(node.keys())复制。 标量节点返回空的不可变集合。- Returns:
- the property names - 属性名
-
size
default int size()Returns the number of children (for objects/arrays). 返回子元素数量(用于对象/数组)。- Returns:
- the size - 大小
-
isEmpty
default boolean isEmpty()Returns whether this node is empty (for objects/arrays). 返回此节点是否为空(用于对象/数组)。- Returns:
- true if empty - 如果为空则返回 true
-
at
-
at
Navigates to a node using JsonPointer. 使用 JsonPointer 导航到节点。- Parameters:
pointer- the JSON Pointer - JSON Pointer- Returns:
- the node at the pointer location - 指针位置的节点
-
select
-
nullNode
-
of
-
of
-
of
-
of
Creates a boolean node. 创建布尔节点。- Parameters:
value- the boolean value - 布尔值- Returns:
- the node - 节点
-
object
Creates an empty object node. 创建空对象节点。- Returns:
- the object node - 对象节点
-
array
Creates an empty array node. 创建空数组节点。- Returns:
- the array node - 数组节点
-