From cc05493c69ba7c2068ba7be87394f8bb59862996 Mon Sep 17 00:00:00 2001 From: Cowtowncoder Date: Mon, 16 Feb 2015 14:10:21 -0800 Subject: [PATCH] Fixed #707 --- release-notes/VERSION | 5 +++++ .../jackson/databind/node/FloatNode.java | 4 +++- .../jackson/databind/node/TestNumberNodes.java | 16 ++++++++++++---- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/release-notes/VERSION b/release-notes/VERSION index 1f643cbdfd..ecc27159a3 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -4,6 +4,11 @@ Project: jackson-databind === Releases === ------------------------------------------------------------------------ +2.4.6 (not yet released) + +#707: Error in getting string representation of an ObjectNode with a float number value + (reported by @navidqar) + 2.4.5 (13-Jan-2015) #635: Reduce cachability of `Map` deserializers, to avoid problems with per-property config changes diff --git a/src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java b/src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java index bb6ee406a0..5259cdc9dd 100644 --- a/src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java +++ b/src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java @@ -89,7 +89,9 @@ public BigInteger bigIntegerValue() { @Override public String asText() { - return NumberOutput.toString(_value); + // As per [jackson-core#179] +// return NumberOutput.toString(_value); + return Float.toString(_value); } @Override diff --git a/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java b/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java index 80269141f1..35137c02d5 100644 --- a/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java +++ b/src/test/java/com/fasterxml/jackson/databind/node/TestNumberNodes.java @@ -128,17 +128,25 @@ public void testDouble() throws Exception // @since 2.2 public void testFloat() { - FloatNode n = FloatNode.valueOf(0.25f); + FloatNode n = FloatNode.valueOf(0.45f); assertStandardEquals(n); assertTrue(0 != n.hashCode()); assertEquals(JsonToken.VALUE_NUMBER_FLOAT, n.asToken()); assertEquals(JsonParser.NumberType.FLOAT, n.numberType()); assertEquals(0, n.intValue()); - assertEquals(0.25, n.doubleValue()); - assertEquals(0.25f, n.floatValue()); + + // NOTE: conversion to double NOT as simple as with exact numbers like 0.25: + assertEquals(0.45f, n.floatValue()); + assertEquals("0.45", n.asText()); + + // so; as double we'll get more complex number; however, should round-trip + // to something that gets printed the same way. But not exact value, alas, hence: + assertEquals("0.45", String.valueOf((float) n.doubleValue())); + assertNotNull(n.decimalValue()); + // possibly surprisingly, however, this will produce same output: assertEquals(BigInteger.ZERO, n.bigIntegerValue()); - assertEquals("0.25", n.asText()); + assertEquals("0.45", n.asText()); // 1.6: assertNodeNumbers(FloatNode.valueOf(4.5f), 4, 4.5f);