public class JSONWriter extends 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 | Method and Description |
---|---|
JSONWriter |
arrayValue(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(String key,
Object value)
Writes a name/value pair for a JSON object.
|
JSONWriter |
startArray()
Write the opening square brace for a JSON array.
|
JSONWriter |
startArray(String key)
Write a JSON array with the specified name.
|
JSONWriter |
startObject()
Writes the opening curly brace for a JSON object.
|
JSONWriter |
startObject(String key)
Writes a JSON object with the specified name.
|
JSONWriter |
writeArray(List<Object> values)
Convenience method to write the elements of a List as a JSON array.
|
JSONWriter |
writeObject(Map<String,Object> values)
Convenience method to write the entries in a Map as a JSON Object.
|
public JSONWriter startObject() throws IOException
IOException
- if an I/O error occurs.public JSONWriter startObject(String key) throws IOException
key
- - the name of the JSON objectIOException
- if an I/O error occurs.public JSONWriter endObject() throws IOException
IOException
- if an I/O error occurs.public JSONWriter startArray() throws IOException
IOException
- if an I/O error occurs.public JSONWriter startArray(String key) throws IOException
key
- - the name of the array.IOException
- if an I/O error occurs.public JSONWriter endArray() throws IOException
IOException
- if an I/O error occurs.public JSONWriter objectValue(String key, Object value) throws 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.IOException
- if an I/O error occurs.public JSONWriter arrayValue(Object value) throws 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.IOException
- if an I/O error occurs.public JSONWriter writeObject(Map<String,Object> values) throws IOException
values
- the Map entries to be writtenIOException
- if an I/O error occurs.public JSONWriter writeArray(List<Object> values) throws IOException
values
- the List of values to be writtenIOException
- if an I/O error occurs.public JSONWriter flush() throws IOException
IOException
- if an I/O error occurs.public void close() throws IOException
IOException
- if an I/O error occurs.Copyright © 2020. All rights reserved.