BAEL-2412
Add new code examples when deserializing real numbers to discrete number types.
This commit is contained in:
parent
24bd4644ff
commit
089162e91c
@ -0,0 +1,9 @@
|
|||||||
|
package org.baeldung.gson.primitives.models;
|
||||||
|
|
||||||
|
public class LongExample {
|
||||||
|
public long value = 1;
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return "{byte: " + value + "}";
|
||||||
|
}
|
||||||
|
}
|
@ -75,7 +75,7 @@ public class UnitTest {
|
|||||||
assertEquals(44, model.value);
|
assertEquals(44, model.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test public void fromJsonNonCompatibleNumberTypes() {
|
@Test public void fromJsonRealToByte() {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
String json = "{\"value\": 2.3}";
|
String json = "{\"value\": 2.3}";
|
||||||
try {
|
try {
|
||||||
@ -89,6 +89,27 @@ public class UnitTest {
|
|||||||
fail();
|
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() {
|
@Test public void fromJsonUnicodeChar() {
|
||||||
Gson gson = new Gson();
|
Gson gson = new Gson();
|
||||||
String json = "{\"value\": \"\\u00AE\"}";
|
String json = "{\"value\": \"\\u00AE\"}";
|
||||||
|
Loading…
x
Reference in New Issue
Block a user