16
16
17
17
package com .google .gson .functional ;
18
18
19
+ import static org .junit .Assert .assertEquals ;
20
+ import static org .junit .Assert .assertTrue ;
21
+
19
22
import com .google .gson .Gson ;
20
23
import com .google .gson .GsonBuilder ;
24
+ import com .google .gson .JsonArray ;
25
+ import com .google .gson .JsonObject ;
21
26
import com .google .gson .ParameterizedTypeFixtures .MyParameterizedType ;
22
27
import com .google .gson .ParameterizedTypeFixtures .MyParameterizedTypeAdapter ;
23
28
import com .google .gson .ParameterizedTypeFixtures .MyParameterizedTypeInstanceCreator ;
24
29
import com .google .gson .common .TestTypes .BagOfPrimitives ;
25
30
import com .google .gson .reflect .TypeToken ;
31
+ import com .google .gson .stream .JsonReader ;
26
32
import java .io .Reader ;
27
33
import java .io .Serializable ;
28
34
import java .io .StringReader ;
32
38
import java .util .ArrayList ;
33
39
import java .util .Arrays ;
34
40
import java .util .List ;
35
- import junit .framework .TestCase ;
41
+ import org .junit .Before ;
42
+ import org .junit .Test ;
36
43
37
44
/**
38
45
* Functional tests for the serialization and deserialization of parameterized types in Gson.
39
46
*
40
47
* @author Inderjeet Singh
41
48
* @author Joel Leitch
42
49
*/
43
- public class ParameterizedTypesTest extends TestCase {
50
+ public class ParameterizedTypesTest {
44
51
private Gson gson ;
45
52
46
- @ Override
47
- protected void setUp () throws Exception {
48
- super .setUp ();
53
+ @ Before
54
+ public void setUp () {
49
55
gson = new Gson ();
50
56
}
51
57
58
+ @ Test
52
59
public void testParameterizedTypesSerialization () throws Exception {
53
60
MyParameterizedType <Integer > src = new MyParameterizedType <>(10 );
54
61
Type typeOfSrc = new TypeToken <MyParameterizedType <Integer >>() {}.getType ();
55
62
String json = gson .toJson (src , typeOfSrc );
56
63
assertEquals (src .getExpectedJson (), json );
57
64
}
58
65
66
+ @ Test
59
67
public void testParameterizedTypeDeserialization () throws Exception {
60
68
BagOfPrimitives bag = new BagOfPrimitives ();
61
69
MyParameterizedType <BagOfPrimitives > expected = new MyParameterizedType <>(bag );
@@ -70,6 +78,7 @@ public void testParameterizedTypeDeserialization() throws Exception {
70
78
assertEquals (expected , actual );
71
79
}
72
80
81
+ @ Test
73
82
public void testTypesWithMultipleParametersSerialization () throws Exception {
74
83
MultiParameters <Integer , Float , Double , String , BagOfPrimitives > src =
75
84
new MultiParameters <>(10 , 1.0F , 2.1D , "abc" , new BagOfPrimitives ());
@@ -81,6 +90,7 @@ public void testTypesWithMultipleParametersSerialization() throws Exception {
81
90
assertEquals (expected , json );
82
91
}
83
92
93
+ @ Test
84
94
public void testTypesWithMultipleParametersDeserialization () throws Exception {
85
95
Type typeOfTarget = new TypeToken <MultiParameters <Integer , Float , Double , String ,
86
96
BagOfPrimitives >>() {}.getType ();
@@ -93,6 +103,7 @@ public void testTypesWithMultipleParametersDeserialization() throws Exception {
93
103
assertEquals (expected , target );
94
104
}
95
105
106
+ @ Test
96
107
public void testParameterizedTypeWithCustomSerializer () {
97
108
Type ptIntegerType = new TypeToken <MyParameterizedType <Integer >>() {}.getType ();
98
109
Type ptStringType = new TypeToken <MyParameterizedType <String >>() {}.getType ();
@@ -109,6 +120,7 @@ public void testParameterizedTypeWithCustomSerializer() {
109
120
assertEquals (MyParameterizedTypeAdapter .<String >getExpectedJson (stringTarget ), json );
110
121
}
111
122
123
+ @ Test
112
124
public void testParameterizedTypesWithCustomDeserializer () {
113
125
Type ptIntegerType = new TypeToken <MyParameterizedType <Integer >>() {}.getType ();
114
126
Type ptStringType = new TypeToken <MyParameterizedType <String >>() {}.getType ();
@@ -130,6 +142,7 @@ public void testParameterizedTypesWithCustomDeserializer() {
130
142
assertEquals ("abc" , stringTarget .value );
131
143
}
132
144
145
+ @ Test
133
146
public void testParameterizedTypesWithWriterSerialization () throws Exception {
134
147
Writer writer = new StringWriter ();
135
148
MyParameterizedType <Integer > src = new MyParameterizedType <>(10 );
@@ -138,6 +151,7 @@ public void testParameterizedTypesWithWriterSerialization() throws Exception {
138
151
assertEquals (src .getExpectedJson (), writer .toString ());
139
152
}
140
153
154
+ @ Test
141
155
public void testParameterizedTypeWithReaderDeserialization () throws Exception {
142
156
BagOfPrimitives bag = new BagOfPrimitives ();
143
157
MyParameterizedType <BagOfPrimitives > expected = new MyParameterizedType <>(bag );
@@ -158,6 +172,7 @@ private static <T> T[] arrayOf(T... args) {
158
172
return args ;
159
173
}
160
174
175
+ @ Test
161
176
public void testVariableTypeFieldsAndGenericArraysSerialization () throws Exception {
162
177
Integer obj = 0 ;
163
178
Integer [] array = { 1 , 2 , 3 };
@@ -174,6 +189,7 @@ public void testVariableTypeFieldsAndGenericArraysSerialization() throws Excepti
174
189
assertEquals (objToSerialize .getExpectedJson (), json );
175
190
}
176
191
192
+ @ Test
177
193
public void testVariableTypeFieldsAndGenericArraysDeserialization () throws Exception {
178
194
Integer obj = 0 ;
179
195
Integer [] array = { 1 , 2 , 3 };
@@ -191,6 +207,7 @@ public void testVariableTypeFieldsAndGenericArraysDeserialization() throws Excep
191
207
assertEquals (objAfterDeserialization .getExpectedJson (), json );
192
208
}
193
209
210
+ @ Test
194
211
public void testVariableTypeDeserialization () throws Exception {
195
212
Type typeOfSrc = new TypeToken <ObjectWithTypeVariables <Integer >>() {}.getType ();
196
213
ObjectWithTypeVariables <Integer > objToSerialize =
@@ -201,6 +218,7 @@ public void testVariableTypeDeserialization() throws Exception {
201
218
assertEquals (objAfterDeserialization .getExpectedJson (), json );
202
219
}
203
220
221
+ @ Test
204
222
public void testVariableTypeArrayDeserialization () throws Exception {
205
223
Integer [] array = { 1 , 2 , 3 };
206
224
@@ -213,6 +231,7 @@ public void testVariableTypeArrayDeserialization() throws Exception {
213
231
assertEquals (objAfterDeserialization .getExpectedJson (), json );
214
232
}
215
233
234
+ @ Test
216
235
public void testParameterizedTypeWithVariableTypeDeserialization () throws Exception {
217
236
List <Integer > list = new ArrayList <>();
218
237
list .add (4 );
@@ -227,6 +246,7 @@ public void testParameterizedTypeWithVariableTypeDeserialization() throws Except
227
246
assertEquals (objAfterDeserialization .getExpectedJson (), json );
228
247
}
229
248
249
+ @ Test
230
250
public void testParameterizedTypeGenericArraysSerialization () throws Exception {
231
251
List <Integer > list = new ArrayList <>();
232
252
list .add (1 );
@@ -240,6 +260,7 @@ public void testParameterizedTypeGenericArraysSerialization() throws Exception {
240
260
assertEquals ("{\" arrayOfListOfTypeParameters\" :[[1,2],[1,2]]}" , json );
241
261
}
242
262
263
+ @ Test
243
264
public void testParameterizedTypeGenericArraysDeserialization () throws Exception {
244
265
List <Integer > list = new ArrayList <>();
245
266
list .add (1 );
@@ -483,18 +504,63 @@ public static final class Amount<Q extends Quantity>
483
504
int value = 30 ;
484
505
}
485
506
507
+ @ Test
486
508
public void testDeepParameterizedTypeSerialization () {
487
509
Amount <MyQuantity > amount = new Amount <>();
488
510
String json = gson .toJson (amount );
489
511
assertTrue (json .contains ("value" ));
490
512
assertTrue (json .contains ("30" ));
491
513
}
492
514
515
+ @ Test
493
516
public void testDeepParameterizedTypeDeserialization () {
494
517
String json = "{value:30}" ;
495
518
Type type = new TypeToken <Amount <MyQuantity >>() {}.getType ();
496
519
Amount <MyQuantity > amount = gson .fromJson (json , type );
497
520
assertEquals (30 , amount .value );
498
521
}
499
522
// End: tests to reproduce issue 103
523
+
524
+ private static void assertCorrectlyDeserialized (Object object ) {
525
+ @ SuppressWarnings ("unchecked" )
526
+ List <Quantity > list = (List <Quantity >) object ;
527
+ assertEquals (1 , list .size ());
528
+ assertEquals (4 , list .get (0 ).q );
529
+ }
530
+
531
+ @ Test
532
+ public void testGsonFromJsonTypeToken () {
533
+ TypeToken <List <Quantity >> typeToken = new TypeToken <List <Quantity >>() {};
534
+ Type type = typeToken .getType ();
535
+
536
+ {
537
+ JsonObject jsonObject = new JsonObject ();
538
+ jsonObject .addProperty ("q" , 4 );
539
+ JsonArray jsonArray = new JsonArray ();
540
+ jsonArray .add (jsonObject );
541
+
542
+ assertCorrectlyDeserialized (gson .fromJson (jsonArray , typeToken ));
543
+ assertCorrectlyDeserialized (gson .fromJson (jsonArray , type ));
544
+ }
545
+
546
+ String json = "[{\" q\" :4}]" ;
547
+
548
+ {
549
+ assertCorrectlyDeserialized (gson .fromJson (json , typeToken ));
550
+ assertCorrectlyDeserialized (gson .fromJson (json , type ));
551
+ }
552
+
553
+ {
554
+ assertCorrectlyDeserialized (gson .fromJson (new StringReader (json ), typeToken ));
555
+ assertCorrectlyDeserialized (gson .fromJson (new StringReader (json ), type ));
556
+ }
557
+
558
+ {
559
+ JsonReader reader = new JsonReader (new StringReader (json ));
560
+ assertCorrectlyDeserialized (gson .fromJson (reader , typeToken ));
561
+
562
+ reader = new JsonReader (new StringReader (json ));
563
+ assertCorrectlyDeserialized (gson .fromJson (reader , type ));
564
+ }
565
+ }
500
566
}
0 commit comments