Returns the JsonToken#NUMBER int value of the next token, consuming it. If the next token is a string, this method will attempt to parse it as an int. If the next token's numeric value cannot be exactly represented by a Java int, this method throws. @throws IllegalStateException if
()
| 588 | * as a number, or exactly represented as an int. |
| 589 | */ |
| 590 | public int nextInt() throws IOException { |
| 591 | quickPeek(); |
| 592 | if (value == null) { |
| 593 | throw new IllegalStateException("Expected an int but was " + peek()); |
| 594 | } |
| 595 | |
| 596 | int result; |
| 597 | try { |
| 598 | result = Integer.parseInt(value); |
| 599 | } catch (NumberFormatException ignored) { |
| 600 | double asDouble = Double.parseDouble(value); // don't catch this NumberFormatException |
| 601 | result = (int) asDouble; |
| 602 | if (result != asDouble) { |
| 603 | throw new NumberFormatException(value); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (result >= 1L && value.startsWith("0")) { |
| 608 | throw new NumberFormatException("JSON forbids octal prefixes: " + value); |
| 609 | } |
| 610 | |
| 611 | advance(); |
| 612 | return result; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Closes this JSON reader and the underlying {@link Reader}. |