Added support for promoteNameToValue for JsonElementReader.
This commit is contained in:
parent
8f8e69a364
commit
f3c14b4614
@ -215,4 +215,12 @@ public final class JsonElementReader extends JsonReader {
|
|||||||
@Override public String toString() {
|
@Override public String toString() {
|
||||||
return getClass().getSimpleName();
|
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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package com.google.gson.stream;
|
package com.google.gson.stream;
|
||||||
|
|
||||||
import com.google.gson.internal.JsonReaderInternalAccess;
|
|
||||||
import java.io.Closeable;
|
import java.io.Closeable;
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -24,6 +23,9 @@ import java.io.Reader;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.google.gson.internal.JsonReaderInternalAccess;
|
||||||
|
import com.google.gson.internal.bind.JsonElementReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads a JSON (<a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>)
|
* Reads a JSON (<a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>)
|
||||||
* encoded value as a stream of tokens. This stream includes both literal
|
* encoded value as a stream of tokens. This stream includes both literal
|
||||||
@ -1240,9 +1242,14 @@ public class JsonReader implements Closeable {
|
|||||||
static {
|
static {
|
||||||
JsonReaderInternalAccess.INSTANCE = new JsonReaderInternalAccess() {
|
JsonReaderInternalAccess.INSTANCE = new JsonReaderInternalAccess() {
|
||||||
@Override public void promoteNameToValue(JsonReader reader) throws IOException {
|
@Override public void promoteNameToValue(JsonReader reader) throws IOException {
|
||||||
|
if (reader instanceof JsonElementReader) {
|
||||||
|
((JsonElementReader)reader).promoteNameToValue();
|
||||||
|
return;
|
||||||
|
}
|
||||||
reader.peek();
|
reader.peek();
|
||||||
if (reader.token != JsonToken.NAME) {
|
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.value = reader.name;
|
||||||
reader.name = null;
|
reader.name = null;
|
||||||
|
@ -16,12 +16,22 @@
|
|||||||
|
|
||||||
package com.google.gson.functional;
|
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.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.InstanceCreator;
|
import com.google.gson.InstanceCreator;
|
||||||
import com.google.gson.JsonArray;
|
import com.google.gson.JsonArray;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParseException;
|
||||||
|
import com.google.gson.JsonParser;
|
||||||
import com.google.gson.JsonPrimitive;
|
import com.google.gson.JsonPrimitive;
|
||||||
import com.google.gson.JsonSerializationContext;
|
import com.google.gson.JsonSerializationContext;
|
||||||
import com.google.gson.JsonSerializer;
|
import com.google.gson.JsonSerializer;
|
||||||
@ -29,13 +39,6 @@ import com.google.gson.JsonSyntaxException;
|
|||||||
import com.google.gson.common.TestTypes;
|
import com.google.gson.common.TestTypes;
|
||||||
import com.google.gson.internal.$Gson$Types;
|
import com.google.gson.internal.$Gson$Types;
|
||||||
import com.google.gson.reflect.TypeToken;
|
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
|
* 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<Double, String> map = new LinkedHashMap<Double, String>();
|
||||||
|
map.put(2.3, "a");
|
||||||
|
JsonElement tree = new JsonParser().parse(json);
|
||||||
|
assertEquals(map, gson.fromJson(tree, new TypeToken<Map<Double, String>>() {}.getType()));
|
||||||
|
}
|
||||||
|
|
||||||
static class Point {
|
static class Point {
|
||||||
private final int x;
|
private final int x;
|
||||||
private final int y;
|
private final int y;
|
||||||
|
Loading…
Reference in New Issue
Block a user