From 24bd4644ff64f5c00bc6ef1b6db2773ec3ff9bf8 Mon Sep 17 00:00:00 2001 From: Javier Date: Wed, 12 Dec 2018 06:00:53 +0100 Subject: [PATCH] BAEL-2412 Add example of deserialization of a double whose precission is too high. --- .../gson/primitives/models/DoubleExample.java | 9 +++++++++ .../java/org/baeldung/gson/primitives/UnitTest.java | 12 ++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 gson/src/main/java/org/baeldung/gson/primitives/models/DoubleExample.java diff --git a/gson/src/main/java/org/baeldung/gson/primitives/models/DoubleExample.java b/gson/src/main/java/org/baeldung/gson/primitives/models/DoubleExample.java new file mode 100644 index 0000000000..5022b6a11e --- /dev/null +++ b/gson/src/main/java/org/baeldung/gson/primitives/models/DoubleExample.java @@ -0,0 +1,9 @@ +package org.baeldung.gson.primitives.models; + +public class DoubleExample { + public double value; + + public String toString() { + return "{float: " + value + "}"; + } +} diff --git a/gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java b/gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java index 2189e990ba..4ee1e67c8e 100644 --- a/gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java +++ b/gson/src/test/java/org/baeldung/gson/primitives/UnitTest.java @@ -53,12 +53,20 @@ public class UnitTest { } @Test public void fromJsonPrecissionMismatch() { - String json = "{\"value\": 12.123456789123456}"; + String json = "{\"value\": 12.123425589123456}"; Gson gson = new Gson(); FloatExample model = gson.fromJson(json, FloatExample.class); - assertEquals(12.123457f, model.value, 0.000001); + assertEquals(12.123426f, model.value, 0.000001); } + @Test public void fromJsonPrecissionMismatchForDouble() { + String json = "{\"value\": 12.123425589123556}"; + Gson gson = new Gson(); + DoubleExample model = gson.fromJson(json, DoubleExample.class); + assertEquals(12.123425589124f, model.value, 0.000001); + } + + @Test public void fromJsonOverflow() { Gson gson = new Gson(); String json = "{\"value\": \"300\"}";