Read the next element of the JSON input stream as a number. @return Number object @throws JsonException if the next element isn't a number @throws UncheckedIOException if an I/O exception is encountered
()
| 261 | * @throws UncheckedIOException if an I/O exception is encountered |
| 262 | */ |
| 263 | public Number nextNumber() { |
| 264 | NumberLexeme lexeme = readNumberLexeme(); |
| 265 | StringBuilder text = lexeme.text; |
| 266 | |
| 267 | try { |
| 268 | // Fast path for integers: Long-valued when no fraction/exponent was present. |
| 269 | if (!lexeme.isDecimal) { |
| 270 | // At most 18 digits (plus a sign) always fits in a long, so the allocation-free parse |
| 271 | // cannot overflow. Longer inputs take the String path, which reports overflow with the |
| 272 | // historical NumberFormatException message. |
| 273 | if (text.length() <= (text.charAt(0) == '-' ? 19 : 18)) { |
| 274 | return Long.parseLong(text, 0, text.length(), 10); |
| 275 | } |
| 276 | return Long.valueOf(text.toString()); |
| 277 | } |
| 278 | double value = parseDouble(text); |
| 279 | if (Double.isInfinite(value) || Double.isNaN(value)) { |
| 280 | throw new JsonException("Number is out of range for a double: " + text + ". " + input); |
| 281 | } |
| 282 | return value; |
| 283 | } catch (NumberFormatException e) { |
| 284 | throw new JsonException("Unable to parse to a number: " + text + ". " + input, e); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Reads the next JSON number token as an exact {@link BigDecimal}, preserving the fractional and |