-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJsonArray.java
More file actions
83 lines (71 loc) · 2.15 KB
/
JsonArray.java
File metadata and controls
83 lines (71 loc) · 2.15 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package cn.colintree.aix.JsonUtils;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.util.YailList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* A non visible container for JSONArray
*
*/
public final class JsonArray extends JsonType {
public static JsonArray parseJsonArray(YailList list) throws JSONException {
return new JsonArray(parseJSONArray(list));
}
public static JSONArray parseJSONArray(YailList list) throws JSONException {
if (JsonObject.canParseJSONObject(list)) {
throw new IllegalArgumentException();
}
int size = list.size();
JSONArray array = new JSONArray();
Object item;
for (int i = 0; i < size; i++) {
item = list.getObject(i);
array.put(item instanceof YailList
? JsonUtils.List2Json((YailList) item)
: item);
}
return array;
}
private final JSONArray array;
JsonArray(ComponentContainer container) {
this();
}
JsonArray() {
this(new JSONArray());
}
JsonArray(JSONArray array) {
super(null);
if (array == null) {
array = new JSONArray();
}
this.array = array;
}
JsonArray(String json) throws JSONException {
this(new JSONArray(json));
}
@Override
public String toString() {
return array.toString();
}
public JSONArray getArray() {
return array;
}
@Override
public YailList toList() {
return toList(array);
}
public static YailList toList(JSONArray array) {
int length = array.length();
Object[] objs = new Object[length];
for (int i = 0; i < length; i++) {
objs[i] = array.opt(i);
if (objs[i] instanceof JSONArray) {
objs[i] = JsonArray.toList((JSONArray) objs[i]);
} else if (objs[i] instanceof JSONObject) {
objs[i] = JsonObject.toList((JSONObject) objs[i]);
}
}
return YailList.makeList(objs);
}
}