-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathJSONList.java
More file actions
312 lines (287 loc) · 7.86 KB
/
JSONList.java
File metadata and controls
312 lines (287 loc) · 7.86 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*Copyright (C) 2020 Tencent. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson;
import java.util.*;
/**
* Custom JSONList implementation based on ArrayList to replace com.alibaba.fastjson.JSONList
* Maintains same API as fastjson but uses standard Java List implementation
* @author Lemon
*/
public interface JSONList<M extends Map<String, Object>, L extends List<Object>> extends List<Object> {
public static final String TAG = "JSONList";
///**
// * Create an empty JSONList
// */
//default JSONList() {
// super();
//}
//
//private int initialCapacity = 10;
///**
// * Create a JSONList with initial capacity
// * @param initialCapacity the initial capacity
// */
//default JSONList(int initialCapacity) {
// super(initialCapacity);
//}
//
///**
// * Create a JSONList from a Collection
// * @param collection the collection to copy from
// */
//default JSONList(Collection<?> collection) {
// super(collection);
//}
//
///**
// * Create a JSONList from a JSON string
// * @param json JSON string
// */
//default JSONList(String json) {
// this();
// List<Object> list = JSON.parseArray(json);
// if (list != null) {
// addAll(list);
// }
//}
//
/**
* Get a JSONMap at the specified index
* @param index the index
* @return the JSONMap or null if not a JSONMap
*/
default M getJSONObject(int index) {
if (index < 0 || index >= size()) {
return null;
}
Object obj = get(index);
if (obj instanceof Map<?, ?>) {
return JSON.createJSONObject((Map<? extends String, ?>) obj);
}
return null;
}
/**
* Get a JSONList at the specified index
* @param index the index
* @return the JSONList or null if not a JSONList
*/
default L getJSONArray(int index) {
if (index < 0 || index >= size()) {
return null;
}
Object obj = get(index);
if (obj instanceof List<?>) {
return JSON.createJSONArray((List<?>) obj);
}
return null;
}
/**
* Get a boolean value at the specified index
* @param index the index
* @return the boolean value or false if not found
*/
default boolean getBooleanValue(int index) {
if (index < 0 || index >= size()) {
return false;
}
Object obj = get(index);
if (obj instanceof Boolean) {
return (Boolean) obj;
} else if (obj instanceof Number) {
return ((Number) obj).intValue() != 0;
} else if (obj instanceof String) {
return Boolean.parseBoolean((String) obj);
}
return false;
}
/**
* Get an integer value at the specified index
* @param index the index
* @return the integer value or 0 if not found
*/
default int getIntValue(int index) {
if (index < 0 || index >= size()) {
return 0;
}
Object obj = get(index);
if (obj instanceof Number) {
return ((Number) obj).intValue();
} else if (obj instanceof String) {
try {
return Integer.parseInt((String) obj);
} catch (NumberFormatException e) {
// Ignore
}
}
return 0;
}
/**
* Get a long value at the specified index
* @param index the index
* @return the long value or 0 if not found
*/
default long getLongValue(int index) {
if (index < 0 || index >= size()) {
return 0L;
}
Object obj = get(index);
if (obj instanceof Number) {
return ((Number) obj).longValue();
} else if (obj instanceof String) {
try {
return Long.parseLong((String) obj);
} catch (NumberFormatException e) {
// Ignore
}
}
return 0L;
}
/**
* Get a double value at the specified index
* @param index the index
* @return the double value or 0 if not found
*/
default double getDoubleValue(int index) {
if (index < 0 || index >= size()) {
return 0.0;
}
Object obj = get(index);
if (obj instanceof Number) {
return ((Number) obj).doubleValue();
} else if (obj instanceof String) {
try {
return Double.parseDouble((String) obj);
} catch (NumberFormatException e) {
// Ignore
}
}
return 0.0;
}
/**
* Get a string value at the specified index
* @param index the index
* @return the string value or null if not found
*/
default String getString(int index) {
if (index < 0 || index >= size()) {
return null;
}
Object obj = get(index);
return obj != null ? obj.toString() : null;
}
default String toJSONString() {
return JSON.toJSONString(this);
}
//@Override
//default boolean containsAll(Collection<?> c) {
// if (c == null || c.isEmpty()) {
// return true;
// }
// return super.containsAll(c);
//}
//
//@Override
//default boolean addAll(Collection<?> c) {
// if (c == null || c.isEmpty()) {
// return true;
// }
// return super.addAll(c);
//}
//
//@Override
//default boolean addAll(int index, Collection<?> c) {
// if (c == null || c.isEmpty()) {
// return true;
// }
//
// int sz = size();
// if (index < 0 || index >= sz) {
// index += sz;
// }
//
// return super.addAll(index, c);
//}
//
//@Override
//default boolean removeAll(Collection<?> c) {
// if (c == null || c.isEmpty()) {
// return true;
// }
// return super.removeAll(c);
//}
//
//@Override
//default boolean retainAll(Collection<?> c) {
// if (c == null || c.isEmpty()) {
// return true;
// }
// return super.retainAll(c);
//}
//
//
//@Override
//default Object get(int index) {
// int sz = size();
// if (index < 0 || index >= sz) {
// index += sz;
// }
//
// return super.get(index);
//}
//
//@Override
//default Object set(int index, Object element) {
// int sz = size();
// if (index < 0 || index >= sz) {
// index += sz;
// }
//
// return super.set(index, element);
//}
//
//@Override
//default void add(int index, Object element) {
// int sz = size();
// if (index < 0 || index >= sz) {
// index += sz;
// }
//
// super.add(index, element);
//}
//
//@Override
//default Object remove(int index) {
// int sz = size();
// if (index < 0 && index >= -sz) {
// index += sz;
// }
// if (index < 0 || index >= sz) {
// return null;
// }
//
// return super.remove(index);
//}
//
//@Override
//default ListIterator<Object> listIterator(int index) {
// int sz = size();
// if (index < 0 && index >= -sz) {
// index += sz;
// }
//
// return super.listIterator(index);
//}
//
//@Override
//default List<Object> subList(int fromIndex, int toIndex) {
// int sz = size();
// if (fromIndex < 0 && fromIndex >= -sz) {
// fromIndex += sz;
// }
// if (toIndex < 0 && toIndex >= -sz) {
// toIndex += sz;
// }
//
// return super.subList(fromIndex, toIndex);
//}
}