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
34 lines (29 loc) · 1.16 KB
/
TestCustomize.java
File metadata and controls
34 lines (29 loc) · 1.16 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
package com.jsoniter;
import junit.framework.TestCase;
import java.io.IOException;
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(Type type, 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(Type type, 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);
}
}