Interface JsonReader

All Superinterfaces:
AutoCloseable, Closeable

public interface JsonReader extends Closeable
JSON Reader - Streaming JSON Parser Interface JSON 读取器 - 流式 JSON 解析器接口

This interface provides a streaming (pull-based) API for reading JSON. It allows efficient parsing of large JSON documents without loading the entire content into memory.

此接口提供用于读取 JSON 的流式(拉取式)API。 它允许高效解析大型 JSON 文档,无需将整个内容加载到内存中。

Example | 示例:

try (JsonReader reader = OpenJson.createReader(inputStream)) {
    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if ("id".equals(name)) {
            long id = reader.nextLong();
        } else if ("name".equals(name)) {
            String value = reader.nextString();
        } else {
            reader.skipValue();
        }
    }
    reader.endObject();
}

Features | 主要功能:

  • Streaming pull-based JSON parsing - 流式拉取式JSON解析
  • Structure navigation (begin/end object/array) - 结构导航(开始/结束对象/数组)
  • Path and position tracking for error reporting - 路径和位置跟踪用于错误报告

Security | 安全性:

  • Thread-safe: Implementation-dependent - 线程安全: 取决于实现
  • Null-safe: Partial (validates inputs) - 空值安全: 部分(验证输入)
Since:
JDK 25, opencode-base-json V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Consumes the next token and asserts it is START_ARRAY.
    void
    Consumes the next token and asserts it is START_OBJECT.
    void
    Closes this reader and releases resources.
    void
    Consumes the next token and asserts it is END_ARRAY.
    void
    Consumes the next token and asserts it is END_OBJECT.
    int
    Returns the current column number (1-based).
    int
    Returns the current line number (1-based).
    Returns the current path in the JSON document.
    boolean
    Returns true if the current array or object has more elements.
    boolean
    Returns whether this reader is lenient.
    Returns the BigDecimal value of the next token and consumes it.
    Returns the BigInteger value of the next token and consumes it.
    boolean
    Returns the boolean value of the next token and consumes it.
    double
    Returns the double value of the next token and consumes it.
    int
    Returns the int value of the next token and consumes it.
    long
    Returns the long value of the next token and consumes it.
    Returns the next property name and consumes it.
    void
    Consumes the next null value.
    Returns the Number value of the next token and consumes it.
    Returns the string value of the next token and consumes it.
    Returns the type of the next token without consuming it.
    void
    setLenient(boolean lenient)
    Sets whether this reader is lenient in parsing.
    default void
    Skips the rest of the current array.
    default void
    Skips the rest of the current object.
    void
    Skips the next value recursively.
  • Method Details

    • beginObject

      void beginObject()
      Consumes the next token and asserts it is START_OBJECT. 消费下一个令牌并断言它是 START_OBJECT。
    • endObject

      void endObject()
      Consumes the next token and asserts it is END_OBJECT. 消费下一个令牌并断言它是 END_OBJECT。
    • beginArray

      void beginArray()
      Consumes the next token and asserts it is START_ARRAY. 消费下一个令牌并断言它是 START_ARRAY。
    • endArray

      void endArray()
      Consumes the next token and asserts it is END_ARRAY. 消费下一个令牌并断言它是 END_ARRAY。
    • hasNext

      boolean hasNext()
      Returns true if the current array or object has more elements. 如果当前数组或对象有更多元素则返回 true。
      Returns:
      true if more elements exist - 如果存在更多元素则返回 true
    • peek

      JsonToken peek()
      Returns the type of the next token without consuming it. 返回下一个令牌的类型但不消费它。
      Returns:
      the next token type - 下一个令牌类型
    • nextName

      String nextName()
      Returns the next property name and consumes it. 返回下一个属性名并消费它。
      Returns:
      the property name - 属性名
    • nextString

      String nextString()
      Returns the string value of the next token and consumes it. 返回下一个令牌的字符串值并消费它。
      Returns:
      the string value - 字符串值
    • nextBoolean

      boolean nextBoolean()
      Returns the boolean value of the next token and consumes it. 返回下一个令牌的布尔值并消费它。
      Returns:
      the boolean value - 布尔值
    • nextNull

      void nextNull()
      Consumes the next null value. 消费下一个 null 值。
    • nextInt

      int nextInt()
      Returns the int value of the next token and consumes it. 返回下一个令牌的 int 值并消费它。
      Returns:
      the int value - int 值
    • nextLong

      long nextLong()
      Returns the long value of the next token and consumes it. 返回下一个令牌的 long 值并消费它。
      Returns:
      the long value - long 值
    • nextDouble

      double nextDouble()
      Returns the double value of the next token and consumes it. 返回下一个令牌的 double 值并消费它。
      Returns:
      the double value - double 值
    • nextBigInteger

      BigInteger nextBigInteger()
      Returns the BigInteger value of the next token and consumes it. 返回下一个令牌的 BigInteger 值并消费它。
      Returns:
      the BigInteger value - BigInteger 值
    • nextBigDecimal

      BigDecimal nextBigDecimal()
      Returns the BigDecimal value of the next token and consumes it. 返回下一个令牌的 BigDecimal 值并消费它。
      Returns:
      the BigDecimal value - BigDecimal 值
    • nextNumber

      Number nextNumber()
      Returns the Number value of the next token and consumes it. 返回下一个令牌的 Number 值并消费它。
      Returns:
      the Number value - Number 值
    • skipValue

      void skipValue()
      Skips the next value recursively. 递归跳过下一个值。
    • skipObject

      default void skipObject()
      Skips the rest of the current object. 跳过当前对象的其余部分。
    • skipArray

      default void skipArray()
      Skips the rest of the current array. 跳过当前数组的其余部分。
    • getPath

      String getPath()
      Returns the current path in the JSON document. 返回 JSON 文档中的当前路径。
      Returns:
      the JSON path - JSON 路径
    • getLineNumber

      int getLineNumber()
      Returns the current line number (1-based). 返回当前行号(从1开始)。
      Returns:
      the line number - 行号
    • getColumnNumber

      int getColumnNumber()
      Returns the current column number (1-based). 返回当前列号(从1开始)。
      Returns:
      the column number - 列号
    • setLenient

      void setLenient(boolean lenient)
      Sets whether this reader is lenient in parsing. 设置此读取器是否宽松解析。
      Parameters:
      lenient - true for lenient parsing - 如果宽松解析则为 true
    • isLenient

      boolean isLenient()
      Returns whether this reader is lenient. 返回此读取器是否宽松。
      Returns:
      true if lenient - 如果宽松则返回 true
    • close

      void close()
      Closes this reader and releases resources. 关闭此读取器并释放资源。
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable