BAEL-2412

Add limit values when dealing with floats (min, max, NaN).
This commit is contained in:
Javier 2018-12-12 06:41:39 +01:00
parent f69d154862
commit 366cb9a0bc
2 changed files with 25 additions and 0 deletions

View File

@ -0,0 +1,6 @@
package org.baeldung.gson.primitives.models;
public class LimitValuesExample {
public float minValue;
public float maxValue;
}

View File

@ -32,6 +32,25 @@ public class UnitTest {
assertEquals(expected, gson.toJson(primitiveBundle));
}
@Test public void toJsonLimitValues() {
LimitValuesExample model = new LimitValuesExample();
model.minValue = Float.MIN_VALUE;
model.maxValue = Float.MAX_VALUE;
Gson gson = new Gson();
String expected = "{\"minValue\":1.4E-45,\"maxValue\":3.4028235E38}";
assertEquals(expected, gson.toJson(model));
}
@Test(expected = IllegalArgumentException.class) public void toJsonNaN() {
FloatExample model = new FloatExample();
model.value = Float.NaN;
Gson gson = new Gson();
gson.toJson(model);
}
@Test public void fromJsonAllPrimitives() {
String json = "{\"byteValue\": 17, \"shortValue\": 3, \"intValue\": 3, "
+ "\"longValue\": 3, \"floatValue\": 3.5" + ", \"doubleValue\": 3.5"