From 0669ff7fd16c26521b924a58c7dd64ab2dc1a46d Mon Sep 17 00:00:00 2001 From: Scott Brown Date: Sat, 12 Mar 2016 11:52:13 -0700 Subject: [PATCH] allow unquoted long and integer keys --- .../com/google/gson/stream/JsonReader.java | 16 ++++++++--- .../com/google/gson/functional/MapTest.java | 28 +++++++++++++++++++ .../google/gson/stream/JsonReaderTest.java | 2 +- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/gson/src/main/java/com/google/gson/stream/JsonReader.java b/gson/src/main/java/com/google/gson/stream/JsonReader.java index 838355cb..7a5e9cbb 100644 --- a/gson/src/main/java/com/google/gson/stream/JsonReader.java +++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java @@ -951,8 +951,12 @@ public class JsonReader implements Closeable { 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; @@ -1179,8 +1183,12 @@ public class JsonReader implements Closeable { 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; diff --git a/gson/src/test/java/com/google/gson/functional/MapTest.java b/gson/src/test/java/com/google/gson/functional/MapTest.java index 590760cb..154ecf55 100755 --- a/gson/src/test/java/com/google/gson/functional/MapTest.java +++ b/gson/src/test/java/com/google/gson/functional/MapTest.java @@ -167,6 +167,34 @@ public class MapTest extends TestCase { assertEquals("456", map.get(123)); } + public void testMapDeserializationWithUnquotedIntegerKeys() { + Type typeOfMap = new TypeToken>() {}.getType(); + Map 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>() {}.getType(); + Map 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>() {}.getType(); + Map 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>() {}.getType(); HashMap map = gson.fromJson("{\"123\":\"456\"}", typeOfMap); diff --git a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java index a192940f..48279dac 100644 --- a/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java +++ b/gson/src/test/java/com/google/gson/stream/JsonReaderTest.java @@ -501,7 +501,7 @@ public final class JsonReaderTest extends TestCase { try { reader.nextInt(); fail(); - } catch (IllegalStateException expected) { + } catch (NumberFormatException expected) { } assertEquals("12.34e5x", reader.nextString()); }