forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCustomizeField.java
More file actions
243 lines (211 loc) · 8.25 KB
/
TestCustomizeField.java
File metadata and controls
243 lines (211 loc) · 8.25 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
package com.jsoniter;
import com.jsoniter.spi.*;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class TestCustomizeField extends TestCase {
static {
// JsonIterator.enableStrictMode();
}
public static class TestObject1 {
public String field1;
}
public void test_customize_field_decoder() throws IOException {
ExtensionManager.registerFieldDecoder(TestObject1.class, "field1", new Decoder() {
@Override
public Object decode(JsonIterator iter) throws IOException {
return Integer.toString(iter.readInt());
}
});
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
TestObject1 myObject = iter.read(TestObject1.class);
assertEquals("100", myObject.field1);
}
public static class TestObject2 {
public int field1;
}
public void test_customize_int_field() throws IOException {
ExtensionManager.registerFieldDecoder(TestObject2.class, "field1", new Decoder.IntDecoder() {
@Override
public int decodeInt(JsonIterator iter) throws IOException {
return Integer.valueOf(iter.readString());
}
});
JsonIterator iter = JsonIterator.parse("{'field1': '100'}".replace('\'', '"'));
TestObject2 myObject = iter.read(TestObject2.class);
assertEquals(100, myObject.field1);
}
public static class TestObject4 {
public int field1;
}
public void test_rename_field() throws IOException {
ExtensionManager.registerExtension(new EmptyExtension() {
@Override
public void updateClassDescriptor(ClassDescriptor desc) {
if (desc.clazz != TestObject4.class) {
return;
}
for (Binding field : desc.allDecoderBindings()) {
if (field.name.equals("field1")) {
field.fromNames = new String[]{"field_1", "Field1"};
}
}
}
});
JsonIterator iter = JsonIterator.parse("{'field_1': 100}{'Field1': 101}".replace('\'', '"'));
TestObject4 myObject1 = iter.read(TestObject4.class);
assertEquals(100, myObject1.field1);
TestObject4 myObject2 = iter.read(TestObject4.class);
assertEquals(101, myObject2.field1);
}
public static class TestObject5 {
private int field1;
public TestObject5(int field1) {
this.field1 = field1;
}
}
public void test_rename_ctor_param() throws IOException {
ExtensionManager.registerExtension(new EmptyExtension() {
@Override
public void updateClassDescriptor(final ClassDescriptor desc) {
if (desc.clazz == TestObject5.class) {
desc.ctor = new ConstructorDescriptor() {{
parameters = (List) Arrays.asList(new Binding(desc.clazz, desc.lookup, int.class) {{
name = "param2";
}});
}};
}
}
});
JsonIterator iter = JsonIterator.parse("{'param2': 1000}".replace('\'', '"'));
TestObject5 obj = iter.read(TestObject5.class);
assertEquals(1000, obj.field1);
}
public static class TestObject6 {
String field;
public void setField(String field) {
this.field = field;
}
}
public void test_rename_setter() throws IOException {
ExtensionManager.registerExtension(new EmptyExtension() {
@Override
public void updateClassDescriptor(ClassDescriptor desc) {
for (Binding field : desc.allDecoderBindings()) {
if (field.clazz == TestObject6.class && field.name.equals("field")) {
field.fromNames = new String[]{"field_1", "Field1"};
}
}
}
});
JsonIterator iter = JsonIterator.parse("{'field_1': 'hello'}".replace('\'', '"'));
TestObject6 obj = iter.read(TestObject6.class);
assertEquals("hello", obj.field);
}
public static class TestObject7 {
public int field1;
}
public void test_customize_field_decoding_using_extension() throws IOException {
ExtensionManager.registerExtension(new EmptyExtension() {
public void updateClassDescriptor(ClassDescriptor desc) {
for (Binding field : desc.allDecoderBindings()) {
if (field.clazz == TestObject7.class && field.name.equals("field1")) {
field.decoder = new Decoder.IntDecoder() {
@Override
public int decodeInt(JsonIterator iter1) throws IOException {
return Integer.valueOf(iter1.readString());
}
};
}
}
}
});
JsonIterator iter = JsonIterator.parse("{'field1': '100'}".replace('\'', '"'));
TestObject7 myObject = iter.read(TestObject7.class);
assertEquals(100, myObject.field1);
}
public static class TestObject8 {
}
public void test_throw_exception_on_unknown_field() throws IOException {
ExtensionManager.registerExtension(new EmptyExtension() {
@Override
public void updateClassDescriptor(ClassDescriptor desc) {
if (desc.clazz == TestObject8.class) {
desc.failOnUnknownFields = true;
}
}
});
JsonIterator iter = JsonIterator.parse("{'field1': '100'}".replace('\'', '"'));
try {
iter.read(TestObject8.class);
fail("should throw exception");
} catch (Exception e) {
// System.out.println(e);
}
}
public static class TestObject9 {
public String field1;
public String field2;
}
public void test_mandatory_fields_not_missing() throws IOException {
ExtensionManager.registerExtension(new EmptyExtension() {
@Override
public void updateClassDescriptor(ClassDescriptor desc) {
if (desc.clazz != TestObject9.class) {
return;
}
for (Binding field : desc.allDecoderBindings()) {
field.failOnMissing = true;
}
}
});
JsonIterator iter = JsonIterator.parse("{'field1': '100', 'field2': '200'}".replace('\'', '"'));
assertEquals("100", iter.read(TestObject9.class).field1);
}
public static class TestObject10 {
public String field1;
public String field2;
}
public void test_mandatory_fields_missing() throws IOException {
ExtensionManager.registerExtension(new EmptyExtension() {
@Override
public void updateClassDescriptor(ClassDescriptor desc) {
if (desc.clazz != TestObject10.class) {
return;
}
for (Binding field : desc.allDecoderBindings()) {
field.failOnMissing = true;
}
}
});
JsonIterator iter = JsonIterator.parse("{'field1': '100'}".replace('\'', '"'));
try {
iter.read(TestObject10.class);
fail("should throw exception");
} catch (Exception e) {
}
}
public static class TestObject11 {
public String field1;
}
public void test_fail_on_present() throws IOException {
ExtensionManager.registerExtension(new EmptyExtension() {
@Override
public void updateClassDescriptor(ClassDescriptor desc) {
if (desc.clazz != TestObject11.class) {
return;
}
for (Binding field : desc.allDecoderBindings()) {
field.failOnPresent = true;
}
}
});
JsonIterator iter = JsonIterator.parse("{'field1': '100'}".replace('\'', '"'));
try {
iter.read(TestObject11.class);
fail("should throw exception");
} catch (Exception e) {
}
}
}