-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathTreeTraversingParser.java
420 lines (363 loc) · 11.3 KB
/
TreeTraversingParser.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
package com.fasterxml.jackson.databind.node;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.base.ParserMinimalBase;
import com.fasterxml.jackson.core.util.JacksonFeatureSet;
import com.fasterxml.jackson.databind.JsonNode;
/**
* Facade over {@link JsonNode} that implements {@link JsonParser} to allow
* accessing contents of JSON tree in alternate form (stream of tokens).
* Useful when a streaming source is expected by code, such as data binding
* functionality.
*/
public class TreeTraversingParser extends ParserMinimalBase
{
/*
/**********************************************************
/* Configuration
/**********************************************************
*/
protected ObjectCodec _objectCodec;
/**
* Traversal context within tree
*/
protected NodeCursor _nodeCursor;
/*
/**********************************************************
/* State
/**********************************************************
*/
/**
* Flag that indicates whether parser is closed or not. Gets
* set when parser is either closed by explicit call
* ({@link #close}) or when end-of-input is reached.
*/
protected boolean _closed;
/*
/**********************************************************
/* Life-cycle
/**********************************************************
*/
public TreeTraversingParser(JsonNode n) { this(n, null); }
public TreeTraversingParser(JsonNode n, ObjectCodec codec)
{
super(0);
_objectCodec = codec;
_nodeCursor = new NodeCursor.RootCursor(n, null);
}
@Override
public void setCodec(ObjectCodec c) {
_objectCodec = c;
}
@Override
public ObjectCodec getCodec() {
return _objectCodec;
}
@Override
public Version version() {
return com.fasterxml.jackson.databind.cfg.PackageVersion.VERSION;
}
@Override
public JacksonFeatureSet<StreamReadCapability> getReadCapabilities() {
// Defaults are fine
return DEFAULT_READ_CAPABILITIES;
}
/*
/**********************************************************
/* Closeable implementation
/**********************************************************
*/
@Override
public void close() throws IOException
{
if (!_closed) {
_closed = true;
_nodeCursor = null;
_currToken = null;
}
}
/*
/**********************************************************
/* Public API, traversal
/**********************************************************
*/
@Override
public JsonToken nextToken() throws IOException
{
_currToken = _nodeCursor.nextToken();
if (_currToken == null) {
_closed = true; // if not already set
return null;
}
switch (_currToken) {
case START_OBJECT:
_nodeCursor = _nodeCursor.startObject();
break;
case START_ARRAY:
_nodeCursor = _nodeCursor.startArray();
break;
case END_OBJECT:
case END_ARRAY:
_nodeCursor = _nodeCursor.getParent();
default:
}
return _currToken;
}
// default works well here:
//public JsonToken nextValue() throws IOException
@Override
public JsonParser skipChildren() throws IOException
{
if (_currToken == JsonToken.START_OBJECT) {
_nodeCursor = _nodeCursor.getParent();
_currToken = JsonToken.END_OBJECT;
} else if (_currToken == JsonToken.START_ARRAY) {
_nodeCursor = _nodeCursor.getParent();
_currToken = JsonToken.END_ARRAY;
}
return this;
}
@Override
public boolean isClosed() {
return _closed;
}
/*
/**********************************************************
/* Public API, token accessors
/**********************************************************
*/
@Override
public String currentName() {
NodeCursor crsr = _nodeCursor;
if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
crsr = crsr.getParent();
}
return (crsr == null) ? null : crsr.getCurrentName();
}
@Deprecated // since 2.17
@Override
public String getCurrentName() { return currentName(); }
@Override public void overrideCurrentName(String name) {
NodeCursor crsr = _nodeCursor;
if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) {
crsr = crsr.getParent();
}
if (crsr != null) {
crsr.overrideCurrentName(name);
}
}
@Override
public JsonStreamContext getParsingContext() {
return _nodeCursor;
}
@Override
public JsonLocation currentLocation() {
return JsonLocation.NA;
}
@Override
public JsonLocation currentTokenLocation() {
return JsonLocation.NA;
}
@Deprecated // since 2.17
@Override
public JsonLocation getTokenLocation() { return currentTokenLocation(); }
@Deprecated // since 2.17
@Override
public JsonLocation getCurrentLocation() { return currentLocation(); }
/*
/**********************************************************
/* Public API, access to textual content
/**********************************************************
*/
@Override
public String getText()
{
if (_currToken == null) {
return null;
}
// need to separate handling a bit...
switch (_currToken) {
case FIELD_NAME:
return _nodeCursor.getCurrentName();
case VALUE_STRING:
return currentNode().textValue();
case VALUE_NUMBER_INT:
case VALUE_NUMBER_FLOAT:
return String.valueOf(currentNode().numberValue());
case VALUE_EMBEDDED_OBJECT:
JsonNode n = currentNode();
if (n != null && n.isBinary()) {
// this will convert it to base64
return n.asText();
}
default:
return _currToken.asString();
}
}
@Override
public char[] getTextCharacters() throws IOException {
return getText().toCharArray();
}
@Override
public int getTextLength() throws IOException {
return getText().length();
}
@Override
public int getTextOffset() throws IOException {
return 0;
}
@Override
public boolean hasTextCharacters() {
// generally we do not have efficient access as char[], hence:
return false;
}
/*
/**********************************************************
/* Public API, typed non-text access
/**********************************************************
*/
//public byte getByteValue() throws IOException
@Override
public NumberType getNumberType() throws IOException {
JsonNode n = currentNumericNode();
return (n == null) ? null : n.numberType();
}
@Override
public NumberTypeFP getNumberTypeFP() throws IOException {
NumberType nt = getNumberType();
if (nt == NumberType.BIG_DECIMAL) {
return NumberTypeFP.BIG_DECIMAL;
}
if (nt == NumberType.DOUBLE) {
return NumberTypeFP.DOUBLE64;
}
if (nt == NumberType.FLOAT) {
return NumberTypeFP.FLOAT32;
}
return NumberTypeFP.UNKNOWN;
}
@Override
public BigInteger getBigIntegerValue() throws IOException
{
return currentNumericNode().bigIntegerValue();
}
@Override
public BigDecimal getDecimalValue() throws IOException {
return currentNumericNode().decimalValue();
}
@Override
public double getDoubleValue() throws IOException {
return currentNumericNode().doubleValue();
}
@Override
public float getFloatValue() throws IOException {
return (float) currentNumericNode().doubleValue();
}
@Override
public int getIntValue() throws IOException {
final NumericNode node = (NumericNode) currentNumericNode();
if (!node.canConvertToInt()) {
reportOverflowInt();
}
return node.intValue();
}
@Override
public long getLongValue() throws IOException {
final NumericNode node = (NumericNode) currentNumericNode();
if (!node.canConvertToLong()) {
reportOverflowLong();
}
return node.longValue();
}
@Override
public Number getNumberValue() throws IOException {
return currentNumericNode().numberValue();
}
@Override
public Object getEmbeddedObject()
{
if (!_closed) {
JsonNode n = currentNode();
if (n != null) {
if (n.isPojo()) {
return ((POJONode) n).getPojo();
}
if (n.isBinary()) {
return ((BinaryNode) n).binaryValue();
}
}
}
return null;
}
@Override
public boolean isNaN() {
if (!_closed) {
JsonNode n = currentNode();
if (n instanceof NumericNode) {
return ((NumericNode) n).isNaN();
}
}
return false;
}
/*
/**********************************************************
/* Public API, typed binary (base64) access
/**********************************************************
*/
@Override
public byte[] getBinaryValue(Base64Variant b64variant)
throws IOException
{
// Multiple possibilities...
JsonNode n = currentNode();
if (n != null) {
// [databind#2096]: although `binaryValue()` works for real binary node
// and embedded "POJO" node, coercion from TextNode may require variant, so:
if (n instanceof TextNode) {
return ((TextNode) n).getBinaryValue(b64variant);
}
return n.binaryValue();
}
// otherwise return null to mark we have no binary content
return null;
}
@Override
public int readBinaryValue(Base64Variant b64variant, OutputStream out)
throws IOException
{
byte[] data = getBinaryValue(b64variant);
if (data != null) {
out.write(data, 0, data.length);
return data.length;
}
return 0;
}
/*
/**********************************************************
/* Internal methods
/**********************************************************
*/
protected JsonNode currentNode() {
if (_closed || _nodeCursor == null) {
return null;
}
return _nodeCursor.currentNode();
}
protected JsonNode currentNumericNode()
throws JacksonException
{
JsonNode n = currentNode();
if (n == null || !n.isNumber()) {
JsonToken t = (n == null) ? null : n.asToken();
throw _constructError("Current token ("+t+") not numeric, cannot use numeric value accessors");
}
return n;
}
@Override
protected void _handleEOF() {
_throwInternal(); // should never get called
}
}