Skip to content

Commit

Permalink
Merge pull request google#809 from sgbrown/unquoted_integer_issue604_…
Browse files Browse the repository at this point in the history
…issue524

allow unquoted long and integer keys
  • Loading branch information
swankjesse committed Mar 12, 2016
2 parents a02f575 + 0669ff7 commit 2b08c88
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
16 changes: 12 additions & 4 deletions gson/src/main/java/com/google/gson/stream/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,12 @@ public long nextLong() throws IOException {
if (p == PEEKED_NUMBER) {
peekedString = new String(buffer, pos, peekedNumberLength);
pos += peekedNumberLength;
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED) {
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED || p == PEEKED_UNQUOTED) {
if (p == PEEKED_UNQUOTED) {
peekedString = nextUnquotedValue();
} else {
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
}
try {
long result = Long.parseLong(peekedString);
peeked = PEEKED_NONE;
Expand Down Expand Up @@ -1179,8 +1183,12 @@ public int nextInt() throws IOException {
if (p == PEEKED_NUMBER) {
peekedString = new String(buffer, pos, peekedNumberLength);
pos += peekedNumberLength;
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED) {
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
} else if (p == PEEKED_SINGLE_QUOTED || p == PEEKED_DOUBLE_QUOTED || p == PEEKED_UNQUOTED) {
if (p == PEEKED_UNQUOTED) {
peekedString = nextUnquotedValue();
} else {
peekedString = nextQuotedValue(p == PEEKED_SINGLE_QUOTED ? '\'' : '"');
}
try {
result = Integer.parseInt(peekedString);
peeked = PEEKED_NONE;
Expand Down
28 changes: 28 additions & 0 deletions gson/src/test/java/com/google/gson/functional/MapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@ public void testMapDeserializationWithIntegerKeys() {
assertEquals("456", map.get(123));
}

public void testMapDeserializationWithUnquotedIntegerKeys() {
Type typeOfMap = new TypeToken<Map<Integer, String>>() {}.getType();
Map<Integer, String> map = gson.fromJson("{123:\"456\"}", typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(123));
assertEquals("456", map.get(123));
}

public void testMapDeserializationWithLongKeys() {
long longValue = 9876543210L;
String json = String.format("{\"%d\":\"456\"}", longValue);
Type typeOfMap = new TypeToken<Map<Long, String>>() {}.getType();
Map<Long, String> map = gson.fromJson(json, typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(longValue));
assertEquals("456", map.get(longValue));
}

public void testMapDeserializationWithUnquotedLongKeys() {
long longKey = 9876543210L;
String json = String.format("{%d:\"456\"}", longKey);
Type typeOfMap = new TypeToken<Map<Long, String>>() {}.getType();
Map<Long, String> map = gson.fromJson(json, typeOfMap);
assertEquals(1, map.size());
assertTrue(map.containsKey(longKey));
assertEquals("456", map.get(longKey));
}

public void testHashMapDeserialization() throws Exception {
Type typeOfMap = new TypeToken<HashMap<Integer, String>>() {}.getType();
HashMap<Integer, String> map = gson.fromJson("{\"123\":\"456\"}", typeOfMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ public void testPeekingUnquotedStringsPrefixedWithIntegers() throws IOException
try {
reader.nextInt();
fail();
} catch (IllegalStateException expected) {
} catch (NumberFormatException expected) {
}
assertEquals("12.34e5x", reader.nextString());
}
Expand Down

0 comments on commit 2b08c88

Please sign in to comment.