forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestObject.java
More file actions
109 lines (88 loc) · 2.85 KB
/
TestObject.java
File metadata and controls
109 lines (88 loc) · 2.85 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
package com.jsoniter.output;
import com.jsoniter.annotation.JsonIgnore;
import com.jsoniter.annotation.JsoniterAnnotationSupport;
import com.jsoniter.spi.TypeLiteral;
import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class TestObject extends TestCase {
static {
JsoniterAnnotationSupport.enable();
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 void test_field() throws IOException {
TestObject1 obj = new TestObject1();
obj.field1 = "hello";
stream.writeVal(obj);
stream.close();
assertEquals("{'field1':'hello'}".replace('\'', '"'), baos.toString());
}
public static class TestObject2 {
@JsonIgnore
private String field1;
public String getField1() {
return field1;
}
}
public void test_getter() throws IOException {
TestObject2 obj = new TestObject2();
obj.field1 = "hello";
stream.writeVal(obj);
stream.close();
assertEquals("{'field1':'hello'}".replace('\'', '"'), baos.toString());
}
public void test_null() throws IOException {
stream.writeVal(new TypeLiteral<TestObject2>() {
}, null);
stream.close();
assertEquals("null".replace('\'', '"'), baos.toString());
}
public static class TestObject3 {
}
public void test_empty_object() throws IOException {
stream.writeVal(new TestObject3());
stream.close();
assertEquals("{}".replace('\'', '"'), baos.toString());
}
public static class TestObject4 {
public String field1;
}
public void test_null_field() throws IOException {
TestObject4 obj = new TestObject4();
stream.writeVal(obj);
stream.close();
assertEquals("{'field1':null}".replace('\'', '"'), baos.toString());
}
public static enum MyEnum {
HELLO
}
public static class TestObject5 {
public MyEnum field1;
}
public void test_enum() throws IOException {
TestObject5 obj = new TestObject5();
obj.field1 = MyEnum.HELLO;
stream.writeVal(obj);
stream.close();
assertEquals("{'field1':'HELLO'}".replace('\'', '"'), baos.toString());
}
public static class TestObject6 {
public int[] field1;
}
public void test_array_field() throws IOException {
TestObject6 obj = new TestObject6();
obj.field1 = new int[]{1, 2, 3};
stream.writeVal(obj);
stream.close();
assertEquals("{\"field1\":[1,2,3]}", baos.toString());
}
}