12
12
13
13
public class Jsoniter implements Closeable {
14
14
15
- private static final boolean [] breaks = new boolean [256 ];
16
15
final static ValueType [] valueTypes = new ValueType [256 ];
17
16
InputStream in ;
18
17
byte [] buf ;
@@ -43,13 +42,6 @@ public class Jsoniter implements Closeable {
43
42
valueTypes ['n' ] = ValueType .NULL ;
44
43
valueTypes ['[' ] = ValueType .ARRAY ;
45
44
valueTypes ['{' ] = ValueType .OBJECT ;
46
- breaks [' ' ] = true ;
47
- breaks ['\t' ] = true ;
48
- breaks ['\n' ] = true ;
49
- breaks ['\r' ] = true ;
50
- breaks [',' ] = true ;
51
- breaks ['}' ] = true ;
52
- breaks [']' ] = true ;
53
45
}
54
46
55
47
public Jsoniter (InputStream in , byte [] buf ) {
@@ -153,7 +145,7 @@ public final String currentBuffer() {
153
145
public final boolean readNull () throws IOException {
154
146
byte c = nextToken ();
155
147
if (c == 'n' ) {
156
- skipUntilBreak ();
148
+ Skip . skipUntilBreak (this );
157
149
return true ;
158
150
}
159
151
unreadByte ();
@@ -164,10 +156,10 @@ public final boolean readBoolean() throws IOException {
164
156
byte c = nextToken ();
165
157
switch (c ) {
166
158
case 't' :
167
- skipUntilBreak ();
159
+ Skip . skipUntilBreak (this );
168
160
return true ;
169
161
case 'f' :
170
- skipUntilBreak ();
162
+ Skip . skipUntilBreak (this );
171
163
return false ;
172
164
default :
173
165
throw reportError ("readBoolean" , "expect t or f, found: " + c );
@@ -245,7 +237,7 @@ public final String readObject() throws IOException {
245
237
byte c = nextToken ();
246
238
switch (c ) {
247
239
case 'n' :
248
- skipUntilBreak ();
240
+ Skip . skipUntilBreak (this );
249
241
return null ;
250
242
case '{' :
251
243
c = nextToken ();
@@ -332,191 +324,16 @@ public final <T> T read(TypeLiteral<T> typeLiteral) throws IOException {
332
324
return (T ) Codegen .getDecoder (typeLiteral .cacheKey , type ).decode (this );
333
325
}
334
326
335
- public final void skip () throws IOException {
336
- byte c = nextToken ();
337
- switch (c ) {
338
- case '"' :
339
- skipString ();
340
- return ;
341
- case '-' :
342
- case '0' :
343
- case '1' :
344
- case '2' :
345
- case '3' :
346
- case '4' :
347
- case '5' :
348
- case '6' :
349
- case '7' :
350
- case '8' :
351
- case '9' :
352
- case 't' :
353
- case 'f' :
354
- case 'n' :
355
- skipUntilBreak ();
356
- return ;
357
- case '[' :
358
- skipArray ();
359
- return ;
360
- case '{' :
361
- skipObject ();
362
- return ;
363
- default :
364
- throw reportError ("Skip" , "do not know how to skip: " + c );
365
- }
366
- }
367
-
368
- final void skipObject () throws IOException {
369
- int level = 1 ;
370
- for (; ; ) {
371
- for (int i = head ; i < tail ; i ++) {
372
- switch (buf [i ]) {
373
- case '"' : // If inside string, skip it
374
- head = i + 1 ;
375
- skipString ();
376
- i = head - 1 ; // it will be i++ soon
377
- break ;
378
- case '{' : // If open symbol, increase level
379
- level ++;
380
- break ;
381
- case '}' : // If close symbol, increase level
382
- level --;
383
-
384
- // If we have returned to the original level, we're done
385
- if (level == 0 ) {
386
- head = i + 1 ;
387
- return ;
388
- }
389
- break ;
390
- }
391
- }
392
- if (!loadMore ()) {
393
- return ;
394
- }
395
- }
396
- }
397
-
398
- final void skipArray () throws IOException {
399
- int level = 1 ;
400
- for (; ; ) {
401
- for (int i = head ; i < tail ; i ++) {
402
- switch (buf [i ]) {
403
- case '"' : // If inside string, skip it
404
- head = i + 1 ;
405
- skipString ();
406
- i = head - 1 ; // it will be i++ soon
407
- break ;
408
- case '[' : // If open symbol, increase level
409
- level ++;
410
- break ;
411
- case ']' : // If close symbol, increase level
412
- level --;
413
-
414
- // If we have returned to the original level, we're done
415
- if (level == 0 ) {
416
- head = i + 1 ;
417
- return ;
418
- }
419
- break ;
420
- }
421
- }
422
- if (!loadMore ()) {
423
- return ;
424
- }
425
- }
426
- }
427
-
428
- final void skipUntilBreak () throws IOException {
429
- // true, false, null, number
430
- for (; ; ) {
431
- for (int i = head ; i < tail ; i ++) {
432
- byte c = buf [i ];
433
- if (breaks [c ]) {
434
- head = i ;
435
- return ;
436
- }
437
- }
438
- if (!loadMore ()) {
439
- return ;
440
- }
441
- }
442
- }
443
-
444
- final void skipString () throws IOException {
445
- for (; ; ) {
446
- int end = findStringEnd ();
447
- if (end == -1 ) {
448
- int j = tail - 1 ;
449
- boolean escaped = true ;
450
- for (; ; ) {
451
- if (j < head || buf [j ] != '\\' ) {
452
- // even number of backslashes
453
- // either end of buffer, or " found
454
- escaped = false ;
455
- break ;
456
- }
457
- j --;
458
- if (j < head || buf [j ] != '\\' ) {
459
- // odd number of backslashes
460
- // it is \" or \\\"
461
- break ;
462
- }
463
- j --;
464
-
465
- }
466
- if (!loadMore ()) {
467
- return ;
468
- }
469
- if (escaped ) {
470
- head = 1 ; // skip the first char as last char readAny is \
471
- }
472
- } else {
473
- head = end ;
474
- return ;
475
- }
476
- }
477
- }
478
-
479
- // adapted from: https://github.com/buger/jsonparser/blob/master/parser.go
480
- // Tries to find the end of string
481
- // Support if string contains escaped quote symbols.
482
- final int findStringEnd () {
483
- boolean escaped = false ;
484
- for (int i = head ; i < tail ; i ++) {
485
- byte c = buf [i ];
486
- if (c == '"' ) {
487
- if (!escaped ) {
488
- return i + 1 ;
489
- } else {
490
- int j = i - 1 ;
491
- for (; ; ) {
492
- if (j < head || buf [j ] != '\\' ) {
493
- // even number of backslashes
494
- // either end of buffer, or " found
495
- return i + 1 ;
496
- }
497
- j --;
498
- if (j < head || buf [j ] != '\\' ) {
499
- // odd number of backslashes
500
- // it is \" or \\\"
501
- break ;
502
- }
503
- j --;
504
- }
505
- }
506
- } else if (c == '\\' ) {
507
- escaped = true ;
508
- }
509
- }
510
- return -1 ;
511
- }
512
-
513
-
514
327
public ValueType whatIsNext () throws IOException {
515
328
ValueType valueType = valueTypes [nextToken ()];
516
329
unreadByte ();
517
330
return valueType ;
518
331
}
519
332
333
+ public void skip () throws IOException {
334
+ Skip .skip (this );
335
+ }
336
+
520
337
public static void registerTypeDecoder (Class clazz , Decoder decoder ) {
521
338
Codegen .addNewDecoder (TypeLiteral .generateCacheKey (clazz ), decoder );
522
339
}
0 commit comments