• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RalleYTN / SimpleJSON / 46

pending completion
46

push

circleci

Ralph Niemitz
Implemented a solution for issue #5

28 of 28 new or added lines in 3 files covered. (100.0%)

1452 of 1598 relevant lines covered (90.86%)

0.91 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.63
/src/main/java/de/ralleytn/simple/json/JSONArray.java
1
/*
2
 *                                  Apache License
3
 *                            Version 2.0, January 2004
4
 *                         http://www.apache.org/licenses/
5
 * 
6
 *    TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
 * 
8
 *    1. Definitions.
9
 * 
10
 *       "License" shall mean the terms and conditions for use, reproduction,
11
 *       and distribution as defined by Sections 1 through 9 of this document.
12
 * 
13
 *       "Licensor" shall mean the copyright owner or entity authorized by
14
 *       the copyright owner that is granting the License.
15
 * 
16
 *       "Legal Entity" shall mean the union of the acting entity and all
17
 *       other entities that control, are controlled by, or are under common
18
 *       control with that entity. For the purposes of this definition,
19
 *       "control" means (i) the power, direct or indirect, to cause the
20
 *       direction or management of such entity, whether by contract or
21
 *       otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
 *       outstanding shares, or (iii) beneficial ownership of such entity.
23
 * 
24
 *       "You" (or "Your") shall mean an individual or Legal Entity
25
 *       exercising permissions granted by this License.
26
 * 
27
 *       "Source" form shall mean the preferred form for making modifications,
28
 *       including but not limited to software source code, documentation
29
 *       source, and configuration files.
30
 * 
31
 *       "Object" form shall mean any form resulting from mechanical
32
 *       transformation or translation of a Source form, including but
33
 *       not limited to compiled object code, generated documentation,
34
 *       and conversions to other media types.
35
 * 
36
 *       "Work" shall mean the work of authorship, whether in Source or
37
 *       Object form, made available under the License, as indicated by a
38
 *       copyright notice that is included in or attached to the work
39
 *       (an example is provided in the Appendix below).
40
 * 
41
 *       "Derivative Works" shall mean any work, whether in Source or Object
42
 *       form, that is based on (or derived from) the Work and for which the
43
 *       editorial revisions, annotations, elaborations, or other modifications
44
 *       represent, as a whole, an original work of authorship. For the purposes
45
 *       of this License, Derivative Works shall not include works that remain
46
 *       separable from, or merely link (or bind by name) to the interfaces of,
47
 *       the Work and Derivative Works thereof.
48
 * 
49
 *       "Contribution" shall mean any work of authorship, including
50
 *       the original version of the Work and any modifications or additions
51
 *       to that Work or Derivative Works thereof, that is intentionally
52
 *       submitted to Licensor for inclusion in the Work by the copyright owner
53
 *       or by an individual or Legal Entity authorized to submit on behalf of
54
 *       the copyright owner. For the purposes of this definition, "submitted"
55
 *       means any form of electronic, verbal, or written communication sent
56
 *       to the Licensor or its representatives, including but not limited to
57
 *       communication on electronic mailing lists, source code control systems,
58
 *       and issue tracking systems that are managed by, or on behalf of, the
59
 *       Licensor for the purpose of discussing and improving the Work, but
60
 *       excluding communication that is conspicuously marked or otherwise
61
 *       designated in writing by the copyright owner as "Not a Contribution."
62
 * 
63
 *       "Contributor" shall mean Licensor and any individual or Legal Entity
64
 *       on behalf of whom a Contribution has been received by Licensor and
65
 *       subsequently incorporated within the Work.
66
 * 
67
 *    2. Grant of Copyright License. Subject to the terms and conditions of
68
 *       this License, each Contributor hereby grants to You a perpetual,
69
 *       worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
 *       copyright license to reproduce, prepare Derivative Works of,
71
 *       publicly display, publicly perform, sublicense, and distribute the
72
 *       Work and such Derivative Works in Source or Object form.
73
 * 
74
 *    3. Grant of Patent License. Subject to the terms and conditions of
75
 *       this License, each Contributor hereby grants to You a perpetual,
76
 *       worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
 *       (except as stated in this section) patent license to make, have made,
78
 *       use, offer to sell, sell, import, and otherwise transfer the Work,
79
 *       where such license applies only to those patent claims licensable
80
 *       by such Contributor that are necessarily infringed by their
81
 *       Contribution(s) alone or by combination of their Contribution(s)
82
 *       with the Work to which such Contribution(s) was submitted. If You
83
 *       institute patent litigation against any entity (including a
84
 *       cross-claim or counterclaim in a lawsuit) alleging that the Work
85
 *       or a Contribution incorporated within the Work constitutes direct
86
 *       or contributory patent infringement, then any patent licenses
87
 *       granted to You under this License for that Work shall terminate
88
 *       as of the date such litigation is filed.
89
 * 
90
 *    4. Redistribution. You may reproduce and distribute copies of the
91
 *       Work or Derivative Works thereof in any medium, with or without
92
 *       modifications, and in Source or Object form, provided that You
93
 *       meet the following conditions:
94
 * 
95
 *       (a) You must give any other recipients of the Work or
96
 *           Derivative Works a copy of this License; and
97
 * 
98
 *       (b) You must cause any modified files to carry prominent notices
99
 *           stating that You changed the files; and
100
 * 
101
 *       (c) You must retain, in the Source form of any Derivative Works
102
 *           that You distribute, all copyright, patent, trademark, and
103
 *           attribution notices from the Source form of the Work,
104
 *           excluding those notices that do not pertain to any part of
105
 *           the Derivative Works; and
106
 * 
107
 *       (d) If the Work includes a "NOTICE" text file as part of its
108
 *           distribution, then any Derivative Works that You distribute must
109
 *           include a readable copy of the attribution notices contained
110
 *           within such NOTICE file, excluding those notices that do not
111
 *           pertain to any part of the Derivative Works, in at least one
112
 *           of the following places: within a NOTICE text file distributed
113
 *           as part of the Derivative Works; within the Source form or
114
 *           documentation, if provided along with the Derivative Works; or,
115
 *           within a display generated by the Derivative Works, if and
116
 *           wherever such third-party notices normally appear. The contents
117
 *           of the NOTICE file are for informational purposes only and
118
 *           do not modify the License. You may add Your own attribution
119
 *           notices within Derivative Works that You distribute, alongside
120
 *           or as an addendum to the NOTICE text from the Work, provided
121
 *           that such additional attribution notices cannot be construed
122
 *           as modifying the License.
123
 * 
124
 *       You may add Your own copyright statement to Your modifications and
125
 *       may provide additional or different license terms and conditions
126
 *       for use, reproduction, or distribution of Your modifications, or
127
 *       for any such Derivative Works as a whole, provided Your use,
128
 *       reproduction, and distribution of the Work otherwise complies with
129
 *       the conditions stated in this License.
130
 * 
131
 *    5. Submission of Contributions. Unless You explicitly state otherwise,
132
 *       any Contribution intentionally submitted for inclusion in the Work
133
 *       by You to the Licensor shall be under the terms and conditions of
134
 *       this License, without any additional terms or conditions.
135
 *       Notwithstanding the above, nothing herein shall supersede or modify
136
 *       the terms of any separate license agreement you may have executed
137
 *       with Licensor regarding such Contributions.
138
 * 
139
 *    6. Trademarks. This License does not grant permission to use the trade
140
 *       names, trademarks, service marks, or product names of the Licensor,
141
 *       except as required for reasonable and customary use in describing the
142
 *       origin of the Work and reproducing the content of the NOTICE file.
143
 * 
144
 *    7. Disclaimer of Warranty. Unless required by applicable law or
145
 *       agreed to in writing, Licensor provides the Work (and each
146
 *       Contributor provides its Contributions) on an "AS IS" BASIS,
147
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
 *       implied, including, without limitation, any warranties or conditions
149
 *       of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
 *       PARTICULAR PURPOSE. You are solely responsible for determining the
151
 *       appropriateness of using or redistributing the Work and assume any
152
 *       risks associated with Your exercise of permissions under this License.
153
 * 
154
 *    8. Limitation of Liability. In no event and under no legal theory,
155
 *       whether in tort (including negligence), contract, or otherwise,
156
 *       unless required by applicable law (such as deliberate and grossly
157
 *       negligent acts) or agreed to in writing, shall any Contributor be
158
 *       liable to You for damages, including any direct, indirect, special,
159
 *       incidental, or consequential damages of any character arising as a
160
 *       result of this License or out of the use or inability to use the
161
 *       Work (including but not limited to damages for loss of goodwill,
162
 *       work stoppage, computer failure or malfunction, or any and all
163
 *       other commercial damages or losses), even if such Contributor
164
 *       has been advised of the possibility of such damages.
165
 * 
166
 *    9. Accepting Warranty or Additional Liability. While redistributing
167
 *       the Work or Derivative Works thereof, You may choose to offer,
168
 *       and charge a fee for, acceptance of support, warranty, indemnity,
169
 *       or other liability obligations and/or rights consistent with this
170
 *       License. However, in accepting such obligations, You may act only
171
 *       on Your own behalf and on Your sole responsibility, not on behalf
172
 *       of any other Contributor, and only if You agree to indemnify,
173
 *       defend, and hold each Contributor harmless for any liability
174
 *       incurred by, or claims asserted against, such Contributor by reason
175
 *       of your accepting any such warranty or additional liability.
176
 * 
177
 *    END OF TERMS AND CONDITIONS
178
 * 
179
 *    APPENDIX: How to apply the Apache License to your work.
180
 * 
181
 *       To apply the Apache License to your work, attach the following
182
 *       boilerplate notice, with the fields enclosed by brackets "{}"
183
 *       replaced with your own identifying information. (Don't include
184
 *       the brackets!)  The text should be enclosed in the appropriate
185
 *       comment syntax for the file format. We also recommend that a
186
 *       file or class name and description of purpose be included on the
187
 *       same "printed page" as the copyright notice for easier
188
 *       identification within third-party archives.
189
 * 
190
 *    Copyright {yyyy} {name of copyright owner}
191
 * 
192
 *    Licensed under the Apache License, Version 2.0 (the "License");
193
 *    you may not use this file except in compliance with the License.
194
 *    You may obtain a copy of the License at
195
 * 
196
 *        http://www.apache.org/licenses/LICENSE-2.0
197
 * 
198
 *    Unless required by applicable law or agreed to in writing, software
199
 *    distributed under the License is distributed on an "AS IS" BASIS,
200
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
 *    See the License for the specific language governing permissions and
202
 * limitations under the License.
203
 */
