diff --git a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java index efaa834f19..3995c63169 100644 --- a/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/internal/bind/ArrayTypeAdapter.java @@ -66,7 +66,7 @@ public ArrayTypeAdapter(Gson context, TypeAdapter componentTypeAdapter, Class return null; } - List list = new ArrayList<>(); + ArrayList list = new ArrayList<>(); in.beginArray(); while (in.hasNext()) { E instance = componentTypeAdapter.read(in); @@ -75,11 +75,20 @@ public ArrayTypeAdapter(Gson context, TypeAdapter componentTypeAdapter, Class in.endArray(); int size = list.size(); - Object array = Array.newInstance(componentType, size); - for (int i = 0; i < size; i++) { - Array.set(array, i, list.get(i)); + // Have to copy primitives one by one to primitive array + if (componentType.isPrimitive()) { + Object array = Array.newInstance(componentType, size); + for (int i = 0; i < size; i++) { + Array.set(array, i, list.get(i)); + } + return array; + } + // But for Object[] can use ArrayList.toArray + else { + @SuppressWarnings("unchecked") + E[] array = (E[]) Array.newInstance(componentType, size); + return list.toArray(array); } - return array; } @SuppressWarnings("unchecked") 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 da8be85bd4..1aa56561ef 100644 --- a/gson/src/test/java/com/google/gson/functional/ArrayTest.java +++ b/gson/src/test/java/com/google/gson/functional/ArrayTest.java @@ -16,20 +16,19 @@ package com.google.gson.functional; +import static org.junit.Assert.assertArrayEquals; + import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonParseException; import com.google.gson.common.TestTypes.BagOfPrimitives; import com.google.gson.common.TestTypes.ClassWithObjects; import com.google.gson.reflect.TypeToken; - -import junit.framework.TestCase; -import static org.junit.Assert.assertArrayEquals; - import java.lang.reflect.Type; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; +import junit.framework.TestCase; /** * Functional tests for Json serialization and deserialization of arrays. * @@ -51,7 +50,7 @@ public void testTopLevelArrayOfIntsSerialization() { } public void testTopLevelArrayOfIntsDeserialization() { - int[] expected = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + int[] expected = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int[] actual = gson.fromJson("[1,2,3,4,5,6,7,8,9]", int[].class); assertArrayEquals(expected, actual); } @@ -173,8 +172,8 @@ public void testArrayOfCollectionDeserialization() throws Exception { Collection[] target = gson.fromJson(json, type); assertEquals(2, target.length); - assertArrayEquals(new Integer[] { 1, 2 }, target[0].toArray(new Integer[0])); - assertArrayEquals(new Integer[] { 3, 4 }, target[1].toArray(new Integer[0])); + assertArrayEquals(new Integer[] {1, 2}, target[0].toArray(new Integer[0])); + assertArrayEquals(new Integer[] {3, 4}, target[1].toArray(new Integer[0])); } public void testArrayOfPrimitivesAsObjectsSerialization() throws Exception { @@ -201,7 +200,7 @@ public void testObjectArrayWithNonPrimitivesSerialization() throws Exception { String classWithObjectsJson = gson.toJson(classWithObjects); String bagOfPrimitivesJson = gson.toJson(bagOfPrimitives); - Object[] objects = new Object[] { classWithObjects, bagOfPrimitives }; + Object[] objects = {classWithObjects, bagOfPrimitives}; String json = gson.toJson(objects); assertTrue(json.contains(classWithObjectsJson)); @@ -209,7 +208,7 @@ public void testObjectArrayWithNonPrimitivesSerialization() throws Exception { } public void testArrayOfNullSerialization() { - Object[] array = new Object[] {null}; + Object[] array = {null}; String json = gson.toJson(array); assertEquals("[null]", json); } @@ -222,8 +221,8 @@ public void testArrayOfNullDeserialization() { /** * Regression tests for Issue 272 */ - public void testMultidimenstionalArraysSerialization() { - String[][] items = new String[][]{ + public void testMultidimensionalArraysSerialization() { + String[][] items = { {"3m Co", "71.72", "0.02", "0.03", "4/2 12:00am", "Manufacturing"}, {"Alcoa Inc", "29.01", "0.42", "1.47", "4/1 12:00am", "Manufacturing"} }; @@ -232,23 +231,28 @@ public void testMultidimenstionalArraysSerialization() { assertTrue(json.contains("Manufacturing\"]]")); } - public void testMultiDimenstionalObjectArraysSerialization() { - Object[][] array = new Object[][] { new Object[] { 1, 2 } }; + public void testMultidimensionalObjectArraysSerialization() { + Object[][] array = {new Object[] { 1, 2 }}; assertEquals("[[1,2]]", gson.toJson(array)); } + public void testMultidimensionalPrimitiveArraysSerialization() { + int[][] array = {{1, 2}, {3, 4}}; + assertEquals("[[1,2],[3,4]]", gson.toJson(array)); + } + /** * Regression test for Issue 205 */ public void testMixingTypesInObjectArraySerialization() { - Object[] array = new Object[] { 1, 2, new Object[] { "one", "two", 3 } }; + Object[] array = {1, 2, new Object[] {"one", "two", 3}}; assertEquals("[1,2,[\"one\",\"two\",3]]", gson.toJson(array)); } /** * Regression tests for Issue 272 */ - public void testMultidimenstionalArraysDeserialization() { + public void testMultidimensionalArraysDeserialization() { String json = "[['3m Co','71.72','0.02','0.03','4/2 12:00am','Manufacturing']," + "['Alcoa Inc','29.01','0.42','1.47','4/1 12:00am','Manufacturing']]"; String[][] items = gson.fromJson(json, String[][].class); @@ -256,6 +260,12 @@ public void testMultidimenstionalArraysDeserialization() { assertEquals("Manufacturing", items[1][5]); } + public void testMultidimensionalPrimitiveArraysDeserialization() { + String json = "[[1,2],[3,4]]"; + int[][] expected = {{1, 2}, {3, 4}}; + assertArrayEquals(expected, gson.fromJson(json, int[][].class)); + } + /** http://code.google.com/p/google-gson/issues/detail?id=342 */ public void testArrayElementsAreArrays() { Object[] stringArrays = {