forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenBuffer.java
More file actions
1235 lines (1085 loc) · 39.7 KB
/
TokenBuffer.java
File metadata and controls
1235 lines (1085 loc) · 39.7 KB
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
package org.codehaus.jackson.util;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.codehaus.jackson.*;
import org.codehaus.jackson.impl.JsonReadContext;
import org.codehaus.jackson.impl.JsonWriteContext;
/**
* Utility class used for efficient storage of {@link JsonToken}
* sequences, needed for temporary buffering.
* Space efficient for different sequence lengths (especially so for smaller
* ones; but not significantly less efficient for larger), highly efficient
* for linear iteration and appending. Implemented as segmented/chunked
* linked list of tokens; only modifications are via appends.
*
* @since 1.5
*/
public class TokenBuffer
/* Won't use JsonGeneratorBase, to minimize overhead for validity
* checking
*/
extends JsonGenerator
{
final static int DEFAULT_FEATURES = JsonParser.Feature.collectDefaults();
/*
***********************************************
* Configuration
***********************************************
*/
/**
* Object codec to use for stream-based object
* conversion through parser/generator interfaces. If null,
* such methods can not be used.
*/
protected ObjectCodec _objectCodec;
/**
* Bit flag composed of bits that indicate which
* {@link org.codehaus.jackson.JsonGenerator.Feature}s
* are enabled.
*<p>
* NOTE: most features have no effect on this class
*/
protected int _generatorFeatures;
protected boolean _closed;
/*
***********************************************
* Token buffering state
***********************************************
*/
/**
* First segment, for contents this buffer has
*/
protected Segment _first;
/**
* Last segment of this buffer, one that is used
* for appending more tokens
*/
protected Segment _last;
/**
* Offset within last segment,
*/
protected int _appendOffset;
/*
***********************************************
* Output state
***********************************************
*/
protected JsonWriteContext _writeContext;
/*
***********************************************
* Life-cycle
***********************************************
*/
/**
* @param codec Object codec to use for stream-based object
* conversion through parser/generator interfaces. If null,
* such methods can not be used.
*/
public TokenBuffer(ObjectCodec codec)
{
_objectCodec = codec;
_generatorFeatures = DEFAULT_FEATURES;
_writeContext = JsonWriteContext.createRootContext();
// at first we have just one segment
_first = _last = new Segment();
_appendOffset = 0;
}
/**
* Method used to create a {@link JsonParser} that can read contents
* stored in this buffer. Will use default <code>_objectCodec</code> for
* object conversions.
*<p>
* Note: instances are not synchronized, that is, they are not thread-safe
* if there are concurrent appends to the underlying buffer.
*
* @return Parser that can be used for reading contents stored in this buffer
*/
public JsonParser asParser()
{
return asParser(_objectCodec);
}
/**
* Method used to create a {@link JsonParser} that can read contents
* stored in this buffer.
*<p>
* Note: instances are not synchronized, that is, they are not thread-safe
* if there are concurrent appends to the underlying buffer.
*
* @param codec Object codec to use for stream-based object
* conversion through parser/generator interfaces. If null,
* such methods can not be used.
*
* @return Parser that can be used for reading contents stored in this buffer
*/
public JsonParser asParser(ObjectCodec codec)
{
return new Parser(_first, codec);
}
/**
* @param src Parser to use for accessing source information
* like location, configured codec
*/
public JsonParser asParser(JsonParser src)
{
Parser p = new Parser(_first, src.getCodec());
p.setLocation(src.getTokenLocation());
return p;
}
/*
*****************************************************************
* Other custom methods not needed for implementing interfaces
*****************************************************************
*/
/**
* Helper method that will write all contents of this buffer
* using given {@link JsonGenerator}.
*<p>
* Note: this method would be enough to implement
* <code>JsonSerializer</code> for <code>TokenBuffer</code> type;
* but we can not have upwards
* references (from core to mapper package); and as such we also
* can not take second argument.
*/
public void serialize(JsonGenerator jgen)
throws IOException, JsonGenerationException
{
Segment segment = _first;
int ptr = -1;
while (true) {
if (++ptr >= Segment.TOKENS_PER_SEGMENT) {
ptr = 0;
segment = segment.next();
if (segment == null) break;
}
JsonToken t = segment.type(ptr);
if (t == null) break;
// Note: copied from 'copyCurrentEvent'...
switch (t) {
case START_OBJECT:
jgen.writeStartObject();
break;
case END_OBJECT:
jgen.writeEndObject();
break;
case START_ARRAY:
jgen.writeStartArray();
break;
case END_ARRAY:
jgen.writeEndArray();
break;
case FIELD_NAME:
jgen.writeFieldName((String) segment.get(ptr));
break;
case VALUE_STRING:
jgen.writeString((String) segment.get(ptr));
break;
case VALUE_NUMBER_INT:
{
Number n = (Number) segment.get(ptr);
if (n instanceof BigInteger) {
jgen.writeNumber((BigInteger) n);
} else if (n instanceof Long) {
jgen.writeNumber(n.longValue());
} else {
jgen.writeNumber(n.intValue());
}
}
break;
case VALUE_NUMBER_FLOAT:
{
Number n = (Number) segment.get(ptr);
if (n instanceof BigDecimal) {
jgen.writeNumber((BigDecimal) n);
} else if (n instanceof Float) {
jgen.writeNumber(n.floatValue());
} else {
jgen.writeNumber(n.doubleValue());
}
}
break;
case VALUE_TRUE:
jgen.writeBoolean(true);
break;
case VALUE_FALSE:
jgen.writeBoolean(false);
break;
case VALUE_NULL:
jgen.writeNull();
break;
case VALUE_EMBEDDED_OBJECT:
jgen.writeObject(segment.get(ptr));
break;
default:
throw new RuntimeException("Internal error: should never end up through this code path");
}
}
}
@Override
public String toString()
{
// Let's print up to 100 first tokens...
final int MAX_COUNT = 100;
StringBuilder sb = new StringBuilder();
sb.append("[TokenBuffer: ");
JsonParser jp = asParser();
int count = 0;
while (true) {
JsonToken t;
try {
t = jp.nextToken();
} catch (IOException ioe) { // should never occur
throw new IllegalStateException(ioe);
}
if (t == null) break;
if (count < MAX_COUNT) {
if (count > 0) {
sb.append(", ");
}
sb.append(t.toString());
}
++count;
}
if (count >= MAX_COUNT) {
sb.append(" ... (truncated ").append(count-MAX_COUNT).append(" entries)");
}
sb.append(']');
return sb.toString();
}
/*
*****************************************************************
* JsonGenerator implementation: configuration
*****************************************************************
*/
@Override
public JsonGenerator enable(Feature f) {
_generatorFeatures |= f.getMask();
return this;
}
@Override
public JsonGenerator disable(Feature f) {
_generatorFeatures &= ~f.getMask();
return this;
}
//public JsonGenerator configure(Feature f, boolean state) { }
@Override
public boolean isEnabled(Feature f) {
return (_generatorFeatures & f.getMask()) != 0;
}
public JsonGenerator useDefaultPrettyPrinter() {
// No-op: we don't indent
return this;
}
public JsonGenerator setCodec(ObjectCodec oc) {
_objectCodec = oc;
return this;
}
public ObjectCodec getCodec() { return _objectCodec; }
public final JsonWriteContext getOutputContext() { return _writeContext; }
/*
*****************************************************************
* JsonGenerator implementation: low-level output handling
*****************************************************************
*/
public void flush() throws IOException { /* NOP */ }
public void close() throws IOException {
_closed = true;
}
public boolean isClosed() { return _closed; }
/*
*****************************************************************
* JsonGenerator implementation: write methods, structural
*****************************************************************
*/
@Override
public final void writeStartArray()
throws IOException, JsonGenerationException
{
_append(JsonToken.START_ARRAY);
_writeContext = _writeContext.createChildArrayContext();
}
@Override
public final void writeEndArray()
throws IOException, JsonGenerationException
{
_append(JsonToken.END_ARRAY);
// Let's allow unbalanced tho... i.e. not run out of root level, ever
JsonWriteContext c = _writeContext.getParent();
if (c != null) {
_writeContext = c;
}
}
@Override
public final void writeStartObject()
throws IOException, JsonGenerationException
{
_append(JsonToken.START_OBJECT);
_writeContext = _writeContext.createChildObjectContext();
}
public final void writeEndObject()
throws IOException, JsonGenerationException
{
_append(JsonToken.END_OBJECT);
// Let's allow unbalanced tho... i.e. not run out of root level, ever
JsonWriteContext c = _writeContext.getParent();
if (c != null) {
_writeContext = c;
}
}
public final void writeFieldName(String name)
throws IOException, JsonGenerationException
{
_append(JsonToken.FIELD_NAME, name);
_writeContext.writeFieldName(name);
}
/*
*****************************************************************
* JsonGenerator implementation: write methods, textual
*****************************************************************
*/
@Override
public void writeString(String text) throws IOException,JsonGenerationException {
if (text == null) {
writeNull();
} else {
_append(JsonToken.VALUE_STRING, text);
}
}
@Override
public void writeString(char[] text, int offset, int len) throws IOException, JsonGenerationException {
writeString(new String(text, offset, len));
}
@Override
public void writeRaw(String text) throws IOException, JsonGenerationException {
_reportUnsupportedOperation();
}
@Override
public void writeRaw(String text, int offset, int len) throws IOException, JsonGenerationException {
_reportUnsupportedOperation();
}
@Override
public void writeRaw(char[] text, int offset, int len) throws IOException, JsonGenerationException {
_reportUnsupportedOperation();
}
@Override
public void writeRaw(char c) throws IOException, JsonGenerationException {
_reportUnsupportedOperation();
}
@Override
public void writeRawValue(String text) throws IOException, JsonGenerationException {
_reportUnsupportedOperation();
}
@Override
public void writeRawValue(String text, int offset, int len) throws IOException, JsonGenerationException {
_reportUnsupportedOperation();
}
@Override
public void writeRawValue(char[] text, int offset, int len) throws IOException, JsonGenerationException {
_reportUnsupportedOperation();
}
/*
*****************************************************************
* JsonGenerator implementation: write methods, primitive types
*****************************************************************
*/
@Override
public void writeNumber(int i) throws IOException, JsonGenerationException {
_append(JsonToken.VALUE_NUMBER_INT, Integer.valueOf(i));
}
@Override
public void writeNumber(long l) throws IOException, JsonGenerationException {
_append(JsonToken.VALUE_NUMBER_INT, Long.valueOf(l));
}
@Override
public void writeNumber(double d) throws IOException,JsonGenerationException {
_append(JsonToken.VALUE_NUMBER_FLOAT, Double.valueOf(d));
}
@Override
public void writeNumber(float f) throws IOException, JsonGenerationException {
_append(JsonToken.VALUE_NUMBER_FLOAT, Float.valueOf(f));
}
@Override
public void writeNumber(BigDecimal dec) throws IOException,JsonGenerationException {
if (dec == null) {
writeNull();
} else {
_append(JsonToken.VALUE_NUMBER_FLOAT, dec);
}
}
@Override
public void writeNumber(BigInteger v) throws IOException, JsonGenerationException {
if (v == null) {
writeNull();
} else {
_append(JsonToken.VALUE_NUMBER_INT, v);
}
}
@Override
public void writeNumber(String encodedValue) throws IOException, JsonGenerationException {
/* Hmmh... let's actually support this as regular String value write:
* should work since there is no quoting when buffering
*/
writeString(encodedValue);
}
@Override
public void writeBoolean(boolean state) throws IOException,JsonGenerationException {
_append(state ? JsonToken.VALUE_TRUE : JsonToken.VALUE_FALSE);
}
@Override
public void writeNull() throws IOException, JsonGenerationException {
_append(JsonToken.VALUE_NULL);
}
/*
*****************************************************************
* JsonGenerator implementation: write methods for POJOs/trees
*****************************************************************
*/
public void writeObject(Object value)
throws IOException, JsonProcessingException
{
// embedded means that no conversions should be done...
_append(JsonToken.VALUE_EMBEDDED_OBJECT, value);
}
public void writeTree(JsonNode rootNode)
throws IOException, JsonProcessingException
{
/* 31-Dec-2009, tatu: no need to convert trees either is there?
* (note: may need to re-evaluate at some point)
*/
_append(JsonToken.VALUE_EMBEDDED_OBJECT, rootNode);
}
/*
*****************************************************************
* JsonGenerator implementation; binary
*****************************************************************
*/
@Override
public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int len)
throws IOException, JsonGenerationException
{
/* 31-Dec-2009, tatu: can do this using multiple alternatives; but for
* now, let's try to limit number of conversions.
* The only (?) tricky thing is that of whether to preserve variant,
* seems pointless, so let's not worry about it unless there's some
* compelling reason to.
*/
byte[] copy = new byte[len];
System.arraycopy(data, offset, copy, 0, len);
writeObject(copy);
}
/*
*****************************************************************
* JsonGenerator implementation; pass-through copy
*****************************************************************
*/
@Override
public void copyCurrentEvent(JsonParser jp) throws IOException, JsonProcessingException
{
switch (jp.getCurrentToken()) {
case START_OBJECT:
writeStartObject();
break;
case END_OBJECT:
writeEndObject();
break;
case START_ARRAY:
writeStartArray();
break;
case END_ARRAY:
writeEndArray();
break;
case FIELD_NAME:
writeFieldName(jp.getCurrentName());
break;
case VALUE_STRING:
writeString(jp.getTextCharacters(), jp.getTextOffset(), jp.getTextLength());
break;
case VALUE_NUMBER_INT:
switch (jp.getNumberType()) {
case INT:
writeNumber(jp.getIntValue());
break;
case BIG_INTEGER:
writeNumber(jp.getBigIntegerValue());
break;
default:
writeNumber(jp.getLongValue());
}
break;
case VALUE_NUMBER_FLOAT:
switch (jp.getNumberType()) {
case BIG_DECIMAL:
writeNumber(jp.getDecimalValue());
break;
case FLOAT:
writeNumber(jp.getFloatValue());
break;
default:
writeNumber(jp.getDoubleValue());
}
break;
case VALUE_TRUE:
writeBoolean(true);
break;
case VALUE_FALSE:
writeBoolean(false);
break;
case VALUE_NULL:
writeNull();
break;
case VALUE_EMBEDDED_OBJECT:
writeObject(jp.getEmbeddedObject());
break;
default:
throw new RuntimeException("Internal error: should never end up through this code path");
}
}
@Override
public void copyCurrentStructure(JsonParser jp) throws IOException, JsonProcessingException {
JsonToken t = jp.getCurrentToken();
// Let's handle field-name separately first
if (t == JsonToken.FIELD_NAME) {
writeFieldName(jp.getCurrentName());
t = jp.nextToken();
// fall-through to copy the associated value
}
switch (t) {
case START_ARRAY:
writeStartArray();
while (jp.nextToken() != JsonToken.END_ARRAY) {
copyCurrentStructure(jp);
}
writeEndArray();
break;
case START_OBJECT:
writeStartObject();
while (jp.nextToken() != JsonToken.END_OBJECT) {
copyCurrentStructure(jp);
}
writeEndObject();
break;
default: // others are simple:
copyCurrentEvent(jp);
}
}
/*
*****************************************************************
* Internal methods
*****************************************************************
*/
protected final void _append(JsonToken type) {
Segment next = _last.append(_appendOffset, type);
if (next == null) {
++_appendOffset;
} else {
_last = next;
_appendOffset = 1; // since we added first at 0
}
}
protected final void _append(JsonToken type, Object value) {
Segment next = _last.append(_appendOffset, type, value);
if (next == null) {
++_appendOffset;
} else {
_last = next;
_appendOffset = 1;
}
}
protected void _reportUnsupportedOperation() {
throw new UnsupportedOperationException("Called operation not supported for TokenBuffer");
}
/*
*****************************************************************
* Supporting classes
*****************************************************************
*/
protected final static class Parser
extends JsonParser
{
protected ObjectCodec _codec;
/*
////////////////////////////////////////////////////
// Parsing state
////////////////////////////////////////////////////
*/
/**
* Currently active segment
*/
protected Segment _segment;
/**
* Pointer to current token within current segment
*/
protected int _segmentPtr;
/**
* Information about parser context, context in which
* the next token is to be parsed (root, array, object).
*/
protected JsonReadContext _parsingContext;
protected boolean _closed;
protected transient ByteArrayBuilder _byteBuilder;
protected JsonLocation _location = null;
/*
////////////////////////////////////////////////////
// Construction, init
////////////////////////////////////////////////////
*/
public Parser(Segment firstSeg, ObjectCodec codec) {
_segment = firstSeg;
_segmentPtr = -1; // not yet read
_codec = codec;
_parsingContext = JsonReadContext.createRootContext(-1, -1);
}
public void setLocation(JsonLocation l) {
_location = l;
}
@Override
public ObjectCodec getCodec() { return _codec; }
@Override
public void setCodec(ObjectCodec c) { _codec = c; }
/*
////////////////////////////////////////////////////
// Closeable implementation
////////////////////////////////////////////////////
*/
@Override
public void close() throws IOException {
if (!_closed) {
_closed = true;
}
}
/*
////////////////////////////////////////////////////
// Public API, traversal
////////////////////////////////////////////////////
*/
@Override
public JsonToken nextToken() throws IOException, JsonParseException
{
// If we are closed, nothing more to do
if (_closed || (_segment == null)) return null;
// Ok, then: any more tokens?
if (++_segmentPtr >= Segment.TOKENS_PER_SEGMENT) {
_segmentPtr = 0;
_segment = _segment.next();
if (_segment == null) {
return null;
}
}
_currToken = _segment.type(_segmentPtr);
// Field name? Need to update context
if (_currToken == JsonToken.FIELD_NAME) {
_parsingContext.setCurrentName((String) _currentObject());
} else if (_currToken == JsonToken.START_OBJECT) {
_parsingContext = _parsingContext.createChildObjectContext(-1, -1);
} else if (_currToken == JsonToken.START_ARRAY) {
_parsingContext = _parsingContext.createChildArrayContext(-1, -1);
} else if (_currToken == JsonToken.END_OBJECT
|| _currToken == JsonToken.END_ARRAY) {
// Closing JSON Object/Array? Close matching context
_parsingContext = _parsingContext.getParent();
// but allow unbalanced cases too (more close markers)
if (_parsingContext == null) {
_parsingContext = JsonReadContext.createRootContext(-1, -1);
}
}
return _currToken;
}
@Override
public JsonParser skipChildren() throws IOException, JsonParseException
{
if (_currToken != JsonToken.START_OBJECT && _currToken != JsonToken.START_ARRAY) {
return this;
}
int open = 1;
/* Since proper matching of start/end markers is handled
* by nextToken(), we'll just count nesting levels here
*/
while (true) {
JsonToken t = nextToken();
if (t == null) {
// error for most parsers, but ok here
return this;
}
switch (t) {
case START_OBJECT:
case START_ARRAY:
++open;
break;
case END_OBJECT:
case END_ARRAY:
if (--open == 0) {
return this;
}
break;
}
}
}
@Override
public boolean isClosed() { return _closed; }
/*
////////////////////////////////////////////////////
// Public API, token accessors
////////////////////////////////////////////////////
*/
@Override
public JsonStreamContext getParsingContext() { return _parsingContext; }
@Override
public JsonLocation getTokenLocation() { return getCurrentLocation(); }
@Override
public JsonLocation getCurrentLocation() {
return (_location == null) ? JsonLocation.NA : _location;
}
@Override
public String getCurrentName() { return _parsingContext.getCurrentName(); }
/*
////////////////////////////////////////////////////
// Public API, access to token information, text
////////////////////////////////////////////////////
*/
@Override
public String getText()
{
if (_currToken != null) { // null only before/after document
switch (_currToken) {
case FIELD_NAME:
case VALUE_STRING:
return (String) _currentObject();
// fall through
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
Object ob = _currentObject();
return (ob == null) ? null : ob.toString();
default:
return _currToken.asString();
}
}
return null;
}
@Override
public char[] getTextCharacters() {
String str = getText();
return (str == null) ? null : str.toCharArray();
}
@Override
public int getTextLength() {
String str = getText();
return (str == null) ? 0 : str.length();
}
@Override
public int getTextOffset() { return 0; }
/*
////////////////////////////////////////////////////
// Public API, access to token information, numeric
////////////////////////////////////////////////////
*/
@Override
public BigInteger getBigIntegerValue() throws IOException, JsonParseException
{
Number n = getNumberValue();
if (n instanceof BigInteger) {
return (BigInteger) n;
}
switch (getNumberType()) {
case BIG_DECIMAL:
return ((BigDecimal) n).toBigInteger();
}
// int/long is simple, but let's also just truncate float/double:
return BigInteger.valueOf(n.longValue());
}
@Override
public BigDecimal getDecimalValue() throws IOException, JsonParseException
{
Number n = getNumberValue();
if (n instanceof BigDecimal) {
return (BigDecimal) n;
}
switch (getNumberType()) {
case INT:
case LONG:
return BigDecimal.valueOf(n.longValue());
case BIG_INTEGER:
return new BigDecimal((BigInteger) n);
}
// float or double
return BigDecimal.valueOf(n.doubleValue());
}
@Override
public double getDoubleValue() throws IOException, JsonParseException {
return getNumberValue().doubleValue();
}
@Override
public float getFloatValue() throws IOException, JsonParseException {
return getNumberValue().floatValue();
}
@Override
public int getIntValue() throws IOException, JsonParseException {
return getNumberValue().intValue();
}
@Override
public long getLongValue() throws IOException, JsonParseException {
return getNumberValue().longValue();
}
@Override
public NumberType getNumberType() throws IOException, JsonParseException
{
Number n = getNumberValue();
if (n instanceof Integer) return NumberType.INT;
if (n instanceof Long) return NumberType.LONG;
if (n instanceof Double) return NumberType.DOUBLE;
if (n instanceof BigDecimal) return NumberType.BIG_DECIMAL;
if (n instanceof Float) return NumberType.FLOAT;
if (n instanceof BigInteger) return NumberType.BIG_INTEGER;
return null;
}
@Override
public final Number getNumberValue() throws IOException, JsonParseException {
_checkIsNumber();
return (Number) _currentObject();
}
/*
////////////////////////////////////////////////////
// Public API, access to token information, other
////////////////////////////////////////////////////
*/
private final static int INT_SPACE = 0x0020;
public Object getEmbeddedObject()
{
if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
return _currentObject();
}
return null;
}
@Override
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException, JsonParseException
{
// First: maybe we some special types?
if (_currToken == JsonToken.VALUE_EMBEDDED_OBJECT) {
// Embedded byte array would work nicely...
Object ob = _currentObject();
if (ob instanceof byte[]) {
return (byte[]) ob;
}
// fall through to error case
}
if (_currToken != JsonToken.VALUE_STRING) {
throw _constructError("Current token ("+_currToken+") not VALUE_STRING (or VALUE_EMBEDDED_OBJECT with byte[]), can not access as binary");
}
final String str = getText();
if (str == null) {
return null;
}
ByteArrayBuilder builder = _byteBuilder;
if (builder == null) {
_byteBuilder = builder = new ByteArrayBuilder(100);
}
_decodeBase64(str, builder, b64variant);
return builder.toByteArray();
}