|
19 | 19 |
|
20 | 20 | package co.elastic.clients.json.jackson;
|
21 | 21 |
|
| 22 | +import co.elastic.clients.json.LookAheadJsonParser; |
| 23 | +import co.elastic.clients.json.UnexpectedJsonEventException; |
22 | 24 | import com.fasterxml.jackson.core.JsonToken;
|
| 25 | +import com.fasterxml.jackson.core.util.JsonParserSequence; |
| 26 | +import com.fasterxml.jackson.databind.util.TokenBuffer; |
23 | 27 | import jakarta.json.JsonArray;
|
24 | 28 | import jakarta.json.JsonObject;
|
25 | 29 | import jakarta.json.JsonValue;
|
|
29 | 33 |
|
30 | 34 | import java.io.IOException;
|
31 | 35 | import java.math.BigDecimal;
|
| 36 | +import java.util.AbstractMap; |
32 | 37 | import java.util.EnumMap;
|
33 | 38 | import java.util.Map;
|
34 | 39 | import java.util.NoSuchElementException;
|
|
42 | 47 | * getter method (e.g. {@link #getInt()} or {@link #getString()} should be called until the next call to {@link #next()}.
|
43 | 48 | * Such calls will throw an {@code IllegalStateException}.
|
44 | 49 | */
|
45 |
| -public class JacksonJsonpParser implements JsonParser { |
| 50 | +public class JacksonJsonpParser implements LookAheadJsonParser { |
46 | 51 |
|
47 | 52 | private final com.fasterxml.jackson.core.JsonParser parser;
|
48 | 53 |
|
@@ -306,7 +311,100 @@ public Stream<JsonValue> getArrayStream() {
|
306 | 311 | */
|
307 | 312 | @Override
|
308 | 313 | public Stream<JsonValue> getValueStream() {
|
309 |
| - return JsonParser.super.getValueStream(); |
| 314 | + return LookAheadJsonParser.super.getValueStream(); |
| 315 | + } |
| 316 | + |
| 317 | + //----- Look ahead methods |
| 318 | + |
| 319 | + public Map.Entry<String, JsonParser> lookAheadFieldValue(String name, String defaultValue) { |
| 320 | + |
| 321 | + TokenBuffer tb = new TokenBuffer(parser, null); |
| 322 | + |
| 323 | + try { |
| 324 | + // The resulting parser must contain the full object, including START_EVENT |
| 325 | + tb.copyCurrentEvent(parser); |
| 326 | + while (parser.nextToken() != JsonToken.END_OBJECT) { |
| 327 | + |
| 328 | + expectEvent(JsonToken.FIELD_NAME); |
| 329 | + // Do not copy current event here, each branch will take care of it |
| 330 | + |
| 331 | + String fieldName = parser.getCurrentName(); |
| 332 | + if (fieldName.equals(name)) { |
| 333 | + // Found |
| 334 | + tb.copyCurrentEvent(parser); |
| 335 | + expectNextEvent(JsonToken.VALUE_STRING); |
| 336 | + tb.copyCurrentEvent(parser); |
| 337 | + |
| 338 | + return new AbstractMap.SimpleImmutableEntry<>( |
| 339 | + parser.getText(), |
| 340 | + new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) |
| 341 | + ); |
| 342 | + } else { |
| 343 | + tb.copyCurrentStructure(parser); |
| 344 | + } |
| 345 | + } |
| 346 | + // Copy ending END_OBJECT |
| 347 | + tb.copyCurrentEvent(parser); |
| 348 | + } catch (IOException e) { |
| 349 | + throw JacksonUtils.convertException(e); |
| 350 | + } |
| 351 | + |
| 352 | + // Field not found |
| 353 | + return new AbstractMap.SimpleImmutableEntry<>( |
| 354 | + defaultValue, |
| 355 | + new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) |
| 356 | + ); |
| 357 | + } |
| 358 | + |
| 359 | + @Override |
| 360 | + public <Variant> Map.Entry<Variant, JsonParser> findVariant(Map<String, Variant> variants) { |
| 361 | + // We're on a START_OBJECT event |
| 362 | + TokenBuffer tb = new TokenBuffer(parser, null); |
| 363 | + |
| 364 | + try { |
| 365 | + // The resulting parser must contain the full object, including START_EVENT |
| 366 | + tb.copyCurrentEvent(parser); |
| 367 | + while (parser.nextToken() != JsonToken.END_OBJECT) { |
| 368 | + |
| 369 | + expectEvent(JsonToken.FIELD_NAME); |
| 370 | + String fieldName = parser.getCurrentName(); |
| 371 | + |
| 372 | + Variant variant = variants.get(fieldName); |
| 373 | + if (variant != null) { |
| 374 | + tb.copyCurrentEvent(parser); |
| 375 | + return new AbstractMap.SimpleImmutableEntry<>( |
| 376 | + variant, |
| 377 | + new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) |
| 378 | + ); |
| 379 | + } else { |
| 380 | + tb.copyCurrentStructure(parser); |
| 381 | + } |
| 382 | + } |
| 383 | + // Copy ending END_OBJECT |
| 384 | + tb.copyCurrentEvent(parser); |
| 385 | + } catch (IOException e) { |
| 386 | + throw JacksonUtils.convertException(e); |
| 387 | + } |
| 388 | + |
| 389 | + // No variant found: return the buffered parser and let the caller decide what to do. |
| 390 | + return new AbstractMap.SimpleImmutableEntry<>( |
| 391 | + null, |
| 392 | + new JacksonJsonpParser(JsonParserSequence.createFlattened(false, tb.asParser(), parser)) |
| 393 | + ); |
| 394 | + } |
| 395 | + |
| 396 | + private void expectNextEvent(JsonToken expected) throws IOException { |
| 397 | + JsonToken event = parser.nextToken(); |
| 398 | + if (event != expected) { |
| 399 | + throw new UnexpectedJsonEventException(this, tokenToEvent.get(event), tokenToEvent.get(expected)); |
| 400 | + } |
| 401 | + } |
| 402 | + |
| 403 | + private void expectEvent(JsonToken expected) { |
| 404 | + JsonToken event = parser.currentToken(); |
| 405 | + if (event != expected) { |
| 406 | + throw new UnexpectedJsonEventException(this, tokenToEvent.get(event), tokenToEvent.get(expected)); |
| 407 | + } |
310 | 408 | }
|
311 | 409 | }
|
312 | 410 |
|
0 commit comments