Skip to content

Commit d09815d

Browse files
authored
Update java-extension.md
1 parent 66c1993 commit d09815d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

java-extension.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,23 @@ when a `Date` class to read, the registered decoder will be called.
4444
JsonIterator iter = JsonIterator.parse("1481365190000");
4545
iter.read(Date.class); // Sat Dec 10 18:19:50 CST 2016
4646
```
47+
48+
Generic types can also be customized by `TypeLiteral`. Here is an example:
49+
50+
```
51+
TypeLiteral<List<Integer>> listOfIntType = new TypeLiteral<List<Integer>>() {
52+
};
53+
JsonIterator.registerTypeDecoder(listOfIntType, new Decoder() {
54+
@Override
55+
public Object decode(JsonIterator iter) throws IOException {
56+
ArrayList<Integer> list = new ArrayList<Integer>();
57+
for (char c : iter.readString().toCharArray()) {
58+
list.add(c - '0');
59+
}
60+
return list;
61+
}
62+
});
63+
JsonIterator iter = JsonIterator.parse("\"1481365190000\"");
64+
List<Integer> listOfInt = iter.read(listOfIntType);
65+
System.out.println(listOfInt); // [1, 4, 8, 1, 3, 6, 5, 1, 9, 0, 0, 0, 0]
66+
```

0 commit comments

Comments
 (0)