forked from DNAProject/DNA-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonReader.java
More file actions
36 lines (29 loc) · 933 Bytes
/
Copy pathJsonReader.java
File metadata and controls
36 lines (29 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package DNA.IO;
import java.io.IOException;
import java.lang.reflect.Array;
import DNA.IO.Json.JArray;
import DNA.IO.Json.JObject;
public class JsonReader {
private JObject json;
public JsonReader(JObject json) {
this.json = json;
}
public <T extends JsonSerializable> T readSerializable(Class<T> t) throws InstantiationException, IllegalAccessException, IOException {
T obj = t.newInstance();
obj.fromJson(this);
return obj;
}
@SuppressWarnings("unchecked")
public <T extends JsonSerializable> T[] readSerializableArray(Class<T> t, int count, String field) throws InstantiationException, IllegalAccessException, IOException {
JArray jsonArr = (JArray)json.get(field);
T[] array = (T[])Array.newInstance(t, count);
for (int i = 0; i < count; i++) {
array[i] = t.newInstance();
array[i].fromJson(new JsonReader(jsonArr.get(i)));
}
return array;
}
public JObject json() {
return json;
}
}