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
273 lines (219 loc) · 8.12 KB
/
TestAnnotation.java
File metadata and controls
273 lines (219 loc) · 8.12 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package com.jsoniter;
import com.jsoniter.annotation.*;
import com.jsoniter.any.Any;
import com.jsoniter.fuzzy.StringIntDecoder;
import com.jsoniter.spi.JsonException;
import com.jsoniter.spi.JsoniterSpi;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class TestAnnotation extends TestCase {
static {
JsoniterAnnotationSupport.enable();
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
// JsonIterator.setMode(DecodingMode.REFLECTION_MODE);
}
public static class TestObject1 {
@JsonProperty("field-1")
public int field1;
@JsonIgnore
public int field2;
}
public void test_rename() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field-1': 100}".replace('\'', '"'));
TestObject1 obj = iter.read(TestObject1.class);
assertEquals(100, obj.field1);
}
public void test_ignore() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field2': 100}".replace('\'', '"'));
TestObject1 obj = iter.read(TestObject1.class);
assertEquals(0, obj.field2);
}
public static class TestObject2 {
private int field1;
@JsonCreator
public TestObject2(@JsonProperty("field1") int field1) {
this.field1 = field1;
}
}
public void test_ctor() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
TestObject2 obj = iter.read(TestObject2.class);
assertEquals(100, obj.field1);
}
public static class TestObject3 {
public int field1;
@JsonCreator
private TestObject3() {
}
}
public void test_private_ctor() throws IOException {
JsoniterSpi.registerTypeDecoder(TestObject3.class, ReflectionDecoderFactory.create(TestObject3.class));
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
TestObject3 obj = iter.read(TestObject3.class);
assertEquals(100, obj.field1);
}
public static class TestObject4 {
private int field1;
private TestObject4() {
}
@JsonCreator
public static TestObject4 createObject(@JsonProperty(value = "field1") int field1) {
TestObject4 obj = new TestObject4();
obj.field1 = field1;
return obj;
}
}
public void test_static_factory() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
TestObject4 obj = iter.read(TestObject4.class);
assertEquals(100, obj.field1);
}
public static class TestObject5 {
private int field1;
public void setField1(int field1) {
this.field1 = field1;
}
}
public void test_single_param_setter() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
TestObject5 obj = iter.read(TestObject5.class);
assertEquals(100, obj.field1);
}
public static class TestObject6 {
private int field1;
@JsonWrapper
public void initialize(@JsonProperty("field1") int field1) {
this.field1 = field1;
}
}
public void test_multi_param_setter() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
TestObject6 obj = iter.read(TestObject6.class);
assertEquals(100, obj.field1);
}
public static class TestObject7 {
@JsonProperty(required = true)
public int field1;
@JsonMissingProperties
public List<String> missingProperties;
}
public void test_required_properties() throws IOException {
JsonIterator iter = JsonIterator.parse("{}");
TestObject7 obj = iter.read(TestObject7.class);
assertEquals(Arrays.asList("field1"), obj.missingProperties);
}
public static class TestObject8 {
@JsonCreator
public TestObject8(@JsonProperty(required = true) int param1) {
}
}
public void test_missing_ctor_arg() throws IOException {
JsonIterator iter = JsonIterator.parse("{}");
try {
iter.read(TestObject8.class);
fail();
} catch (JsonException e) {
System.out.println(e);
}
}
@JsonObject(asExtraForUnknownProperties = true)
public static class TestObject9 {
@JsonExtraProperties
public Map<String, Any> extraProperties;
}
public void test_extra_properties() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field1\": 100}");
TestObject9 obj = iter.read(TestObject9.class);
assertEquals(100, obj.extraProperties.get("field1").toInt());
}
public static class TestObject10 {
@JsonProperty(decoder = StringIntDecoder.class)
public int field1;
}
public void test_property_decoder() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field1\": \"100\"}");
TestObject10 obj = iter.read(TestObject10.class);
assertEquals(100, obj.field1);
}
public static class TestObject11 {
@JsonProperty(decoder = StringIntDecoder.class)
public Integer field1;
}
public void test_integer_property_decoder() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field1\": \"100\"}");
TestObject11 obj = iter.read(TestObject11.class);
assertEquals(Integer.valueOf(100), obj.field1);
}
public static class TestObject12 {
@JsonProperty(from = {"field_1", "field-1"})
public int field1;
}
public void test_bind_from_multiple_names() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field-1\": 100, \"field-1\": 101}");
TestObject12 obj = iter.read(TestObject12.class);
assertEquals(101, obj.field1);
}
@JsonObject(asExtraForUnknownProperties = true)
public static class TestObject13 {
}
public void test_unknown_properties() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field-1\": 100, \"field-1\": 101}");
try {
iter.read(TestObject13.class);
fail();
} catch (JsonException e) {
System.out.println(e);
}
}
public static class TestObject14 {
@JsonProperty(required = true)
public int field1;
@JsonMissingProperties
public List<String> missingProperties;
}
public void test_required_properties_not_missing() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field1\": 100}");
TestObject14 obj = iter.read(TestObject14.class);
assertNull(obj.missingProperties);
assertEquals(100, obj.field1);
}
@JsonObject(unknownPropertiesBlacklist = {"field1"})
public static class TestObject15 {
}
public void test_unknown_properties_blacklist() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"field1\": 100}");
try {
iter.read(TestObject15.class);
fail();
} catch (JsonException e) {
System.out.println(e);
}
}
public static class TestObject16 {
@JsonProperty(implementation = LinkedList.class)
public List<Integer> values;
}
public void test_specify_property() throws IOException {
JsonIterator iter = JsonIterator.parse("{\"values\": [100]}");
TestObject16 obj = iter.read(TestObject16.class);
assertEquals(Arrays.asList(100), obj.values);
assertEquals(LinkedList.class, obj.values.getClass());
}
public static class TestObject17 {
public int field1;
public void setField1(int field1) {
this.field1 = field1;
}
@JsonCreator
public void initialize(@JsonProperty("field1") int field1) {
}
}
public void test_name_conflict() throws IOException {
JsonIterator iter = JsonIterator.parse("{}");
assertNotNull(iter.read(TestObject17.class));
}
}