1616 */
1717package com .alibaba .dubbo .common .serialize .fastjson ;
1818
19+ import static org .hamcrest .CoreMatchers .not ;
20+ import static org .hamcrest .CoreMatchers .nullValue ;
21+ import static org .hamcrest .core .Is .is ;
22+ import static org .junit .Assert .assertEquals ;
23+ import static org .junit .Assert .assertThat ;
24+ import static org .junit .Assert .assertTrue ;
25+
26+ import com .alibaba .dubbo .common .serialize .fastjson .model .Organization ;
1927import com .alibaba .dubbo .common .serialize .fastjson .model .Person ;
2028import com .alibaba .fastjson .JSONObject ;
21- import org .junit .Test ;
22-
2329import java .io .ByteArrayInputStream ;
2430import java .io .EOFException ;
2531import java .io .IOException ;
2632import java .io .StringReader ;
27-
28- import static org . hamcrest . CoreMatchers . not ;
29- import static org . hamcrest . CoreMatchers . nullValue ;
30- import static org . hamcrest . core . Is . is ;
31- import static org .junit .Assert . assertThat ;
33+ import java . lang . reflect . Method ;
34+ import java . lang . reflect . Type ;
35+ import java . util . List ;
36+ import javax . lang . model . util . Types ;
37+ import org .junit .Test ;
3238
3339public class FastJsonObjectInputTest {
3440 private FastJsonObjectInput fastJsonObjectInput ;
@@ -144,4 +150,43 @@ public void testReadObjectWithoutClass() throws IOException, ClassNotFoundExcept
144150 assertThat (readObject .getString ("name" ), is ("John" ));
145151 assertThat (readObject .getInteger ("age" ), is (30 ));
146152 }
147- }
153+
154+ @ Test
155+ public void testReadObjectWithTowType () throws Exception {
156+ fastJsonObjectInput = new FastJsonObjectInput (new StringReader ("[{\" name\" :\" John\" ,\" age\" :30},{\" name\" :\" Born\" ,\" age\" :24}]" ));
157+
158+ Method methodReturnType = getClass ().getMethod ("towLayer" );
159+ Type type = methodReturnType .getGenericReturnType ();
160+ List <Person > o = fastJsonObjectInput .readObject (List .class , type );
161+
162+ assertTrue (o instanceof List );
163+ assertTrue (o .get (0 ) instanceof Person );
164+
165+ assertThat (o .size (), is (2 ));
166+ assertThat (o .get (1 ).getName (), is ("Born" ));
167+ }
168+
169+ @ Test
170+ public void testReadObjectWithThreeType () throws Exception {
171+ fastJsonObjectInput = new FastJsonObjectInput (new StringReader ("{\" data\" :[{\" name\" :\" John\" ,\" age\" :30},{\" name\" :\" Born\" ,\" age\" :24}]}" ));
172+
173+ Method methodReturnType = getClass ().getMethod ("threeLayer" );
174+ Type type = methodReturnType .getGenericReturnType ();
175+ Organization <List <Person >> o = fastJsonObjectInput .readObject (Organization .class , type );
176+
177+ assertTrue (o instanceof Organization );
178+ assertTrue (o .getData () instanceof List );
179+ assertTrue (o .getData ().get (0 ) instanceof Person );
180+
181+ assertThat (o .getData ().size (), is (2 ));
182+ assertThat (o .getData ().get (1 ).getName (), is ("Born" ));
183+ }
184+
185+ public List <Person > towLayer () {
186+ return null ;
187+ }
188+
189+ public Organization <List <Person >> threeLayer () {
190+ return null ;
191+ }
192+ }
0 commit comments