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
ConstructorsConstructorDescriptionJSONWriter(OutputStream out) Creates a new JSON writer with default capacity (4KB) and no indentation.JSONWriter(OutputStream out, IndentConfig indentConfig) Creates a new JSON writer with default capacity and optional indentation.JSONWriter(WritableByteChannel channel) Creates a new JSON writer with default capacity (4KB) and no indentation.JSONWriter(WritableByteChannel channel, int bufferCapacity) Creates a new JSON writer with specified buffer capacity and no indentation.JSONWriter(WritableByteChannel channel, int bufferCapacity, IndentConfig indentConfig) Creates a new JSON writer with specified buffer capacity and optional indentation. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Flushes and closes the writer.voidflush()Flushes any buffered data to the channel.voidwriteBoolean(boolean value) Writes a boolean value.voidWrites the end of a JSON array ']'.voidWrites the end of a JSON object '}'.voidWrites an object key (property name).voidWrites a null value.voidwriteNumber(Number value) Writes a number value.voidWrites the start of a JSON array '['.voidWrites the start of a JSON object '{'.voidwriteString(String value) Writes a string value.
-
Constructor Details
-
JSONWriter
Creates a new JSON writer with default capacity (4KB) and no indentation.- Parameters:
out- the output stream to write to
-
JSONWriter
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
Creates a new JSON writer with default capacity (4KB) and no indentation.- Parameters:
channel- the channel to write to
-
JSONWriter
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
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 Details
-
writeStartObject
Writes the start of a JSON object '{'. This must be matched by a correspondingwriteEndObject()call.- Throws:
IOException- if there is an error writing data
-
writeEndObject
Writes the end of a JSON object '}'.- Throws:
IOException- if there is an error writing data
-
writeStartArray
Writes the start of a JSON array '['. This must be matched by a correspondingwriteEndArray()call.- Throws:
IOException- if there is an error writing data
-
writeEndArray
Writes the end of a JSON array ']'.- Throws:
IOException- if there is an error writing data
-
writeKey
Writes an object key (property name).- Parameters:
key- the key name- Throws:
IOException- if there is an error writing data
-
writeString
Writes a string value.- Parameters:
value- the string value- Throws:
IOException- if there is an error writing data
-
writeNumber
Writes a number value.- Parameters:
value- the number value- Throws:
IOException- if there is an error writing data
-
writeBoolean
Writes a boolean value.- Parameters:
value- the boolean value- Throws:
IOException- if there is an error writing data
-
writeNull
Writes a null value.- Throws:
IOException- if there is an error writing data
-
flush
Flushes 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:
IOException- if there is an error sending data
-
close
Flushes 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:
IOException- if there is an error flushing data
-