forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestNested.java
More file actions
89 lines (78 loc) · 2.64 KB
/
TestNested.java
File metadata and controls
89 lines (78 loc) · 2.64 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.jsoniter.output;
import com.jsoniter.spi.TypeLiteral;
import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestNested extends TestCase {
private ByteArrayOutputStream baos;
private JsonStream stream;
public void setUp() {
baos = new ByteArrayOutputStream();
stream = new JsonStream(baos, 4096);
}
public static class TestObject1 {
public String field1;
public String field2;
}
public void test_array_of_objects() throws IOException {
TestObject1 obj1 = new TestObject1();
obj1.field1 = "1";
obj1.field2 = "2";
stream.writeVal(new TestObject1[]{obj1});
stream.close();
assertEquals("[{'field1':'1','field2':'2'}]".replace('\'', '"'), baos.toString());
}
public void test_collection_of_objects() throws IOException {
final TestObject1 obj1 = new TestObject1();
obj1.field1 = "1";
obj1.field2 = "2";
stream.writeVal(new TypeLiteral<List<TestObject1>>() {
}, new ArrayList() {{
add(obj1);
}});
stream.close();
assertEquals("[{'field1':'1','field2':'2'}]".replace('\'', '"'), baos.toString());
}
public static class TestObject2 {
public TestObject1[] objs;
}
public void test_object_of_array() throws IOException {
stream.indentionStep = 2;
TestObject2 obj = new TestObject2();
obj.objs = new TestObject1[1];
obj.objs[0] = new TestObject1();
obj.objs[0].field1 = "1";
obj.objs[0].field2 = "2";
stream.writeVal(obj);
stream.close();
assertEquals("{\n" +
" \"objs\":[\n" +
" {\n" +
" \"field1\":\"1\",\n" +
" \"field2\":\"2\"\n" +
" }\n" +
" ]\n" +
"}".replace('\'', '"'), baos.toString());
}
public void test_map_of_objects() throws IOException {
stream.indentionStep = 2;
final TestObject1 obj1 = new TestObject1();
obj1.field1 = "1";
obj1.field2 = "2";
stream.writeVal(new TypeLiteral<Map<String, TestObject1>>() {
}, new HashMap() {{
put("hello", obj1);
}});
stream.close();
assertEquals("{\n" +
" \"hello\":{\n" +
" \"field1\":\"1\",\n" +
" \"field2\":\"2\"\n" +
" }\n" +
"}".replace('\'', '"'), baos.toString());
}
}