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
76 lines (63 loc) · 2.61 KB
/
TestCustomizeSetter.java
File metadata and controls
76 lines (63 loc) · 2.61 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
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 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 ObjectWithDefaultSetter2 {
String _field;
private void setField(String field) {
this._field = field;
}
}
public void test_default_setter_with_reflection() throws IOException {
ExtensionManager.registerTypeDecoder(ObjectWithDefaultSetter2.class, new ReflectionDecoder(ObjectWithDefaultSetter2.class));
JsonIterator iter = JsonIterator.parse("{'field': 'hello'}".replace('\'', '"'));
ObjectWithDefaultSetter2 obj = iter.read(ObjectWithDefaultSetter2.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 {
ExtensionManager.registerExtension(new EmptyExtension() {
@Override
public void updateClassDescriptor(final ClassDescriptor desc) {
if (desc.clazz == ObjectWithCustomizedSetter.class) {
desc.setters = (List) Arrays.asList(new SetterDescriptor(){{
methodName = "initialize";
parameters = (List) Arrays.asList(new Binding(desc.clazz, desc.lookup, String.class) {{
name = "field1";
}}, new Binding(desc.clazz, desc.lookup, String.class) {{
name = "field2";
}});
}});
}
}
});
JsonIterator iter = JsonIterator.parse("{'field1': 'hello', 'field2': 'world'}".replace('\'', '"'));
ObjectWithCustomizedSetter obj = iter.read(ObjectWithCustomizedSetter.class);
assertEquals("hello", obj.field1);
assertEquals("world", obj.field2);
}
}