Skip to content

Commit 2b08c88

Browse files
committed
Merge pull request google#809 from sgbrown/unquoted_integer_issue604_issue524
allow unquoted long and integer keys
2 parents a02f575 + 0669ff7 commit 2b08c88

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

gson/src/main/java/com/google/gson/stream/JsonReader.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -951,8 +951,12 @@ public long nextLong() throws IOException {
951951
if (p == PEEKED_NUMBER) {
952952
peekedString = new String(buffer, pos, peekedNumberLength);
953953
pos += peekedNumberLength;
954-
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED) {
955-
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
954+
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED || p == PEEKED_UNQUOTED) {
955+
if (p == PEEKED_UNQUOTED) {
956+
peekedString = nextUnquotedValue();
957+
} else {
958+
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
959+
}
956960
try {
957961
long result = Long.parseLong(peekedString);
958962
peeked = PEEKED_NONE;
@@ -1179,8 +1183,12 @@ public int nextInt() throws IOException {
11791183
if (p == PEEKED_NUMBER) {
11801184
peekedString = new String(buffer, pos, peekedNumberLength);
11811185
pos += peekedNumberLength;
1182-
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED) {
1183-
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
1186+
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED || p == PEEKED_UNQUOTED) {
1187+
if (p == PEEKED_UNQUOTED) {
1188+
peekedString = nextUnquotedValue();
1189+
} else {
1190+
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
1191+
}
11841192
try {
11851193
result = Integer.parseInt(peekedString);
11861194
peeked = PEEKED_NONE;

gson/src/test/java/com/google/gson/functional/MapTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,34 @@ public void testMapDeserializationWithIntegerKeys() {
167167
assertEquals("456", map.get(123));
168168
}
169169

170+
public void testMapDeserializationWithUnquotedIntegerKeys() {
171+
Type typeOfMap = new TypeToken<Map<Integer, String>>() {}.getType();
172+
Map<Integer, String> map = gson.fromJson("{123:\"456\"}", typeOfMap);
173+
assertEquals(1, map.size());
174+
assertTrue(map.containsKey(123));
175+
assertEquals("456", map.get(123));
176+
}
177+
178+
public void testMapDeserializationWithLongKeys() {
179+
long longValue = 9876543210L;
180+
String json = String.format("{\"%d\":\"456\"}", longValue);
181+
Type typeOfMap = new TypeToken<Map<Long, String>>() {}.getType();
182+
Map<Long, String> map = gson.fromJson(json, typeOfMap);
183+
assertEquals(1, map.size());
184+
assertTrue(map.containsKey(longValue));
185+
assertEquals("456", map.get(longValue));
186+
}
187+
188+
public void testMapDeserializationWithUnquotedLongKeys() {
189+
long longKey = 9876543210L;
190+
String json = String.format("{%d:\"456\"}", longKey);
191+
Type typeOfMap = new TypeToken<Map<Long, String>>() {}.getType();
192+
Map<Long, String> map = gson.fromJson(json, typeOfMap);
193+
assertEquals(1, map.size());
194+
assertTrue(map.containsKey(longKey));
195+
assertEquals("456", map.get(longKey));
196+
}
197+
170198
public void testHashMapDeserialization() throws Exception {
171199
Type typeOfMap = new TypeToken<HashMap<Integer, String>>() {}.getType();
172200
HashMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);

gson/src/test/java/com/google/gson/stream/JsonReaderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ public void testPeekingUnquotedStringsPrefixedWithIntegers() throws IOException
501501
try {
502502
reader.nextInt();
503503
fail();
504-
} catch (IllegalStateException expected) {
504+
} catch (NumberFormatException expected) {
505505
}
506506
assertEquals("12.34e5x", reader.nextString());
507507
}

0 commit comments

Comments
 (0)