BAEL-2412

Add new code examples when deserializing real numbers to discrete number types.
This commit is contained in:
Javier 2018-12-12 06:12:12 +01:00
parent 24bd4644ff
commit 089162e91c
2 changed files with 31 additions and 1 deletions

View File

@ -0,0 +1,9 @@
package org.baeldung.gson.primitives.models;
public class LongExample {
public long value = 1;
public String toString() {
return "{byte: " + value + "}";
}
}

View File

@ -75,7 +75,7 @@ public class UnitTest {
assertEquals(44, model.value);
}
@Test public void fromJsonNonCompatibleNumberTypes() {
@Test public void fromJsonRealToByte() {
Gson gson = new Gson();
String json = "{\"value\": 2.3}";
try {
@ -89,6 +89,27 @@ public class UnitTest {
fail();
}
@Test public void fromJsonRealToLong() {
Gson gson = new Gson();
String json = "{\"value\": 2.3}";
try {
gson.fromJson(json, LongExample.class);
} catch (Exception ex) {
assertTrue(ex instanceof JsonSyntaxException);
assertTrue(ex.getCause() instanceof NumberFormatException);
return;
}
fail();
}
@Test public void fromJsonRealToLongEndingIn0() {
Gson gson = new Gson();
String json = "{\"value\": 2.0}";
LongExample model = gson.fromJson(json, LongExample.class);
assertEquals(2, model.value);
}
@Test public void fromJsonUnicodeChar() {
Gson gson = new Gson();
String json = "{\"value\": \"\\u00AE\"}";