• 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

88.09
/src/main/java/de/ralleytn/simple/json/JSONParser.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.StringReader;
209
import java.util.List;
210
import java.util.Map;
211
import java.util.Stack;
212

213
import de.ralleytn.simple.json.internal.Yylex;
214
import de.ralleytn.simple.json.internal.Yytoken;
215

216

217
/**
218
 * Parses JSON data (<u>not</u> thread-safe).
219
 * @author FangYidong(fangyidong@yahoo.com.cn)
220
 * @author Ralph Niemitz/RalleYTN(ralph.niemitz@gmx.de)
221
 * @version 2.1.0
222
 * @since 1.0.0
223
 */
224
public class JSONParser {
1✔
225
        
226
        private static final int STATUS_INIT = 0;
227
        private static final int STATUS_IN_FINISHED_VALUE = 1;
228
        private static final int STATUS_IN_OBJECT = 2;
229
        private static final int STATUS_IN_ARRAY = 3;
230
        private static final int STATUS_PASSED_PAIR_KEY = 4;
231
        private static final int STATUS_IN_PAIR_VALUE = 5;
232
        private static final int STATUS_END = 6;
233
        private static final int STATUS_IN_ERROR = -1;
234

235
        private Stack<Object> handlerStatusStack;
236
        private Yylex lexer = new Yylex((Reader)null);
1✔
237
        private Yytoken token;
238
        private Yytoken lastToken;
239
        private int status = STATUS_INIT;
1✔
240
        
241
        private final void nextToken() throws JSONParseException, IOException {
242
                
243
                this.token = this.lexer.yylex();
1✔
244
                
245
                if(this.token == null) {
1✔
246
                        
247
                        this.token = new Yytoken(Yytoken.TYPE_EOF, null);
1✔
248
                }
249
        }
1✔
250
        
251
        private final void init(Stack<Object> statusStack, Stack<Object> valueStack) {
252
                
253
                if(this.token.type == Yytoken.TYPE_VALUE) {
1✔
254
                        
255
                        this.status = STATUS_IN_FINISHED_VALUE;
1✔
256
                        statusStack.push(this.status);
1✔
257
                        valueStack.push(this.token.value);
1✔
258
                        
259
                } else if(this.token.type == Yytoken.TYPE_LEFT_BRACE) {
1✔
260
                        
261
                        this.status = STATUS_IN_OBJECT;
1✔
262
                        statusStack.push(this.status);
1✔
263
                        valueStack.push(new JSONObject());
1✔
264
                        
265
                } else if(this.token.type == Yytoken.TYPE_LEFT_SQUARE) {
1✔
266
                        
267
                        this.status = STATUS_IN_ARRAY;
1✔
268
                        statusStack.push(this.status);
1✔
269
                        valueStack.push(new JSONArray());
1✔
270
                        
271
                } else {
272
                        
273
                        this.status = STATUS_IN_ERROR;
1✔
274
                }
275
        }
1✔
276
        
277
        private final Object inFinishedValue(Stack<Object> valueStack) throws JSONParseException {
278
                
279
                if(this.token.type == Yytoken.TYPE_EOF) {
1✔
280
                        
281
                        return valueStack.pop();
1✔
282
                        
283
                } else {
284
                        
285
                        throw new JSONParseException(this.getPosition(), JSONParseException.ERROR_UNEXPECTED_TOKEN, this.token);
1✔
286
                }
287
        }
288
        
289
        private final void inObject(Stack<Object> statusStack, Stack<Object> valueStack) {
290
                
291
                if(this.token.type == Yytoken.TYPE_VALUE) {
1✔
292
                        
293
                        if(this.token.value instanceof String) {
1✔
294
                                
295
                                String key = (String)this.token.value;
1✔
296
                                valueStack.push(key);
1✔
297
                                this.status = STATUS_PASSED_PAIR_KEY;
1✔
298
                                statusStack.push(this.status);
1✔
299
                                
300
                        } else {
1✔
301
                                
302
                                this.status = STATUS_IN_ERROR;
1✔
303
                        }
304
                        
305
                } else if(this.token.type == Yytoken.TYPE_RIGHT_BRACE) {
1✔
306
                        
307
                        if(valueStack.size() > 1){
1✔
308
                                
309
                                statusStack.pop();
1✔
310
                                valueStack.pop();
1✔
311
                                this.status = (int)statusStack.peek();
1✔
312
                                
313
                        } else {
314
                                
315
                                this.status = STATUS_IN_FINISHED_VALUE;
1✔
316
                        }
317
                        
318
                } else if(this.token.type != Yytoken.TYPE_COMMA) {
1✔
319
                        
320
                        this.status = STATUS_IN_ERROR;
1✔
321
                }
322
        }
1✔
323
        
324
        @SuppressWarnings("unchecked")
325
        private final void inPassedPairKey(Stack<Object> statusStack, Stack<Object> valueStack) {
326
                
327
                if(this.token.type == Yytoken.TYPE_VALUE) {
1✔
328
                        
329
                        statusStack.pop();
1✔
330
                        String key = (String)valueStack.pop();
1✔
331
                        Map<Object, Object> parent = (Map<Object, Object>)valueStack.peek();
1✔
332
                        parent.put(key, this.token.value);
1✔
333
                        this.status = (int)statusStack.peek();
1✔
334
                        
335
                } else if(this.token.type == Yytoken.TYPE_LEFT_SQUARE) {
1✔
336
                        
337
                        statusStack.pop();
1✔
338
                        String key = (String)valueStack.pop();
1✔
339
                        Map<Object, Object> parent = (Map<Object, Object>)valueStack.peek();
1✔
340
                        List<Object> newArray = new JSONArray();
1✔
341
                        parent.put(key, newArray);
1✔
342
                        this.status = STATUS_IN_ARRAY;
1✔
343
                        statusStack.push(this.status);
1✔
344
                        valueStack.push(newArray);
1✔
345
                        
346
                } else if(this.token.type == Yytoken.TYPE_LEFT_BRACE) {
1✔
347
                        
348
                        statusStack.pop();
1✔
349
                        String key = (String)valueStack.pop();
1✔
350
                        Map<Object, Object> parent = (Map<Object, Object>)valueStack.peek();
1✔
351
                        Map<Object, Object> newObject = new JSONObject();
1✔
352
                        parent.put(key, newObject);
1✔
353
                        this.status = STATUS_IN_OBJECT;
1✔
354
                        statusStack.push(this.status);
1✔
355
                        valueStack.push(newObject);
1✔
356
                        
357
                } else if(this.token.type != Yytoken.TYPE_COLON) {
1✔
358
                        
359
                        this.status = STATUS_IN_ERROR;
1✔
360
                }
361
        }
1✔
362
        
363
        @SuppressWarnings("unchecked")
364
        private final void inArray(Stack<Object> statusStack, Stack<Object> valueStack) {
365
                
366
                if(this.token.type == Yytoken.TYPE_VALUE) {
1✔
367
                        
368
                        List<Object> val = (List<Object>)valueStack.peek();
1✔
369
                        val.add(this.token.value);
1✔
370
                        
371
                } else if(this.token.type == Yytoken.TYPE_RIGHT_SQUARE) {
1✔
372
                        
373
                        if(valueStack.size() > 1) {
1✔
374
                                
375
                                statusStack.pop();
1✔
376
                                valueStack.pop();
1✔
377
                                this.status = (int)statusStack.peek();
1✔
378
                        
379
                        } else {
380
                                
381
                                this.status = STATUS_IN_FINISHED_VALUE;
1✔
382
                        }
383
                        
384
                } else if(this.token.type == Yytoken.TYPE_LEFT_BRACE) {
1✔
385
                        
386
                        List<Object> val = (List<Object>)valueStack.peek();
1✔
387
                        Map<Object, Object> newObject = new JSONObject();
1✔
388
                        val.add(newObject);
1✔
389
                        this.status = STATUS_IN_OBJECT;
1✔
390
                        statusStack.push(this.status);
1✔
391
                        valueStack.push(newObject);
1✔
392
                        
393
                } else if(this.token.type == Yytoken.TYPE_LEFT_SQUARE) {
1✔
394
                        
395
                        List<Object> val = (List<Object>)valueStack.peek();
1✔
396
                        List<Object> newArray = new JSONArray();
1✔
397
                        val.add(newArray);
1✔
398
                        this.status = STATUS_IN_ARRAY;
1✔
399
                        statusStack.push(this.status);
1✔
400
                        valueStack.push(newArray);
1✔
401
                        
402
                } else if(this.token.type != Yytoken.TYPE_COMMA) {
1✔
403
                        
404
                        this.status = STATUS_IN_ERROR;
1✔
405
                }
406
        }
1✔
407
        
408
    /**
409
     * Resets the parser to the initial state without resetting the underlying reader.
410
     * @since 1.0.0
411
     */
412
    public void reset() {
413
            
414
        this.token = null;
1✔
415
        this.lastToken = null;
1✔
416
        this.status = STATUS_INIT;
1✔
417
        this.handlerStatusStack = null;
1✔
418
    }
1✔
419
    
420
    /**
421
     * Resets the parser to the initial state with a new character reader.
422
     * @param reader the new character reader
423
     * @since 1.0.0
424
     */
425
        public void reset(Reader reader) {
426
                
427
                this.lexer.yyreset(reader);
1✔
428
                this.reset();
1✔
429
        }
1✔
430
        
431
        private void reset(Reader reader, boolean resume) {
432
                
433
                if(!resume || this.handlerStatusStack == null) {
1✔
434
                        
435
                        this.reset(reader);
1✔
436
                        this.handlerStatusStack = new Stack<>();
1✔
437
                }
438
        }
1✔
439
        
440
        /**
441
         * @return the position where the current token begins
442
         * @since 1.0.0
443
         */
444
        public int getPosition() {
445
                
446
                return this.lexer.getPosition();
1✔
447
        }
448
        
449
        /**
450
         * Parses JSON data.
451
         * @param json the JSON data
452
         * @return An instance of:
453
         * <ul>
454
         * <li>{@linkplain JSONObject}</li>
455
         * <li>{@linkplain JSONArray}</li>
456
         * <li>{@linkplain String}</li>
457
         * <li>{@linkplain Number}</li>
458
         * <li>{@linkplain Boolean}</li>
459
         * <li>{@code null}</li>
460
         * </ul>
461
         * @throws JSONParseException if the JSON is invalid
462
         * @since 1.0.0
463
         */
464
        public Object parse(String json) throws JSONParseException {
465
                
466
                return this.parse(json, false);
1✔
467
        }
468

469
        /**
470
         * Parses JSON data.
471
         * @param json the JSON data
472
         * @param strict {@code true} for strict validation of JSON data; {@code false} if missing colons and commas should be tolerated
473
         * @return An instance of:
474
         * <ul>
475
         * <li>{@linkplain JSONObject}</li>
476
         * <li>{@linkplain JSONArray}</li>
477
         * <li>{@linkplain String}</li>
478
         * <li>{@linkplain Number}</li>
479
         * <li>{@linkplain Boolean}</li>
480
         * <li>{@code null}</li>
481
         * </ul>
482
         * @throws JSONParseException if the JSON is invalid
483
         * @since 2.1.0
484
         */
485
        public Object parse(String json, boolean strict) throws JSONParseException {
486
                
487
                try(StringReader reader = new StringReader(json)) {
1✔
488
                        
489
                        return this.parse(reader, strict);
1✔
490
                        
491
                } catch(IOException exception) {
×
492
                        
493
                        // WILL NEVER HAPPEN!
494
                        throw new RuntimeException(exception);
×
495
                }
496
        }
497
        
498
        /**
499
         * Parses JSON data from a {@linkplain Reader}.
500
         * @param reader the {@linkplain Reader}
501
         * @return An instance of:
502
         * <ul>
503
         * <li>{@linkplain JSONObject}</li>
504
         * <li>{@linkplain JSONArray}</li>
505
         * <li>{@linkplain String}</li>
506
         * <li>{@linkplain Number}</li>
507
         * <li>{@linkplain Boolean}</li>
508
         * <li>{@code null}</li>
509
         * </ul>
510
         * @throws IOException if an I/O error occurs
511
         * @throws JSONParseException if the JSON is invalid
512
         * @since 1.0.0
513
         */
514
        public Object parse(Reader reader) throws IOException, JSONParseException {
515
                
516
                return this.parse(reader, false);
1✔
517
        }
518
        
519
        /**
520
         * Parses JSON data from a {@linkplain Reader}.
521
         * @param reader the {@linkplain Reader}
522
         * @param strict {@code true} for strict validation of JSON data; {@code false} if missing colons and commas should be tolerated
523
         * @return An instance of:
524
         * <ul>
525
         * <li>{@linkplain JSONObject}</li>
526
         * <li>{@linkplain JSONArray}</li>
527
         * <li>{@linkplain String}</li>
528
         * <li>{@linkplain Number}</li>
529
         * <li>{@linkplain Boolean}</li>
530
         * <li>{@code null}</li>
531
         * </ul>
532
         * @throws IOException if an I/O error occurs
533
         * @throws JSONParseException if the JSON is invalid
534
         * @since 2.1.0
535
         */
536
        public Object parse(Reader reader, boolean strict) throws IOException, JSONParseException {
537
                
538
                this.reset(reader);
1✔
539
                Stack<Object> statusStack = new Stack<>();
1✔
540
                Stack<Object> valueStack = new Stack<>();
1✔
541

542
                do {
543
                        
544
                        this.nextToken();
1✔
545
                        
546
                        if(strict) {
1✔
547
                                
548
                                this.validateCommaAndColonPlacement();
×
549
                        }
550
                        
551
                        switch(this.status) {
1✔
552
                                case STATUS_INIT:
553
                                        this.init(statusStack, valueStack);
1✔
554
                                        break;
1✔
555
                                case STATUS_IN_FINISHED_VALUE:
556
                                        return this.inFinishedValue(valueStack);
1✔
557
                                case STATUS_IN_OBJECT:
558
                                        this.inObject(statusStack, valueStack);
1✔
559
                                        break;
1✔
560
                                case STATUS_PASSED_PAIR_KEY:
561
                                        this.inPassedPairKey(statusStack, valueStack);
1✔
562
                                        break;
1✔
563
                                case STATUS_IN_ARRAY:
564
                                        this.inArray(statusStack, valueStack);
1✔
565
                                        break;
1✔
566
                                default:
567
                                        // WILL NEVER HAPPEN
568
                        }
569

570
                        if(this.status == STATUS_IN_ERROR) {
1✔
571
                                
572
                                throw new JSONParseException(getPosition(), JSONParseException.ERROR_UNEXPECTED_TOKEN, token);
1✔
573
                        }
574
                        
575
                        this.lastToken = this.token;
1✔
576
        
577
                } while(this.token.type != Yytoken.TYPE_EOF);
1✔
578

579
                // TODO
580
                // Is this code reachable? If yes, then how?
581
                
582
                throw new JSONParseException(this.getPosition(), JSONParseException.ERROR_UNEXPECTED_TOKEN, this.token);
×
583
        }
584
        
585
        /**
586
         * Goes over a JSON string step by step using a {@linkplain JSONContentHandler}.
587
         * @param string the JSON string
588
         * @param contentHandler the {@linkplain JSONContentHandler}
589
         * @throws JSONParseException if the JSON is invalid or the {@linkplain JSONContentHandler} throws it
590
         * @since 1.0.0
591
         */
592
        public void parse(String string, JSONContentHandler contentHandler) throws JSONParseException {
593
                
594
                this.parse(string != null ? string.trim() : null, contentHandler, false);
1✔
595
        }
1✔
596
        
597
        /**
598
         * Goes over a JSON string step by step using a {@linkplain JSONContentHandler}.
599
         * @param string the JSON string
600
         * @param contentHandler the {@linkplain JSONContentHandler}
601
         * @param resume Indicates if the previous parsing operation should be continued.
602
         * @throws JSONParseException if the JSON is invalid or the {@linkplain JSONContentHandler} throws it
603
         * @since 1.0.0
604
         */
605
        public void parse(String string, JSONContentHandler contentHandler, boolean resume) throws JSONParseException {
606

607
                try(StringReader reader = new StringReader(string != null ? string.trim() : null)) {
1✔
608
                        
609
                        this.parse(reader, contentHandler, resume);
1✔
610
                
611
                } catch(IOException exception){
×
612

613
                        throw new JSONParseException(-1, JSONParseException.ERROR_UNEXPECTED_EXCEPTION, exception);
×
614
                }
1✔
615
        }
1✔
616
        
617
        /**
618
         * Goes over JSON data step by step using a {@linkplain JSONContentHandler}.
619
         * @param reader the {@linkplain Reader}
620
         * @param contentHandler the {@linkplain JSONContentHandler}
621
         * @throws IOException if an I/O error occurs or the {@linkplain JSONContentHandler} throws it
622
         * @throws JSONParseException if the JSON is invalid or the {@linkplain JSONContentHandler} throws it
623
         * @since 1.0.0
624
         */
625
        public void parse(Reader reader, JSONContentHandler contentHandler) throws IOException, JSONParseException {
626
                
627
                this.parse(reader, contentHandler, false);
×
628
        }
×
629
        
630
        /**
631
         * Goes over JSON data step by step using a {@linkplain JSONContentHandler}.
632
         * @see JSONContentHandler
633
         * @param reader the {@linkplain Reader}
634
         * @param contentHandler the {@linkplain JSONContentHandler}
635
         * @param resume Indicates if the previous parsing operation should be continued.
636
         * @throws IOException if an I/O error occurs or the {@linkplain JSONContentHandler} throws it
637
         * @throws JSONParseException if the JSON is invalid or the {@linkplain JSONContentHandler} throws it
638
         * @since 1.0.0
639
         */
640
        public void parse(Reader reader, JSONContentHandler contentHandler, boolean resume) throws IOException, JSONParseException {
641
                
642
                this.parse(reader, contentHandler, resume, false);
1✔
643
        }
1✔
644
        
645
        /**
646
         * Goes over JSON data step by step using a {@linkplain JSONContentHandler}.
647
         * @see JSONContentHandler
648
         * @param reader the {@linkplain Reader}
649
         * @param contentHandler the {@linkplain JSONContentHandler}
650
         * @param resume Indicates if the previous parsing operation should be continued.
651
         * @param strict {@code true} for strict validation of JSON data; {@code false} if missing colons and commas should be tolerated
652
         * @throws IOException if an I/O error occurs or the {@linkplain JSONContentHandler} throws it
653
         * @throws JSONParseException if the JSON is invalid or the {@linkplain JSONContentHandler} throws it
654
         * @since 2.1.0
655
         */
656
        public void parse(Reader reader, JSONContentHandler contentHandler, boolean resume, boolean strict) throws IOException, JSONParseException {
657
                
658
                this.reset(reader, resume);
1✔
659
                Stack<Object> statusStack = this.handlerStatusStack;        
1✔
660
                
661
                try {
662
                        
663
                        do {
664
                                
665
                                if(strict) {
1✔
666
                                        
667
                                        this.validateCommaAndColonPlacement();
×
668
                                }
669
                                
670
                                switch(this.status) {
1✔
671
                                        case STATUS_INIT:
672
                                                if(this.handleStatusInit(statusStack, contentHandler))
1✔
673
                                                        return;
×
674
                                                break;
675
                                        case STATUS_IN_FINISHED_VALUE:
676
                                                if(this.handleStatusInFinishedValue(statusStack, contentHandler))
1✔
677
                                                        return;
1✔
678
                                                break;
679
                                        case STATUS_IN_OBJECT:
680
                                                if(this.handleStatusInObject(statusStack, contentHandler))
1✔
681
                                                        return;
×
682
                                                break;
683
                                        case STATUS_PASSED_PAIR_KEY:
684
                                                if(this.handleStatusPassedPairKey(statusStack, contentHandler))
1✔
685
                                                        return;
×
686
                                                break;
687
                                        case STATUS_IN_PAIR_VALUE:
688
                                                if(this.handleStatusInPairValue(statusStack, contentHandler))
1✔
689
                                                        return;
×
690
                                                break;
691
                                        case STATUS_IN_ARRAY:
692
                                                if(this.handleStatusInArray(statusStack, contentHandler))
1✔
693
                                                        return;
×
694
                                                break;
695
                                        case STATUS_END:
696
                                                return;
1✔
697
                                        default:
698
                                                throw new JSONParseException(this.getPosition(), JSONParseException.ERROR_UNEXPECTED_TOKEN, this.token);
1✔
699
                                }
700
                                
701
                                this.lastToken = this.token;
1✔
702
                                
703
                        } while(this.token.type != Yytoken.TYPE_EOF);
1✔
704
                
705
                } catch(IOException | JSONParseException | RuntimeException | Error exception) {
1✔
706
                        
707
                        this.status = STATUS_IN_ERROR;
1✔
708
                        throw exception;
1✔
709
                }
1✔
710
                
711
                // TODO
712
                // Is this code reachable? If yes, then how?
713
                
714
                this.status = STATUS_IN_ERROR;
1✔
715
                throw new JSONParseException(this.getPosition(), JSONParseException.ERROR_UNEXPECTED_TOKEN, this.token);
1✔
716
        }
717
        
718
        private void validateCommaAndColonPlacement() throws JSONParseException {
719
                
720
                if(this.token.type == Yytoken.TYPE_VALUE) {
×
721
                        
722
                        if(this.status == STATUS_IN_ARRAY) {
×
723
                                
724
                                if(!(this.lastToken.type == Yytoken.TYPE_COMMA || this.lastToken.type == Yytoken.TYPE_LEFT_SQUARE)) {
×
725
                                        
726
                                        throw new JSONParseException(getPosition(), JSONParseException.ERROR_UNEXPECTED_TOKEN, token);
×
727
                                }
728
                                
729
                        } else if(this.status == STATUS_IN_OBJECT) {
×
730
                                
731
                                if(!(this.lastToken.type == Yytoken.TYPE_COLON ||
×
732
                                         this.lastToken.type == Yytoken.TYPE_LEFT_BRACE ||
733
                                         this.lastToken.type == Yytoken.TYPE_COMMA)) {
734

735
                                        throw new JSONParseException(getPosition(), JSONParseException.ERROR_UNEXPECTED_TOKEN, token);
×
736
                                }
737
                        }
738
                }
739
        }
×
740
        
741
        private boolean handleStatusInArray(Stack<Object> statusStack, JSONContentHandler contentHandler) throws JSONParseException, IOException {
742
                
743
                this.nextToken();
1✔
744
                
745
                if(this.token.type == Yytoken.TYPE_VALUE) {
1✔
746
                        
747
                        if(!contentHandler.primitive(this.token.value)) {
1✔
748
                                
749
                                return true;
×
750
                        }
751
                        
752
                } else if(this.token.type == Yytoken.TYPE_RIGHT_SQUARE) {
1✔
753
                        
754
                        if(statusStack.size() > 1) {
1✔
755
                                
756
                                statusStack.pop();
1✔
757
                                this.status = (int)statusStack.peek();
1✔
758
                                
759
                        } else {
760
                                
761
                                this.status = STATUS_IN_FINISHED_VALUE;
1✔
762
                        }
763
                        
764
                        if(!contentHandler.endArray()) {
1✔
765
                                
766
                                return true;
×
767
                        }
768
                        
769
                } else if(this.token.type == Yytoken.TYPE_LEFT_BRACE) {
1✔
770
                        
771
                        return this.pushStatusInObject(statusStack, contentHandler);
1✔
772
                        
773
                } else if(this.token.type == Yytoken.TYPE_LEFT_SQUARE) {
1✔
774
                        
775
                        return this.pushStatusInArray(statusStack, contentHandler);
1✔
776
                        
777
                } else if(this.token.type != Yytoken.TYPE_COMMA) {
1✔
778
                        
779
                        this.status = STATUS_IN_ERROR;
1✔
780
                }
781
                
782
                return false;
1✔
783
        }
784
        
785
        private boolean handleStatusInPairValue(Stack<Object> statusStack, JSONContentHandler contentHandler) throws JSONParseException, IOException {
786
                
787
                statusStack.pop();
1✔
788
                this.status = (int)statusStack.peek();
1✔
789
                
790
                if(!contentHandler.endObjectEntry()) {
1✔
791
                        
792
                        return true;
×
793
                }
794
                
795
                return false;
1✔
796
        }
797
        
798
        private boolean handleStatusPassedPairKey(Stack<Object> statusStack, JSONContentHandler contentHandler) throws JSONParseException, IOException {
799
                
800
                this.nextToken();
1✔
801
                
802
                if(this.token.type == Yytoken.TYPE_VALUE) {
1✔
803
                        
804
                        statusStack.pop();
1✔
805
                        this.status = (int)statusStack.peek();
1✔
806
                        
807
                        if(!contentHandler.primitive(this.token.value) || !contentHandler.endObjectEntry()) {
1✔
808
                                
809
                                return true;
×
810
                        }
811
                        
812
                } else if(this.token.type == Yytoken.TYPE_LEFT_SQUARE) {
1✔
813
                        
814
                        statusStack.pop();
1✔
815
                        statusStack.push(STATUS_IN_PAIR_VALUE);
1✔
816
                        this.status = STATUS_IN_ARRAY;
1✔
817
                        statusStack.push(this.status);
1✔
818
                        
819
                        if(!contentHandler.startArray()) {
1✔
820
                                
821
                                return true;
×
822
                        }
823
                        
824
                } else if(this.token.type == Yytoken.TYPE_LEFT_BRACE) {
1✔
825
                        
826
                        statusStack.pop();
1✔
827
                        statusStack.push(STATUS_IN_PAIR_VALUE);
1✔
828
                        this.status = STATUS_IN_OBJECT;
1✔
829
                        statusStack.push(this.status);
1✔
830
                        
831
                        if(!contentHandler.startObject()) {
1✔
832
                                
833
                                return true;
×
834
                        }
835
                        
836
                } else if(this.token.type != Yytoken.TYPE_COLON) {
1✔
837
                        
838
                        this.status = STATUS_IN_ERROR;
1✔
839
                }
840
                
841
                return false;
1✔
842
        }
843
        
844
        private boolean handleStatusInObject(Stack<Object> statusStack, JSONContentHandler contentHandler) throws JSONParseException, IOException {
845
                
846
                this.nextToken();
1✔
847
                
848
                if(this.token.type == Yytoken.TYPE_VALUE) {
1✔
849
                        
850
                        if(this.token.value instanceof String) {
1✔
851
                                
852
                                String key = (String)this.token.value;
1✔
853
                                this.status = STATUS_PASSED_PAIR_KEY;
1✔
854
                                statusStack.push(this.status);
1✔
855
                                
856
                                if(!contentHandler.startObjectEntry(key)) {
1✔
857
                                        
858
                                        return true;
×
859
                                }
860
                                
861
                        } else {
1✔
862
                                
863
                                this.status = STATUS_IN_ERROR;
1✔
864
                        }
865
                        
866
                } else if(this.token.type == Yytoken.TYPE_RIGHT_BRACE) {
1✔
867
                        
868
                        if(statusStack.size() > 1) {
1✔
869
                                
870
                                statusStack.pop();
1✔
871
                                this.status = (int)statusStack.peek();
1✔
872
                                
873
                        } else {
874
                                
875
                                this.status = STATUS_IN_FINISHED_VALUE;
1✔
876
                        }
877
                        
878
                        if(!contentHandler.endObject()) {
1✔
879
                                
880
                                return true;
×
881
                        }
882
                        
883
                } else if(this.token.type != Yytoken.TYPE_COMMA) {
1✔
884
                        
885
                        this.status = STATUS_IN_ERROR;
1✔
886
                }
887
                
888
                return false;
1✔
889
        }
890
        
891
        private boolean handleStatusInFinishedValue(Stack<Object> statusStack, JSONContentHandler contentHandler) throws JSONParseException, IOException {
892
                
893
                this.nextToken();
1✔
894
                
895
                if(this.token.type == Yytoken.TYPE_EOF) {
1✔
896
                        
897
                        contentHandler.endJSON();
1✔
898
                        this.status = STATUS_END;
1✔
899
                        return true;
1✔
900
                        
901
                } else {
902
                        
903
                        this.status = STATUS_IN_ERROR;
1✔
904
                }
905
                
906
                return false;
1✔
907
        }
908
        
909
        private boolean handleStatusInit(Stack<Object> statusStack, JSONContentHandler contentHandler) throws JSONParseException, IOException {
910
                
911
                contentHandler.startJSON();
1✔
912
                this.nextToken();
1✔
913
                
914
                if(this.token.type == Yytoken.TYPE_VALUE) {
1✔
915
                        
916
                        this.status = STATUS_IN_FINISHED_VALUE;
1✔
917
                        statusStack.push(this.status);
1✔
918
                        
919
                        if(!contentHandler.primitive(this.token.value)) {
1✔
920
                                
921
                                return true;
×
922
                        }
923
                        
924
                } else if(this.token.type == Yytoken.TYPE_LEFT_BRACE) {
1✔
925
                        
926
                        return this.pushStatusInObject(statusStack, contentHandler);
1✔
927
                        
928
                } else if(this.token.type == Yytoken.TYPE_LEFT_SQUARE) {
1✔
929
                        
930
                        return this.pushStatusInArray(statusStack, contentHandler);
1✔
931
                        
932
                } else {
933
                        
934
                        this.status = STATUS_IN_ERROR;
1✔
935
                }
936
                
937
                return false;
1✔
938
        }
939
        
940
        private boolean pushStatusInArray(Stack<Object> statusStack, JSONContentHandler contentHandler) throws JSONParseException, IOException {
941
                
942
                this.status = STATUS_IN_ARRAY;
1✔
943
                statusStack.push(this.status);
1✔
944
                
945
                if(!contentHandler.startArray()) {
1✔
946
                        
947
                        return true;
×
948
                }
949
                
950
                return false;
1✔
951
        }
952
        
953
        private boolean pushStatusInObject(Stack<Object> statusStack, JSONContentHandler contentHandler) throws JSONParseException, IOException {
954
                
955
                this.status = STATUS_IN_OBJECT;
1✔
956
                statusStack.push(this.status);
1✔
957
                
958
                if(!contentHandler.startObject()) {
1✔
959
                        
960
                        return true;
×
961
                }
962
                
963
                return false;
1✔
964
        }
965
}
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