Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ArrayTypeAdapter for Object[] #1716

Merged
merged 3 commits into from Jul 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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