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
()
| 225 | * @throws UncheckedIOException if an I/O exception is encountered |
| 226 | */ |
| 227 | public Number nextNumber() { |
| 228 | expect(JsonType.NUMBER); |
| 229 | StringBuilder builder = new StringBuilder(); |
| 230 | boolean isDecimal = false; |
| 231 | |
| 232 | // Optional leading minus. (Per RFC 8259 §6, a leading '+' is not allowed.) |
| 233 | if (input.peek() == '-') { |
| 234 | builder.append((char) input.read()); |
| 235 | } |
| 236 | |
| 237 | // Integer part: either "0" or [1-9] [0-9]*. |
| 238 | int first = input.peek(); |
| 239 | if (first == '0') { |
| 240 | builder.append((char) input.read()); |
| 241 | // Leading zeros ("00", "01", ...) are not allowed. |
| 242 | if (isDigit(input.peek())) { |
| 243 | throw new JsonException("Leading zeros are not permitted in JSON numbers. " + input); |
| 244 | } |
| 245 | } else if (first >= '1' && first <= '9') { |
| 246 | while (isDigit(input.peek())) { |
| 247 | builder.append((char) input.read()); |
| 248 | } |
| 249 | } else { |
| 250 | throw new JsonException("Expected digit but saw " + describeChar(first) + ". " + input); |
| 251 | } |
| 252 | |
| 253 | // Optional fractional part: '.' 1*DIGIT |
| 254 | if (input.peek() == '.') { |
| 255 | isDecimal = true; |
| 256 | builder.append((char) input.read()); |
| 257 | if (!isDigit(input.peek())) { |
| 258 | throw new JsonException( |
| 259 | "Expected at least one digit after '.' but saw " |
| 260 | + describeChar(input.peek()) |
| 261 | + ". " |
| 262 | + input); |
| 263 | } |
| 264 | while (isDigit(input.peek())) { |
| 265 | builder.append((char) input.read()); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | // Optional exponent part: ('e' | 'E') ('+' | '-')? 1*DIGIT |
| 270 | if (input.peek() == 'e' || input.peek() == 'E') { |
| 271 | isDecimal = true; |
| 272 | builder.append((char) input.read()); |
| 273 | if (input.peek() == '+' || input.peek() == '-') { |
| 274 | builder.append((char) input.read()); |
| 275 | } |
| 276 | if (!isDigit(input.peek())) { |
| 277 | throw new JsonException( |
| 278 | "Expected at least one digit in exponent but saw " |
| 279 | + describeChar(input.peek()) |
| 280 | + ". " |
| 281 | + input); |
| 282 | } |
| 283 | while (isDigit(input.peek())) { |
| 284 | builder.append((char) input.read()); |