Interface ReactiveJsonWriter
- All Superinterfaces:
AutoCloseable, Closeable
Reactive JSON Writer - Reactive streaming JSON writer interface
响应式JSON写入器 - 响应式流式JSON写入器接口
This interface provides a reactive API for writing JSON using Java's Flow API. It enables non-blocking serialization of object streams to JSON output.
此接口使用Java的Flow API提供响应式JSON写入API。 它支持将对象流非阻塞地序列化为JSON输出。
Features | 主要功能:
- Reactive Streams compatible via java.util.concurrent.Flow
- Writes object streams as JSON arrays or objects
- Non-blocking I/O support
- Configurable formatting options
Usage Example | 使用示例:
// Writing a stream of users as a JSON array
Flow.Publisher<User> userStream = getUserStream();
try (ReactiveJsonWriter writer = ReactiveJsonWriter.create(
new FileOutputStream("users.json"))) {
CompletableFuture<Void> future = writer.writeAsArray(userStream);
future.join(); // Wait for completion
}
// Non-blocking write
writer.writeAsArray(userStream)
.thenRun(() -> System.out.println("Write complete"))
.exceptionally(ex -> {
ex.printStackTrace();
return null;
});
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 TypeMethodDescriptionstatic ReactiveJsonWritercreate(OutputStream output) Creates a reactive JSON writer to an output stream.static ReactiveJsonWritercreate(OutputStream output, int bufferSize) Creates a reactive JSON writer with custom buffer size.static ReactiveJsonWritercreatePretty(OutputStream output) Creates a reactive JSON writer with pretty printing.voidflush()Flushes any buffered content.longGets the number of bytes written so far.longGets the number of elements written so far.booleanisOpen()Checks if the writer is open.<T> CompletableFuture<Void> write(Flow.Publisher<T> source) Writes objects from a publisher as JSON.<T> CompletableFuture<Void> writeAsArray(Flow.Publisher<T> source) Writes objects from a publisher as a JSON array.<T> CompletableFuture<Void> writeObject(T object) Writes a single object as JSON.
-
Method Details
-
create
Creates a reactive JSON writer to an output stream. 创建写入输出流的响应式JSON写入器。- Parameters:
output- the output stream | 输出流- Returns:
- the writer | 写入器
-
create
Creates a reactive JSON writer with custom buffer size. 使用自定义缓冲区大小创建响应式JSON写入器。- Parameters:
output- the output stream | 输出流bufferSize- the buffer size | 缓冲区大小- Returns:
- the writer | 写入器
-
createPretty
Creates a reactive JSON writer with pretty printing. 创建带美化输出的响应式JSON写入器。- Parameters:
output- the output stream | 输出流- Returns:
- the writer | 写入器
-
write
Writes objects from a publisher as JSON. 将发布者的对象写入为JSON。Each object is written as a separate JSON value (NDJSON format).
每个对象作为单独的JSON值写入(NDJSON格式)。
- Type Parameters:
T- the element type | 元素类型- Parameters:
source- the source publisher | 源发布者- Returns:
- a future that completes when writing is done | 写入完成时完成的future
-
writeAsArray
Writes objects from a publisher as a JSON array. 将发布者的对象写入为JSON数组。Objects are enclosed in array brackets and separated by commas.
对象被括在数组括号中,并用逗号分隔。
- Type Parameters:
T- the element type | 元素类型- Parameters:
source- the source publisher | 源发布者- Returns:
- a future that completes when writing is done | 写入完成时完成的future
-
writeObject
Writes a single object as JSON. 将单个对象写入为JSON。- Type Parameters:
T- the object type | 对象类型- Parameters:
object- the object to write | 要写入的对象- Returns:
- a future that completes when writing is done | 写入完成时完成的future
-
flush
void flush()Flushes any buffered content. 刷新任何缓冲的内容。 -
isOpen
boolean isOpen()Checks if the writer is open. 检查写入器是否打开。- Returns:
- true if open | 如果打开返回true
-
getElementsWritten
long getElementsWritten()Gets the number of elements written so far. 获取到目前为止写入的元素数。- Returns:
- the count | 计数
-
getBytesWritten
long getBytesWritten()Gets the number of bytes written so far. 获取到目前为止写入的字节数。- Returns:
- the byte count | 字节计数
-