Interface JsonNode

All Known Implementing Classes:
JsonNode.ArrayNode, JsonNode.BooleanNode, JsonNode.NullNode, JsonNode.NumberNode, JsonNode.ObjectNode, JsonNode.StringNode

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: Yes (immutable record) - 线程安全: 是(不可变记录)
  • 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 Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Array Node - Represents a JSON array.
    static final record 
    Boolean Node - Represents a JSON boolean.
    static final record 
    Null Node - Represents a JSON null.
    static final record 
    Number Node - Represents a JSON number.
    static final class 
    Object Node - Represents a JSON object.
    static final record 
    String Node - Represents a JSON string.
  • Method Summary

    Modifier and Type
    Method
    Description
    Creates an empty array node.
    default BigDecimal
    Returns this node's value as a BigDecimal.
    default BigInteger
    Returns this node's value as a BigInteger.
    default boolean
    Returns this node's value as a boolean.
    default boolean
    asBoolean(boolean defaultValue)
    Returns this node's value as a boolean with default.
    default double
    Returns this node's value as a double.
    default double
    asDouble(double defaultValue)
    Returns this node's value as a double with default.
    default int
    Returns this node's value as an int.
    default int
    asInt(int defaultValue)
    Returns this node's value as an int with default.
    default long
    Returns this node's value as a long.
    default long
    asLong(long defaultValue)
    Returns this node's value as a long with default.
    default String
    Returns this node's value as a string.
    default JsonNode
    at(JsonPointer pointer)
    Navigates to a node using JsonPointer.
    default JsonNode
    at(String pointer)
    Navigates to a node using JSON Pointer (RFC 6901).
    default JsonNode
    get(int index)
    Gets a child node by index (for arrays).
    default JsonNode
    get(String key)
    Gets a child node by property name (for objects).
    default boolean
    has(String key)
    Returns whether this node has a property (for objects).
    default boolean
    Returns whether this node is an array.
    default boolean
    Returns whether this node is a boolean.
    default boolean
    Returns whether this node is a container (object or array).
    default boolean
    Returns whether this node is empty (for objects/arrays).
    default boolean
    Returns whether this node is null.
    default boolean
    Returns whether this node is a number.
    default boolean
    Returns whether this node is an object.
    default boolean
    Returns whether this node is a string.
    default boolean
    Returns whether this node represents a value (not container).
    default Set<String>
    Returns the property names (for objects).
    static JsonNode
    Creates a null node.
    Creates an empty object node.
    static JsonNode
    of(boolean value)
    Creates a boolean node.
    static JsonNode
    of(Number value)
    Creates a number node.
    static JsonNode
    of(Object value)
    Creates a node from a value.
    static JsonNode
    of(String value)
    Creates a string node.
    default List<JsonNode>
    select(String path)
    Selects nodes using JSONPath expression.
    default int
    Returns the number of children (for objects/arrays).
  • 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

      default String 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

      default BigDecimal asBigDecimal()
      Returns this node's value as a BigDecimal. 返回此节点的值作为 BigDecimal。
      Returns:
      the BigDecimal value - BigDecimal 值
    • asBigInteger

      default BigInteger asBigInteger()
      Returns this node's value as a BigInteger. 返回此节点的值作为 BigInteger。
      Returns:
      the BigInteger value - BigInteger 值
    • get

      default JsonNode get(String key)
      Gets a child node by property name (for objects). 按属性名获取子节点(用于对象)。
      Parameters:
      key - the property name - 属性名
      Returns:
      the child node, or null - 子节点,或 null
    • get

      default JsonNode get(int index)
      Gets a child node by index (for arrays). 按索引获取子节点(用于数组)。
      Parameters:
      index - the array index - 数组索引
      Returns:
      the child node, or null - 子节点,或 null
    • has

      default boolean has(String key)
      Returns whether this node has a property (for objects). 返回此节点是否有属性(用于对象)。
      Parameters:
      key - the property name - 属性名
      Returns:
      true if has property - 如果有属性则返回 true
    • keys

      default Set<String> keys()
      Returns the property names (for objects). 返回属性名(用于对象)。
      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

      default JsonNode at(String pointer)
      Navigates to a node using JSON Pointer (RFC 6901). 使用 JSON Pointer (RFC 6901) 导航到节点。
      Parameters:
      pointer - the JSON Pointer string - JSON Pointer 字符串
      Returns:
      the node at the pointer location - 指针位置的节点
    • at

      default JsonNode at(JsonPointer pointer)
      Navigates to a node using JsonPointer. 使用 JsonPointer 导航到节点。
      Parameters:
      pointer - the JSON Pointer - JSON Pointer
      Returns:
      the node at the pointer location - 指针位置的节点
    • select

      default List<JsonNode> select(String path)
      Selects nodes using JSONPath expression. 使用 JSONPath 表达式选择节点。
      Parameters:
      path - the JSONPath expression - JSONPath 表达式
      Returns:
      matching nodes - 匹配的节点
    • nullNode

      static JsonNode nullNode()
      Creates a null node. 创建 null 节点。
      Returns:
      the null node - null 节点
    • of

      static JsonNode of(Object value)
      Creates a node from a value. 从值创建节点。
      Parameters:
      value - the value - 值
      Returns:
      the node - 节点
    • of

      static JsonNode of(String value)
      Creates a string node. 创建字符串节点。
      Parameters:
      value - the string value - 字符串值
      Returns:
      the node - 节点
    • of

      static JsonNode of(Number value)
      Creates a number node. 创建数字节点。
      Parameters:
      value - the number value - 数字值
      Returns:
      the node - 节点
    • of

      static JsonNode of(boolean value)
      Creates a boolean node. 创建布尔节点。
      Parameters:
      value - the boolean value - 布尔值
      Returns:
      the node - 节点
    • object

      static JsonNode.ObjectNode object()
      Creates an empty object node. 创建空对象节点。
      Returns:
      the object node - 对象节点
    • array

      static JsonNode.ArrayNode array()
      Creates an empty array node. 创建空数组节点。
      Returns:
      the array node - 数组节点