Improve ArrayTypeAdapter for Object[] (#1716)

* Improve ArrayTypeAdapter for Object[]

* Fix typo in test method names
This commit is contained in:
Marcono1234 2022-07-31 23:49:02 +02:00 committed by GitHub
parent 5f2513a407
commit a45c55739f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 20 deletions

View File

@ -66,7 +66,7 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
return null;
}
List<E> list = new ArrayList<>();
ArrayList<E> list = new ArrayList<>();
in.beginArray();
while (in.hasNext()) {
E instance = componentTypeAdapter.read(in);
@ -75,11 +75,20 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
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")

View File

@ -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 class ArrayTest extends TestCase {
}
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 class ArrayTest extends TestCase {
Collection<Integer>[] 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 class ArrayTest extends TestCase {
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 class ArrayTest extends TestCase {
}
public void testArrayOfNullSerialization() {
Object[] array = new Object[] {null};
Object[] array = {null};
String json = gson.toJson(array);
assertEquals("[null]", json);
}
@ -222,8 +221,8 @@ public class ArrayTest extends TestCase {
/**
* 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 class ArrayTest extends TestCase {
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 class ArrayTest extends TestCase {
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 = {