Improve ArrayTypeAdapter for Object[] (#1716)
* Improve ArrayTypeAdapter for Object[] * Fix typo in test method names
This commit is contained in:
parent
5f2513a407
commit
a45c55739f
|
@ -66,7 +66,7 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
List<E> list = new ArrayList<>();
|
ArrayList<E> list = new ArrayList<>();
|
||||||
in.beginArray();
|
in.beginArray();
|
||||||
while (in.hasNext()) {
|
while (in.hasNext()) {
|
||||||
E instance = componentTypeAdapter.read(in);
|
E instance = componentTypeAdapter.read(in);
|
||||||
|
@ -75,12 +75,21 @@ public final class ArrayTypeAdapter<E> extends TypeAdapter<Object> {
|
||||||
in.endArray();
|
in.endArray();
|
||||||
|
|
||||||
int size = list.size();
|
int size = list.size();
|
||||||
|
// Have to copy primitives one by one to primitive array
|
||||||
|
if (componentType.isPrimitive()) {
|
||||||
Object array = Array.newInstance(componentType, size);
|
Object array = Array.newInstance(componentType, size);
|
||||||
for (int i = 0; i < size; i++) {
|
for (int i = 0; i < size; i++) {
|
||||||
Array.set(array, i, list.get(i));
|
Array.set(array, i, list.get(i));
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
// But for Object[] can use ArrayList.toArray
|
||||||
|
else {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
E[] array = (E[]) Array.newInstance(componentType, size);
|
||||||
|
return list.toArray(array);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Override public void write(JsonWriter out, Object array) throws IOException {
|
@Override public void write(JsonWriter out, Object array) throws IOException {
|
||||||
|
|
|
@ -16,20 +16,19 @@
|
||||||
|
|
||||||
package com.google.gson.functional;
|
package com.google.gson.functional;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.JsonParseException;
|
import com.google.gson.JsonParseException;
|
||||||
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
import com.google.gson.common.TestTypes.BagOfPrimitives;
|
||||||
import com.google.gson.common.TestTypes.ClassWithObjects;
|
import com.google.gson.common.TestTypes.ClassWithObjects;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
import junit.framework.TestCase;
|
||||||
/**
|
/**
|
||||||
* Functional tests for Json serialization and deserialization of arrays.
|
* Functional tests for Json serialization and deserialization of arrays.
|
||||||
*
|
*
|
||||||
|
@ -201,7 +200,7 @@ public class ArrayTest extends TestCase {
|
||||||
String classWithObjectsJson = gson.toJson(classWithObjects);
|
String classWithObjectsJson = gson.toJson(classWithObjects);
|
||||||
String bagOfPrimitivesJson = gson.toJson(bagOfPrimitives);
|
String bagOfPrimitivesJson = gson.toJson(bagOfPrimitives);
|
||||||
|
|
||||||
Object[] objects = new Object[] { classWithObjects, bagOfPrimitives };
|
Object[] objects = {classWithObjects, bagOfPrimitives};
|
||||||
String json = gson.toJson(objects);
|
String json = gson.toJson(objects);
|
||||||
|
|
||||||
assertTrue(json.contains(classWithObjectsJson));
|
assertTrue(json.contains(classWithObjectsJson));
|
||||||
|
@ -209,7 +208,7 @@ public class ArrayTest extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testArrayOfNullSerialization() {
|
public void testArrayOfNullSerialization() {
|
||||||
Object[] array = new Object[] {null};
|
Object[] array = {null};
|
||||||
String json = gson.toJson(array);
|
String json = gson.toJson(array);
|
||||||
assertEquals("[null]", json);
|
assertEquals("[null]", json);
|
||||||
}
|
}
|
||||||
|
@ -222,8 +221,8 @@ public class ArrayTest extends TestCase {
|
||||||
/**
|
/**
|
||||||
* Regression tests for Issue 272
|
* Regression tests for Issue 272
|
||||||
*/
|
*/
|
||||||
public void testMultidimenstionalArraysSerialization() {
|
public void testMultidimensionalArraysSerialization() {
|
||||||
String[][] items = new String[][]{
|
String[][] items = {
|
||||||
{"3m Co", "71.72", "0.02", "0.03", "4/2 12:00am", "Manufacturing"},
|
{"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"}
|
{"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\"]]"));
|
assertTrue(json.contains("Manufacturing\"]]"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMultiDimenstionalObjectArraysSerialization() {
|
public void testMultidimensionalObjectArraysSerialization() {
|
||||||
Object[][] array = new Object[][] { new Object[] { 1, 2 } };
|
Object[][] array = {new Object[] { 1, 2 }};
|
||||||
assertEquals("[[1,2]]", gson.toJson(array));
|
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
|
* Regression test for Issue 205
|
||||||
*/
|
*/
|
||||||
public void testMixingTypesInObjectArraySerialization() {
|
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));
|
assertEquals("[1,2,[\"one\",\"two\",3]]", gson.toJson(array));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Regression tests for Issue 272
|
* 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'],"
|
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']]";
|
+ "['Alcoa Inc','29.01','0.42','1.47','4/1 12:00am','Manufacturing']]";
|
||||||
String[][] items = gson.fromJson(json, String[][].class);
|
String[][] items = gson.fromJson(json, String[][].class);
|
||||||
|
@ -256,6 +260,12 @@ public class ArrayTest extends TestCase {
|
||||||
assertEquals("Manufacturing", items[1][5]);
|
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 */
|
/** http://code.google.com/p/google-gson/issues/detail?id=342 */
|
||||||
public void testArrayElementsAreArrays() {
|
public void testArrayElementsAreArrays() {
|
||||||
Object[] stringArrays = {
|
Object[] stringArrays = {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user