Skip to content

Commit

Permalink
Improve ArrayTypeAdapter for Object[] (#1716)
Browse files Browse the repository at this point in the history
* Improve ArrayTypeAdapter for Object[]

* Fix typo in test method names
  • Loading branch information
Marcono1234 authored Jul 31, 2022
1 parent 5f2513a commit a45c557
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ArrayTypeAdapter(Gson context, TypeAdapter<E> componentTypeAdapter, Class
return null;
}

List<E> list = new ArrayList<>();
ArrayList<E> list = new ArrayList<>();
in.beginArray();
while (in.hasNext()) {
E instance = componentTypeAdapter.read(in);
Expand All @@ -75,11 +75,20 @@ public ArrayTypeAdapter(Gson context, TypeAdapter<E> 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")
Expand Down
40 changes: 25 additions & 15 deletions gson/src/test/java/com/google/gson/functional/ArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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);
}
Expand Down Expand Up @@ -173,8 +172,8 @@ public void testArrayOfCollectionDeserialization() throws Exception {
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 {
Expand All @@ -201,15 +200,15 @@ 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));
assertTrue(json.contains(bagOfPrimitivesJson));
}

public void testArrayOfNullSerialization() {
Object[] array = new Object[] {null};
Object[] array = {null};
String json = gson.toJson(array);
assertEquals("[null]", json);
}
Expand All @@ -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"}
};
Expand All @@ -232,30 +231,41 @@ 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);
assertEquals("3m Co", items[0][0]);
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 = {
Expand Down

0 comments on commit a45c557

Please sign in to comment.