Skip to content

Commit 891b186

Browse files
siavashsoleymanisiavashsoleymani
and
siavashsoleymani
authored
#2885 hasFractionalPart method added (#2891)
#2885: Add JsonNode.hasFractionalPart() Co-authored-by: siavashsoleymani <[email protected]>
1 parent 2fdaefd commit 891b186

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

src/main/java/com/fasterxml/jackson/databind/JsonNode.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,14 @@ public final boolean isNumber() {
316316
*/
317317
public boolean isInt() { return false; }
318318

319+
/**
320+
* Method that can be used to check whether contained value
321+
* has a fractional part or not.
322+
*
323+
* @return True if the value has fractional part
324+
*/
325+
public boolean hasFractionalPart() { return false; }
326+
319327
/**
320328
* Method that can be used to check whether contained value
321329
* is a number represented as Java <code>long</code>.

src/main/java/com/fasterxml/jackson/databind/node/DecimalNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public class DecimalNode
5757
@Override
5858
public boolean isBigDecimal() { return true; }
5959

60+
@Override
61+
public boolean hasFractionalPart() {
62+
return !(_value.signum() == 0 || _value.scale() <= 0 || _value.stripTrailingZeros().scale() <= 0);
63+
}
64+
6065
@Override public boolean canConvertToInt() {
6166
return (_value.compareTo(MIN_INTEGER) >= 0) && (_value.compareTo(MAX_INTEGER) <= 0);
6267
}

src/main/java/com/fasterxml/jackson/databind/node/DoubleNode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public class DoubleNode
5252
@Override
5353
public boolean isDouble() { return true; }
5454

55+
@Override
56+
public boolean hasFractionalPart() {
57+
return !Double.isNaN(_value) && !Double.isInfinite(_value) && _value % 1 != 0.0;
58+
}
59+
5560
@Override public boolean canConvertToInt() {
5661
return (_value >= Integer.MIN_VALUE && _value <= Integer.MAX_VALUE);
5762
}

src/main/java/com/fasterxml/jackson/databind/node/FloatNode.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.fasterxml.jackson.databind.node;
22

3+
import com.fasterxml.jackson.core.JsonGenerator;
4+
import com.fasterxml.jackson.core.JsonParser;
5+
import com.fasterxml.jackson.core.JsonToken;
6+
import com.fasterxml.jackson.core.io.NumberOutput;
7+
import com.fasterxml.jackson.databind.SerializerProvider;
38
import java.io.IOException;
49
import java.math.BigDecimal;
510
import java.math.BigInteger;
611

7-
import com.fasterxml.jackson.core.*;
8-
import com.fasterxml.jackson.core.io.NumberOutput;
9-
import com.fasterxml.jackson.databind.SerializerProvider;
10-
1112
/**
1213
* <code>JsonNode</code> implementation for efficiently containing 32-bit
1314
* `float` values.
@@ -52,6 +53,11 @@ public class FloatNode extends NumericNode
5253
@Override
5354
public boolean isFloat() { return true; }
5455

56+
@Override
57+
public boolean hasFractionalPart() {
58+
return !Float.isNaN(_value) && !Float.isInfinite(_value) && _value % 1 != 0.0;
59+
}
60+
5561
@Override public boolean canConvertToInt() {
5662
return (_value >= Integer.MIN_VALUE && _value <= Integer.MAX_VALUE);
5763
}

src/test/java/com/fasterxml/jackson/databind/node/NumberNodesTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public void testIntViaMapper() throws Exception
5050
assertType(result, IntNode.class);
5151
assertFalse(result.isLong());
5252
assertFalse(result.isFloatingPointNumber());
53+
assertFalse(result.hasFractionalPart());
5354
assertFalse(result.isDouble());
5455
assertFalse(result.isNull());
5556
assertFalse(result.isTextual());
@@ -131,6 +132,7 @@ public void testLongViaMapper() throws Exception
131132
assertType(result, LongNode.class);
132133
assertFalse(result.isInt());
133134
assertFalse(result.isFloatingPointNumber());
135+
assertFalse(result.hasFractionalPart());
134136
assertFalse(result.isDouble());
135137
assertFalse(result.isNull());
136138
assertFalse(result.isTextual());
@@ -185,6 +187,7 @@ public void testDoubleViaMapper() throws Exception
185187
assertFalse(result.isNull());
186188
assertType(result, DoubleNode.class);
187189
assertTrue(result.isFloatingPointNumber());
190+
assertTrue(result.hasFractionalPart());
188191
assertTrue(result.isDouble());
189192
assertFalse(result.isInt());
190193
assertFalse(result.isLong());
@@ -211,7 +214,9 @@ public void testFloat()
211214
assertEquals(JsonToken.VALUE_NUMBER_FLOAT, n.asToken());
212215
assertEquals(JsonParser.NumberType.FLOAT, n.numberType());
213216
assertEquals(0, n.intValue());
214-
217+
assertTrue(n.isFloatingPointNumber());
218+
assertTrue(n.hasFractionalPart());
219+
215220
// NOTE: conversion to double NOT as simple as with exact numbers like 0.25:
216221
assertEquals(0.45f, n.floatValue());
217222
assertEquals("0.45", n.asText());
@@ -281,6 +286,7 @@ public void testDecimalNode() throws Exception
281286
assertType(result, DecimalNode.class);
282287
assertFalse(result.isInt());
283288
assertTrue(result.isFloatingPointNumber());
289+
assertTrue(result.hasFractionalPart());
284290
assertTrue(result.isBigDecimal());
285291
assertFalse(result.isDouble());
286292
assertFalse(result.isNull());

0 commit comments

Comments
 (0)