Skip to content

Commit 2f98256

Browse files
authored
Merge pull request jamsesso#10 from jamsesso/infinity_nan_stringeqzero
Implement truthy rules for infinity and NaN values, and fix empty string comparison to 0
2 parents a94d21b + 5265067 commit 2f98256

File tree

8 files changed

+62
-3
lines changed

8 files changed

+62
-3
lines changed

src/main/java/io/github/jamsesso/jsonlogic/JsonLogic.java

+22
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ public static boolean truthy(Object value) {
100100
}
101101

102102
if (value instanceof Number) {
103+
if (value instanceof Double) {
104+
Double d = (Double) value;
105+
106+
if (d.isNaN()) {
107+
return false;
108+
}
109+
else if (d.isInfinite()) {
110+
return true;
111+
}
112+
}
113+
114+
if (value instanceof Float) {
115+
Float f = (Float) value;
116+
117+
if (f.isNaN()) {
118+
return false;
119+
}
120+
else if (f.isInfinite()) {
121+
return true;
122+
}
123+
}
124+
103125
return ((Number) value).doubleValue() != 0.0;
104126
}
105127

src/main/java/io/github/jamsesso/jsonlogic/evaluator/expressions/EqualityExpression.java

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
8282

8383
private boolean compareNumberToString(Number left, String right) {
8484
try {
85+
if (right.trim().isEmpty()) {
86+
right = "0";
87+
}
88+
8589
return Double.parseDouble(right) == left.doubleValue();
8690
}
8791
catch (NumberFormatException e) {

src/main/java/io/github/jamsesso/jsonlogic/evaluator/expressions/InExpression.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public String key() {
2020
@Override
2121
public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationException {
2222
if (arguments.size() < 2) {
23-
throw new JsonLogicEvaluationException("in expects exactly 2 arguments");
23+
return false;
2424
}
2525

2626
// Handle string in (substring)
@@ -32,6 +32,6 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
3232
return new ArrayLike(arguments.get(1)).contains(arguments.get(0));
3333
}
3434

35-
throw new JsonLogicEvaluationException("in expects the second argument to be either a string or array");
35+
return false;
3636
}
3737
}

src/main/java/io/github/jamsesso/jsonlogic/evaluator/expressions/MathExpression.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.function.BiFunction;
77

88
public class MathExpression implements PreEvaluatedArgumentsExpression {
9-
public static final MathExpression ADD = new MathExpression("+", (a, b) -> a + b);
9+
public static final MathExpression ADD = new MathExpression("+", Double::sum);
1010
public static final MathExpression SUBTRACT = new MathExpression("-", (a, b) -> a - b, 2);
1111
public static final MathExpression MULTIPLY = new MathExpression("*", (a, b) -> a * b);
1212
public static final MathExpression DIVIDE = new MathExpression("/", (a, b) -> a / b, 2);
@@ -52,6 +52,10 @@ public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationEx
5252
if (key.equals("-") && arguments.get(0) instanceof Number) {
5353
return -1 * ((Number) arguments.get(0)).doubleValue();
5454
}
55+
56+
if (key.equals("/")) {
57+
return null;
58+
}
5559
}
5660

5761
// Collect all of the arguments

src/test/java/io/github/jamsesso/jsonlogic/EqualityExpressionTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ public void testSameValueDifferentType() throws JsonLogicException {
2121
public void testDifferentValueDifferentType() throws JsonLogicException {
2222
assertEquals(true, jsonLogic.apply("{\"==\": [[], false]}", null));
2323
}
24+
25+
@Test
26+
public void testEmptyStringAndZeroComparison() throws JsonLogicException {
27+
assertEquals(true, jsonLogic.apply("{\"==\": [\" \", 0]}", null));
28+
}
2429
}

src/test/java/io/github/jamsesso/jsonlogic/InExpressionTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.stream.Stream;
1010

1111
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertFalse;
1213

1314
public class InExpressionTests {
1415
private static final JsonLogic jsonLogic = new JsonLogic();
@@ -55,4 +56,14 @@ public void testAllVariables() throws JsonLogicException {
5556

5657
assertEquals(true, jsonLogic.apply("{\"in\": [{\"var\": \"value\"}, {\"var\": \"list\"}]}", data));
5758
}
59+
60+
@Test
61+
public void testSingleArgument() throws JsonLogicException {
62+
assertFalse((boolean) jsonLogic.apply("{\"in\": [\"Spring\"]}", null));
63+
}
64+
65+
@Test
66+
public void testBadSecondArgument() throws JsonLogicException {
67+
assertFalse((boolean) jsonLogic.apply("{\"in\": [\"Spring\", 3]}", null));
68+
}
5869
}

src/test/java/io/github/jamsesso/jsonlogic/MathExpressionTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,9 @@ public void testMax() throws JsonLogicException {
102102

103103
assertEquals(3.0, result);
104104
}
105+
106+
@Test
107+
public void testDivideSingleNumber() throws JsonLogicException {
108+
assertEquals(null, jsonLogic.apply("{\"/\": [0]}", null));
109+
}
105110
}

src/test/java/io/github/jamsesso/jsonlogic/TruthyTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,13 @@ public void testTruthyValues() {
3434

3535
// Null
3636
assertFalse(JsonLogic.truthy(null));
37+
38+
// NaN and Infinity
39+
assertFalse(JsonLogic.truthy(Double.NaN));
40+
assertFalse(JsonLogic.truthy(Float.NaN));
41+
assertTrue(JsonLogic.truthy(Double.POSITIVE_INFINITY));
42+
assertTrue(JsonLogic.truthy(Double.NEGATIVE_INFINITY));
43+
assertTrue(JsonLogic.truthy(Float.POSITIVE_INFINITY));
44+
assertTrue(JsonLogic.truthy(Float.NEGATIVE_INFINITY));
3745
}
3846
}

0 commit comments

Comments
 (0)