Inserts any necessary separators and whitespace before a literal value, inline array, or inline object. Also adjusts the stack to expect either a closing bracket or another element. @param root true if the value is a new array or object, the two values permitted as top-level elements.
(boolean root)
| 518 | * permitted as top-level elements. |
| 519 | */ |
| 520 | private void beforeValue(boolean root) throws IOException { |
| 521 | switch (peek()) { |
| 522 | case EMPTY_DOCUMENT: // first in document |
| 523 | if (!lenient && !root) { |
| 524 | throw new IllegalStateException( |
| 525 | "JSON must start with an array or an object."); |
| 526 | } |
| 527 | replaceTop(JsonScope.NONEMPTY_DOCUMENT); |
| 528 | break; |
| 529 | |
| 530 | case EMPTY_ARRAY: // first in array |
| 531 | replaceTop(JsonScope.NONEMPTY_ARRAY); |
| 532 | newline(); |
| 533 | break; |
| 534 | |
| 535 | case NONEMPTY_ARRAY: // another in array |
| 536 | out.append(','); |
| 537 | newline(); |
| 538 | break; |
| 539 | |
| 540 | case DANGLING_NAME: // value for name |
| 541 | out.append(separator); |
| 542 | replaceTop(JsonScope.NONEMPTY_OBJECT); |
| 543 | break; |
| 544 | |
| 545 | case NONEMPTY_DOCUMENT: |
| 546 | throw new IllegalStateException( |
| 547 | "JSON must have only one top-level value."); |
| 548 | |
| 549 | default: |
| 550 | throw new IllegalStateException("Nesting problem: " + stack); |
| 551 | } |
| 552 | } |
| 553 | } |