137 lines
4.4 KiB
Java
137 lines
4.4 KiB
Java
/*
|
|
* Copyright (C) 2008 Google Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
package com.google.gson.functional;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.common.MoreAsserts;
|
|
import com.google.gson.common.TestTypes.MyEnum;
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
import java.lang.reflect.Type;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
|
|
/**
|
|
* Functional tests for Json serialization and deserialization of arrays.
|
|
*
|
|
* @author Inderjeet Singh
|
|
* @author Joel Leitch
|
|
*/
|
|
public class ArrayTest extends TestCase {
|
|
private Gson gson;
|
|
|
|
@Override
|
|
protected void setUp() throws Exception {
|
|
super.setUp();
|
|
gson = new Gson();
|
|
}
|
|
|
|
public void testTopLevelArrayOfIntsSerialization() {
|
|
int[] target = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
|
assertEquals("[1,2,3,4,5,6,7,8,9]", gson.toJson(target));
|
|
}
|
|
|
|
public void testTopLevelArrayOfIntsDeserialization() {
|
|
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);
|
|
MoreAsserts.assertEquals(expected, actual);
|
|
}
|
|
|
|
public void testEmptyArraySerialization() {
|
|
int[] target = {};
|
|
assertEquals("[]", gson.toJson(target));
|
|
}
|
|
|
|
public void testEmptyArrayDeserialization() {
|
|
int[] actualObject = gson.fromJson("[]", int[].class);
|
|
assertTrue(actualObject.length == 0);
|
|
|
|
Integer[] actualObject2 = gson.fromJson("[]", Integer[].class);
|
|
assertTrue(actualObject2.length == 0);
|
|
}
|
|
|
|
public void testNullsInArraySerialization() {
|
|
String[] array = {"foo", null, "bar"};
|
|
String expected = "[\"foo\",null,\"bar\"]";
|
|
String json = gson.toJson(array);
|
|
assertEquals(expected, json);
|
|
}
|
|
|
|
public void testNullsInArrayDeserialization() {
|
|
String json = "[\"foo\",null,\"bar\"]";
|
|
String[] expected = {"foo", null, "bar"};
|
|
String[] target = gson.fromJson(json, expected.getClass());
|
|
for (int i = 0; i < expected.length; ++i) {
|
|
assertEquals(expected[i], target[i]);
|
|
}
|
|
}
|
|
|
|
public void testArrayOfStringsSerialization() {
|
|
String[] target = {"Hello", "World"};
|
|
assertEquals("[\"Hello\",\"World\"]", gson.toJson(target));
|
|
}
|
|
|
|
public void testArrayOfStringsDeserialization() {
|
|
String json = "[\"Hello\",\"World\"]";
|
|
String[] target = gson.fromJson(json, String[].class);
|
|
assertEquals("Hello", target[0]);
|
|
assertEquals("World", target[1]);
|
|
}
|
|
|
|
public void testTopLevelEnumInASingleElementArrayDeserialization() {
|
|
String json = "[" + MyEnum.VALUE1.getExpectedJson() + "]";
|
|
MyEnum target = gson.fromJson(json, MyEnum.class);
|
|
assertEquals(json, "[" + target.getExpectedJson() + "]");
|
|
}
|
|
|
|
@SuppressWarnings("unchecked")
|
|
public void testArrayOfCollectionSerialization() throws Exception {
|
|
StringBuilder sb = new StringBuilder("[");
|
|
int arraySize = 3;
|
|
|
|
Type typeToSerialize = new TypeToken<Collection<Integer>[]>() {}.getType();
|
|
Collection<Integer>[] arrayOfCollection = new ArrayList[arraySize];
|
|
for (int i = 0; i < arraySize; ++i) {
|
|
int startValue = (3 * i) + 1;
|
|
sb.append('[').append(startValue).append(',').append(startValue + 1).append(']');
|
|
ArrayList<Integer> tmpList = new ArrayList<Integer>();
|
|
tmpList.add(startValue);
|
|
tmpList.add(startValue + 1);
|
|
arrayOfCollection[i] = tmpList;
|
|
|
|
if (i < arraySize - 1) {
|
|
sb.append(',');
|
|
}
|
|
}
|
|
sb.append(']');
|
|
|
|
String json = gson.toJson(arrayOfCollection, typeToSerialize);
|
|
assertEquals(sb.toString(), json);
|
|
}
|
|
|
|
public void testArrayOfCollectionDeserialization() throws Exception {
|
|
String json = "[[1,2],[3,4]]";
|
|
Type type = new TypeToken<Collection<Integer>[]>() {}.getType();
|
|
Collection<Integer>[] target = gson.fromJson(json, type);
|
|
|
|
assertEquals(2, target.length);
|
|
MoreAsserts.assertEquals(new Integer[] { 1, 2 }, target[0].toArray(new Integer[0]));
|
|
MoreAsserts.assertEquals(new Integer[] { 3, 4 }, target[1].toArray(new Integer[0]));
|
|
}
|
|
}
|