- java.lang.Object
-
- org.bluezoo.json.JSONWriter
-
public class JSONWriter extends java.lang.ObjectStreaming JSON writer with NIO-first design.This class provides an efficient, streaming approach to JSON serialization that writes to a
WritableByteChannel. The writer uses an internal buffer and automatically sends chunks to the channel when the buffer fills beyond a threshold.This class does not perform well-formedness checking on its input: the user of the class is required to supply events in the correct order and close objects and arrays they open. However, it will escape characters supplied in string data.
Usage Example
// Write to a file FileChannel channel = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE); JSONWriter writer = new JSONWriter(channel); writer.writeStartObject(); writer.writeKey("name"); writer.writeString("Alice"); writer.writeKey("age"); writer.writeNumber(30); writer.writeEndObject(); writer.close(); // Or write to an OutputStream OutputStream out = ...; JSONWriter writer = new JSONWriter(Channels.newChannel(out));Thread Safety
This class is NOT thread-safe. It is intended for use on a single thread.
- Author:
- Chris Burdess
-
-
Constructor Summary
Constructors Constructor Description JSONWriter(java.io.OutputStream out)Creates a new JSON writer with default capacity (4KB) and no indentation.JSONWriter(java.io.OutputStream out, IndentConfig indentConfig)Creates a new JSON writer with default capacity and optional indentation.JSONWriter(java.nio.channels.WritableByteChannel channel)Creates a new JSON writer with default capacity (4KB) and no indentation.JSONWriter(java.nio.channels.WritableByteChannel channel, int bufferCapacity)Creates a new JSON writer with specified buffer capacity and no indentation.JSONWriter(java.nio.channels.WritableByteChannel channel, int bufferCapacity, IndentConfig indentConfig)Creates a new JSON writer with specified buffer capacity and optional indentation.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Flushes and closes the writer.voidflush()Flushes any buffered data to the channel.voidwriteBoolean(boolean value)Writes a boolean value.voidwriteEndArray()Writes the end of a JSON array ']'.voidwriteEndObject()Writes the end of a JSON object '}'.voidwriteKey(java.lang.String key)Writes an object key (property name).voidwriteNull()Writes a null value.voidwriteNumber(java.lang.Number value)Writes a number value.voidwriteStartArray()Writes the start of a JSON array '['.voidwriteStartObject()Writes the start of a JSON object '{'.voidwriteString(java.lang.String value)Writes a string value.
-
-
-
Constructor Detail
-
JSONWriter
public JSONWriter(java.io.OutputStream out)
Creates a new JSON writer with default capacity (4KB) and no indentation.- Parameters:
out- the output stream to write to
-
JSONWriter
public JSONWriter(java.io.OutputStream out, IndentConfig indentConfig)Creates a new JSON writer with default capacity and optional indentation.- Parameters:
out- the output stream to write toindentConfig- the indentation configuration, or null for no indentation
-
JSONWriter
public JSONWriter(java.nio.channels.WritableByteChannel channel)
Creates a new JSON writer with default capacity (4KB) and no indentation.- Parameters:
channel- the channel to write to
-
JSONWriter
public JSONWriter(java.nio.channels.WritableByteChannel channel, int bufferCapacity)Creates a new JSON writer with specified buffer capacity and no indentation.- Parameters:
channel- the channel to write tobufferCapacity- initial buffer capacity in bytes
-
JSONWriter
public JSONWriter(java.nio.channels.WritableByteChannel channel, int bufferCapacity, IndentConfig indentConfig)Creates a new JSON writer with specified buffer capacity and optional indentation.- Parameters:
channel- the channel to write tobufferCapacity- initial buffer capacity in bytesindentConfig- the indentation configuration, or null for no indentation
-
-
Method Detail
-
writeStartObject
public void writeStartObject() throws java.io.IOExceptionWrites the start of a JSON object '{'. This must be matched by a correspondingwriteEndObject()call.- Throws:
java.io.IOException- if there is an error writing data
-
writeEndObject
public void writeEndObject() throws java.io.IOExceptionWrites the end of a JSON object '}'.- Throws:
java.io.IOException- if there is an error writing data
-
writeStartArray
public void writeStartArray() throws java.io.IOExceptionWrites the start of a JSON array '['. This must be matched by a correspondingwriteEndArray()call.- Throws:
java.io.IOException- if there is an error writing data
-
writeEndArray
public void writeEndArray() throws java.io.IOExceptionWrites the end of a JSON array ']'.- Throws:
java.io.IOException- if there is an error writing data
-
writeKey
public void writeKey(java.lang.String key) throws java.io.IOExceptionWrites an object key (property name).- Parameters:
key- the key name- Throws:
java.io.IOException- if there is an error writing data
-
writeString
public void writeString(java.lang.String value) throws java.io.IOExceptionWrites a string value.- Parameters:
value- the string value- Throws:
java.io.IOException- if there is an error writing data
-
writeNumber
public void writeNumber(java.lang.Number value) throws java.io.IOExceptionWrites a number value.- Parameters:
value- the number value- Throws:
java.io.IOException- if there is an error writing data
-
writeBoolean
public void writeBoolean(boolean value) throws java.io.IOExceptionWrites a boolean value.- Parameters:
value- the boolean value- Throws:
java.io.IOException- if there is an error writing data
-
writeNull
public void writeNull() throws java.io.IOExceptionWrites a null value.- Throws:
java.io.IOException- if there is an error writing data
-
flush
public void flush() throws java.io.IOExceptionFlushes any buffered data to the channel.This sends any remaining data in the buffer to the channel, even if the buffer is not full.
- Throws:
java.io.IOException- if there is an error sending data
-
close
public void close() throws java.io.IOExceptionFlushes and closes the writer.After calling this method, the writer should not be used again. Note: This does NOT close the underlying channel - the caller is responsible for closing the channel.
- Throws:
java.io.IOException- if there is an error flushing data
-
-