From f67940cb638fcf4d3443b057176bb1b84d054ac0 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Fri, 9 Sep 2011 04:20:25 +0000 Subject: [PATCH] Retire two old bad features - overriding primitive type adapters - auto unwrapping of single element arrays --- gson/GSON 2.0 NOTES.txt | 10 +++++ .../google/gson/ObjectTypeAdapterTest.java | 5 ++- .../com/google/gson/functional/ArrayTest.java | 37 ------------------- 3 files changed, 14 insertions(+), 38 deletions(-) create mode 100644 gson/GSON 2.0 NOTES.txt diff --git a/gson/GSON 2.0 NOTES.txt b/gson/GSON 2.0 NOTES.txt new file mode 100644 index 00000000..b251b24d --- /dev/null +++ b/gson/GSON 2.0 NOTES.txt @@ -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 \ No newline at end of file diff --git a/gson/src/test/java/com/google/gson/ObjectTypeAdapterTest.java b/gson/src/test/java/com/google/gson/ObjectTypeAdapterTest.java index 852d0ba7..5a60ca63 100644 --- a/gson/src/test/java/com/google/gson/ObjectTypeAdapterTest.java +++ b/gson/src/test/java/com/google/gson/ObjectTypeAdapterTest.java @@ -19,6 +19,7 @@ package com.google.gson; import com.google.gson.internal.bind.MiniGson; import com.google.gson.internal.bind.TypeAdapter; import java.util.Arrays; +import java.util.Collections; import java.util.Map; import junit.framework.TestCase; @@ -27,9 +28,11 @@ public final class ObjectTypeAdapterTest extends TestCase { private final TypeAdapter adapter = gson.getAdapter(Object.class); 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(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 { diff --git a/gson/src/test/java/com/google/gson/functional/ArrayTest.java b/gson/src/test/java/com/google/gson/functional/ArrayTest.java index a7808dc2..93e3a6bb 100644 --- a/gson/src/test/java/com/google/gson/functional/ArrayTest.java +++ b/gson/src/test/java/com/google/gson/functional/ArrayTest.java @@ -141,9 +141,6 @@ public class ArrayTest extends TestCase { String[] arrayType = gson.fromJson(json, String[].class); assertEquals(1, arrayType.length); assertEquals("hello", arrayType[0]); - - String type = gson.fromJson(json, String.class); - assertEquals("hello", type); } @SuppressWarnings("unchecked") @@ -181,22 +178,6 @@ public class ArrayTest extends TestCase { 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 { Object[] objs = new Object[] {1, "abc", 0.3f, 5L}; String json = gson.toJson(objs); @@ -215,24 +196,6 @@ public class ArrayTest extends TestCase { 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 { ClassWithObjects classWithObjects = new ClassWithObjects(); BagOfPrimitives bagOfPrimitives = new BagOfPrimitives();