Interface JsonReader
- All Superinterfaces:
AutoCloseable, 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 TypeMethodDescriptionvoidConsumes the next token and asserts it is START_ARRAY.voidConsumes the next token and asserts it is START_OBJECT.voidclose()Closes this reader and releases resources.voidendArray()Consumes the next token and asserts it is END_ARRAY.voidConsumes the next token and asserts it is END_OBJECT.intReturns the current column number (1-based).intReturns the current line number (1-based).getPath()Returns the current path in the JSON document.booleanhasNext()Returns true if the current array or object has more elements.booleanReturns 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.booleanReturns the boolean value of the next token and consumes it.doubleReturns the double value of the next token and consumes it.intnextInt()Returns the int value of the next token and consumes it.longnextLong()Returns the long value of the next token and consumes it.nextName()Returns the next property name and consumes it.voidnextNull()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.peek()Returns the type of the next token without consuming it.voidsetLenient(boolean lenient) Sets whether this reader is lenient in parsing.default voidSkips the rest of the current array.default voidSkips the rest of the current object.voidSkips 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-