-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathJsonNode.java
More file actions
429 lines (374 loc) · 14.5 KB
/
JsonNode.java
File metadata and controls
429 lines (374 loc) · 14.5 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
package org.codehaus.jackson;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
/**
* Base class for all JSON nodes, which form the basis of JSON
* Tree Model that Jackson implements.
* One way to think of these nodes is to considere them
* similar to DOM nodes in XML DOM trees.
*<p>
* As a general design rule, most accessors ("getters") are included
* in this base class, to allow for traversing structure without
* type casts. Most mutators, however, need to be accessed through
* specific sub-classes. This seems sensible because proper type
* information is generally available when building or modifying
* trees, but less often when reading a tree (newly built from
* parsed Json content).
*/
public abstract class JsonNode
implements Iterable<JsonNode>
{
final static List<JsonNode> NO_NODES = Collections.emptyList();
final static List<String> NO_STRINGS = Collections.emptyList();
protected JsonNode() { }
/*
////////////////////////////////////////////////////
// Public API, type introspection
////////////////////////////////////////////////////
*/
// // First high-level division between values, containers and "missing"
/**
* Method that returns true for all value nodes: ones that are not containers, and that do not represent "missing"
* nodes in the path.Such value nodes represent String, Number, Boolean and null values from JSON.<p>
* Note: one and only one of methods {@link #isValueNode},
* {@link #isContainerNode} and {@link #isMissingNode} ever returns true for any given node.
*
* @return Undocumented.
*/
public boolean isValueNode() { return false; }
/**
* Method that returns true for container nodes: Arrays and Objects.
*<p>
* Note: one and only one of methods {@link #isValueNode},
* {@link #isContainerNode} and {@link #isMissingNode} ever
* returns true for any given node.
* @return Undocumented.
*/
public boolean isContainerNode() { return false; }
/**
* Method that returns true for "virtual" nodes which represent
* missing entries constructed by path accessor methods when
* there is no actual node matching given criteria.
*<p>
* Note: one and only one of methods {@link #isValueNode},
* {@link #isContainerNode} and {@link #isMissingNode} ever
* returns true for any given node.
* @return Undocumented.
*/
public boolean isMissingNode() { return false; }
// // Then more specific type introspection
// // (along with defaults to be overridden)
/**
* @return True if this node represents Json Array
*/
public boolean isArray() { return false; }
/**
* @return True if this node represents Json Object
*/
public boolean isObject() { return false; }
/**
* Method that can be used to check if the node is a wrapper
* for a POJO ("Plain Old Java Object" aka "bean".
* Returns true only for
* instances of org.codehaus.jackson.node.POJONode.
*
* @return True if this node wraps a POJO
*/
public boolean isPojo() { return false; }
/**
* @return True if this node represents a numeric Json
* value
*/
public boolean isNumber() { return false; }
/**
* @return True if this node represents an integral (integer)
* numeric Json value
*/
public boolean isIntegralNumber() { return false; }
/**
* @return True if this node represents a non-integral
* numeric Json value
*/
public boolean isFloatingPointNumber() { return false; }
/**
* @return True if this node represents an integral
* numeric Json value that withs in Java int value space
*/
public boolean isInt() { return false; }
/**
* @return True if this node represents an integral
* numeric Json value that fits in Java long value space
* (but not int value space, i.e. {@link #isInt} returns false)
*/
public boolean isLong() { return false; }
public boolean isDouble() { return false; }
public boolean isBigDecimal() { return false; }
public boolean isBigInteger() { return false; }
public boolean isTextual() { return false; }
/**
* Method that can be used to check if this node was created from
* Json boolean value (literals "true" and "false").
* @return Undocumented.
*/
public boolean isBoolean() { return false; }
/**
* Method that can be used to check if this node was created from
* Json liternal null value.
* @return Undocumented.
*/
public boolean isNull() { return false; }
/**
* Method that can be used to check if this node represents
* binary data (Base64 encoded). Although this will be externally
* written as Json String value, {@link #isTextual} will
* return false if this method returns true.
*
* @return True if this node represents base64 encoded binary data
*/
public boolean isBinary() { return false; }
/**
* Method that can be used for efficient type detection when using stream abstraction for traversing nodes.Will
* return the first {@link JsonToken} that equivalent stream event would produce (for most nodes there is just one
* token but for structured/container types multiple)
*
* @return Undocumented.
* @since 1.3
*/
public abstract JsonToken asToken();
/**
* If this node is a numeric type (as per {@link #isNumber}),
* returns native type that node uses to store the numeric
* value.
* @return Undocumented.
*/
public abstract JsonParser.NumberType getNumberType();
/*
////////////////////////////////////////////////////
// Public API, value access
////////////////////////////////////////////////////
*/
/**
* Method to use for accessing String values.
* Does <b>NOT</b> do any conversions for non-String value nodes;
* for non-String values (ones for which {@link #isTextual} returns
* false) null will be returned.
* For String values, null is never returned (but empty Strings may be)
*
* @return Textual value this node contains, iff it is a textual
* json node (comes from Json String value entry)
*/
public String getTextValue() { return null; }
/**
* Method to use for accessing binary content of binary nodes (nodes
* for which {@link #isBinary} returns true); or for Text Nodes
* (ones for which {@link #getTextValue} returns non-null value),
* to read decoded base64 data.For other types of nodes, returns null.
*
* @return Binary data this node contains, iff it is a binary
* node; null otherwise
* @throws java.io.IOException Undocumented.
*/
public byte[] getBinaryValue() throws IOException
{
return null;
}
public boolean getBooleanValue() { return false; }
/**
* Returns numeric value for this node, <b>if and only if</b>
* this node is numeric ({@link #isNumber} returns true); otherwise
* returns null
*
* @return Number value this node contains, if any (null for non-number
* nodes).
*/
public Number getNumberValue() { return null; }
public int getIntValue() { return 0; }
public long getLongValue() { return 0L; }
public double getDoubleValue() { return 0.0; }
public BigDecimal getDecimalValue() { return BigDecimal.ZERO; }
public BigInteger getBigIntegerValue() { return BigInteger.ZERO; }
/**
* Method for accessing value of the specified element of
* an array node.For other nodes, null is always returned.<p>
* For array nodes, index specifies
* exact location within array and allows for efficient iteration
* over child elements (underlying storage is guaranteed to
* be efficiently indexable, i.e. has random-access to elements).
* If index is less than 0, or equal-or-greater than
* <code>node.size()</code>, null is returned; no exception is
* thrown for any index.
*
* @param index Undocumented.
* @return Node that represent value of the specified element,
* if this node is an array and has specified element.
* Null otherwise.
*/
public JsonNode get(int index) { return null; }
/**
* Method for accessing value of the specified field of an object node.If this node is not an object (or it does not
* have a value for specified field name), or if there is no field with such name, null is returned.
*
* @param fieldName Undocumented.
* @return Node that represent value of the specified field, if this node is an object and has value for the
* specified field. Null otherwise.
*/
public JsonNode get(String fieldName) { return null; }
/**
* Alias for {@link #get(String)}.
*
* @param fieldName Undocumented.
* @return Undocumented.
* @deprecated Use {@link #get(String)} instead.
*/
@Deprecated
public final JsonNode getFieldValue(String fieldName) { return get(fieldName); }
/**
* Alias for {@link #get(int)}.
*
* @param index Undocumented.
* @return Undocumented.
* @deprecated Use {@link #get(int)} instead.
*/
@Deprecated
public final JsonNode getElementValue(int index) { return get(index); }
/**
* Method that will return valid String representation of
* the container value, if the node is a value node
* (method {@link #isValueNode} returns true), otherwise null.
*<p>
* Note: to serialize nodes of any type, you should call
* {@link #toString} instead.
* @return Undocumented.
*/
public abstract String getValueAsText();
/*
////////////////////////////////////////////////////
// Public API, container access
////////////////////////////////////////////////////
*/
/**
* Method that returns number of child nodes this node contains:
* for Array nodes, number of child elements, for Object nodes,
* number of fields, and for all other nodes 0.
*
* @return For non-container nodes returns 0; for arrays number of
* contained elements, and for objects number of fields.
*/
public int size() { return 0; }
/**
* Same as calling {@link #getElements}; implemented so that
* convenience "for-each" loop can be used for looping over elements
* of Json Array constructs.
*/
@Override
public final Iterator<JsonNode> iterator() { return getElements(); }
/**
* Method for accessing all value nodes of this Node, iff this node is a Json Array or Object node.In case of Object
* node, field names (keys) are not included, only values. For other types of nodes, returns empty iterator.
*
* @return Undocumented.
*/
public Iterator<JsonNode> getElements() { return NO_NODES.iterator(); }
/**
* Method for accessing names of all fields for this Node, iff
* this node is a Json Object node.
* @return Undocumented.
*/
public Iterator<String> getFieldNames() { return NO_STRINGS.iterator(); }
/*
////////////////////////////////////////////////////
// Public API, path handling
////////////////////////////////////////////////////
*/
/**
* This method is similar to {@link #get(String)}, except that instead of returning null if no such value exists
* (due to this node not being an object, or object not having value for the specified field), a "missing node"
* (node that returns true for {@link #isMissingNode}) will be returned.This allows for convenient and safe chained
* access via path calls.
*
* @param fieldName Undocumented.
* @return Undocumented.
*/
public abstract JsonNode path(String fieldName);
/**
* Alias of {@link #path(String)}.
*
* @param fieldName Undocumented.
* @return Undocumented.
* @deprecated Use {@link #path(String)} instead
*/
@Deprecated
public final JsonNode getPath(String fieldName) { return path(fieldName); }
/**
* This method is similar to {@link #get(int)}, except that instead of returning null if no such element exists (due
* to index being out of range, or this node not being an array), a "missing node" (node that returns true for
* {@link #isMissingNode}) will be returned.This allows for convenient and safe chained access via path calls.
*
* @param index Undocumented.
* @return Undocumented.
*/
public abstract JsonNode path(int index);
/**
* Alias of {@link #path(int)}.
*
* @param index Undocumented.
* @return Undocumented.
* @deprecated Use {@link #path(int)} instead
*/
@Deprecated
public final JsonNode getPath(int index) { return path(index); }
/*
////////////////////////////////////////////////////
// Public API, serialization
////////////////////////////////////////////////////
*/
/**
* Method that can be called to serialize this node and
* all of its descendants using specified JSON generator.
*
* @param jg Undocumented.
* @throws java.io.IOException Undocumented.
* @throws org.codehaus.jackson.JsonGenerationException Undocumented.
* @deprecated Use methods that are part of {@link JsonGenerator}
* or org.codehaus.jackson.map.ObjectMapper
* instead.
*/
@Deprecated
public abstract void writeTo(JsonGenerator jg)
throws IOException, JsonGenerationException;
/*
////////////////////////////////////////////////////
// Public API: converting to/from Streaming API
////////////////////////////////////////////////////
*/
/**
* Method for constructing a {@link JsonParser} instance for iterating over contents of the tree that this node is
* root of.Functionally equivalent to first serializing tree using {@link #writeTo} and then re-parsing but much
* more efficient.
*
* @return Undocumented.
*/
public abstract JsonParser traverse();
/*
////////////////////////////////////////////////////
// Overridden standard methods
////////////////////////////////////////////////////
*/
/**
*<p>
* Note: marked as abstract to ensure all implementation
* classes define it properly.
*/
@Override
public abstract String toString();
/**
*<p>
* Note: marked as abstract to ensure all implementation
* classes define it properly.
* @param o Undocumented.
*/
@Override
public abstract boolean equals(Object o);
}