forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDemo.java
More file actions
48 lines (40 loc) · 1.47 KB
/
TestDemo.java
File metadata and controls
48 lines (40 loc) · 1.47 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
package com.jsoniter;
import junit.framework.TestCase;
import java.io.IOException;
public class TestDemo extends TestCase {
public void test_bind_api() throws IOException {
JsonIterator iter = JsonIterator.parse("[0,1,2,3]");
int[] val = iter.read(int[].class);
System.out.println(val[3]);
}
public void test_any_api() throws IOException {
JsonIterator iter = JsonIterator.parse("[0,1,2,3]");
System.out.println(iter.readAny().toInt(3));
}
public void test_iterator_api() throws IOException {
JsonIterator iter = JsonIterator.parse("[0,1,2,3]");
int total = 0;
while (iter.readArray()) {
total += iter.readInt();
}
System.out.println(total);
}
public static class ABC {
public Any a;
}
public void test_abc() throws IOException {
JsonIterator iter = JsonIterator.parse("{'a': {'b': {'c': 'd'}}}".replace('\'', '"'));
ABC abc = iter.read(ABC.class);
System.out.println(abc.a.get("b", "c"));
}
public void test_iterator_api_and_bind() throws IOException {
JsonIterator iter = JsonIterator.parse("[123, {'name': 'taowen', 'tags': ['crazy', 'hacker']}]".replace('\'', '"'));
iter.readArray();
int userId = iter.readInt();
iter.readArray();
User user = iter.read(User.class);
user.userId = userId;
iter.readArray(); // end of array
System.out.println(user);
}
}