A JSONWriter instance provides a value
method for appending values to the text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and object
and endObject
methods which make and bound object values. All of these methods return the JSONWriter instance, permitting a cascade style. For example,
new JSONWriter(myWriter) .object() .key("JSON") .value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be array
or object
. There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested up to 20 levels deep.
This can sometimes be easier than using a JSONObject to build a string. @version 2010-03-11
This will output any object graph deeply (or null). Object references are properly handled. For example, if you had A->B, B->C, and C->A, then A will be serialized with a B object in it, B will be serialized with a C object in it, and then C will be serialized with a reference to A (ref), not a redefinition of A.
{@code [}{ "id": 912345678901, "text": "How do I stream JSON in Java?", "geo": null, "user": { "name": "json_newb", "followers_count": 41 } }, { "id": 912345678902, "text": "@json_newb just use JsonWriter!", "geo": [50.454722, -104.606667], "user": { "name": "jesse", "followers_count": 2 } } ]}This code encodes the above structure:
{@code}public void writeJsonStream(OutputStream out, Listmessages) throws IOException JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8")); writer.setIndentSpaces(4); writeMessagesArray(writer, messages); writer.close(); } public void writeMessagesArray(JsonWriter writer, List messages) throws IOException { writer.beginArray(); for (Message message : messages) { writeMessage(writer, message); } writer.endArray(); } public void writeMessage(JsonWriter writer, Message message) throws IOException { writer.beginObject(); writer.name("id").value(message.getId()); writer.name("text").value(message.getText()); if (message.getGeo() != null) { writer.name("geo"); writeDoublesArray(writer, message.getGeo()); } else { writer.name("geo").nullValue(); } writer.name("user"); writeUser(writer, message.getUser()); writer.endObject(); } public void writeUser(JsonWriter writer, User user) throws IOException { writer.beginObject(); writer.name("name").value(user.getName()); writer.name("followers_count").value(user.getFollowersCount()); writer.endObject(); } public void writeDoublesArray(JsonWriter writer, List doubles) throws IOException { writer.beginArray(); for (Double value : doubles) { writer.value(value); } writer.endArray(); }}
Each {@code JsonWriter} may be used to write a single JSON stream.Instances of this class are not thread safe. Calls that would result in a malformed JSON string will fail with an {@link IllegalStateException}. @author Jesse Wilson @since 1.6
{@code [}{ "id": 912345678901, "text": "How do I stream JSON in Java?", "geo": null, "user": { "name": "json_newb", "followers_count": 41 } }, { "id": 912345678902, "text": "@json_newb just use JsonWriter!", "geo": [50.454722, -104.606667], "user": { "name": "jesse", "followers_count": 2 } } ]}This code encodes the above structure:
{@code}public void writeJsonStream(OutputStream out, Listmessages) throws IOException JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8")); writer.setIndentSpaces(4); writeMessagesArray(writer, messages); writer.close(); } public void writeMessagesArray(JsonWriter writer, List messages) throws IOException { writer.beginArray(); for (Message message : messages) { writeMessage(writer, message); } writer.endArray(); } public void writeMessage(JsonWriter writer, Message message) throws IOException { writer.beginObject(); writer.name("id").value(message.getId()); writer.name("text").value(message.getText()); if (message.getGeo() != null) { writer.name("geo"); writeDoublesArray(writer, message.getGeo()); } else { writer.name("geo").nullValue(); } writer.name("user"); writeUser(writer, message.getUser()); writer.endObject(); } public void writeUser(JsonWriter writer, User user) throws IOException { writer.beginObject(); writer.name("name").value(user.getName()); writer.name("followers_count").value(user.getFollowersCount()); writer.endObject(); } public void writeDoublesArray(JsonWriter writer, List doubles) throws IOException { writer.beginArray(); for (Double value : doubles) { writer.value(value); } writer.endArray(); }}
Each {@code JsonWriter} may be used to write a single JSON stream.Instances of this class are not thread safe. Calls that would result in a malformed JSON string will fail with an {@link IllegalStateException}. @author Jesse Wilson @since 1.6
For example, an empty JSON object can be written as follows:
JsonWriter jsonWriter = new JsonWriter(...); jsonWriter.writeObject(new JsonObjectBuilder().build()); jsonWriter.close();
It uses {@link javax.json.stream.JsonGenerator} internally for writing.The generator is created using one of the {@link Json}'s {@code createGenerator} methods
@author Jitendra Kotamraju
A JSONWriter instance provides a value
method for appending values to the text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and object
and endObject
methods which make and bound object values. All of these methods return the JSONWriter instance, permitting a cascade style. For example,
new JSONWriter(myWriter) .object() .key("JSON") .value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be array
or object
. There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested up to 20 levels deep.
This can sometimes be easier than using a JSONObject to build a string. @author JSON.org @version 2
A JSONWriter instance provides a value
method for appending values to the text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and object
and endObject
methods which make and bound object values. All of these methods return the JSONWriter instance, permitting a cascade style. For example,
new JSONWriter(myWriter) .object() .key("JSON") .value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be array
or object
. There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested up to 20 levels deep.
This can sometimes be easier than using a JSONObject to build a string. @author JSON.org @version 2
A JSONWriter instance provides a value
method for appending values to the text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and object
and endObject
methods which make and bound object values. All of these methods return the JSONWriter instance, permitting a cascade style. For example,
new JSONWriter(myWriter) .object() .key("JSON") .value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be array
or object
. There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested up to 20 levels deep.
This can sometimes be easier than using a JSONObject to build a string. @author JSON.org @version 2011-11-24
value
method for appending values to the text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and object
and endObject
methods which make and bound object values. All of these methods return the JSONWriter instance, permitting a cascade style. For example, new JSONWriter(myWriter) .object() .key("JSON") .value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}The first method called must be
array
or object
. There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested up to 20 levels deep. This can sometimes be easier than using a JSONObject to build a string.
@author JSON.org
@version 2
A JSONWriter instance provides a value
method for appending values to the text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and object
and endObject
methods which make and bound object values. All of these methods return the JSONWriter instance, permitting a cascade style. For example,
new JSONWriter(myWriter) .object() .key("JSON") .value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be array
or object
. There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested up to 20 levels deep.
This can sometimes be easier than using a JSONObject to build a string. @author JSON.org @version 2
A JSONWriter instance provides a value
method for appending values to the text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and object
and endObject
methods which make and bound object values. All of these methods return the JSONWriter instance, permitting a cascade style. For example,
new JSONWriter(myWriter).object().key("JSON").value("Hello, World!") .endObject();which writes
{"JSON":"Hello, World!"}
The first method called must be array
or object
. There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested up to 20 levels deep.
This can sometimes be easier than using a JSONObject to build a string. @author JSON.org @version 2011-11-24
value
method for appending values to the text, and a key
method for adding keys before values in objects. There are array
and endArray
methods that make and bound array values, and object
and endObject
methods which make and bound object values. All of these methods return the JSONWriter instance, permitting a cascade style. For example, new JSONWriter(myWriter).object().key("JSON").value("Hello, World!").endObject();which writes
{"JSON":"Hello, World!"}The first method called must be
array
or object
. There are no methods for adding commas or colons. JSONWriter adds them for you. Objects and arrays can be nested up to 20 levels deep. This can sometimes be easier than using a JSONObject to build a string.
@author JSON.org
@version 2
StringWriter json = new StringWriter(); JsonWriter writer = JsonWriter.of(json); writer .beginObject() .prop("aBoolean", true) .prop("aInt", 123) .prop("aString", "foo") .beginObject().name("aList") .beginArray() .beginObject().prop("key", "ABC").endObject() .beginObject().prop("key", "DEF").endObject() .endArray() .endObject() .close();@since 4.2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|