Flushable, AutoCloseablepublic class ZSONWriter extends Object implements AutoCloseable, Flushable
You can call methods in this class to write data by item or by object.
Writing data by item refers to writing data items into the output JSON stream one by one. An example is provided as follows:
StringWriter stringWriter = new StringWriter();
try (ZSONWriter writer = new ZSONWriter(stringWriter)) {
writer.startObject();
writer.writeName("id");
writer.writeString("ca31221a-bd9a-4e82-acbe-f43a3ca9461c");
writer.writeName("active");
writer.writeBoolean(false);
writer.writeName("age");
writer.writeNumber(21);
writer.writeName("name");
writer.writeString("Gibbs Leblanc");
writer.writeName("time");
writer.writeNumber(1600066331510L);
writer.writeName("tags");
writer.startArray();
writer.writeString("mollit");
writer.writeString("labore");
writer.endArray();
writer.endObject();
writer.flush();
}
The output is as follows:
{"id":"ca31221a-bd9a-4e82-acbe-f43a3ca9461c","active":false,"age":21,"name":"Gibbs Leblanc","time":1600066331510,"tags":["mollit","labore"]}"
Writing data by object refers to writing an entire object into the output JSON stream. An example is provided as
follows:
StringWriter stringWriter = new StringWriter();
try (ZSONWriter writer = new ZSONWriter(stringWriter)) {
writer.startObject();
writer.writeName("friends");
writer.startArray();
Profile bullockProfile = new Profile(0, "Alexander Bullock");
writer.writeObject(bullockProfile);
Profile noelProfile = new Profile(1, "Santos Noel");
writer.writeObject(noelProfile);
writer.endArray();
writer.endObject();
writer.flush();
}
public static class Profile {
private final int id;
private final String name;
public Profile(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
The output is as follows:
{"friends":[{"id":0,"name":"Alexander Bullock"},{"id":1,"name":"Santos Noel"}]}
| 构造器 | 说明 |
|---|---|
ZSONWriter(Writer writer) |
A constructor used to create a
ZSONWriter instance for producing an output JSON object. |
ZSONWriter(Writer writer,
boolean isPretty) |
A constructor used to create a
ZSONWriter instance for producing an output JSON object (pretty printing
enabled or disabled). |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
close() |
Closes this resource, relinquishing any underlying resources.
|
void |
endArray() |
Ends writing arrays.
|
void |
endObject() |
Ends writing objects.
|
void |
flush() |
Flushes this stream by writing any buffered output to the underlying
stream.
|
void |
startArray() |
Starts to write arrays.
|
void |
startObject() |
Starts to write objects.
|
void |
writeBoolean(boolean value) |
Writes data of the
boolean type. |
void |
writeName(String name) |
Writes the name of the JSON object.
|
void |
writeNull() |
Writes data of the
null type. |
void |
writeNumber(Number value) |
Writes data of the
Number type. |
void |
writeObject(Object object) |
Writes data of the
Object type. |
void |
writeString(String value) |
Writes data of the
String type. |
public ZSONWriter(Writer writer)
ZSONWriter instance for producing an output JSON object.writer - Indicates the output string stream of the JSON object.public ZSONWriter(Writer writer, boolean isPretty)
ZSONWriter instance for producing an output JSON object (pretty printing
enabled or disabled).writer - Indicates the output string stream of the JSON object.isPretty - Specifies whether pretty printing is enabled for the output string stream.public void startArray()
ZSONException - Throws this exception if a JSON format error is caused by the written arrays.endArray()public void startObject()
ZSONException - Throws this exception if a JSON format error is caused by the written objects.endArray()public void writeName(String name)
name - Indicates the JSON object name to write.ZSONException - Throws this exception if a JSON format error is caused by the written name.public void writeBoolean(boolean value)
boolean type.value - Indicates the data to write.ZSONException - Throws this exception if a JSON format error is caused by the written boolean
data.public void writeNull()
null type.ZSONException - Throws this exception if a JSON format error is caused by the written null data.public void writeNumber(Number value)
Number type.value - Indicates the data to write.ZSONException - Throws this exception if a JSON format error is caused by the written Number data.public void writeString(String value)
String type.value - Indicates the data to write.ZSONException - Throws this exception if a JSON format error is caused by the written String data.public void writeObject(Object object)
Object type.
An entire object can be written.
object - Indicates the object to write.ZSONException - Throws this exception if a JSON format error is caused by the written Object data.public void endArray()
ZSONException - Throws this exception if a JSON format error is caused by the written arrays.startArray()public void endObject()
ZSONException - Throws this exception if a JSON format error is caused by the written objects.startObject()public void flush()
Flushablepublic void close()
AutoCloseabletry-with-resources statement.
While this interface method is declared to throw
Exception, implementers are strongly encouraged to
declare concrete implementations of the close method to
throw more specific exceptions, or to throw no exception at all
if the close operation cannot fail.
Cases where the close operation may fail require careful
attention by implementers. It is strongly advised to relinquish
the underlying resources and to internally mark the
resource as closed, prior to throwing the exception. The
close method is unlikely to be invoked more than once and so
this ensures that the resources are released in a timely manner.
Furthermore it reduces problems that could arise when the resource
wraps, or is wrapped, by another resource.
Implementers of this interface are also strongly advised
to not have the close method throw InterruptedException.
This exception interacts with a thread's interrupted status,
and runtime misbehavior is likely to occur if an
InterruptedException is Throwable#addSuppressed.
More generally, if it would cause problems for an
exception to be suppressed, the AutoCloseable.close
method should not throw it.
Note that unlike the close
method of Closeable, this close method
is not required to be idempotent. In other words,
calling this close method more than once may have some
visible side effect, unlike Closeable.close which is
required to have no effect if called more than once.
However, implementers of this interface are strongly encouraged
to make their close methods idempotent.
close 在接口中 AutoCloseable