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
121 lines (107 loc) · 3.73 KB
/
TestNested.java
File metadata and controls
121 lines (107 loc) · 3.73 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.jsoniter.output;
import com.jsoniter.annotation.JsonProperty;
import com.jsoniter.spi.JsoniterSpi;
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 {
static {
// JsonStream.setMode(EncodingMode.DYNAMIC_MODE);
}
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";
String output = JsonStream.serialize(new TestObject1[]{obj1});
assertTrue(output.contains("field1"));
assertTrue(output.contains("field2"));
}
public void test_collection_of_objects() throws IOException {
final TestObject1 obj1 = new TestObject1();
obj1.field1 = "1";
obj1.field2 = "2";
String output = JsonStream.serialize(new TypeLiteral<List<TestObject1>>() {
}, new ArrayList() {{
add(obj1);
}});
assertTrue(output.contains("field1"));
assertTrue(output.contains("field2"));
}
public static class TestObject2 {
public TestObject1[] objs;
}
public void test_object_of_array() throws IOException {
if (JsoniterSpi.getCurrentConfig().encodingMode() != EncodingMode.REFLECTION_MODE) {
return;
}
JsonStream.setIndentionStep(2);
try {
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());
} finally {
JsonStream.setIndentionStep(0);
}
}
public void test_map_of_objects() throws IOException {
if (JsoniterSpi.getCurrentConfig().encodingMode() != EncodingMode.REFLECTION_MODE) {
return;
}
JsonStream.setIndentionStep(2);
try {
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());
} finally {
JsonStream.setIndentionStep(0);
}
}
public static class TestObject3 {
@JsonProperty(defaultValueToOmit = "void")
public TestObject3 reference;
}
public void test_recursive_class() {
// recursive reference will not be supported
// however recursive structure is supported
TestObject3 obj = new TestObject3();
assertEquals("{\"reference\":null}", JsonStream.serialize(obj));
}
}