Skip to content

Commit 366cb9a

Browse files
BAEL-2412
Add limit values when dealing with floats (min, max, NaN).
1 parent f69d154 commit 366cb9a

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

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 LimitValuesExample {
4+
public float minValue;
5+
public float maxValue;
6+
}

gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ public class UnitTest {
3232
assertEquals(expected, gson.toJson(primitiveBundle));
3333
}
3434

35+
@Test public void toJsonLimitValues() {
36+
LimitValuesExample model = new LimitValuesExample();
37+
model.minValue = Float.MIN_VALUE;
38+
model.maxValue = Float.MAX_VALUE;
39+
40+
Gson gson = new Gson();
41+
42+
String expected = "{\"minValue\":1.4E-45,\"maxValue\":3.4028235E38}";
43+
assertEquals(expected, gson.toJson(model));
44+
}
45+
46+
@Test(expected = IllegalArgumentException.class) public void toJsonNaN() {
47+
FloatExample model = new FloatExample();
48+
model.value = Float.NaN;
49+
50+
Gson gson = new Gson();
51+
gson.toJson(model);
52+
}
53+
3554
@Test public void fromJsonAllPrimitives() {
3655
String json = "{\"byteValue\": 17, \"shortValue\": 3, \"intValue\": 3, "
3756
+ "\"longValue\": 3, \"floatValue\": 3.5" + ", \"doubleValue\": 3.5"

0 commit comments

Comments
 (0)