1010
1111public class UnitTest {
1212 @ Test public void toJsonAllPrimitives () {
13- GsonBundle gsonBundle = new GsonBundle ();
13+ PrimitiveBundle primitiveBundle = new PrimitiveBundle ();
14+
15+ // @formatter:off
16+ primitiveBundle .byteValue = (byte ) 0x00001111 ;
17+ primitiveBundle .shortValue = (short ) 3 ;
18+ primitiveBundle .intValue = 3 ;
19+ primitiveBundle .longValue = 3 ;
20+ primitiveBundle .floatValue = 3.5f ;
21+ primitiveBundle .doubleValue = 3.5 ;
22+ primitiveBundle .booleanValue = true ;
23+ primitiveBundle .charValue = 'a' ;
24+ // @formatter:on
25+
1426 Gson gson = new Gson ();
1527
1628 String expected = "{\" byteValue\" :17,\" shortValue\" :3,\" intValue\" :3," + "\" longValue\" :3,\" floatValue\" :3.5" + ",\" doubleValue\" :3.5" + ",\" booleanValue\" :true,\" charValue\" :\" a\" }" ;
1729
18- assertEquals (expected , gson .toJson (gsonBundle ));
30+ assertEquals (expected , gson .toJson (primitiveBundle ));
1931 }
2032
2133 @ Test public void fromJsonAllPrimitives () {
2234 String json = "{\" byteValue\" : 17, \" shortValue\" : 3, \" intValue\" : 3, " + "\" longValue\" : 3, \" floatValue\" : 3.5" + ", \" doubleValue\" : 3.5" + ", \" booleanValue\" : true, \" charValue\" : \" a\" }" ;
2335
2436 Gson gson = new Gson ();
25- GsonBundle model = gson .fromJson (json , GsonBundle .class );
37+ PrimitiveBundle model = gson .fromJson (json , PrimitiveBundle .class );
2638
27- assertEquals (17 , model .byteValue );
28- assertEquals (3 , model .shortValue );
29- assertEquals (3 , model .intValue );
30- assertEquals (3 , model .longValue );
39+ // @formatter:off
40+ assertEquals (17 , model .byteValue );
41+ assertEquals (3 , model .shortValue );
42+ assertEquals (3 , model .intValue );
43+ assertEquals (3 , model .longValue );
3144 assertEquals (3.5 , model .floatValue , 0.0001 );
3245 assertEquals (3.5 , model .doubleValue , 0.0001 );
33- assertTrue (model .booleanValue );
46+ assertTrue ( model .booleanValue );
3447 assertEquals ('a' , model .charValue );
48+ // @formatter:on
3549 }
3650
3751 @ Test public void toJsonByteToBitString () {
@@ -143,7 +157,19 @@ public class UnitTest {
143157 fail ();
144158 }
145159
146- @ Test public void fromJsonBooleanfromYes () {
160+ @ Test public void fromJsonBooleanFrom2ValueIntegerSolution () {
161+ String json = "{\" value\" : 1}" ;
162+ GsonBuilder builder = new GsonBuilder ();
163+ builder .registerTypeAdapter (GsonBoolean .class , new GsonBoolean2ValueIntegerDeserializer ());
164+
165+ Gson gson = builder .create ();
166+
167+ GsonBoolean model = gson .fromJson (json , GsonBoolean .class );
168+
169+ assertTrue (model .value );
170+ }
171+
172+ @ Test public void fromJsonBooleanFromYes () {
147173 String json = "{\" value\" : yes}" ;
148174 Gson gson = new Gson ();
149175
@@ -162,19 +188,54 @@ public class UnitTest {
162188 assertFalse (model .value );
163189 }
164190
191+ // @formatter:off
165192 static class GsonBitStringDeserializer implements JsonDeserializer <GsonBitString > {
166- @ Override public GsonBitString deserialize (JsonElement jsonElement , Type type , JsonDeserializationContext jsonDeserializationContext ) throws JsonParseException {
193+ @ Override public GsonBitString deserialize (
194+ JsonElement jsonElement ,
195+ Type type ,
196+ JsonDeserializationContext jsonDeserializationContext ) throws JsonParseException {
197+
167198 GsonBitString gsonBitString = new GsonBitString ();
168- gsonBitString .value = (byte ) Integer .parseInt (jsonElement .getAsJsonObject ().getAsJsonPrimitive ("value" ).getAsString (), 2 );
199+ gsonBitString .value = (byte ) Integer .parseInt (
200+ jsonElement .getAsJsonObject ()
201+ .getAsJsonPrimitive ("value" )
202+ .getAsString ()
203+ , 2 );
169204 return gsonBitString ;
170205 }
171206 }
172207
173208 static class GsonBitStringSerializer implements JsonSerializer <GsonBitString > {
174- @ Override public JsonElement serialize (GsonBitString gsonBundle , Type type , JsonSerializationContext jsonSerializationContext ) {
209+ @ Override public JsonElement serialize (
210+ GsonBitString model ,
211+ Type type ,
212+ JsonSerializationContext jsonSerializationContext ) {
213+
175214 JsonObject jsonObject = new JsonObject ();
176- jsonObject .addProperty ("value" , Integer .toBinaryString (gsonBundle .value ));
215+ jsonObject .addProperty ("value" , Integer .toBinaryString (model .value ));
177216 return jsonObject ;
178217 }
179218 }
219+
220+ static class GsonBoolean2ValueIntegerDeserializer implements JsonDeserializer <GsonBoolean > {
221+ @ Override public GsonBoolean deserialize (
222+ JsonElement jsonElement ,
223+ Type type ,
224+ JsonDeserializationContext jsonDeserializationContext ) throws JsonParseException {
225+
226+ GsonBoolean model = new GsonBoolean ();
227+ int value = jsonElement .getAsJsonObject ().getAsJsonPrimitive ("value" ).getAsInt ();
228+ if (value == 0 ) {
229+ model .value = false ;
230+ } else if (value == 1 ) {
231+ model .value = true ;
232+ } else {
233+ throw new JsonParseException ("Unexpected value. Trying to deserialize "
234+ + "a boolean from an integer different than 0 and 1." );
235+ }
236+
237+ return model ;
238+ }
239+ }
240+ // @formatter:on
180241}
0 commit comments