204
package de.ralleytn.simple.json;
205

206
import java.io.IOException;
207
import java.io.Reader;
208
import java.io.StringWriter;
209
import java.io.Writer;
210
import java.lang.reflect.Array;
211
import java.text.DateFormat;
212
import java.text.ParseException;
213
import java.util.ArrayList;
214
import java.util.Collection;
215
import java.util.Date;
216
import java.util.Map;
217

218
import de.ralleytn.simple.json.internal.Util;
219

220
/**
221
 * Represents a JSON array.
222
 * @author FangYidong(fangyidong@yahoo.com.cn)
223
 * @author Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
224
 * @version 2.1.0
225
 * @since 1.0.0
226
 */
227
public class JSONArray extends ArrayList<Object> {
228

229
        private static final long serialVersionUID = 3957988303675231981L;
230
        
231
        /**
232
         * Constructs an empty {@linkplain JSONArray}.
233
         * @since 1.0.0
234
         */
235
        public JSONArray() {}
1✔
236
        
237
        /**
238
         * Constructs a {@linkplain JSONArray} with the elements of the given {@linkplain Collection}.
239
         * @param collection the {@linkplain Collection}
240
         * @since 1.0.0
241
         */
242
        public JSONArray(Collection<?> collection){
243
                
244
                super(collection);
1✔
245
        }
1✔
246
        
247
        /**
248
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
249
         * @param array the array
250
         * @param <T> the array type
251
         * @since 1.0.0
252
         */
253
        public <T>JSONArray(T[] array) {
1✔
254
                
255
                for(T element : array) {
1✔
256
                        
257
                        this.add(element);
1✔
258
                }
259
        }
1✔
260
        
261
        /**
262
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
263
         * @param array the array
264
         * @since 1.0.0
265
         */
266
        public JSONArray(byte[] array) {
1✔
267
                
268
                for(byte element : array) {
1✔
269
                        
270
                        this.add(element);
1✔
271
                }
272
        }
1✔
273
        
274
        /**
275
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
276
         * @param array the array
277
         * @since 1.0.0
278
         */
279
        public JSONArray(boolean[] array) {
1✔
280
                
281
                for(boolean element : array) {
1✔
282
                        
283
                        this.add(element);
1✔
284
                }
285
        }
1✔
286
        
287
        /**
288
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
289
         * @param array the array
290
         * @since 1.0.0
291
         */
292
        public JSONArray(char[] array) {
1✔
293
                
294
                for(char element : array) {
1✔
295
                        
296
                        this.add(element);
1✔
297
                }
298
        }
1✔
299
        
300
        /**
301
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
302
         * @param array the array
303
         * @since 1.0.0
304
         */
305
        public JSONArray(short[] array) {
1✔
306
                
307
                for(short element : array) {
1✔
308
                        
309
                        this.add(element);
1✔
310
                }
311
        }
1✔
312
        
313
        /**
314
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
315
         * @param array the array
316
         * @since 1.0.0
317
         */
318
        public JSONArray(int[] array) {
1✔
319
                
320
                for(int element : array) {
1✔
321
                        
322
                        this.add(element);
1✔
323
                }
324
        }
1✔
325
        
326
        /**
327
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
328
         * @param array the array
329
         * @since 1.0.0
330
         */
331
        public JSONArray(long[] array) {
1✔
332
                
333
                for(long element : array) {
1✔
334
                        
335
                        this.add(element);
1✔
336
                }
337
        }
1✔
338
        
339
        /**
340
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
341
         * @param array the array
342
         * @since 1.0.0
343
         */
344
        public JSONArray(float[] array) {
1✔
345
                
346
                for(float element : array) {
1✔
347
                        
348
                        this.add(element);
1✔
349
                }
350
        }
1✔
351
        
352
        /**
353
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
354
         * @param array the array
355
         * @since 1.0.0
356
         */
357
        public JSONArray(double[] array) {
1✔
358
                
359
                for(double element : array) {
1✔
360
                        
361
                        this.add(element);
1✔
362
                }
363
        }
1✔
364
        
365
        /**
366
         * Constructs a {@linkplain JSONArray} with the elements of the given array.
367
         * This constructor is a little slower than the others because it uses the Reflection API.
368
         * @param array the array
369
         * @since 1.0.0
370
         */
371
        public JSONArray(Object array) {
1✔
372

373
                if(array != null && array.getClass().isArray()) {
1✔
374
                        
375
                        int length = Array.getLength(array);
1✔
376
                        
377
                        for(int index = 0; index < length; index++) {
1✔
378
                                
379
                                this.add(Array.get(array, index));
1✔
380
                        }
381
                }
382
        }
1✔
383
        
384
        /**
385
         * Constructs a {@linkplain JSONArray} from a JSON string.
386
         * @param json the JSON string
387
         * @throws JSONParseException if the JSON is invalid
388
         * @since 1.0.0
389
         */
390
        public JSONArray(String json) throws JSONParseException {
391
                
392
                super((JSONArray)new JSONParser().parse(json));
1✔
393
        }
1✔
394
        
395
        /**
396
         * Constructs a {@linkplain JSONArray} from a JSON string.
397
         * @param json the JSON string
398
         * @param strict {@code true} for strict validation of JSON data; {@code false} if missing colons and commas should be tolerated
399
         * @throws JSONParseException if the JSON is invalid
400
         * @since 2.1.0
401
         */
402
        public JSONArray(String json, boolean strict) throws JSONParseException {
403
                
404
                super((JSONArray)new JSONParser().parse(json, strict));
×
405
        }
×
406
        
407
        /**
408
         * Constructs a {@linkplain JSONArray} from JSON data read from a {@linkplain Reader}.
409
         * @param reader the {@linkplain Reader}
410
         * @throws JSONParseException if the JSON is invalid
411
         * @throws IOException if an I/O error occurred
412
         * @since 1.0.0
413
         */
414
        public JSONArray(Reader reader) throws JSONParseException, IOException {
415
                
416
                super((JSONArray)new JSONParser().parse(reader));
1✔
417
        }
1✔
418
        
419
        /**
420
         * Constructs a {@linkplain JSONArray} from JSON data read from a {@linkplain Reader}.
421
         * @param reader the {@linkplain Reader}
422
         * @param strict {@code true} for strict validation of JSON data; {@code false} if missing colons and commas should be tolerated
423
         * @throws JSONParseException if the JSON is invalid
424
         * @throws IOException if an I/O error occurred
425
         * @since 2.1.0
426
         */
427
        public JSONArray(Reader reader, boolean strict) throws JSONParseException, IOException {
428
                
429
                super((JSONArray)new JSONParser().parse(reader));
×
430
        }
×
431
        
432
        /**
433
         * Writes this {@linkplain JSONArray} as a JSON string on the given {@linkplain Writer}.
434
         * @param writer the {@linkplain Writer}
435
         * @throws IOException if an I/O error occurred
436
         * @since 1.0.0
437
         */
438
        public void write(Writer writer) throws IOException {
439
                
440
                Util.write(this, writer);
1✔
441
        }
1✔
442

443
        /**
444
         * Converts this {@linkplain JSONArray} to a JSON string.
445
         * @return this {@linkplain JSONArray} as a JSON string
446
         * @since 1.0.0
447
         */
448
        @Override
449
        public String toString() {
450
                
451
                try(StringWriter writer = new StringWriter()) {
1✔
452
                        
453
                        Util.write(this, writer);
1✔
454
                        return writer.toString();
1✔
455
                        
456
                } catch(IOException exception) {
×
457

458
                        // WILL NEVER HAPPEN!
459
                        // DO NOTHING!
460
                }
461
                
462
                return null;
×
463
        }
464
        
465
        @Override
466
        public boolean equals(Object object) {
467
                
468
                if(object != null) {
1✔
469
                        
470
                        if(object instanceof Collection) {
1✔
471
                                
472
                                Collection<?> collection = (Collection<?>)object;
1✔
473
                                
474
                                if(collection.size() == this.size()) {
1✔
475
                                        
476
                                        int index = 0;
1✔
477
                                        
478
                                        for(Object element : collection) {
1✔
479
                                                
480
                                                if(!((element == null && this.get(index) == null) || this.get(index).equals(element))) {
1✔
481
                                                        
482
                                                        return false;
1✔
483
                                                }
484
                                                
485
                                                index++;
1✔
486
                                        }
1✔
487
                                        
488
                                        return true;
1✔
489
                                }
490
                                
491
                        } else if(object.getClass().isArray()) {
1✔
492
                                
493
                                int length = Array.getLength(object);
1✔
494
                                
495
                                if(length == this.size()) {
1✔
496
                                        
497
                                        for(int index = 0; index < length; index++) {
1✔
498
                                                
499
                                                Object element = Array.get(object, index);
1✔
500

501
                                                if(!((element == null && this.get(index) == null) || this.get(index).equals(element))) {
1✔
502
                                                        
503
                                                        return false;
1✔
504
                                                }
505
                                        }
506
                                        
507
                                        return true;
1✔
508
                                }
509
                        }
510
                }
511
                
512
                return false;
1✔
513
        }
514
        
515
        /**
516
         * If the value is a {@linkplain JSONObject} already, it will be casted and returned.
517
         * If the value is a {@linkplain Map}, it will be wrapped in a {@linkplain JSONObject}. The wrapped {@linkplain JSONObject} will be returned.
518
         * In any other case this method returns {@code null}.
519
         * @param index index of the value
520
         * @return a {@code JSONObject} or {@code null}
521
         * @since 1.0.0
522
         */
523
        public JSONObject getObject(int index) {
524
                
525
                return Util.getObject(this.get(index));
1✔
526
        }
527
        
528
        /**
529
         * If the value already is a {@linkplain JSONArray}, it will be casted and returned.
530
         * If the value is an array or {@linkplain Collection}, it will be wrapped in a {@linkplain JSONArray}.
531
         * The {@linkplain JSONArray} will be returned.
532
         * In any other case this method returns {@code null}.
533
         * @param index index of the value
534
         * @return a {@linkplain JSONArray} or {@code null}
535
         * @since 1.0.0
536
         */
537
        public JSONArray getArray(int index) {
538
                
539
                return Util.getArray(this.get(index));
1✔
540
        }
541
        
542
        /**
543
         * If the value is already a {@linkplain Boolean}, it will be casted and returned.
544
         * If the value is a {@linkplain String}, it will be parsed. The result is returned.
545
         * If the value is a {@linkplain Number}, this method will return {@code true} in case its {@code long} value is {@code 1}.
546
         * It will return {@code false} if it is a number but the {@code long} value is <u>not</u> {@code 1}.
547
         * In any other case this method returns {@code null}.
548
         * @param index index of the value
549
         * @return a {@linkplain Boolean} or {@code null}
550
         * @since 1.0.0
551
         */
552
        public Boolean getBoolean(int index) {
553
                
554
                return Util.getBoolean(this.get(index));
1✔
555
        }
556
        
557
        /**
558
         * If the value is a {@linkplain Number}, its {@code byte} value is returned.
559
         * If the value is a {@linkplain String}, it will be parsed. The result is returned.
560
         * If the value is a {@linkplain Boolean}, this method returns {@code 1} in case the value is {@code true} otherwise {@code 0}.
561
         * In any other case this method returns {@code null}.
562
         * @param index index of the value
563
         * @return a {@linkplain Byte} or {@code null}
564
         * @since 1.0.0
565
         */
566
        public Byte getByte(int index) {
567
                
568
                return Util.getByte(this.get(index));
1✔
569
        }
570
        
571
        /**
572
         * If the value is a {@linkplain Number}, its {@code short} value is returned.
573
         * If the value is a {@linkplain String}, it will be parsed. The result is returned.
574
         * If the value is a {@linkplain Boolean}, this method returns {@code 1} in case the value is {@code true} otherwise {@code 0}.
575
         * In any other case this method returns {@code null}.
576
         * @param index index of the value
577
         * @return a {@linkplain Short} or {@code null}
578
         * @since 1.0.0
579
         */
580
        public Short getShort(int index) {
581
                
582
                return Util.getShort(this.get(index));
1✔
583
        }
584
        
585
        /**
586
         * If the value is a {@linkplain Number}, its {@code int} value is returned.
587
         * If the value is a {@linkplain String}, it will be parsed. The result is returned.
588
         * If the value is a {@linkplain Boolean}, this method returns {@code 1} in case the value is {@code true} otherwise {@code 0}.
589
         * In any other case this method returns {@code null}.
590
         * @param index index of the value
591
         * @return a {@linkplain Integer} or {@code null}
592
         * @since 1.0.0
593
         */
594
        public Integer getInteger(int index) {
595
                
596
                return Util.getInteger(this.get(index));
1✔
597
        }
598
        
599
        /**
600
         * If the value is a {@linkplain Number}, its {@code long} value is returned.
601
         * If the value is a {@linkplain String}, it will be parsed. The result is returned.
602
         * If the value is a {@linkplain Boolean}, this method returns {@code 1} in case the value is {@code true} otherwise {@code 0}.
603
         * In any other case this method returns {@code null}.
604
         * @param index index of the value
605
         * @return a {@linkplain Long} or {@code null}
606
         * @since 1.0.0
607
         */
608
        public Long getLong(int index) {
609
                
610
                return Util.getLong(this.get(index));
1✔
611
        }
612
        
613
        /**
614
         * If the value is a {@linkplain Number}, its {@code float} value is returned.
615
         * If the value is a {@linkplain String}, it will be parsed. The result is returned.
616
         * If the value is a {@linkplain Boolean}, this method returns {@code 1} in case the value is {@code true} otherwise {@code 0}.
617
         * In any other case this method returns {@code null}.
618
         * @param index index of the value
619
         * @return a {@linkplain Float} or {@code null}
620
         * @since 1.0.0
621
         */
622
        public Float getFloat(int index) {
623
                
624
                return Util.getFloat(this.get(index));
1✔
625
        }
626
        
627
        /**
628
         * If the value is a {@linkplain Number}, its {@code double} value is returned.
629
         * If the value is a {@linkplain String}, it will be parsed. The result is returned.
630
         * If the value is a {@linkplain Boolean}, this method returns {@code 1} in case the value is {@code true} otherwise {@code 0}.
631
         * In any other case this method returns {@code null}.
632
         * @param index index of the value
633
         * @return a {@linkplain Double} or {@code null}
634
         * @since 1.0.0
635
         */
636
        public Double getDouble(int index) {
637
                
638
                return Util.getDouble(this.get(index));
1✔
639
        }
640
        
641
        /**
642
         * Returns the same as the value's {@link Object#toString()} method.
643
         * If the actual value is {@code null}, this method will return {@code null}.
644
         * @param index index of the value
645
         * @return a {@linkplain String} or {@code null}
646
         * @since 1.0.0
647
         */
648
        public String getString(int index) {
649
                
650
                return Util.getString(this.get(index));
1✔
651
        }
652

653
        /**
654
         * If the value already is a {@linkplain Date}, it will be casted and returned.
655
         * Otherwise the result of the value's {@link Object#toString()} will be parsed by the given
656
         * {@linkplain DateFormat}. The result is returned.
657
         * If the actual value is {@code null}, this method will return {@code null}.
658
         * @param index index of the value
659
         * @param format the {@linkplain DateFormat} used to parse the date
660
         * @return a {@linkplain Date} or {@code null}
661
         * @throws ParseException if the date could not be parsed
662
         * @since 1.0.0
663
         */
664
        public Date getDate(int index, DateFormat format) throws ParseException {
665
                
666
                return Util.getDate(this.get(index), format);
1✔
667
        }
668
        
669
        /**
670
         * If the {@linkplain String} representation of the value equals the name of the enum constant
671
         * in the given enum type, it will return the enum constant.
672
         * In any other case this method returns {@code null}.
673
         * @param index index of the value
674
         * @param type the enum type
675
         * @param <T> the return type
676
         * @return an {@linkplain Enum} or {@code null}
677
         * @since 1.0.0
678
         */
679
        public <T extends Enum<T>>T getEnum(int index, Class<T> type) {
680
                
681
                return Util.getEnum(this.get(index), type);
1✔
682
        }
683

684
        /**
685
         * Converts this {@linkplain JSONArray} into an array of {@linkplain Boolean}s using the {@link #getBoolean(int)} method.
686
         * @return an array {@linkplain Boolean}s
687
         * @since 1.0.0
688
         */
689
        public Boolean[] toBooleanArray() {
690
                
691
                Boolean[] array = new Boolean[this.size()];
1✔
692
                
693
                for(int index = 0; index < array.length; index++) {
1✔
694
                        
695
                        array[index] = this.getBoolean(index);
1✔
696
                }
697
                
698
                return array;
1✔
699
        }
700

701
        /**
702
         * Converts this {@linkplain JSONArray} into an array of {@linkplain Byte}s using the {@link #getByte(int)} method.
703
         * @return an array of {@linkplain Byte}s
704
         * @since 1.0.0
705
         */
706
        public Byte[] toByteArray() {
707
                
708
                Byte[] array = new Byte[this.size()];
1✔
709
                
710
                for(int index = 0; index < array.length; index++) {
1✔
711
                        
712
                        array[index] = this.getByte(index);
1✔
713
                }
714
                
715
                return array;
1✔
716
        }
717

718
        /**
719
         * Converts this {@linkplain JSONArray} into an array of {@linkplain Short}s using the {@link #getShort(int)} method.
720
         * @return an array of {@linkplain Short}s
721
         * @since 1.0.0
722
         */
723
        public Short[] toShortArray() {
724
                
725
                Short[] array = new Short[this.size()];
1✔
726
                
727
                for(int index = 0; index < array.length; index++) {
1✔
728
                        
729
                        array[index] = this.getShort(index);
1✔
730
                }
731
                
732
                return array;
1✔
733
        }
734

735
        /**
736
         * Converts this {@linkplain JSONArray} into an array of {@linkplain Integer}s using the {@link #getInteger(int)} method.
737
         * @return an array of {@linkplain Integer}s
738
         * @since 1.0.0
739
         */
740
        public Integer[] toIntegerArray() {
741
                
742
                Integer[] array = new Integer[this.size()];
1✔
743
                
744
                for(int index = 0; index < array.length; index++) {
1✔
745
                        
746
                        array[index] = this.getInteger(index);
1✔
747
                }
748
                
749
                return array;
1✔
750
        }
751

752
        /**
753
         * Converts this {@linkplain JSONArray} into an array of {@linkplain Long}s using the {@link #getLong(int)} method.
754
         * @return an array of {@linkplain Long}s
755
         * @since 1.0.0
756
         */
757
        public Long[] toLongArray() {
758
                
759
                Long[] array = new Long[this.size()];
1✔
760
                
761
                for(int index = 0; index < array.length; index++) {
1✔
762
                        
763
                        array[index] = this.getLong(index);
1✔
764
                }
765
                
766
                return array;
1✔
767
        }
768

769
        /**
770
         * Converts this {@linkplain JSONArray} into an array of {@linkplain Float}s using the {@link #getFloat(int)} method.
771
         * @return an array of {@linkplain Float}s
772
         * @since 1.0.0
773
         */
774
        public Float[] toFloatArray() {
775
                
776
                Float[] array = new Float[this.size()];
1✔
777
                
778
                for(int index = 0; index < array.length; index++) {
1✔
779
                        
780
                        array[index] = this.getFloat(index);
1✔
781
                }
782
                
783
                return array;
1✔
784
        }
785
        
786
        /**
787
         * Converts this {@linkplain JSONArray} into an array of {@linkplain Double}s using the {@link #getDouble(int)} method.
788
         * @return an array of {@linkplain Double}s
789
         * @since 1.0.0
790
         */
791
        public Double[] toDoubleArray() {
792
                
793
                Double[] array = new Double[this.size()];
1✔
794
                
795
                for(int index = 0; index < array.length; index++) {
1✔
796
                        
797
                        array[index] = this.getDouble(index);
1✔
798
                }
799
                
800
                return array;
1✔
801
        }
802
        
803
        /**
804
         * Converts this {@linkplain JSONArray} into an array of {@linkplain String}s using the {@link #getString(int)} method.
805
         * @return an array of {@linkplain String}s
806
         * @since 1.0.0
807
         */
808
        public String[] toStringArray() {
809
                
810
                String[] array = new String[this.size()];
1✔
811
                
812
                for(int index = 0; index < array.length; index++) {
1✔
813
                        
814
                        array[index] = this.getString(index);
1✔
815
                }
816
                
817
                return array;
1✔
818
        }
819
        
820
        /**
821
         * Converts this {@linkplain JSONArray} into an array of {@linkplain JSONObject}s using the {@link #getObject(int)} method.
822
         * @return an array of {@linkplain JSONObject}s
823
         * @since 1.0.0
824
         */
825
        public JSONObject[] toObjectArray() {
826
                
827
                JSONObject[] array = new JSONObject[this.size()];
1✔
828
                
829
                for(int index = 0; index < array.length; index++) {
1✔
830
                        
831
                        array[index] = this.getObject(index);
1✔
832
                }
833
                
834
                return array;
1✔
835
        }
836
        
837
        /**
838
         * Converts this {@linkplain JSONArray} into an array of {@linkplain JSONArray}s using the {@link #getArray(int)} method.
839
         * @return an array of {@linkplain JSONArray}s.
840
         * @since 1.0.0
841
         */
842
        public JSONArray[] toArrayArray() {
843
                
844
                JSONArray[] array = new JSONArray[this.size()];
1✔
845
                
846
                for(int index = 0; index < array.length; index++) {
1✔
847
                        
848
                        array[index] = this.getArray(index);
1✔
849
                }
850
                
851
                return array;
1✔
852
        }
853
        
854
        /**
855
         * Converts this {@linkplain JSONArray} to a primitive {@code byte} array.
856
         * @return a primitive {@code byte} array
857
         * @since 2.0.0
858
         */
859
        public byte[] toPrimitiveByteArray() {
860
                
861
                byte[] array = new byte[this.size()];
1✔
862
                
863
                for(int index = 0; index < array.length; index++) {
1✔
864
                        
865
                        array[index] = this.getByte(index);
1✔
866
                }
867
                
868
                return array;
1✔
869
        }
870
        
871
        /**
872
         * Converts this {@linkplain JSONArray} to a primitive {@code short} array.
873
         * @return a primitive {@code short} array
874
         * @since 2.0.0
875
         */
876
        public short[] toPrimitiveShortArray() {
877
                
878
                short[] array = new short[this.size()];
1✔
879
                
880
                for(int index = 0; index < array.length; index++) {
1✔
881
                        
882
                        array[index] = this.getShort(index);
1✔
883
                }
884
                
885
                return array;
1✔
886
        }
887
        
888
        /**
889
         * Converts this {@linkplain JSONArray} to a primitive {@code int} array.
890
         * @return a primitive {@code int} array
891
         * @since 2.0.0
892
         */
893
        public int[] toPrimitiveIntArray() {
894
                
895
                int[] array = new int[this.size()];
1✔
896
                
897
                for(int index = 0; index < array.length; index++) {
1✔
898
                        
899
                        array[index] = this.getInteger(index);
1✔
900
                }
901
                
902
                return array;
1✔
903
        }
904
        
905
        /**
906
         * Converts this {@linkplain JSONArray} to a primitive {@code long} array.
907
         * @return a primitive {@code long} array
908
         * @since 2.0.0
909
         */
910
        public long[] toPrimitiveLongArray() {
911
                
912
                long[] array = new long[this.size()];
1✔
913
                
914
                for(int index = 0; index < array.length; index++) {
1✔
915
                        
916
                        array[index] = this.getLong(index);
1✔
917
                }
918
                
919
                return array;
1✔
920
        }
921
        
922
        /**
923
         * Converts this {@linkplain JSONArray} to a primitive {@code float} array.
924
         * @return a primitive {@code float} array
925
         * @since 2.0.0
926
         */
927
        public float[] toPrimitiveFloatArray() {
928
                
929
                float[] array = new float[this.size()];
1✔
930
                
931
                for(int index = 0; index < array.length; index++) {
1✔
932
                        
933
                        array[index] = this.getFloat(index);
1✔
934
                }
935
                
936
                return array;
1✔
937
        }
938
        
939
        /**
940
         * Converts this {@linkplain JSONArray} to a primitive {@code double} array.
941
         * @return a primitive {@code double} array
942
         * @since 2.0.0
943
         */
944
        public double[] toPrimitiveDoubleArray() {
945
                
946
                double[] array = new double[this.size()];
1✔
947
                
948
                for(int index = 0; index < array.length; index++) {
1✔
949
                        
950
                        array[index] = this.getDouble(index);
1✔
951
                }
952
                
953
                return array;
1✔
954
        }
955
        
956
        /**
957
         * Converts this {@linkplain JSONArray} to a primitive {@code boolean} array.
958
         * @return a primitive {@code boolean} array
959
         * @since 2.0.0
960
         */
961
        public boolean[] toPrimitiveBooleanArray() {
962
                
963
                boolean[] array = new boolean[this.size()];
1✔
964
                
965
                for(int index = 0; index < array.length; index++) {
1✔
966
                        
967
                        array[index] = this.getBoolean(index);
1✔
968
                }
969
                
970
                return array;
1✔
971
        }
972
        
973
        /**
974
         * Converts this {@linkplain JSONArray} to a XML string.
975
         * @param rootName name of the root element
976
         * @return this JSON array as a XML string
977
         * @since 1.1.0
978
         */
979
        public String toXML(String rootName) {
980
                
981
                StringBuilder builder = new StringBuilder();
1✔
982
                builder.append('<');
1✔
983
                builder.append(rootName);
1✔
984
                builder.append(" length=");
1✔
985
                builder.append(this.size());
1✔
986
                builder.append('>');
1✔
987
                
988
                for(Object element : this) {
1✔
989

990
                                   if(element instanceof JSONObject) {builder.append(((JSONObject)element).toXML("item"));
1✔
991
                        } else if(element instanceof JSONArray) {builder.append(((JSONArray)element).toXML("item"));
1✔
992
                        } else {
993
                                        
994
                                builder.append("<item>");
1✔
995
                                
996
                                if(element != null) {
1✔
997
                                        
998
                                        builder.append(String.valueOf(element));
1✔
999
                                }
1000
                                
1001
                                builder.append("</item>");
1✔
1002
                        }
1003
                }
1✔
1004
                
1005
                builder.append("</");
1✔
1006
                builder.append(rootName);
1✔
1007
                builder.append('>');
1✔
1008
                
1009
                return builder.toString();
1✔
1010
        }
1011
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc