forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCustomizeSetter.java
More file actions
63 lines (53 loc) · 2.03 KB
/
TestCustomizeSetter.java
File metadata and controls
63 lines (53 loc) · 2.03 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
package com.jsoniter;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class TestCustomizeSetter extends TestCase {
static {
// JsonIterator.enableStrictMode();
}
public static class ObjectWithDefaultSetter {
String field;
public void setField(String field) {
this.field = field;
}
}
public void test_default_setter() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field': 'hello'}".replace('\'', '"'));
ObjectWithDefaultSetter obj = iter.read(ObjectWithDefaultSetter.class);
assertEquals("hello", obj.field);
}
public static class ObjectWithCustomizedSetter {
String field1;
String field2;
public void initialize(String field1, String field2) {
this.field1 = field1;
this.field2 = field2;
}
}
public void test_customized_setter() throws IOException {
JsonIterator.registerExtension(new EmptyExtension() {
@Override
public List<CustomizedSetter> getSetters(Class clazz) {
if (clazz == ObjectWithCustomizedSetter.class) {
return (List) Arrays.asList(new CustomizedSetter(){{
methodName = "initialize";
parameters = (List) Arrays.asList(new Binding() {{
name = "field1";
valueType = String.class;
}}, new Binding() {{
name = "field2";
valueType = String.class;
}});
}});
}
return null;
}
});
JsonIterator iter = JsonIterator.parse("{'field1': 'hello', 'field2': 'world'}".replace('\'', '"'));
ObjectWithCustomizedSetter obj = iter.read(ObjectWithCustomizedSetter.class);
assertEquals("hello", obj.field1);
assertEquals("world", obj.field2);
}
}