Interface YmlNode

All Superinterfaces:
Iterable<YmlNode>
All Known Implementing Classes:
DefaultYmlNode

public interface YmlNode extends Iterable<YmlNode>
YAML Node Interface - Represents a node in YAML document tree YAML 节点接口 - 表示 YAML 文档树中的节点

This interface represents a node in the YAML document tree structure. A node can be a scalar (string, number, boolean), sequence (list), or mapping (map).

此接口表示 YAML 文档树结构中的节点。 节点可以是标量(字符串、数字、布尔值)、序列(列表)或映射(映射)。

Features | 主要功能:

  • Typed value access (scalar, sequence, mapping, null) - 类型化值访问(标量、序列、映射、空)
  • Dot-notation path navigation - 点号路径导航
  • Object binding support - 对象绑定支持
  • Iterable for sequence/mapping traversal - 可迭代的序列/映射遍历

Usage Examples | 使用示例:

YmlNode root = YmlDocument.parse(yaml).getRoot();

// Check node type
if (root.isMapping()) {
    String name = root.get("name").asText();
}

// Use path access
String city = root.at("address.city").asText();

// Iterate sequence
for (YmlNode item : root.get("items")) {
    System.out.println(item.asText());
}

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Null-safe: Yes (returns null node for missing keys) - 空值安全: 是(缺失键返回空节点)
Since:
JDK 25, opencode-base-yml V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static enum 
    Node Type - Types of YAML nodes 节点类型 - YAML 节点的类型
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Gets the boolean value.
    boolean
    asBoolean(boolean defaultValue)
    Gets the boolean value with default.
    double
    Gets the double value.
    double
    asDouble(double defaultValue)
    Gets the double value with default.
    int
    Gets the integer value.
    int
    asInt(int defaultValue)
    Gets the integer value with default.
    long
    Gets the long value.
    long
    asLong(long defaultValue)
    Gets the long value with default.
    Gets the string value.
    asText(String defaultValue)
    Gets the string value with default.
    at(String path)
    Gets a node using dot-notation path.
    get(int index)
    Gets a child node by index (for sequence nodes).
    get(String key)
    Gets a child node by key (for mapping nodes).
    Gets the raw underlying value.
    Gets the node type.
    boolean
    has(String key)
    Checks if the mapping contains a key.
    default boolean
    Checks if this is a mapping node.
    default boolean
    Checks if this is a null node.
    default boolean
    Checks if this is a scalar node.
    default boolean
    Checks if this is a sequence node.
    Gets all keys (for mapping nodes).
    static YmlNode
    of(Object value)
    Creates a YmlNode from the given value.
    int
    Gets the size (for sequence or mapping nodes).
    Converts to a List.
    Converts to a Map.
    <T> T
    toObject(Class<T> clazz)
    Converts to a Java object.
    Converts to a YAML string.
    Gets all child nodes.

    Methods inherited from interface Iterable

    forEach, iterator, spliterator
  • Method Details

    • of

      static YmlNode of(Object value)
      Creates a YmlNode from the given value. 从给定值创建 YmlNode。
      Parameters:
      value - the raw value | 原始值
      Returns:
      the node | 节点
    • getType

      YmlNode.NodeType getType()
      Gets the node type. 获取节点类型。
      Returns:
      the node type | 节点类型
    • isScalar

      default boolean isScalar()
      Checks if this is a scalar node. 检查是否为标量节点。
      Returns:
      true if scalar | 如果是标量则返回 true
    • isSequence

      default boolean isSequence()
      Checks if this is a sequence node. 检查是否为序列节点。
      Returns:
      true if sequence | 如果是序列则返回 true
    • isMapping

      default boolean isMapping()
      Checks if this is a mapping node. 检查是否为映射节点。
      Returns:
      true if mapping | 如果是映射则返回 true
    • isNull

      default boolean isNull()
      Checks if this is a null node. 检查是否为空节点。
      Returns:
      true if null | 如果是空则返回 true
    • asText

      String asText()
      Gets the string value. 获取字符串值。
      Returns:
      the string value | 字符串值
    • asText

      String asText(String defaultValue)
      Gets the string value with default. 获取字符串值(带默认值)。
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      the string value or default | 字符串值或默认值
    • asInt

      int asInt()
      Gets the integer value. 获取整数值。
      Returns:
      the integer value | 整数值
    • asInt

      int asInt(int defaultValue)
      Gets the integer value with default. 获取整数值(带默认值)。
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      the integer value or default | 整数值或默认值
    • asLong

      long asLong()
      Gets the long value. 获取长整数值。
      Returns:
      the long value | 长整数值
    • asLong

      long asLong(long defaultValue)
      Gets the long value with default. 获取长整数值(带默认值)。
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      the long value or default | 长整数值或默认值
    • asBoolean

      boolean asBoolean()
      Gets the boolean value. 获取布尔值。
      Returns:
      the boolean value | 布尔值
    • asBoolean

      boolean asBoolean(boolean defaultValue)
      Gets the boolean value with default. 获取布尔值(带默认值)。
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      the boolean value or default | 布尔值或默认值
    • asDouble

      double asDouble()
      Gets the double value. 获取双精度值。
      Returns:
      the double value | 双精度值
    • asDouble

      double asDouble(double defaultValue)
      Gets the double value with default. 获取双精度值(带默认值)。
      Parameters:
      defaultValue - the default value | 默认值
      Returns:
      the double value or default | 双精度值或默认值
    • get

      YmlNode get(String key)
      Gets a child node by key (for mapping nodes). 通过键获取子节点(用于映射节点)。
      Parameters:
      key - the key | 键
      Returns:
      the child node, or null node if not found | 子节点,如果未找到则返回空节点
    • get

      YmlNode get(int index)
      Gets a child node by index (for sequence nodes). 通过索引获取子节点(用于序列节点)。
      Parameters:
      index - the index | 索引
      Returns:
      the child node | 子节点
    • at

      YmlNode at(String path)
      Gets a node using dot-notation path. 使用点号路径获取节点。
      Parameters:
      path - the path (e.g., "a.b.c" or "items[0].name") | 路径(如:"a.b.c" 或 "items[0].name")
      Returns:
      the node at path, or null node if not found | 路径处的节点,如果未找到则返回空节点
    • has

      boolean has(String key)
      Checks if the mapping contains a key. 检查映射是否包含键。
      Parameters:
      key - the key | 键
      Returns:
      true if contains | 如果包含则返回 true
    • size

      int size()
      Gets the size (for sequence or mapping nodes). 获取大小(用于序列或映射节点)。
      Returns:
      the size | 大小
    • keys

      Set<String> keys()
      Gets all keys (for mapping nodes). 获取所有键(用于映射节点)。
      Returns:
      the keys | 键集合
    • values

      List<YmlNode> values()
      Gets all child nodes. 获取所有子节点。
      Returns:
      the child nodes | 子节点列表
    • toObject

      <T> T toObject(Class<T> clazz)
      Converts to a Java object. 转换为 Java 对象。
      Type Parameters:
      T - the type parameter | 类型参数
      Parameters:
      clazz - the target type | 目标类型
      Returns:
      the object | 对象
    • toMap

      Map<String,Object> toMap()
      Converts to a Map. 转换为 Map。
      Returns:
      the Map | Map
    • toList

      List<Object> toList()
      Converts to a List. 转换为 List。
      Returns:
      the List | List
    • toYaml

      String toYaml()
      Converts to a YAML string. 转换为 YAML 字符串。
      Returns:
      the YAML string | YAML 字符串
    • getRawValue

      Object getRawValue()
      Gets the raw underlying value. 获取原始底层值。
      Returns:
      the raw value | 原始值