forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestCustomizeField.java
More file actions
152 lines (131 loc) · 5.12 KB
/
TestCustomizeField.java
File metadata and controls
152 lines (131 loc) · 5.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
package com.jsoniter;
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 {
JsonIterator.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 {
JsonIterator.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 TestObject3 {
public int field1;
}
public void test_customize_using_extension() throws IOException {
JsonIterator.registerExtension(new EmptyExtension() {
@Override
public Decoder createDecoder(Binding field) {
if (field.clazz == TestObject3.class && field.name.equals("field1")) {
return new Decoder.IntDecoder() {
@Override
public int decodeInt(JsonIterator iter) throws IOException {
return Integer.valueOf(iter.readString());
}
};
}
return null;
}
});
JsonIterator iter = JsonIterator.parse("{'field1': '100'}".replace('\'', '"'));
TestObject3 myObject = iter.read(TestObject3.class);
assertEquals(100, myObject.field1);
}
public static class TestObject4 {
public int field1;
}
public void test_rename_field() throws IOException {
JsonIterator.registerExtension(new EmptyExtension() {
@Override
public String[] getBindFrom(Binding field) {
if (field.clazz == TestObject4.class && field.name.equals("field1")) {
return new String[]{"field_1", "Field1"};
}
return null;
}
});
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 {
JsonIterator.registerExtension(new EmptyExtension() {
@Override
public CustomizedConstructor getConstructor(Class clazz) {
if (clazz == TestObject5.class) {
return new CustomizedConstructor() {{
parameters = (List) Arrays.asList(new Binding() {{
name="param1";
valueType = int.class;
}});
}};
}
return null;
}
@Override
public String[] getBindFrom(Binding field) {
if (field.clazz == TestObject5.class && "param1".equals(field.name)) {
return new String[]{"param2"};
}
return null;
}
});
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 {
JsonIterator.registerExtension(new EmptyExtension() {
@Override
public String[] getBindFrom(Binding field) {
if (field.clazz == TestObject6.class && field.name.equals("field")) {
return new String[]{"field_1", "Field1"};
}
return null;
}
});
JsonIterator iter = JsonIterator.parse("{'field_1': 'hello'}".replace('\'', '"'));
TestObject6 obj = iter.read(TestObject6.class);
assertEquals("hello", obj.field);
}
}