forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestGenerics.java
More file actions
61 lines (50 loc) · 2.35 KB
/
TestGenerics.java
File metadata and controls
61 lines (50 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.jsoniter;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.*;
import static org.junit.Assert.assertArrayEquals;
public class TestGenerics extends TestCase {
public void test_int_list() throws IOException {
JsonIterator iter = JsonIterator.parse("[1,2,3]");
List<Integer> val = iter.read(new TypeLiteral<ArrayList<Integer>>() {
});
assertArrayEquals(new Integer[]{1, 2, 3}, val.toArray(new Integer[0]));
}
public void test_string_list() throws IOException {
JsonIterator iter = JsonIterator.parse("['hello', 'world']".replace('\'', '"'));
List<String> val = iter.read(new TypeLiteral<List<String>>() {
});
assertArrayEquals(new String[]{"hello", "world"}, val.toArray(new String[0]));
}
public void test_linked_list() throws IOException {
JsonIterator iter = JsonIterator.parse("['hello', 'world']".replace('\'', '"'));
List<String> val = iter.read(new TypeLiteral<LinkedList<String>>() {
});
assertArrayEquals(new String[]{"hello", "world"}, val.toArray(new String[0]));
}
public void test_string_set() throws IOException {
JsonIterator iter = JsonIterator.parse("['hello']".replace('\'', '"'));
Set<String> val = iter.read(new TypeLiteral<Set<String>>() {
});
assertArrayEquals(new String[]{"hello"}, val.toArray(new String[0]));
}
public void test_string_map() throws IOException {
JsonIterator iter = JsonIterator.parse("{'hello': 'world'}".replace('\'', '"'));
Map<String, String> val = iter.read(new TypeLiteral<Map<String, String>>() {
});
assertEquals("world", val.get("hello"));
}
public void test_list_of_list() throws Exception {
JsonIterator iter = JsonIterator.parse("[[1,2],[3,4]]");
List<List<Integer>> listOfList = iter.read(new TypeLiteral<List<List<Integer>>>() {
});
System.out.println(listOfList);
assertEquals(Integer.valueOf(4), listOfList.get(1).get(1));
}
public void test_complex_object() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field1': 100, 'field2': [[1,2],[3,4]]}".replace('\'', '"'));
ComplexObject val = iter.read(ComplexObject.class);
assertEquals(100, val.field1);
assertEquals(4.0d, val.field2.get(1).get(1));
}
}