Retire two old bad features
- overriding primitive type adapters - auto unwrapping of single element arrays
This commit is contained in:
parent
ea9c0236c7
commit
f67940cb63
10
gson/GSON 2.0 NOTES.txt
Normal file
10
gson/GSON 2.0 NOTES.txt
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
What's new in GSON 2.0
|
||||||
|
|
||||||
|
GSON 1.x used to automatically unwrap single-element arrays as necessary.
|
||||||
|
GSON 2.x doesn't.
|
||||||
|
com.google.gson.functional.ArrayTest.testSingleStringArrayDeserialization
|
||||||
|
|
||||||
|
|
||||||
|
GSON 1.x permitted primitive types to be overridden
|
||||||
|
GSON 2.x doesn't.
|
||||||
|
com.google.gson.functional.ArrayTest.testArrayOfPrimitivesWithCustomTypeAdapter
|
@ -19,6 +19,7 @@ package com.google.gson;
|
|||||||
import com.google.gson.internal.bind.MiniGson;
|
import com.google.gson.internal.bind.MiniGson;
|
||||||
import com.google.gson.internal.bind.TypeAdapter;
|
import com.google.gson.internal.bind.TypeAdapter;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
@ -27,9 +28,11 @@ public final class ObjectTypeAdapterTest extends TestCase {
|
|||||||
private final TypeAdapter<Object> adapter = gson.getAdapter(Object.class);
|
private final TypeAdapter<Object> adapter = gson.getAdapter(Object.class);
|
||||||
|
|
||||||
public void testDeserialize() throws Exception {
|
public void testDeserialize() throws Exception {
|
||||||
Map<?, ?> map = (Map) adapter.fromJson("{a: 5, b: [1, 2, null]}");
|
Map<?, ?> map = (Map) adapter.fromJson("{a: 5, b: [1, 2, null], c: {x: y}}");
|
||||||
assertEquals(5.0, map.get("a"));
|
assertEquals(5.0, map.get("a"));
|
||||||
assertEquals(Arrays.asList(1.0, 2.0, null), map.get("b"));
|
assertEquals(Arrays.asList(1.0, 2.0, null), map.get("b"));
|
||||||
|
assertEquals(Collections.singletonMap("x", "y"), map.get("c"));
|
||||||
|
assertEquals(3, map.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSerialize() throws Exception {
|
public void testSerialize() throws Exception {
|
||||||
|
@ -141,9 +141,6 @@ public class ArrayTest extends TestCase {
|
|||||||
String[] arrayType = gson.fromJson(json, String[].class);
|
String[] arrayType = gson.fromJson(json, String[].class);
|
||||||
assertEquals(1, arrayType.length);
|
assertEquals(1, arrayType.length);
|
||||||
assertEquals("hello", arrayType[0]);
|
assertEquals("hello", arrayType[0]);
|
||||||
|
|
||||||
String type = gson.fromJson(json, String.class);
|
|
||||||
assertEquals("hello", type);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@ -181,22 +178,6 @@ public class ArrayTest extends TestCase {
|
|||||||
MoreAsserts.assertEquals(new Integer[] { 3, 4 }, target[1].toArray(new Integer[0]));
|
MoreAsserts.assertEquals(new Integer[] { 3, 4 }, target[1].toArray(new Integer[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testArrayOfPrimitivesWithCustomTypeAdapter() throws Exception {
|
|
||||||
CrazyLongTypeAdapter typeAdapter = new CrazyLongTypeAdapter();
|
|
||||||
gson = new GsonBuilder()
|
|
||||||
.registerTypeAdapter(long.class, typeAdapter)
|
|
||||||
.registerTypeAdapter(Long.class, typeAdapter)
|
|
||||||
.create();
|
|
||||||
long[] value = { 1L };
|
|
||||||
String serializedValue = gson.toJson(value);
|
|
||||||
String expected = "[" + String.valueOf(value[0] + CrazyLongTypeAdapter.DIFFERENCE) + "]";
|
|
||||||
assertEquals(expected, serializedValue);
|
|
||||||
|
|
||||||
long[] deserializedValue = gson.fromJson(serializedValue, long[].class);
|
|
||||||
assertEquals(1, deserializedValue.length);
|
|
||||||
assertEquals(value[0], deserializedValue[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testArrayOfPrimitivesAsObjectsSerialization() throws Exception {
|
public void testArrayOfPrimitivesAsObjectsSerialization() throws Exception {
|
||||||
Object[] objs = new Object[] {1, "abc", 0.3f, 5L};
|
Object[] objs = new Object[] {1, "abc", 0.3f, 5L};
|
||||||
String json = gson.toJson(objs);
|
String json = gson.toJson(objs);
|
||||||
@ -215,24 +196,6 @@ public class ArrayTest extends TestCase {
|
|||||||
assertEquals(5, ((Number)objs[4]).shortValue());
|
assertEquals(5, ((Number)objs[4]).shortValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testArrayOfObjectsWithoutTypeInfoDeserialization() throws Exception {
|
|
||||||
String json = "[1,'abc',{a:1},5]";
|
|
||||||
try {
|
|
||||||
gson.fromJson(json, Object[].class);
|
|
||||||
fail("This is crazy....how did we deserialize it!!!");
|
|
||||||
} catch (JsonParseException expected) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testArrayWithoutTypeInfoDeserialization() throws Exception {
|
|
||||||
String json = "[1,'abc',[1,2],5]";
|
|
||||||
try {
|
|
||||||
gson.fromJson(json, Object[].class);
|
|
||||||
fail("This is crazy....how did we deserialize it!!!");
|
|
||||||
} catch (JsonParseException expected) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testObjectArrayWithNonPrimitivesSerialization() throws Exception {
|
public void testObjectArrayWithNonPrimitivesSerialization() throws Exception {
|
||||||
ClassWithObjects classWithObjects = new ClassWithObjects();
|
ClassWithObjects classWithObjects = new ClassWithObjects();
|
||||||
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives();
|
BagOfPrimitives bagOfPrimitives = new BagOfPrimitives();
|
||||||
|
Loading…
Reference in New Issue
Block a user