forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCustomize.java
More file actions
119 lines (106 loc) · 4.58 KB
/
TestCustomize.java
File metadata and controls
119 lines (106 loc) · 4.58 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
package com.jsoniter;
import junit.framework.TestCase;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Date;
public class TestCustomize extends TestCase {
public void test_customize_type() throws IOException {
Jsoniter.registerTypeDecoder(Date.class, new Decoder() {
@Override
public Object decode(Jsoniter iter) throws IOException {
return new Date(iter.readLong());
}
});
Jsoniter iter = Jsoniter.parse("1481365190000");
Date date = iter.read(Date.class);
assertEquals(1481365190000L, date.getTime());
}
public void test_customize_field() throws IOException {
Jsoniter.registerFieldDecoder(CustomizedObject.class, "field1", new Decoder() {
@Override
public Object decode(Jsoniter iter) throws IOException {
return Integer.toString(iter.readInt());
}
});
Jsoniter iter = Jsoniter.parse("{'field1': 100}".replace('\'', '"'));
CustomizedObject myObject = iter.read(CustomizedObject.class);
assertEquals("100", myObject.field1);
}
public void test_customize_all_fields() throws IOException {
Jsoniter.registerExtension(new EmptyExtension() {
@Override
public Decoder createDecoder(Field field) {
if (field.getDeclaringClass() == CustomizedObject.class && field.getName().equals("field1")) {
return new Decoder() {
@Override
public Object decode(Jsoniter iter) throws IOException {
return Integer.toString(iter.readInt());
}
};
}
return null;
}
});
Jsoniter iter = Jsoniter.parse("{'field1': 100}".replace('\'', '"'));
CustomizedObject myObject = iter.read(CustomizedObject.class);
assertEquals("100", myObject.field1);
}
public void test_change_field_name() throws IOException {
Jsoniter.registerExtension(new EmptyExtension() {
@Override
public Decoder createDecoder(Field field) {
if (field.getDeclaringClass() == CustomizedObject.class && field.getName().equals("field1")) {
return new Decoder() {
@Override
public Object decode(Jsoniter iter) throws IOException {
return Integer.toString(iter.readInt());
}
};
}
return null;
}
@Override
public String[] getAlternativeFieldNames(Field field) {
if (field.getDeclaringClass() == CustomizedObject.class && field.getName().equals("field1")) {
return new String[]{"field_1", "Field1"};
}
return null;
}
});
Jsoniter iter = Jsoniter.parse("{'field_1': 100}{'Field1': 101}".replace('\'', '"'));
CustomizedObject myObject1 = iter.read(CustomizedObject.class);
assertEquals("100", myObject1.field1);
CustomizedObject myObject2 = iter.read(CustomizedObject.class);
assertEquals("101", myObject2.field1);
}
public void test_customized_decoder_with_int_field() throws IOException {
Jsoniter.registerFieldDecoder(CustomizedObject.class, "field3", new Decoder.IntDecoder() {
@Override
public int decodeInt(Jsoniter iter) throws IOException {
return iter.readInt() * 5;
}
@Override
public Object decode(Jsoniter iter) throws IOException {
return null;
}
});
Jsoniter iter = Jsoniter.parse("{'field3': 100}".replace('\'', '"'));
CustomizedObject myObject = iter.read(CustomizedObject.class);
assertEquals(100 * 5, myObject.field3);
}
public void test_customized_constructor() throws IOException {
Jsoniter.registerExtension(new EmptyExtension(){
@Override
public String codegenNewInstance(Type type) {
if (type == CtorCustomizedObject.class) {
return "new " + CtorCustomizedObject.class.getName() + "(iter.readInt())";
}
return null;
}
});
Jsoniter iter = Jsoniter.parse("100".replace('\'', '"'));
CtorCustomizedObject myObject = iter.read(CtorCustomizedObject.class);
assertEquals(100, myObject.getField1());
}
}