Determine whether an element is pending for the current container from the JSON input stream. @return true if an element is pending; otherwise false @throws JsonException if no container is open @throws UncheckedIOException if an I/O exception is encountered
()
| 518 | * @throws UncheckedIOException if an I/O exception is encountered |
| 519 | */ |
| 520 | public boolean hasNext() { |
| 521 | if (containerDepth == 0) { |
| 522 | throw new JsonException( |
| 523 | "Unable to determine if an item has next when not in a container type. " + input); |
| 524 | } |
| 525 | |
| 526 | skipWhitespace(input); |
| 527 | boolean seenElement = containerDepth > 0 && containerHasElement[containerDepth - 1]; |
| 528 | |
| 529 | if (input.peek() == ',') { |
| 530 | if (!seenElement) { |
| 531 | throw new JsonException("Unexpected ',' before first element of container. " + input); |
| 532 | } |
| 533 | input.read(); |
| 534 | peekedType = null; |
| 535 | // We've moved past the separator, so we're once again expecting an element rather than |
| 536 | // another comma. Clear the flag so a repeat hasNext() before reading is a no-op. |
| 537 | clearSeenElement(); |
| 538 | skipWhitespace(input); |
| 539 | JsonType afterComma = peek(); |
| 540 | // Trailing comma leniency: '[1,]' and '{"a":1,}' are accepted. |
| 541 | return afterComma != JsonType.END_COLLECTION && afterComma != JsonType.END_MAP; |
| 542 | } |
| 543 | |
| 544 | JsonType type = peek(); |
| 545 | if (type == JsonType.END_COLLECTION || type == JsonType.END_MAP) { |
| 546 | return false; |
| 547 | } |
| 548 | if (seenElement) { |
| 549 | throw new JsonException("Expected ',' or end of container but saw " + type + ". " + input); |
| 550 | } |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Process the opening square bracket of a JSON array. |