AutoCloseablepublic class ZSONReader extends Object implements AutoCloseable
You can call these methods to read streams by item or by object.
Reading a stream by item refers to parsing characters in the input JSON stream one by one. Items in the JSON stream
are classified based on ZSONType and read via proper methods. An example is provided as follows:
Input JSON stream:
{
"id": "ca31221a-bd9a-4e82-acbe-f43a3ca9461c",
"active": false,
"age": 21,
"name": "Gibbs Leblanc",
"time": 1600066331510,
"tags": [
"mollit",
"labore"
]
}
Sample code for reading the stream by item:
try (ZSONReader reader = new ZSONReader(new StringReader(text))) {
reader.startObject();
while (reader.hasNext()) {
String filedName = reader.readName();
if ("id".equals(filedName)) {
String id = reader.readString();
} else if ("active".equals(filedName)) {
boolean isActive = reader.readBoolean();
} else if ("age".equals(filedName)) {
int age = reader.readInt();
} else if ("name".equals(filedName)) {
String name = reader.readString();
} else if ("time".equals(filedName)) {
long time = reader.readLong();
} else if ("tags".equals(filedName)) {
List<String> tags = new ArrayList<>();
reader.startArray();
while (reader.hasNext()) {
tags.add(reader.readString());
}
reader.endArray();
} else {
reader.skipValue();
}
}
reader.endObject();
}
Reading a stream by object refers to parsing the input JSON stream by type or object. JSON streams can be quickly
read if the type or object is specified. An example is provided as follows:
Input JSON stream:
{
"friends": [
{
"id": 0,
"name": "Alexander Bullock"
},
{
"id": 1,
"name": "Santos Noel"
}
]
}
Sample code for reading the stream by object:
try (ZSONReader reader = new ZSONReader(new StringReader(text))) {
reader.startObject();
if ("friends".equals(reader.readName())) {
List<Profile> profiles = new ArrayList<>();
reader.startArray();
while (reader.hasNext()) {
Profile profile = reader.read(Profile.class);
profiles.add(profile);
}
reader.endArray();
}
reader.endObject();
}
Alternatively:
try (ZSONReader reader = new ZSONReader(new StringReader(text))) {
reader.startObject();
if ("friends".equals(reader.readName())) {
List<Profile> profiles = new ArrayList<>();
reader.startArray();
while (reader.hasNext()) {
Profile profile = new Profile();
reader.read(profile);
profiles.add(profile);
}
reader.endArray();
}
reader.endObject();
}
public static class Profile {
private int id;
private String name;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
You can also read the following extended JSON objects:
{
"comment1": "comment1", // Single-line comment
/* Multi-line comment*/
"comment2": "comment2",
unquoted: "unquoted",
'singleQuote': 'singleQuote',
"arbitraryCommas": [1,,,1,,,1]
}
JSON streams can be read both by item and by object, except that extended JSON objects can be read only by object.ZSONType| 构造器 | 说明 |
|---|---|
ZSONReader(Reader reader) |
A constructor used to create a
ZSONReader instance for reading the input from reader. |
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
close() |
Closes this resource, relinquishing any underlying resources.
|
void |
endArray() |
Ends reading JSON arrays.
|
void |
endObject() |
Ends reading JSON objects.
|
boolean |
hasNext() |
Checks whether the input JSON stream contains more readable data.
|
ZSONType |
peek() |
Obtains the current JSON data type.
|
<T> T |
read(Class<T> clazz) |
Reads a JSON object of the
T type. |
<T> T |
read(Class<T> clazz,
boolean isExt) |
Reads a JSON object of the
T type (extended JSON objects readable or not). |
void |
read(Object obj) |
Reads a specified
obj from the JSON input. |
void |
read(Object obj,
boolean isExt) |
Reads a specified
obj from the JSON input (extended JSON objects readable or not). |
boolean |
readBoolean() |
Reads data of the
boolean type. |
double |
readDouble() |
Reads data of the
double type. |
float |
readFloat() |
Reads data of the
float type. |
int |
readInt() |
Reads data of the
int type. |
long |
readLong() |
Reads data of the
long type. |
String |
readName() |
Reads the name of the JSON object.
|
Object |
readNull() |
Reads data of the
null type. |
String |
readString() |
Reads data of the
String type. |
void |
skipValue() |
Skips the value of the current JSON data.
|
void |
startArray() |
Starts to read JSON arrays.
|
void |
startObject() |
Starts to read JSON objects.
|
public ZSONReader(Reader reader)
ZSONReader instance for reading the input from reader.reader - Indicates the input stream in JSON format.ZSONException - Throws this exception if the reader value is not in JSON format.public void startArray()
ZSONException - Throws this exception if the read data is not a JSON array.endArray(),
ZSONType.ARRAYpublic void startObject()
ZSONException - Throws this exception if the read data is not a JSON object.endObject(),
ZSONType.OBJECTpublic boolean hasNext()
This method can be used to check whether there is more data to read in an array or object. You should call this
method after startArray() or startObject() is executed.
true if the JSON stream contains more readable data; returns false otherwise.ZSONException - Throws this exception if this method is not called after startArray() or startObject().public String readName()
ZSONException - Throws this exception if the data type is not ZSONType#NAME or this method is not
called after startObject().ZSONType.NAMEpublic boolean readBoolean()
boolean type.boolean type.ZSONException - Throws this exception if the data type is not ZSONType#BOOLEAN or this method is
not called after startArray() or startObject().ZSONType.BOOLEANpublic double readDouble()
double type.double type.ZSONException - Throws this exception if the data type is not ZSONType#FLOAT or this method is not
called after startArray() or startObject().ZSONType.FLOATpublic int readInt()
int type.int type.ZSONException - Throws this exception if the data type is not ZSONType#INTEGER or this method is
not called after startArray() or startObject().ZSONType.INTEGERpublic float readFloat()
float type.float type.ZSONException - Throws this exception if the data type is not ZSONType#FLOAT or this method is not
called after startArray() or startObject().ZSONType.FLOATpublic long readLong()
long type.long type.ZSONException - Throws this exception if the data type is not ZSONType#INTEGER or this method is
not called after startArray() or startObject().ZSONType.INTEGERpublic Object readNull()
null type.null.ZSONException - Throws this exception if the data type is not ZSONType#NULL or this method is not
called after startArray() or startObject().ZSONType.NULLpublic String readString()
String type.String type.ZSONException - Throws this exception if the data type is not ZSONType#STRING or this method is not
called after startArray() or startObject().ZSONType.STRINGpublic ZSONType peek()
The returned JSON data type is used to determine which method can be used to read the data. For details about the
example, data type, and method, see ZSONType.
ZSONException - Throws this exception if an internal error occurs.public void skipValue()
Example code:
String text = "{\"skip\": \"skipped\", \"id\": \"uuid\"}";
try (ZSONReader reader = new ZSONReader(new StringReader(text))) {
reader.startObject();
if ("skip".equals(reader.readName())) {
reader.skipValue();
}
reader.readName();
reader.readString();
reader.endObject();
}
ZSONException - Throws this exception if the skipped data is not the value of a JSON object.public void endArray()
ZSONException - Throws this exception if the read data is not a JSON array.startArray(),
ZSONType.ARRAYpublic void endObject()
ZSONException - Throws this exception if the read data is not a JSON object.startObject(),
ZSONType.OBJECTpublic <T> T read(Class<T> clazz)
T type.
You can call this method to read an entire object from the JSON input. An example is provided as follows:
String text = "{\"key\": \"value\"}";
try (ZSONReader reader = new ZSONReader(new StringReader(text))) {
Map map = reader.read(Map.class);
}
T - Indicates the type of the object to read.clazz - Indicates the class of the object to read.ZSONException - Throws this exception if the type of the data is incorrect.public <T> T read(Class<T> clazz, boolean isExt)
T type (extended JSON objects readable or not).
When isExt is true, extended JSON objects can be read.
You can call this method to read an entire object from the JSON input. An example is provided as follows:
try (ZSONReader reader = new ZSONReader(new StringReader(text))) {
Map map = reader.read(Map.class, true);
}
T - Indicates the type of the object to read.clazz - Indicates the class of the object to read.isExt - Specifies whether extended JSON objects can be read.ZSONException - Throws this exception if the type of the data is incorrect.public void read(Object obj)
obj from the JSON input.
You can call this method to read an entire object from the JSON input. An example is provided as follows:
public class Kv {
public String key;
public String value;
}
String text = "{\"key\": \"akey\", \"value\": \"avalue\"}";
try (ZSONReader reader = new ZSONReader(new StringReader(text))) {
Kv kv = new Kv();
reader.read(kv);
}
obj - Indicates the object to read.ZSONException - Throws this exception if the type of the data is incorrect.public void read(Object obj, boolean isExt)
obj from the JSON input (extended JSON objects readable or not).
When isExt is true, extended JSON objects can be read.
You can call this method to read an entire object from the JSON input. An example is provided as follows:
public class Kv {
public String key;
public String value;
}
String text = "{\"key\": \"akey\", \"value\": \"avalue\"}";
try (ZSONReader reader = new ZSONReader(new StringReader(text))) {
Kv kv = new Kv();
reader.read(kv, true);
}
obj - Indicates the object to read.isExt - Specifies whether extended JSON objects can be read.ZSONException - Throws this exception if the type of the data is incorrect.public 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