Skip to content

Commit 7f15b27

Browse files
committed
add:加个泛型用例
1 parent 566c0c3 commit 7f15b27

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.nutz.json;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Date;
6+
7+
import org.junit.Test;
8+
import org.nutz.lang.Times;
9+
10+
/**
11+
* @author kerbores@gmail.com
12+
*
13+
*/
14+
public class GenericTypeTest {
15+
16+
public static class A<T> {
17+
18+
T data;
19+
20+
public T getData() {
21+
return data;
22+
}
23+
24+
public void setData(T data) {
25+
this.data = data;
26+
}
27+
28+
}
29+
30+
public static class B {
31+
int id;
32+
String name;
33+
Date birth;
34+
/**
35+
* @param id
36+
* @param name
37+
* @param birth
38+
*/
39+
public B(int id, String name, Date birth) {
40+
super();
41+
this.id = id;
42+
this.name = name;
43+
this.birth = birth;
44+
}
45+
public int getId() {
46+
return id;
47+
}
48+
public void setId(int id) {
49+
this.id = id;
50+
}
51+
public String getName() {
52+
return name;
53+
}
54+
public void setName(String name) {
55+
this.name = name;
56+
}
57+
public Date getBirth() {
58+
return birth;
59+
}
60+
public void setBirth(Date birth) {
61+
this.birth = birth;
62+
}
63+
64+
65+
}
66+
67+
@Test
68+
public void test() {
69+
A a = new A();
70+
a.setData(new B(1, "test", Times.now()));
71+
String s = Json.toJson(a);
72+
A<B> a1 = Json.fromJson(A.class, s);
73+
assertEquals(B.class, a1.getData().getClass());
74+
}
75+
76+
}

0 commit comments

Comments
 (0)