public class JSONWriter
extends java.lang.Object
JSONFactory FACTORY = JSONFactory.instance(); JSONWriter w = FACTORY.makeWriter(writer); w.startObject(); w.objectValue("abc", "123"); w.startObject("def"); w.startArray(); w.endArray(); w.endObject(); w.endObject(); w.close();will produce
{ "abc":"123", def:[] }}These methods can be chained together, so the above could also be written as
w.startObject() .objectValue("abc", "123") .startObject("def") .startArray() .endArray() .endObject() .endObject();
Modifier and Type | Class and Description |
---|---|
private static class |
JSONWriter.Container |
private static class |
JSONWriter.ContainerType |
Modifier and Type | Field and Description |
---|---|
private java.util.Stack<JSONWriter.Container> |
where |
private java.io.Writer |
writer |
Constructor and Description |
---|
JSONWriter(java.io.Writer writer) |
Modifier and Type | Method and Description |
---|---|
JSONWriter |
arrayValue(java.lang.Object value)
Writes a value into a JSON array.
|
void |
close()
Closes the JSONWriter.
|
JSONWriter |
endArray()
Writes the closing square brace for a JSON array.
|
JSONWriter |
endObject()
Writes the closing curly brace for a JSON object.
|
JSONWriter |
flush()
Flushes JSONWriter to writer.
|
JSONWriter |
objectValue(java.lang.String key,
java.lang.Object value)
Writes a name/value pair for a JSON object.
|
JSONWriter |
startArray()
Write the opening square brace for a JSON array.
|
JSONWriter |
startArray(java.lang.String key)
Write a JSON array with the specified name.
|
JSONWriter |
startObject()
Writes the opening curly brace for a JSON object.
|
JSONWriter |
startObject(java.lang.String key)
Writes a JSON object with the specified name.
|
JSONWriter |
writeArray(java.util.List<java.lang.Object> values)
Convenience method to write the elements of a List as a JSON array.
|
private void |
writeEscapedString(java.lang.String value) |
JSONWriter |
writeObject(java.util.Map<java.lang.String,java.lang.Object> values)
Convenience method to write the entries in a Map as a JSON Object.
|
private void |
writeSeparatorIfNeeded() |
private void |
writeValue(java.lang.Object value) |
private final java.io.Writer writer
private java.util.Stack<JSONWriter.Container> where
public JSONWriter startObject() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public JSONWriter startObject(java.lang.String key) throws java.io.IOException
key
- - the name of the JSON objectjava.io.IOException
- if an I/O error occurs.public JSONWriter endObject() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public JSONWriter startArray() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public JSONWriter startArray(java.lang.String key) throws java.io.IOException
key
- - the name of the array.java.io.IOException
- if an I/O error occurs.public JSONWriter endArray() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public JSONWriter objectValue(java.lang.String key, java.lang.Object value) throws java.io.IOException
key
- - the name of the JSON object.value
- - the value of the JSON object.
The value may be a Map, in which case entries in the Map
are written as elements of the current JSON Object.java.io.IOException
- if an I/O error occurs.public JSONWriter arrayValue(java.lang.Object value) throws java.io.IOException
value
- - the value of an array element.
The value may be a List, in which case elements of the List
are written as elements of the current JSON array.java.io.IOException
- if an I/O error occurs.public JSONWriter writeObject(java.util.Map<java.lang.String,java.lang.Object> values) throws java.io.IOException
values
- the Map entries to be writtenjava.io.IOException
- if an I/O error occurs.public JSONWriter writeArray(java.util.List<java.lang.Object> values) throws java.io.IOException
values
- the List of values to be writtenjava.io.IOException
- if an I/O error occurs.public JSONWriter flush() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.public void close() throws java.io.IOException
java.io.IOException
- if an I/O error occurs.private void writeValue(java.lang.Object value) throws java.io.IOException
java.io.IOException
private void writeSeparatorIfNeeded() throws java.io.IOException
java.io.IOException
private void writeEscapedString(java.lang.String value) throws java.io.IOException
java.io.IOException