MCPcopy
hub / github.com/google/gson / nextLong

Method nextLong

gson/src/main/java/com/google/gson/stream/JsonReader.java:1072–1116  ·  view source on GitHub ↗

Returns the {@link JsonToken#NUMBER long} value of the next token, consuming it. If the next token is a string, this method will attempt to parse it as a long. If the next token's numeric value cannot be exactly represented by a Java {@code long}, this method throws. @throws IllegalStateException i

()

Source from the content-addressed store, hash-verified

1070 * exactly represented as a long.
1071 */
1072 public long nextLong() throws IOException {
1073 int p = peeked;
1074 if (p == PEEKED_NONE) {
1075 p = doPeek();
1076 }
1077
1078 if (p == PEEKED_LONG) {
1079 peeked = PEEKED_NONE;
1080 pathIndices[stackSize - 1]++;
1081 return peekedLong;
1082 }
1083
1084 if (p == PEEKED_NUMBER) {
1085 peekedString = new String(buffer, pos, peekedNumberLength);
1086 pos += peekedNumberLength;
1087 } else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED || p == PEEKED_UNQUOTED) {
1088 if (p == PEEKED_UNQUOTED) {
1089 peekedString = nextUnquotedValue();
1090 } else {
1091 peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
1092 }
1093 validateAscii(peekedString);
1094 try {
1095 long result = Long.parseLong(peekedString);
1096 peeked = PEEKED_NONE;
1097 pathIndices[stackSize - 1]++;
1098 return result;
1099 } catch (NumberFormatException ignored) {
1100 // Fall back to parse as a double below.
1101 }
1102 } else {
1103 throw unexpectedTokenError("a long");
1104 }
1105
1106 peeked = PEEKED_BUFFERED;
1107 double asDouble = Double.parseDouble(peekedString); // don't catch this NumberFormatException.
1108 long result = (long) asDouble;
1109 if (result != asDouble) { // Make sure no precision was lost casting to 'long'.
1110 throw new NumberFormatException("Expected a long but was " + peekedString + locationString());
1111 }
1112 peekedString = null;
1113 peeked = PEEKED_NONE;
1114 pathIndices[stackSize - 1]++;
1115 return result;
1116 }
1117
1118 /**
1119 * Returns the string up to but not including {@code quote}, unescaping any character escape

Calls 6

doPeekMethod · 0.95
nextUnquotedValueMethod · 0.95
nextQuotedValueMethod · 0.95
validateAsciiMethod · 0.95
unexpectedTokenErrorMethod · 0.95
locationStringMethod · 0.95