-
-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathJSONArray.java
152 lines (136 loc) · 4.38 KB
/
JSONArray.java
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
152
package net.minidev.json;
/*
* Copyright 2011-2024 JSON-SMART authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import net.minidev.json.reader.JsonWriter;
/**
* A JSON array. JSONObject supports java.util.List interface.
*
* @author FangYidong <[email protected]>
* @author Uriel Chemouni <[email protected]>
*/
public class JSONArray extends ArrayList<Object> implements JSONAwareEx, JSONStreamAwareEx {
private static final long serialVersionUID = 9106884089231309568L;
public JSONArray() {}
public JSONArray(int initialCapacity) {
super(initialCapacity);
}
public static String toJSONString(List<? extends Object> list) {
return toJSONString(list, JSONValue.COMPRESSION);
}
/**
* Convert a list to JSON text. The result is a JSON array. If this list is also a JSONAware,
* JSONAware specific behaviours will be omitted at this top level.
*
* @see net.minidev.json.JSONValue#toJSONString(Object)
* @param list
* @param compression Indicate compression level
* @return JSON text, or "null" if list is null.
*/
public static String toJSONString(List<? extends Object> list, JSONStyle compression) {
StringBuilder sb = new StringBuilder();
try {
writeJSONString(list, sb, compression);
} catch (IOException e) {
// Can not append on a string builder
}
return sb.toString();
}
/**
* Encode a list into JSON text and write it to out. If this list is also a JSONStreamAware or a
* JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level.
*
* @see JSONValue#writeJSONString(Object, Appendable)
* @param list
* @param out
*/
public static void writeJSONString(
Iterable<? extends Object> list, Appendable out, JSONStyle compression) throws IOException {
if (list == null) {
out.append("null");
return;
}
JsonWriter.JSONIterableWriter.writeJSONString(list, out, compression);
}
/**
* Encode a list into JSON text and write it to out. If this list is also a JSONStreamAware or a
* JSONAware, JSONStreamAware and JSONAware specific behaviours will be ignored at this top level.
*
* @param list
* @param out
* @throws IOException
*/
public static void writeJSONString(List<? extends Object> list, Appendable out)
throws IOException {
writeJSONString(list, out, JSONValue.COMPRESSION);
}
/**
* Appends the specified element and returns this. same effect that add(E e) method.
*
* @param element element to be appended to this array.
* @return this
*/
public JSONArray appendElement(Object element) {
add(element);
return this;
}
/**
* Merges the specified object into this array. can trigger an add(E e) or addAll(E e) method.
*
* @param o2
*/
public void merge(Object o2) {
JSONObject.merge(this, o2);
}
/** Explicitly Serialize Object as JSon String */
public String toJSONString() {
return toJSONString(this, JSONValue.COMPRESSION);
}
public String toJSONString(JSONStyle compression) {
return toJSONString(this, compression);
}
/** Override native toString() */
public String toString() {
return toJSONString();
}
/**
* JSONAwareEx interface
*
* @param compression compression param
*/
public String toString(JSONStyle compression) {
return toJSONString(compression);
}
/**
* JSONStreamAwareEx interface
*
* @param out output stream
*/
public void writeJSONString(Appendable out) throws IOException {
writeJSONString(this, out, JSONValue.COMPRESSION);
}
/**
* JSONStreamAwareEx interface
*
* @param out output stream
* @param compression compression param
*/
public void writeJSONString(Appendable out, JSONStyle compression) throws IOException {
writeJSONString(this, out, compression);
}
}