forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestSpiTypeDecoder.java
More file actions
125 lines (110 loc) · 4.28 KB
/
TestSpiTypeDecoder.java
File metadata and controls
125 lines (110 loc) · 4.28 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
package com.jsoniter;
import com.jsoniter.spi.Decoder;
import com.jsoniter.spi.JsonException;
import com.jsoniter.spi.JsoniterSpi;
import com.jsoniter.spi.TypeLiteral;
import junit.framework.TestCase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
public class TestSpiTypeDecoder extends TestCase {
static {
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
}
public static class TestObject1 {
public int field1;
}
public void test_TypeDecoder() throws IOException {
JsoniterSpi.registerTypeDecoder(TestObject1.class, new Decoder() {
@Override
public Object decode(JsonIterator iter) throws IOException {
iter.skip();
TestObject1 obj = new TestObject1();
obj.field1 = 101;
return obj;
}
});
TestObject1 obj = JsonIterator.deserialize(
"{'field1': 100}".replace('\'', '"'), TestObject1.class);
assertEquals(101, obj.field1);
}
public void test_TypeDecoder_for_generics() throws IOException {
TypeLiteral<List<TestObject1>> typeLiteral = new TypeLiteral<List<TestObject1>>() {
};
JsoniterSpi.registerTypeDecoder(typeLiteral, new Decoder() {
@Override
public Object decode(JsonIterator iter) throws IOException {
iter.skip();
TestObject1 obj = new TestObject1();
obj.field1 = 101;
return Arrays.asList(obj);
}
});
List<TestObject1> objs = JsonIterator.deserialize(
"{'field1': 100}".replace('\'', '"'), typeLiteral);
assertEquals(101, objs.get(0).field1);
}
public static class MyDate {
Date date;
}
static {
JsoniterSpi.registerTypeDecoder(MyDate.class, new Decoder() {
@Override
public Object decode(final JsonIterator iter) throws IOException {
return new MyDate() {{
date = new Date(iter.readLong());
}};
}
});
}
public void test_direct() throws IOException {
JsonIterator iter = JsonIterator.parse("1481365190000");
MyDate date = iter.read(MyDate.class);
assertEquals(1481365190000L, date.date.getTime());
}
public static class FieldWithMyDate {
public MyDate field;
}
public void test_as_field_type() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field': 1481365190000}".replace('\'', '"'));
FieldWithMyDate obj = iter.read(FieldWithMyDate.class);
assertEquals(1481365190000L, obj.field.date.getTime());
}
public void test_as_array_element() throws IOException {
JsonIterator iter = JsonIterator.parse("[1481365190000]");
MyDate[] dates = iter.read(MyDate[].class);
assertEquals(1481365190000L, dates[0].date.getTime());
}
public static class MyList {
public List<String> list;
}
public void test_list_or_single_element() {
final TypeLiteral<List<String>> listOfString = new TypeLiteral<List<String>>() {
};
JsoniterSpi.registerTypeDecoder(MyList.class, new Decoder() {
@Override
public Object decode(JsonIterator iter) throws IOException {
ValueType valueType = iter.whatIsNext();
MyList myList = new MyList();
switch (valueType) {
case ARRAY:
myList.list = iter.read(listOfString);
return myList;
case STRING:
myList.list = new ArrayList<String>();
myList.list.add(iter.readString());
return myList;
default:
throw new JsonException("unexpected input");
}
}
});
MyList list1 = JsonIterator.deserialize("\"hello\"", MyList.class);
assertEquals("hello", list1.list.get(0));
MyList list2 = JsonIterator.deserialize("[\"hello\",\"world\"]", MyList.class);
assertEquals("hello", list2.list.get(0));
assertEquals("world", list2.list.get(1));
}
}