/
JSON.java
1784 lines (1610 loc) · 49.2 KB
/
JSON.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.ajax;
import java.io.Externalizable;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.Loader;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
/**
* JSON Parser and Generator.
* <p>
* This class provides some static methods to convert POJOs to and from JSON
* notation. The mapping from JSON to java is:
*
* <pre>
* object --> Map
* array --> Object[]
* number --> Double or Long
* string --> String
* null --> null
* bool --> Boolean
* </pre>
*
* The java to JSON mapping is:
*
* <pre>
* String --> string
* Number --> number
* Map --> object
* List --> array
* Array --> array
* null --> null
* Boolean--> boolean
* Object --> string (dubious!)
* </pre>
*
* The interface {@link JSON.Convertible} may be implemented by classes that
* wish to externalize and initialize specific fields to and from JSON objects.
* Only directed acyclic graphs of objects are supported.
* <p>
* The interface {@link JSON.Generator} may be implemented by classes that know
* how to render themselves as JSON and the {@link #toString(Object)} method
* will use {@link JSON.Generator#addJSON(Appendable)} to generate the JSON.
* The class {@link JSON.Literal} may be used to hold pre-generated JSON object.
* <p>
* The interface {@link JSON.Convertor} may be implemented to provide static
* converters for objects that may be registered with
* {@link #registerConvertor(Class, Convertor)}.
* These converters are looked up by class, interface and super class by
* {@link #getConvertor(Class)}.
* <p>
* If a JSON object has a "class" field, then a java class for that name is
* loaded and the method {@link #convertTo(Class, Map)} is used to find a
* {@link JSON.Convertor} for that class.
* <p>
* If a JSON object has a "x-class" field then a direct lookup for a
* {@link JSON.Convertor} for that class name is done (without loading the class).
*/
public class JSON
{
static final Logger LOG = Log.getLogger(JSON.class);
public static final JSON DEFAULT = new JSON();
private final Map<String, Convertor> _convertors = new ConcurrentHashMap<>();
private int _stringBufferSize = 1024;
private Function<List<?>, Object> _arrayConverter = List::toArray;
public JSON()
{
}
/**
* Reset the default JSON behaviors to default
*/
public static void reset()
{
DEFAULT._convertors.clear();
DEFAULT._stringBufferSize = 1024;
}
/**
* @return the initial stringBuffer size to use when creating JSON strings
* (default 1024)
*/
public int getStringBufferSize()
{
return _stringBufferSize;
}
/**
* @param stringBufferSize the initial stringBuffer size to use when creating JSON
* strings (default 1024)
*/
public void setStringBufferSize(int stringBufferSize)
{
_stringBufferSize = stringBufferSize;
}
/**
* Register a {@link Convertor} for a class or interface.
*
* @param forClass The class or interface that the convertor applies to
* @param convertor the convertor
*/
public static void registerConvertor(Class forClass, Convertor convertor)
{
DEFAULT.addConvertor(forClass, convertor);
}
public static JSON getDefault()
{
return DEFAULT;
}
@Deprecated
public static void setDefault(JSON json)
{
}
public static String toString(Object object)
{
StringBuilder buffer = new StringBuilder(DEFAULT.getStringBufferSize());
DEFAULT.append(buffer, object);
return buffer.toString();
}
public static String toString(Map object)
{
StringBuilder buffer = new StringBuilder(DEFAULT.getStringBufferSize());
DEFAULT.appendMap(buffer, object);
return buffer.toString();
}
public static String toString(Object[] array)
{
StringBuilder buffer = new StringBuilder(DEFAULT.getStringBufferSize());
DEFAULT.appendArray(buffer, array);
return buffer.toString();
}
/**
* @param s String containing JSON object or array.
* @return A Map, Object array or primitive array parsed from the JSON.
*/
public static Object parse(String s)
{
return DEFAULT.parse(new StringSource(s), false);
}
/**
* @param s String containing JSON object or array.
* @param stripOuterComment If true, an outer comment around the JSON is ignored.
* @return A Map, Object array or primitive array parsed from the JSON.
*/
public static Object parse(String s, boolean stripOuterComment)
{
return DEFAULT.parse(new StringSource(s), stripOuterComment);
}
/**
* @param in Reader containing JSON object or array.
* @return A Map, Object array or primitive array parsed from the JSON.
* @throws IOException if unable to parse
*/
public static Object parse(Reader in) throws IOException
{
return DEFAULT.parse(new ReaderSource(in), false);
}
/**
* @param in Reader containing JSON object or array.
* @param stripOuterComment If true, an outer comment around the JSON is ignored.
* @return A Map, Object array or primitive array parsed from the JSON.
* @throws IOException if unable to parse
*/
public static Object parse(Reader in, boolean stripOuterComment) throws IOException
{
return DEFAULT.parse(new ReaderSource(in), stripOuterComment);
}
/**
* @param in Reader containing JSON object or array.
* @return A Map, Object array or primitive array parsed from the JSON.
* @throws IOException if unable to parse
* @deprecated use {@link #parse(Reader)}
*/
@Deprecated
public static Object parse(InputStream in) throws IOException
{
return DEFAULT.parse(new StringSource(IO.toString(in)), false);
}
/**
* @param in Stream containing JSON object or array.
* @param stripOuterComment If true, an outer comment around the JSON is ignored.
* @return A Map, Object array or primitive array parsed from the JSON.
* @throws IOException if unable to parse
* @deprecated use {@link #parse(Reader, boolean)}
*/
@Deprecated
public static Object parse(InputStream in, boolean stripOuterComment) throws IOException
{
return DEFAULT.parse(new StringSource(IO.toString(in)), stripOuterComment);
}
private void quotedEscape(Appendable buffer, String input)
{
try
{
buffer.append('"');
if (input != null && !input.isEmpty())
{
escapeString(buffer, input);
}
buffer.append('"');
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
public void escapeString(Appendable buffer, String input) throws IOException
{
// default escaping here.
for (int i = 0; i < input.length(); ++i)
{
char c = input.charAt(i);
// ASCII printable range
if ((c >= 0x20) && (c <= 0x7E))
{
// Special cases for quotation-mark, reverse-solidus, and solidus
if ((c == '"') || (c == '\\')
/* solidus is optional - per Carsten Bormann (IETF)
|| (c == '/') */)
{
buffer.append('\\').append(c);
}
else
{
// ASCII printable (that isn't escaped above)
buffer.append(c);
}
}
else
{
// All other characters are escaped (in some way)
// First we deal with the special short-form escaping
if (c == '\b') // backspace
buffer.append("\\b");
else if (c == '\f') // form-feed
buffer.append("\\f");
else if (c == '\n') // line feed
buffer.append("\\n");
else if (c == '\r') // carriage return
buffer.append("\\r");
else if (c == '\t') // tab
buffer.append("\\t");
else if (c < 0x20 || c == 0x7F) // all control characters
{
// default behavior is to encode
buffer.append(String.format("\\u%04x", (short)c));
}
else
{
// optional behavior in JSON spec
escapeUnicode(buffer, c);
}
}
}
}
/**
* Per spec, unicode characters are by default NOT escaped.
* This overridable allows for alternate behavior to escape those with your choice
* of encoding.
*
* <pre>
* protected void escapeUnicode(Appendable buffer, char c) throws IOException
* {
* // Unicode is slash-u escaped
* buffer.append(String.format("\\u%04x", (int)c));
* }
* </pre>
*/
protected void escapeUnicode(Appendable buffer, char c) throws IOException
{
buffer.append(c);
}
/**
* Convert Object to JSON
*
* @param object The object to convert
* @return The JSON String
*/
public String toJSON(Object object)
{
StringBuilder buffer = new StringBuilder(getStringBufferSize());
append(buffer, object);
return buffer.toString();
}
/**
* Convert JSON to Object
*
* @param json The json to convert
* @return The object
*/
public Object fromJSON(String json)
{
Source source = new StringSource(json);
return parse(source);
}
@Deprecated
public void append(StringBuffer buffer, Object object)
{
append((Appendable)buffer, object);
}
/**
* Append object as JSON to string buffer.
*
* @param buffer the buffer to append to
* @param object the object to append
*/
public void append(Appendable buffer, Object object)
{
try
{
if (object == null)
{
buffer.append("null");
}
// Most likely first
else if (object instanceof Map)
{
appendMap(buffer, (Map)object);
}
else if (object instanceof String)
{
appendString(buffer, (String)object);
}
else if (object instanceof Number)
{
appendNumber(buffer, (Number)object);
}
else if (object instanceof Boolean)
{
appendBoolean(buffer, (Boolean)object);
}
else if (object.getClass().isArray())
{
appendArray(buffer, object);
}
else if (object instanceof Character)
{
appendString(buffer, object.toString());
}
else if (object instanceof Convertible)
{
appendJSON(buffer, (Convertible)object);
}
else if (object instanceof Generator)
{
appendJSON(buffer, (Generator)object);
}
else
{
// Check Convertor before Collection to support JSONCollectionConvertor
Convertor convertor = getConvertor(object.getClass());
if (convertor != null)
{
appendJSON(buffer, convertor, object);
}
else if (object instanceof Collection)
{
appendArray(buffer, (Collection)object);
}
else
{
appendString(buffer, object.toString());
}
}
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Deprecated
public void appendNull(StringBuffer buffer)
{
appendNull((Appendable)buffer);
}
public void appendNull(Appendable buffer)
{
try
{
buffer.append("null");
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Deprecated
public void appendJSON(final StringBuffer buffer, final Convertor convertor, final Object object)
{
appendJSON((Appendable)buffer, convertor, object);
}
public void appendJSON(final Appendable buffer, final Convertor convertor, final Object object)
{
appendJSON(buffer, new Convertible()
{
@Override
public void fromJSON(Map object)
{
}
@Override
public void toJSON(Output out)
{
convertor.toJSON(object, out);
}
});
}
@Deprecated
public void appendJSON(final StringBuffer buffer, Convertible converter)
{
appendJSON((Appendable)buffer, converter);
}
public void appendJSON(final Appendable buffer, Convertible converter)
{
ConvertableOutput out = new ConvertableOutput(buffer);
converter.toJSON(out);
out.complete();
}
@Deprecated
public void appendJSON(StringBuffer buffer, Generator generator)
{
generator.addJSON(buffer);
}
public void appendJSON(Appendable buffer, Generator generator)
{
generator.addJSON(buffer);
}
@Deprecated
public void appendMap(StringBuffer buffer, Map<?, ?> map)
{
appendMap((Appendable)buffer, map);
}
public void appendMap(Appendable buffer, Map<?, ?> map)
{
try
{
if (map == null)
{
appendNull(buffer);
return;
}
buffer.append('{');
Iterator<?> iter = map.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry<?, ?> entry = (Map.Entry<?, ?>)iter.next();
quotedEscape(buffer, entry.getKey().toString());
buffer.append(':');
append(buffer, entry.getValue());
if (iter.hasNext())
buffer.append(',');
}
buffer.append('}');
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Deprecated
public void appendArray(StringBuffer buffer, Collection collection)
{
appendArray((Appendable)buffer, collection);
}
public void appendArray(Appendable buffer, Collection collection)
{
try
{
if (collection == null)
{
appendNull(buffer);
return;
}
buffer.append('[');
Iterator iter = collection.iterator();
boolean first = true;
while (iter.hasNext())
{
if (!first)
buffer.append(',');
first = false;
append(buffer, iter.next());
}
buffer.append(']');
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Deprecated
public void appendArray(StringBuffer buffer, Object array)
{
appendArray((Appendable)buffer, array);
}
public void appendArray(Appendable buffer, Object array)
{
try
{
if (array == null)
{
appendNull(buffer);
return;
}
buffer.append('[');
int length = Array.getLength(array);
for (int i = 0; i < length; i++)
{
if (i != 0)
buffer.append(',');
append(buffer, Array.get(array, i));
}
buffer.append(']');
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Deprecated
public void appendBoolean(StringBuffer buffer, Boolean b)
{
appendBoolean((Appendable)buffer, b);
}
public void appendBoolean(Appendable buffer, Boolean b)
{
try
{
if (b == null)
{
appendNull(buffer);
return;
}
buffer.append(b ? "true" : "false");
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Deprecated
public void appendNumber(StringBuffer buffer, Number number)
{
appendNumber((Appendable)buffer, number);
}
public void appendNumber(Appendable buffer, Number number)
{
try
{
if (number == null)
{
appendNull(buffer);
return;
}
buffer.append(String.valueOf(number));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
@Deprecated
public void appendString(StringBuffer buffer, String string)
{
appendString((Appendable)buffer, string);
}
public void appendString(Appendable buffer, String string)
{
if (string == null)
{
appendNull(buffer);
return;
}
quotedEscape(buffer, string);
}
// Parsing utilities
protected String toString(char[] buffer, int offset, int length)
{
return new String(buffer, offset, length);
}
protected Map<String, Object> newMap()
{
return new HashMap<>();
}
/**
* @param size the size of the array
* @return a new array
* @deprecated use {@link #setArrayConverter(Function)} instead.
*/
@Deprecated
protected Object[] newArray(int size)
{
return new Object[size];
}
protected JSON contextForArray()
{
return this;
}
protected JSON contextFor(String field)
{
return this;
}
protected Object convertTo(Class type, Map map)
{
if (type != null && Convertible.class.isAssignableFrom(type))
{
try
{
Convertible conv = (Convertible)type.getDeclaredConstructor().newInstance();
conv.fromJSON(map);
return conv;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
Convertor convertor = getConvertor(type);
if (convertor != null)
{
return convertor.fromJSON(map);
}
return map;
}
/**
* Register a {@link Convertor} for a class or interface.
*
* @param forClass The class or interface that the convertor applies to
* @param convertor the convertor
*/
public void addConvertor(Class forClass, Convertor convertor)
{
_convertors.put(forClass.getName(), convertor);
}
/**
* Lookup a convertor for a class.
* <p>
* If no match is found for the class, then the interfaces for the class are
* tried. If still no match is found, then the super class and it's
* interfaces are tried recursively.
*
* @param forClass The class
* @return a {@link JSON.Convertor} or null if none were found.
*/
protected Convertor getConvertor(Class forClass)
{
Class cls = forClass;
Convertor convertor = _convertors.get(cls.getName());
if (convertor == null && this != DEFAULT)
convertor = DEFAULT.getConvertor(cls);
while (convertor == null && cls != Object.class)
{
Class[] ifs = cls.getInterfaces();
int i = 0;
while (convertor == null && i < ifs.length)
{
convertor = _convertors.get(ifs[i++].getName());
}
if (convertor == null)
{
cls = cls.getSuperclass();
convertor = _convertors.get(cls.getName());
}
}
return convertor;
}
/**
* Register a {@link JSON.Convertor} for a named class or interface.
*
* @param name name of a class or an interface that the convertor applies to
* @param convertor the convertor
*/
public void addConvertorFor(String name, Convertor convertor)
{
_convertors.put(name, convertor);
}
/**
* Removes a registered {@link JSON.Convertor} for the given named class or interface.
*
* @param name name of a class or an interface for a registered {@link JSON.Convertor}
* @return the {@link JSON.Convertor} that was removed, or null
*/
public Convertor removeConvertorFor(String name)
{
return _convertors.remove(name);
}
/**
* @return the function to customize the Java representation of JSON arrays
* @see #setArrayConverter(Function)
*/
public Function<List<?>, Object> getArrayConverter()
{
return _arrayConverter;
}
/**
* <p>Sets the function to convert JSON arrays from their default Java
* representation, a {@code List<Object>}, to another Java data structure
* such as an {@code Object[]}.</p>
*
* @param arrayConverter the function to customize the Java representation of JSON arrays
* @see #getArrayConverter()
*/
public void setArrayConverter(Function<List<?>, Object> arrayConverter)
{
_arrayConverter = Objects.requireNonNull(arrayConverter);
}
/**
* Lookup a convertor for a named class.
*
* @param name name of the class
* @return a {@link JSON.Convertor} or null if none were found.
*/
public Convertor getConvertorFor(String name)
{
Convertor convertor = _convertors.get(name);
if (convertor == null && this != DEFAULT)
convertor = DEFAULT.getConvertorFor(name);
return convertor;
}
public Object parse(Source source, boolean stripOuterComment)
{
int commentState = 0; // 0=no comment, 1="/", 2="/*", 3="/* *" -1="//"
if (!stripOuterComment)
return parse(source);
int stripState = 1; // 0=no strip, 1=wait for /*, 2= wait for */
Object o = null;
while (source.hasNext())
{
char c = source.peek();
// handle // or /* comment
if (commentState == 1)
{
switch (c)
{
case '/':
commentState = -1;
break;
case '*':
commentState = 2;
if (stripState == 1)
{
commentState = 0;
stripState = 2;
}
}
}
// handle comment
else if (commentState > 1)
{
switch (c)
{
case '*':
commentState = 3;
break;
case '/':
if (commentState == 3)
{
commentState = 0;
if (stripState == 2)
return o;
}
else
commentState = 2;
break;
default:
commentState = 2;
}
}
// handle // comment
else if (commentState < 0)
{
switch (c)
{
case '\r':
case '\n':
commentState = 0;
break;
default:
break;
}
}
// handle unknown
else
{
if (!Character.isWhitespace(c))
{
if (c == '/')
commentState = 1;
else if (c == '*')
commentState = 3;
else if (o == null)
{
o = parse(source);
continue;
}
}
}
source.next();
}
return o;
}
public Object parse(Source source)
{
int commentState = 0; // 0=no comment, 1="/", 2="/*", 3="/* *" -1="//"
while (source.hasNext())
{
char c = source.peek();
// handle // or /* comment
if (commentState == 1)
{
switch (c)
{
case '/':
commentState = -1;
break;
case '*':
commentState = 2;
}
}
// handle comment
else if (commentState > 1)
{
switch (c)
{
case '*':
commentState = 3;
break;
case '/':
if (commentState == 3)
commentState = 0;
else
commentState = 2;
break;
default:
commentState = 2;
}
}
// handle // comment
else if (commentState < 0)
{
switch (c)
{
case '\r':
case '\n':
commentState = 0;
break;
default:
break;
}
}
// handle unknown
else
{
switch (c)
{
case '{':
return parseObject(source);
case '[':
return parseArray(source);
case '"':
return parseString(source);
case '-':
return parseNumber(source);
case 'n':
complete("null", source);
return null;
case 't':
complete("true", source);
return Boolean.TRUE;
case 'f':
complete("false", source);
return Boolean.FALSE;
case 'u':
complete("undefined", source);
return null;
case 'N':
complete("NaN", source);
return null;
case '/':
commentState = 1;
break;
default:
if (Character.isDigit(c))
return parseNumber(source);
else if (Character.isWhitespace(c))