| 1 |
package com.fasterxml.jackson.jr.stree.util; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.io.OutputStream; |
| 5 |
import java.math.BigDecimal; |
| 6 |
import java.math.BigInteger; |
| 7 |
|
| 8 |
import com.fasterxml.jackson.core.*; |
| 9 |
import com.fasterxml.jackson.core.base.ParserMinimalBase; |
| 10 |
import com.fasterxml.jackson.jr.stree.JrsArray; |
| 11 |
import com.fasterxml.jackson.jr.stree.JrsNumber; |
| 12 |
import com.fasterxml.jackson.jr.stree.JrsObject; |
| 13 |
import com.fasterxml.jackson.jr.stree.JrsValue; |
| 14 |
import com.fasterxml.jackson.jr.stree.PackageVersion; |
| 15 |
|
| 16 |
/** |
| 17 |
* Facade over {@link JrsValue} that implements {@link JsonParser} to allow |
| 18 |
* accessing contents of JSON tree in alternate form (stream of tokens). |
| 19 |
* Useful when a streaming source is expected by code, such as data binding |
| 20 |
* functionality. |
| 21 |
*/ |
| 22 |
public class JrsTreeTraversingParser extends ParserMinimalBase |
| 23 |
{ |
| 24 |
/* |
| 25 |
/********************************************************************** |
| 26 |
/* Configuration |
| 27 |
/********************************************************************** |
| 28 |
*/ |
| 29 |
|
| 30 |
/** |
| 31 |
* Optional codec, to allow chaining. |
| 32 |
*/ |
| 33 |
protected ObjectCodec _objectCodec; |
| 34 |
|
| 35 |
/** |
| 36 |
* Traversal context within tree |
| 37 |
*/ |
| 38 |
protected JrsValueCursor _nodeCursor; |
| 39 |
|
| 40 |
/* |
| 41 |
/********************************************************************** |
| 42 |
/* State |
| 43 |
/********************************************************************** |
| 44 |
*/ |
| 45 |
|
| 46 |
/** |
| 47 |
* Sometimes parser needs to buffer a single look-ahead token; if so, |
| 48 |
* it'll be stored here. This is currently used for handling |
| 49 |
*/ |
| 50 |
protected JsonToken _nextToken; |
| 51 |
|
| 52 |
/** |
| 53 |
* Flag needed to handle recursion into contents of child |
| 54 |
* Array/Object nodes. |
| 55 |
*/ |
| 56 |
protected boolean _startContainer; |
| 57 |
|
| 58 |
/** |
| 59 |
* Flag that indicates whether parser is closed or not. Gets |
| 60 |
* set when parser is either closed by explicit call |
| 61 |
* ({@link #close}) or when end-of-input is reached. |
| 62 |
*/ |
| 63 |
protected boolean _closed; |
| 64 |
|
| 65 |
/* |
| 66 |
/********************************************************************** |
| 67 |
/* Life-cycle |
| 68 |
/********************************************************************** |
| 69 |
*/ |
| 70 |
|
| 71 |
public JrsTreeTraversingParser(JrsValue n) { this(n, null); } |
| 72 |
|
| 73 |
public JrsTreeTraversingParser(JrsValue n, ObjectCodec codec) |
| 74 |
{ |
| 75 |
super(0); |
| 76 |
_objectCodec = codec; |
| 77 |
if (n.isArray()) { |
| 78 |
_nextToken = JsonToken.START_ARRAY; |
| 79 |
_nodeCursor = new JrsValueCursor.ArrayCursor((JrsArray) n, null); |
| 80 |
} else if (n.isObject()) { |
| 81 |
_nextToken = JsonToken.START_OBJECT; |
| 82 |
_nodeCursor = new JrsValueCursor.ObjectCursor((JrsObject) n, null); |
| 83 |
} else { // value node |
| 84 |
_nodeCursor = new JrsValueCursor.RootCursor(n, null); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void setCodec(ObjectCodec c) { |
| 90 |
_objectCodec = c; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public ObjectCodec getCodec() { |
| 95 |
return _objectCodec; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public Version version() { |
| 100 |
return PackageVersion.VERSION; |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
/********************************************************************** |
| 105 |
/* Closeable implementation |
| 106 |
/********************************************************************** |
| 107 |
*/ |
| 108 |
|
| 109 |
@Override |
| 110 |
public void close() throws IOException |
| 111 |
{ |
| 112 |
if (!_closed) { |
| 113 |
_closed = true; |
| 114 |
_nodeCursor = null; |
| 115 |
_currToken = null; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/* |
| 120 |
/********************************************************************** |
| 121 |
/* Public API, traversal |
| 122 |
/********************************************************************** |
| 123 |
*/ |
| 124 |
|
| 125 |
@Override |
| 126 |
public JsonToken nextToken() throws IOException |
| 127 |
{ |
| 128 |
if (_nextToken != null) { |
| 129 |
_currToken = _nextToken; |
| 130 |
_nextToken = null; |
| 131 |
return _currToken; |
| 132 |
} |
| 133 |
// are we to descend to a container child? |
| 134 |
if (_startContainer) { |
| 135 |
_startContainer = false; |
| 136 |
// minor optimization: empty containers can be skipped |
| 137 |
if (!_nodeCursor.currentHasChildren()) { |
| 138 |
_currToken = (_currToken == JsonToken.START_OBJECT) ? |
| 139 |
JsonToken.END_OBJECT : JsonToken.END_ARRAY; |
| 140 |
return _currToken; |
| 141 |
} |
| 142 |
_nodeCursor = _nodeCursor.iterateChildren(); |
| 143 |
_currToken = _nodeCursor.nextToken(); |
| 144 |
if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) { |
| 145 |
_startContainer = true; |
| 146 |
} |
| 147 |
return _currToken; |
| 148 |
} |
| 149 |
// No more content? |
| 150 |
if (_nodeCursor == null) { |
| 151 |
_closed = true; // if not already set |
| 152 |
return null; |
| 153 |
} |
| 154 |
// Otherwise, next entry from current cursor |
| 155 |
_currToken = _nodeCursor.nextToken(); |
| 156 |
if (_currToken != null) { |
| 157 |
if (_currToken == JsonToken.START_OBJECT || _currToken == JsonToken.START_ARRAY) { |
| 158 |
_startContainer = true; |
| 159 |
} |
| 160 |
return _currToken; |
| 161 |
} |
| 162 |
// null means no more children; need to return end marker |
| 163 |
_currToken = _nodeCursor.endToken(); |
| 164 |
_nodeCursor = _nodeCursor.getParent(); |
| 165 |
return _currToken; |
| 166 |
} |
| 167 |
|
| 168 |
// default works well here: |
| 169 |
//public JsonToken nextValue() throws IOException |
| 170 |
|
| 171 |
@Override |
| 172 |
public JsonParser skipChildren() throws IOException |
| 173 |
{ |
| 174 |
if (_currToken == JsonToken.START_OBJECT) { |
| 175 |
_startContainer = false; |
| 176 |
_currToken = JsonToken.END_OBJECT; |
| 177 |
} else if (_currToken == JsonToken.START_ARRAY) { |
| 178 |
_startContainer = false; |
| 179 |
_currToken = JsonToken.END_ARRAY; |
| 180 |
} |
| 181 |
return this; |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public boolean isClosed() { |
| 186 |
return _closed; |
| 187 |
} |
| 188 |
|
| 189 |
/* |
| 190 |
/********************************************************** |
| 191 |
/* Public API, token accessors |
| 192 |
/********************************************************** |
| 193 |
*/ |
| 194 |
|
| 195 |
@Override |
| 196 |
public String getCurrentName() { |
| 197 |
return (_nodeCursor == null) ? null : _nodeCursor.getCurrentName(); |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public void overrideCurrentName(String name) |
| 202 |
{ |
| 203 |
if (_nodeCursor != null) { |
| 204 |
_nodeCursor.overrideCurrentName(name); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public JsonStreamContext getParsingContext() { |
| 210 |
return _nodeCursor; |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public JsonLocation getTokenLocation() { |
| 215 |
return JsonLocation.NA; |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public JsonLocation getCurrentLocation() { |
| 220 |
return JsonLocation.NA; |
| 221 |
} |
| 222 |
|
| 223 |
/* |
| 224 |
/********************************************************** |
| 225 |
/* Public API, access to textual content |
| 226 |
/********************************************************** |
| 227 |
*/ |
| 228 |
|
| 229 |
@Override |
| 230 |
public String getText() |
| 231 |
{ |
| 232 |
if (_closed) { |
| 233 |
return null; |
| 234 |
} |
| 235 |
// need to separate handling a bit... |
| 236 |
switch (_currToken) { |
| 237 |
case FIELD_NAME: |
| 238 |
return _nodeCursor.getCurrentName(); |
| 239 |
case VALUE_STRING: |
| 240 |
case VALUE_NUMBER_INT: |
| 241 |
case VALUE_NUMBER_FLOAT: |
| 242 |
return currentNode().asText(); |
| 243 |
// 28-Dec-2015, tatu: Not yet supported: |
| 244 |
// case VALUE_EMBEDDED_OBJECT: |
| 245 |
default: |
| 246 |
return (_currToken == null) ? null : _currToken.asString(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
public char[] getTextCharacters() throws IOException { |
| 252 |
return getText().toCharArray(); |
| 253 |
} |
| 254 |
|
| 255 |
@Override |
| 256 |
public int getTextLength() throws IOException { |
| 257 |
return getText().length(); |
| 258 |
} |
| 259 |
|
| 260 |
@Override |
| 261 |
public int getTextOffset() throws IOException { |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
@Override |
| 266 |
public boolean hasTextCharacters() { |
| 267 |
// generally we do not have efficient access as char[], hence: |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
/* |
| 272 |
/********************************************************** |
| 273 |
/* Public API, typed non-text access |
| 274 |
/********************************************************** |
| 275 |
*/ |
| 276 |
|
| 277 |
//public byte getByteValue() throws IOException |
| 278 |
|
| 279 |
@Override |
| 280 |
public NumberType getNumberType() throws IOException { |
| 281 |
JrsValue n = currentNumericNode(); |
| 282 |
return (n == null) ? null : n.numberType(); |
| 283 |
} |
| 284 |
|
| 285 |
@Override |
| 286 |
public BigInteger getBigIntegerValue() throws IOException |
| 287 |
{ |
| 288 |
return currentNumericNode().asBigInteger(); |
| 289 |
} |
| 290 |
|
| 291 |
@Override |
| 292 |
public BigDecimal getDecimalValue() throws IOException { |
| 293 |
return currentNumericNode().asBigDecimal(); |
| 294 |
} |
| 295 |
|
| 296 |
@Override |
| 297 |
public double getDoubleValue() throws IOException { |
| 298 |
return currentNumericValue().doubleValue(); |
| 299 |
} |
| 300 |
|
| 301 |
@Override |
| 302 |
public float getFloatValue() throws IOException { |
| 303 |
return (float) currentNumericValue().doubleValue(); |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
public long getLongValue() throws IOException { |
| 308 |
return currentNumericValue().longValue(); |
| 309 |
} |
| 310 |
|
| 311 |
@Override |
| 312 |
public int getIntValue() throws IOException { |
| 313 |
return currentNumericValue().intValue(); |
| 314 |
} |
| 315 |
|
| 316 |
@Override |
| 317 |
public Number getNumberValue() throws IOException { |
| 318 |
return currentNumericValue(); |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
public Object getEmbeddedObject() { |
| 323 |
// 28-Dec-2015, tatu: Embedded values ("POJO nodes") not yet supported |
| 324 |
return null; |
| 325 |
} |
| 326 |
|
| 327 |
/* |
| 328 |
/********************************************************** |
| 329 |
/* Public API, typed binary (base64) access |
| 330 |
/********************************************************** |
| 331 |
*/ |
| 332 |
|
| 333 |
@Override |
| 334 |
public byte[] getBinaryValue(Base64Variant b64variant) throws IOException { |
| 335 |
// 28-Dec-2015, tatu: Binary nodes not yet supported |
| 336 |
return null; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
@Override |
| 341 |
public int readBinaryValue(Base64Variant b64variant, OutputStream out) throws IOException { |
| 342 |
// 28-Dec-2015, tatu: Binary nodes not yet supported |
| 343 |
return -1; |
| 344 |
} |
| 345 |
|
| 346 |
/* |
| 347 |
/********************************************************** |
| 348 |
/* Internal methods |
| 349 |
/********************************************************** |
| 350 |
*/ |
| 351 |
|
| 352 |
protected JrsValue currentNode() { |
| 353 |
if (_closed || _nodeCursor == null) { |
| 354 |
return null; |
| 355 |
} |
| 356 |
return _nodeCursor.currentNode(); |
| 357 |
} |
| 358 |
|
| 359 |
protected JrsNumber currentNumericNode() throws JsonParseException |
| 360 |
{ |
| 361 |
JrsValue n = currentNode(); |
| 362 |
if ((n == null) || !(n instanceof JrsNumber)) { |
| 363 |
JsonToken t = (n == null) ? null : n.asToken(); |
| 364 |
throw _constructError("Current token ("+t+") not numeric, can not use numeric value accessors"); |
| 365 |
} |
| 366 |
return (JrsNumber) n; |
| 367 |
} |
| 368 |
|
| 369 |
protected Number currentNumericValue() throws JsonParseException { |
| 370 |
return currentNumericNode().getValue(); |
| 371 |
} |
| 372 |
|
| 373 |
@Override |
| 374 |
protected void _handleEOF() throws JsonParseException { |
| 375 |
_throwInternal(); // should never get called |
| 376 |
} |
| 377 |
} |