Skip to content

Commit f0d27bc

Browse files
authored
Merge pull request eugenp#5833 from f41thful/BAEL-2412
BAEL-2412
2 parents fb210d0 + 4ea8670 commit f0d27bc

File tree

10 files changed

+345
-0
lines changed

10 files changed

+345
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class BooleanExample {
4+
public boolean value;
5+
6+
public String toString() {
7+
return "{boolean: " + value + "}";
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class ByteExample {
4+
public byte value = (byte) 1;
5+
6+
public String toString() {
7+
return "{byte: " + value + "}";
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class CharExample {
4+
public char value;
5+
6+
public String toString() {
7+
return "{char: " + value + "}";
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class DoubleExample {
4+
public double value;
5+
6+
public String toString() {
7+
return "{float: " + value + "}";
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class FloatExample {
4+
public float value;
5+
6+
public String toString() {
7+
return "{float: " + value + "}";
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class InfinityValuesExample {
4+
public float negativeInfinity;
5+
public float positiveInfinity;
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class LongExample {
4+
public long value = 1;
5+
6+
public String toString() {
7+
return "{byte: " + value + "}";
8+
}
9+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class PrimitiveBundle {
4+
public byte byteValue;
5+
public short shortValue;
6+
public int intValue;
7+
public long longValue;
8+
public float floatValue;
9+
public double doubleValue;
10+
public boolean booleanValue;
11+
public char charValue;
12+
13+
public String toString() {
14+
return "{" + "byte: " + byteValue + ", " + "short: " + shortValue + ", "
15+
+ "int: " + intValue + ", " + "long: " + longValue + ", "
16+
+ "float: " + floatValue + ", " + "double: " + doubleValue + ", "
17+
+ "boolean: " + booleanValue + ", " + "char: " + charValue + "}";
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.baeldung.gson.primitives.models;
2+
3+
public class PrimitiveBundleInitialized {
4+
// @formatter:off
5+
public byte byteValue = (byte) 1;
6+
public short shortValue = (short) 1;
7+
public int intValue = 1;
8+
public long longValue = 1L;
9+
public float floatValue = 1.0f;
10+
public double doubleValue = 1;
11+
// @formatter:on
12+
13+
public String toString() {
14+
return "{" + "byte: " + byteValue + ", " + "short: " + shortValue + ", "
15+
+ "int: " + intValue + ", " + "long: " + longValue + ", "
16+
+ "float: " + floatValue + ", " + "double: " + doubleValue + "}";
17+
}
18+
}
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
package org.baeldung.gson.primitives;
2+
3+
import com.google.gson.*;
4+
import org.baeldung.gson.primitives.models.*;
5+
import org.junit.Test;
6+
7+
import java.lang.reflect.Type;
8+
9+
import static junit.framework.TestCase.*;
10+
11+
public class PrimitiveValuesUnitTest {
12+
@Test public void whenSerializingToJSON_thenShouldCreateJSON() {
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+
26+
Gson gson = new Gson();
27+
28+
String expected = "{\"byteValue\":17,\"shortValue\":3,\"intValue\":3,"
29+
+ "\"longValue\":3,\"floatValue\":3.5" + ",\"doubleValue\":3.5"
30+
+ ",\"booleanValue\":true,\"charValue\":\"a\"}";
31+
32+
assertEquals(expected, gson.toJson(primitiveBundle));
33+
}
34+
35+
@Test(expected = IllegalArgumentException.class) public void
36+
whenSerializingInfinity_thenShouldRaiseAnException() {
37+
InfinityValuesExample model = new InfinityValuesExample();
38+
model.negativeInfinity = Float.NEGATIVE_INFINITY;
39+
model.positiveInfinity = Float.POSITIVE_INFINITY;
40+
41+
Gson gson = new Gson();
42+
43+
gson.toJson(model);
44+
}
45+
46+
@Test(expected = IllegalArgumentException.class) public void
47+
whenSerializingNaN_thenShouldRaiseAnException() {
48+
FloatExample model = new FloatExample();
49+
model.value = Float.NaN;
50+
51+
Gson gson = new Gson();
52+
gson.toJson(model);
53+
}
54+
55+
@Test public void whenDeserializingFromJSON_thenShouldParseTheValueInTheString() {
56+
String json = "{\"byteValue\": 17, \"shortValue\": 3, \"intValue\": 3, "
57+
+ "\"longValue\": 3, \"floatValue\": 3.5" + ", \"doubleValue\": 3.5"
58+
+ ", \"booleanValue\": true, \"charValue\": \"a\"}";
59+
60+
Gson gson = new Gson();
61+
PrimitiveBundle model = gson.fromJson(json, PrimitiveBundle.class);
62+
63+
// @formatter:off
64+
assertEquals(17, model.byteValue);
65+
assertEquals(3, model.shortValue);
66+
assertEquals(3, model.intValue);
67+
assertEquals(3, model.longValue);
68+
assertEquals(3.5, model.floatValue, 0.0001);
69+
assertEquals(3.5, model.doubleValue, 0.0001);
70+
assertTrue( model.booleanValue);
71+
assertEquals('a', model.charValue);
72+
// @formatter:on
73+
}
74+
75+
@Test public void whenDeserializingHighPrecissionNumberIntoFloat_thenShouldPerformRounding() {
76+
String json = "{\"value\": 12.123425589123456}";
77+
Gson gson = new Gson();
78+
FloatExample model = gson.fromJson(json, FloatExample.class);
79+
assertEquals(12.123426f, model.value, 0.000001);
80+
}
81+
82+
@Test public void whenDeserializingHighPrecissiongNumberIntoDouble_thenShouldPerformRounding() {
83+
String json = "{\"value\": 12.123425589123556}";
84+
Gson gson = new Gson();
85+
DoubleExample model = gson.fromJson(json, DoubleExample.class);
86+
assertEquals(12.123425589124f, model.value, 0.000001);
87+
}
88+
89+
90+
@Test public void whenDeserializingValueThatOverflows_thenShouldOverflowSilently() {
91+
Gson gson = new Gson();
92+
String json = "{\"value\": \"300\"}";
93+
ByteExample model = gson.fromJson(json, ByteExample.class);
94+
95+
assertEquals(44, model.value);
96+
}
97+
98+
@Test public void whenDeserializingRealIntoByte_thenShouldRaiseAnException() {
99+
Gson gson = new Gson();
100+
String json = "{\"value\": 2.3}";
101+
try {
102+
gson.fromJson(json, ByteExample.class);
103+
} catch (Exception ex) {
104+
assertTrue(ex instanceof JsonSyntaxException);
105+
assertTrue(ex.getCause() instanceof NumberFormatException);
106+
return;
107+
}
108+
109+
fail();
110+
}
111+
112+
@Test public void whenDeserializingRealIntoLong_thenShouldRaiseAnException() {
113+
Gson gson = new Gson();
114+
String json = "{\"value\": 2.3}";
115+
try {
116+
gson.fromJson(json, LongExample.class);
117+
} catch (Exception ex) {
118+
assertTrue(ex instanceof JsonSyntaxException);
119+
assertTrue(ex.getCause() instanceof NumberFormatException);
120+
return;
121+
}
122+
123+
fail();
124+
}
125+
126+
@Test public void whenDeserializingRealWhoseDecimalPartIs0_thenShouldParseItCorrectly() {
127+
Gson gson = new Gson();
128+
String json = "{\"value\": 2.0}";
129+
LongExample model = gson.fromJson(json, LongExample.class);
130+
assertEquals(2, model.value);
131+
}
132+
133+
@Test public void whenDeserializingUnicodeChar_thenShouldParseItCorrectly() {
134+
Gson gson = new Gson();
135+
String json = "{\"value\": \"\\u00AE\"}";
136+
CharExample model = gson.fromJson(json, CharExample.class);
137+
138+
assertEquals('\u00AE', model.value);
139+
}
140+
141+
@Test public void whenDeserializingNullValues_thenShouldIgnoreThoseFields() {
142+
Gson gson = new Gson();
143+
// @formatter:off
144+
String json = "{\"byteValue\": null, \"shortValue\": null, "
145+
+ "\"intValue\": null, " + "\"longValue\": null, \"floatValue\": null"
146+
+ ", \"doubleValue\": null}";
147+
// @formatter:on
148+
PrimitiveBundleInitialized model = gson.fromJson(json,
149+
PrimitiveBundleInitialized.class);
150+
151+
assertEquals(1, model.byteValue);
152+
assertEquals(1, model.shortValue);
153+
assertEquals(1, model.intValue);
154+
assertEquals(1, model.longValue);
155+
assertEquals(1, model.floatValue, 0.0001);
156+
assertEquals(1, model.doubleValue, 0.0001);
157+
}
158+
159+
@Test(expected = JsonSyntaxException.class) public void
160+
whenDeserializingTheEmptyString_thenShouldRaiseAnException() {
161+
Gson gson = new Gson();
162+
// @formatter:off
163+
String json = "{\"byteValue\": \"\", \"shortValue\": \"\", "
164+
+ "\"intValue\": \"\", " + "\"longValue\": \"\", \"floatValue\": \"\""
165+
+ ", \"doubleValue\": \"\"" + ", \"booleanValue\": \"\"}";
166+
// @formatter:on
167+
gson.fromJson(json, PrimitiveBundleInitialized.class);
168+
}
169+
170+
@Test public void whenDeserializingTheEmptyStringIntoChar_thenShouldHaveTheEmtpyChar() {
171+
Gson gson = new Gson();
172+
// @formatter:off
173+
String json = "{\"charValue\": \"\"}";
174+
// @formatter:on
175+
CharExample model = gson.fromJson(json, CharExample.class);
176+
177+
assertEquals(Character.MIN_VALUE, model.value);
178+
}
179+
180+
@Test public void whenDeserializingValidValueAppearingInAString_thenShouldParseTheValue() {
181+
Gson gson = new Gson();
182+
// @formatter:off
183+
String json = "{\"byteValue\": \"15\", \"shortValue\": \"15\", "
184+
+ "\"intValue\": \"15\", " + "\"longValue\": \"15\", \"floatValue\": \"15.0\""
185+
+ ", \"doubleValue\": \"15.0\"}";
186+
// @formatter:on
187+
PrimitiveBundleInitialized model = gson.fromJson(json,
188+
PrimitiveBundleInitialized.class);
189+
190+
assertEquals(15, model.byteValue);
191+
assertEquals(15, model.shortValue);
192+
assertEquals(15, model.intValue);
193+
assertEquals(15, model.longValue);
194+
assertEquals(15, model.floatValue, 0.0001);
195+
assertEquals(15, model.doubleValue, 0.0001);
196+
}
197+
198+
@Test public void whenDeserializingABooleanFrom0Or1Integer_thenShouldRaiseAnException() {
199+
String json = "{\"value\": 1}";
200+
Gson gson = new Gson();
201+
202+
try {
203+
gson.fromJson(json, BooleanExample.class);
204+
} catch (Exception ex) {
205+
assertTrue(ex instanceof JsonSyntaxException);
206+
assertTrue(ex.getCause() instanceof IllegalStateException);
207+
return;
208+
}
209+
210+
fail();
211+
}
212+
213+
@Test public void whenDeserializingWithCustomDeserializerABooleanFrom0Or1Integer_thenShouldWork() {
214+
String json = "{\"value\": 1}";
215+
GsonBuilder builder = new GsonBuilder();
216+
builder.registerTypeAdapter(BooleanExample.class,
217+
new BooleanAs2ValueIntegerDeserializer());
218+
219+
Gson gson = builder.create();
220+
221+
BooleanExample model = gson.fromJson(json, BooleanExample.class);
222+
223+
assertTrue(model.value);
224+
}
225+
226+
// @formatter:off
227+
static class BooleanAs2ValueIntegerDeserializer implements JsonDeserializer<BooleanExample> {
228+
@Override public BooleanExample deserialize(
229+
JsonElement jsonElement,
230+
Type type,
231+
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
232+
233+
BooleanExample model = new BooleanExample();
234+
int value = jsonElement.getAsJsonObject().getAsJsonPrimitive("value").getAsInt();
235+
if (value == 0) {
236+
model.value = false;
237+
} else if (value == 1) {
238+
model.value = true;
239+
} else {
240+
throw new JsonParseException("Unexpected value. Trying to deserialize "
241+
+ "a boolean from an integer different than 0 and 1.");
242+
}
243+
244+
return model;
245+
}
246+
}
247+
// @formatter:on
248+
}

0 commit comments

Comments
 (0)