FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
APIJSON/APIJSONORM/src/main/java/apijson/JSONArray.java at no-json-lib · APIJSON/APIJSON · GitHub
APIJSON
/
APIJSON
Public
Notifications
You must be signed in to change notification settings
Fork
2.3k
Star
18.4k
Code
Issues
236
Pull requests
0
Actions
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
APIJSON
/
APIJSONORM
/
src
/
main
/
java
/
apijson
/
JSONArray.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
376 lines (325 loc) · 8.79 KB
Breadcrumbs
APIJSON
/
APIJSONORM
/
src
/
main
/
java
/
apijson
/
JSONArray.java
Copy path
File metadata and controls
376 lines (325 loc) · 8.79 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package
apijson
;
import
java
.
util
.*;
/**
* Custom JSONArray implementation based on ArrayList to replace com.alibaba.fastjson.JSONArray
* Maintains same API as fastjson but uses standard Java List implementation
* @author Lemon
*/
public
class
JSONArray
extends
JSON
implements
List
<
Object
> {
private
static
final
String
TAG
=
"JSONArray"
;
private
ArrayList
<
Object
>
list
=
new
ArrayList
<>();
/**
* Create an empty JSONArray
*/
public
JSONArray
() {
super
();
}
private
int
initialCapacity
=
10
;
/**
* Create a JSONArray with initial capacity
* @param initialCapacity the initial capacity
*/
public
JSONArray
(
int
initialCapacity
) {
this
.
initialCapacity
=
initialCapacity
;
this
.
list
=
new
ArrayList
<>(
initialCapacity
);
}
/**
* Create a JSONArray from a Collection
* @param collection the collection to copy from
*/
public
JSONArray
(
Collection
<?>
collection
) {
super
();
if
(
collection
!=
null
) {
addAll
(
collection
);
}
}
/**
* Create a JSONArray from a JSON string
* @param json JSON string
*/
public
JSONArray
(
String
json
) {
this
();
List
<
Object
>
list
=
JSON
.
parseArray
(
json
);
if
(
list
!=
null
) {
addAll
(
list
);
}
}
/**
* Get a JSONObject at the specified index
* @param index the index
* @return the JSONObject or null if not a JSONObject
*/
public
JSONObject
getJSONObject
(
int
index
) {
if
(
index
<
0
||
index
>=
size
()) {
return
null
;
}
Object
obj
=
get
(
index
);
if
(
obj
instanceof
JSONObject
) {
return
(
JSONObject
)
obj
;
}
else
if
(
obj
instanceof
Map
) {
return
new
JSONObject
(
obj
);
}
return
null
;
}
/**
* Get a JSONArray at the specified index
* @param index the index
* @return the JSONArray or null if not a JSONArray
*/
public
JSONArray
getJSONArray
(
int
index
) {
if
(
index
<
0
||
index
>=
size
()) {
return
null
;
}
Object
obj
=
get
(
index
);
if
(
obj
instanceof
List
) {
@
SuppressWarnings
(
"unchecked"
)
List
<
Object
>
list
= (
List
<
Object
>)
obj
;
return
new
JSONArray
(
list
);
}
else
if
(
obj
instanceof
List
<?>) {
return
(
JSONArray
)
obj
;
}
return
null
;
}
/**
* Get a boolean value at the specified index
* @param index the index
* @return the boolean value or false if not found
*/
public
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
*/
public
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
*/
public
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
*/
public
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
*/
public
String
getString
(
int
index
) {
if
(
index
<
0
||
index
>=
size
()) {
return
null
;
}
Object
obj
=
get
(
index
);
return
obj
!=
null
?
obj
.
toString
() :
null
;
}
/**
* Add a value to the JSONArray
* @param obj the value to add
* @return this JSONArray
*/
public
JSONArray
fluentAdd
(
Object
obj
) {
add
(
obj
);
return
this
;
}
@
Override
public
String
toString
() {
return
JSON
.
toJSONString
(
this
);
}
@
Override
public
int
size
() {
return
list
.
size
();
}
@
Override
public
boolean
isEmpty
() {
return
list
.
isEmpty
();
}
@
Override
public
boolean
contains
(
Object
o
) {
return
list
.
contains
(
o
);
}
@
Override
public
Iterator
<
Object
>
iterator
() {
return
list
.
iterator
();
}
@
Override
public
Object
[]
toArray
() {
return
list
.
toArray
();
}
@
Override
public
<
T
>
T
[]
toArray
(
T
[]
a
) {
return
list
.
toArray
(
a
);
}
@
Override
public
boolean
add
(
Object
o
) {
return
list
.
add
(
o
);
}
@
Override
public
boolean
remove
(
Object
o
) {
return
list
.
remove
(
o
);
}
@
Override
public
boolean
containsAll
(
Collection
<?>
c
) {
if
(
c
==
null
||
c
.
isEmpty
()) {
return
true
;
}
return
list
.
containsAll
(
c
);
}
@
Override
public
boolean
addAll
(
Collection
<?>
c
) {
if
(
c
==
null
||
c
.
isEmpty
()) {
return
true
;
}
return
list
.
addAll
(
c
);
}
@
Override
public
boolean
addAll
(
int
index
,
Collection
<?>
c
) {
int
sz
=
size
();
if
(
index
<
0
) {
index
+=
sz
;
}
if
(
c
==
null
||
c
.
isEmpty
()) {
return
true
;
}
return
list
.
addAll
(
index
,
c
);
}
@
Override
public
boolean
removeAll
(
Collection
<?>
c
) {
if
(
c
==
null
||
c
.
isEmpty
()) {
return
true
;
}
return
list
.
removeAll
(
c
);
}
@
Override
public
boolean
retainAll
(
Collection
<?>
c
) {
if
(
c
==
null
||
c
.
isEmpty
()) {
return
true
;
}
return
list
.
retainAll
(
c
);
}
@
Override
public
void
clear
() {
list
.
clear
();
}
@
Override
public
Object
get
(
int
index
) {
int
sz
=
size
();
if
(
index
<
0
) {
index
+=
sz
;
}
return
list
.
get
(
index
);
}
@
Override
public
Object
set
(
int
index
,
Object
element
) {
return
list
.
set
(
index
,
element
);
}
@
Override
public
void
add
(
int
index
,
Object
element
) {
list
.
add
(
index
,
element
);
}
@
Override
public
Object
remove
(
int
index
) {
int
sz
=
size
();
if
(
index
<
0
) {
index
+=
sz
;
}
if
(
index
>=
sz
) {
return
null
;
}
return
list
.
remove
(
index
);
}
@
Override
public
int
indexOf
(
Object
o
) {
return
list
.
indexOf
(
o
);
}
@
Override
public
int
lastIndexOf
(
Object
o
) {
return
list
.
lastIndexOf
(
o
);
}
@
Override
public
ListIterator
<
Object
>
listIterator
() {
return
list
.
listIterator
();
}
@
Override
public
ListIterator
<
Object
>
listIterator
(
int
index
) {
int
sz
=
size
();
if
(
index
<
0
) {
index
+=
sz
;
}
return
list
.
listIterator
(
index
);
}
@
Override
public
List
<
Object
>
subList
(
int
fromIndex
,
int
toIndex
) {
if
(
fromIndex
<
0
) {
fromIndex
+=
size
();
}
if
(
toIndex
<
0
) {
toIndex
+=
size
();
}
return
list
.
subList
(
fromIndex
,
toIndex
);
}
}
Back
|
FazBrowse Home
|
New Git URL