Interface JsonWriter
- All Superinterfaces:
AutoCloseable, Closeable, Flushable
JSON Writer - Streaming JSON Generator Interface
JSON 写入器 - 流式 JSON 生成器接口
This interface provides a streaming (push-based) API for writing JSON. It allows efficient generation of large JSON documents without building an in-memory tree structure.
此接口提供用于写入 JSON 的流式(推送式)API。 它允许高效生成大型 JSON 文档,无需构建内存中的树结构。
Example | 示例:
try (JsonWriter writer = OpenJson.createWriter(outputStream)) {
writer.beginObject()
.name("id").value(123)
.name("name").value("John")
.name("tags").beginArray()
.value("java")
.value("json")
.endArray()
.endObject();
}
Features | 主要功能:
- Streaming push-based JSON generation - 流式推送式JSON生成
- Method chaining API for fluent writing - 方法链API实现流畅写入
- Configurable indentation, null serialization, and HTML-safe mode - 可配置缩进、null序列化和HTML安全模式
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 TypeMethodDescriptionBegins writing a JSON array.Begins writing a JSON object.voidclose()Closes this writer and releases resources.endArray()Ends writing a JSON array.Ends writing a JSON object.voidflush()Flushes any buffered data to the underlying output.booleanReturns whether HTML-safe mode is enabled.booleanReturns whether this writer is lenient.Writes a raw JSON value (without escaping).Writes a property name.Writes a null value.default JsonWriterWrites a name/value pair.default JsonWriterWrites a name/value pair.default JsonWriterWrites a name/value pair.default JsonWriterWrites a name/value pair.default JsonWriterWrites a name/value pair.default JsonWriterWrites a name/value pair.default JsonWriterpropertyNull(String name) Writes a name/null pair.setHtmlSafe(boolean htmlSafe) Sets whether HTML-safe mode is enabled.Sets the indentation string for pretty printing.setLenient(boolean lenient) Sets whether this writer is lenient.setSerializeNulls(boolean serializeNulls) Sets whether to serialize nulls.value(boolean value) Writes a boolean value.value(double value) Writes a double value.default JsonWritervalue(float value) Writes a float value.value(int value) Writes an int value.value(long value) Writes a long value.Writes a Number value.Writes a string value.default JsonWritervalue(BigDecimal value) Writes a BigDecimal value.default JsonWritervalue(BigInteger value) Writes a BigInteger value.
-
Method Details
-
beginObject
JsonWriter beginObject()Begins writing a JSON object. 开始写入 JSON 对象。- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
endObject
JsonWriter endObject()Ends writing a JSON object. 结束写入 JSON 对象。- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
beginArray
JsonWriter beginArray()Begins writing a JSON array. 开始写入 JSON 数组。- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
endArray
JsonWriter endArray()Ends writing a JSON array. 结束写入 JSON 数组。- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
name
Writes a property name. 写入属性名。- Parameters:
name- the property name - 属性名- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes a string value. 写入字符串值。- Parameters:
value- the string value - 字符串值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes a boolean value. 写入布尔值。- Parameters:
value- the boolean value - 布尔值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes an int value. 写入 int 值。- Parameters:
value- the int value - int 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes a long value. 写入 long 值。- Parameters:
value- the long value - long 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes a double value. 写入 double 值。- Parameters:
value- the double value - double 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes a float value. 写入 float 值。- Parameters:
value- the float value - float 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes a Number value. 写入 Number 值。- Parameters:
value- the Number value - Number 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes a BigInteger value. 写入 BigInteger 值。- Parameters:
value- the BigInteger value - BigInteger 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
value
Writes a BigDecimal value. 写入 BigDecimal 值。- Parameters:
value- the BigDecimal value - BigDecimal 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
nullValue
JsonWriter nullValue()Writes a null value. 写入 null 值。- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
jsonValue
Writes a raw JSON value (without escaping). 写入原始 JSON 值(不转义)。- Parameters:
json- the raw JSON string - 原始 JSON 字符串- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
property
Writes a name/value pair. 写入名称/值对。- Parameters:
name- the property name - 属性名value- the string value - 字符串值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
property
Writes a name/value pair. 写入名称/值对。- Parameters:
name- the property name - 属性名value- the boolean value - 布尔值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
property
Writes a name/value pair. 写入名称/值对。- Parameters:
name- the property name - 属性名value- the int value - int 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
property
Writes a name/value pair. 写入名称/值对。- Parameters:
name- the property name - 属性名value- the long value - long 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
property
Writes a name/value pair. 写入名称/值对。- Parameters:
name- the property name - 属性名value- the double value - double 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
property
Writes a name/value pair. 写入名称/值对。- Parameters:
name- the property name - 属性名value- the Number value - Number 值- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
propertyNull
Writes a name/null pair. 写入名称/null 对。- Parameters:
name- the property name - 属性名- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
setIndent
Sets the indentation string for pretty printing. 设置美化打印的缩进字符串。- Parameters:
indent- the indentation (e.g., " " or "\t") - 缩进(如 " " 或 "\t")- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
setSerializeNulls
Sets whether to serialize nulls. 设置是否序列化 null 值。- Parameters:
serializeNulls- true to serialize nulls - 如果序列化 null 则为 true- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
setLenient
Sets whether this writer is lenient. 设置此写入器是否宽松。- Parameters:
lenient- true for lenient writing - 如果宽松写入则为 true- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
isLenient
boolean isLenient()Returns whether this writer is lenient. 返回此写入器是否宽松。- Returns:
- true if lenient - 如果宽松则返回 true
-
setHtmlSafe
Sets whether HTML-safe mode is enabled. 设置是否启用 HTML 安全模式。- Parameters:
htmlSafe- true to escape HTML characters - 如果转义 HTML 字符则为 true- Returns:
- this writer for method chaining - 用于方法链的此写入器
-
isHtmlSafe
boolean isHtmlSafe()Returns whether HTML-safe mode is enabled. 返回是否启用 HTML 安全模式。- Returns:
- true if HTML-safe - 如果 HTML 安全则返回 true
-
flush
-
close
void close()Closes this writer and releases resources. 关闭此写入器并释放资源。- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-