diff --git a/gson/src/main/java/com/google/gson/internal/bind/JsonElementReader.java b/gson/src/main/java/com/google/gson/internal/bind/JsonElementReader.java
index b3c5ad05..673fb5fb 100644
--- a/gson/src/main/java/com/google/gson/internal/bind/JsonElementReader.java
+++ b/gson/src/main/java/com/google/gson/internal/bind/JsonElementReader.java
@@ -215,4 +215,12 @@ public final class JsonElementReader extends JsonReader {
@Override public String toString() {
return getClass().getSimpleName();
}
+
+ public void promoteNameToValue() throws IOException {
+ expect(JsonToken.NAME);
+ Iterator> i = (Iterator>) peekStack();
+ Map.Entry, ?> entry = (Map.Entry, ?>) i.next();
+ stack.add(entry.getValue());
+ stack.add(new JsonPrimitive((String)entry.getKey()));
+ }
}
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 13f3b36f..efb56465 100644
--- a/gson/src/main/java/com/google/gson/stream/JsonReader.java
+++ b/gson/src/main/java/com/google/gson/stream/JsonReader.java
@@ -16,7 +16,6 @@
package com.google.gson.stream;
-import com.google.gson.internal.JsonReaderInternalAccess;
import java.io.Closeable;
import java.io.EOFException;
import java.io.IOException;
@@ -24,6 +23,9 @@ import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
+import com.google.gson.internal.JsonReaderInternalAccess;
+import com.google.gson.internal.bind.JsonElementReader;
+
/**
* Reads a JSON (RFC 4627)
* encoded value as a stream of tokens. This stream includes both literal
@@ -1240,9 +1242,14 @@ public class JsonReader implements Closeable {
static {
JsonReaderInternalAccess.INSTANCE = new JsonReaderInternalAccess() {
@Override public void promoteNameToValue(JsonReader reader) throws IOException {
+ if (reader instanceof JsonElementReader) {
+ ((JsonElementReader)reader).promoteNameToValue();
+ return;
+ }
reader.peek();
if (reader.token != JsonToken.NAME) {
- throw new IllegalStateException("Expected a name but was " + reader.peek());
+ throw new IllegalStateException("Expected a name but was " + reader.peek() + " "
+ + reader.getSnippet());
}
reader.value = reader.name;
reader.name = null;
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 530bc9bd..6065ca06 100755
--- a/gson/src/test/java/com/google/gson/functional/MapTest.java
+++ b/gson/src/test/java/com/google/gson/functional/MapTest.java
@@ -16,12 +16,22 @@
package com.google.gson.functional;
+import java.lang.reflect.Type;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.TreeMap;
+
+import junit.framework.TestCase;
+
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
+import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
@@ -29,13 +39,6 @@ import com.google.gson.JsonSyntaxException;
import com.google.gson.common.TestTypes;
import com.google.gson.internal.$Gson$Types;
import com.google.gson.reflect.TypeToken;
-import java.lang.reflect.Type;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.Map;
-import java.util.TreeMap;
-import junit.framework.TestCase;
/**
* Functional test for Json serialization and deserialization for Maps
@@ -488,6 +491,14 @@ public class MapTest extends TestCase {
}
}
+ public void testMapNamePromotionWithJsonElementReader() {
+ String json = "{'2.3':'a'}";
+ Map map = new LinkedHashMap();
+ map.put(2.3, "a");
+ JsonElement tree = new JsonParser().parse(json);
+ assertEquals(map, gson.fromJson(tree, new TypeToken