Repository navigation
Expand file tree
/
Copy pathJSONArray.java
More file actions
151 lines (132 loc) · 3.74 KB
/
Copy pathJSONArray.java
File metadata and controls
151 lines (132 loc) · 3.74 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package org.tron.json;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Drop-in replacement for {@code com.alibaba.fastjson.JSONArray}.
*
* @deprecated Compatibility shim from the fastjson removal. New code should use
* Jackson directly ({@link com.fasterxml.jackson.databind.node.ArrayNode})
* instead of this helper.
*/
@Deprecated
public class JSONArray implements Iterable<Object> {
private final ArrayNode node;
public JSONArray(ArrayNode node) {
this.node = node;
}
public JSONArray() {
this.node = JSON.MAPPER.createArrayNode();
}
public static JSONArray parseArray(String text) {
if (JSON.isNullLiteral(text)) {
return null;
}
try {
JsonNode node = JSON.MAPPER.readTree(text);
if (node == null || node.isNull()) {
return null;
}
if (!node.isArray()) {
throw new JSONException("Expected JSON array but got: " + node.getNodeType());
}
return new JSONArray((ArrayNode) node);
} catch (JSONException e) {
throw e;
} catch (Exception e) {
throw new JSONException(e.getMessage(), e);
}
}
public int size() {
return node.size();
}
private void rangeCheck(int index) {
if (index < 0 || index >= node.size()) {
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + node.size());
}
}
@VisibleForTesting
public Object get(int index) {
rangeCheck(index);
return JSONObject.convertNode(node.get(index));
}
public JSONObject getJSONObject(int index) {
rangeCheck(index);
JsonNode child = node.get(index);
if (child.isNull()) {
return null;
}
if (child.isObject()) {
return new JSONObject((ObjectNode) child);
}
// Fastjson auto-parses stringified JSON objects
if (child.isTextual()) {
return JSON.parseObject(child.asText());
}
throw new JSONException("Element at index " + index + " is not an object");
}
@VisibleForTesting
public String getString(int index) {
rangeCheck(index);
JsonNode child = node.get(index);
if (child.isNull()) {
return null;
}
if (child.isContainerNode()) {
try {
return JSON.MAPPER.writeValueAsString(child);
} catch (Exception e) {
throw new JSONException("Serialization failed: " + e.getMessage(), e);
}
}
return child.asText(null);
}
// -------------------------------------------------------------------------
// Mutation helpers
// -------------------------------------------------------------------------
public JSONArray add(JSONObject value) {
node.add(value == null ? node.nullNode() : value.unwrap());
return this;
}
@JsonValue
public ArrayNode unwrap() {
return node;
}
@Override
public Iterator<Object> iterator() {
List<Object> list = new ArrayList<>();
node.forEach(child -> list.add(JSONObject.convertNode(child)));
return list.iterator();
}
@Override
public String toString() {
try {
return JSON.MAPPER.writeValueAsString(node);
} catch (Exception e) {
throw new JSONException("Serialization failed: " + e.getMessage(), e);
}
}
public String toJSONString() {
return toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof JSONArray)) {
return false;
}
return node.equals(((JSONArray) o).node);
}
@Override
public int hashCode() {
return node.hashCode();
}
}