forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAnnotation.java
More file actions
95 lines (79 loc) · 2.55 KB
/
TestAnnotation.java
File metadata and controls
95 lines (79 loc) · 2.55 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
package com.jsoniter.output;
import com.jsoniter.annotation.JsonIgnore;
import com.jsoniter.annotation.JsonProperty;
import com.jsoniter.annotation.JsonUnwrapper;
import com.jsoniter.annotation.JsoniterAnnotationSupport;
import com.jsoniter.spi.Encoder;
import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public class TestAnnotation 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 {
@JsonProperty(to = {"field-1"})
public String field1;
}
public void test_property() throws IOException {
TestObject1 obj = new TestObject1();
obj.field1 = "hello";
stream.writeVal(obj);
stream.close();
assertEquals("{\"field-1\":\"hello\"}", baos.toString());
}
public static class TestObject2 {
@JsonProperty(encoder = Encoder.StringIntEncoder.class)
public int field1;
}
public void test_encoder() throws IOException {
TestObject2 obj = new TestObject2();
obj.field1 = 100;
stream.writeVal(obj);
stream.close();
assertEquals("{\"field1\":\"100\"}", baos.toString());
}
public static class TestObject3 {
@JsonIgnore
public int field1;
}
public void test_ignore() throws IOException {
TestObject3 obj = new TestObject3();
obj.field1 = 100;
stream.writeVal(obj);
stream.close();
assertEquals("{}", baos.toString());
}
public static class TestObject4 {
public int field1;
public int getField1() {
return field1;
}
}
public void test_name_conflict() throws IOException {
TestObject4 obj = new TestObject4();
stream.writeVal(obj);
stream.close();
assertEquals("{\"field1\":0}", baos.toString());
}
public static class TestObject5 {
@JsonUnwrapper
public void unwrap(JsonStream stream) throws IOException {
stream.writeObjectField("hello");
stream.writeVal("world");
}
}
public void test_unwrapper() throws IOException {
TestObject5 obj = new TestObject5();
stream.writeVal(obj);
stream.close();
assertEquals("{\"hello\":\"world\"}", baos.toString());
}
}