From 3a077886e94eda00370dfd408df648b2fdb3eede Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Wed, 11 Nov 2015 23:39:02 +0100 Subject: [PATCH] Fixed EMPTY operator. --- .../main/java/com/jayway/jsonpath/Criteria.java | 15 +++++++++++++-- .../internal/filter/EvaluatorFactory.java | 8 ++++---- .../jsonpath/internal/filter/FilterCompiler.java | 8 ++++---- .../internal/filter/RelationalOperator.java | 2 +- .../java/com/jayway/jsonpath/FilterParseTest.java | 4 ++-- .../test/java/com/jayway/jsonpath/FilterTest.java | 2 +- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/json-path/src/main/java/com/jayway/jsonpath/Criteria.java b/json-path/src/main/java/com/jayway/jsonpath/Criteria.java index ff69229a..0a665b57 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/Criteria.java +++ b/json-path/src/main/java/com/jayway/jsonpath/Criteria.java @@ -350,9 +350,20 @@ public class Criteria implements Predicate { * * @return the criteria */ + @Deprecated public Criteria notEmpty() { - this.criteriaType = RelationalOperator.NOT_EMPTY; - this.right = ValueNode.TRUE; + return empty(false); + } + + /** + * The notEmpty operator checks that an array or String is empty. + * + * @param empty should be empty + * @return the criteria + */ + public Criteria empty(boolean empty) { + this.criteriaType = RelationalOperator.EMPTY; + this.right = empty ? ValueNode.TRUE : ValueNode.FALSE; return this; } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java index c1647515..8b9a72ef 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java @@ -20,7 +20,7 @@ public class EvaluatorFactory { evaluators.put(RelationalOperator.GTE, new GreaterThanEqualsEvaluator()); evaluators.put(RelationalOperator.REGEX, new RegexpEvaluator()); evaluators.put(RelationalOperator.SIZE, new SizeEvaluator()); - evaluators.put(RelationalOperator.NOT_EMPTY, new NotEmptyEvaluator()); + evaluators.put(RelationalOperator.EMPTY, new EmptyEvaluator()); evaluators.put(RelationalOperator.IN, new InEvaluator()); evaluators.put(RelationalOperator.NIN, new NotInEvaluator()); evaluators.put(RelationalOperator.ALL, new AllEvaluator()); @@ -130,13 +130,13 @@ public class EvaluatorFactory { } } - private static class NotEmptyEvaluator implements Evaluator { + private static class EmptyEvaluator implements Evaluator { @Override public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateContext ctx) { if(left.isStringNode()){ - return !left.asStringNode().isEmpty(); + return left.asStringNode().isEmpty() == right.asBooleanNode().getBoolean(); } else if(left.isJsonNode()){ - return !left.asJsonNode().isEmpty(ctx); + return left.asJsonNode().isEmpty(ctx) == right.asBooleanNode().getBoolean(); } return false; } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java index e6fc3d9b..48e7ff44 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java @@ -150,10 +150,10 @@ public class FilterCompiler { } } RelationalOperator operator = readRelationalOperator(); - ValueNode right = ValueNode.TRUE; - if(operator != RelationalOperator.NOT_EMPTY) { - right = readValueNode(); - } + //ValueNode right = ValueNode.TRUE; + //if(operator != RelationalOperator.EMPTY) { + ValueNode right = readValueNode(); + //} return new RelationalExpressionNode(left, operator, right); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java index bc18b818..37e43e81 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java @@ -19,7 +19,7 @@ public enum RelationalOperator { EXISTS("¦EXISTS¦"), TYPE("¦TYPE¦"), MATCHES("¦MATCHES¦"), - NOT_EMPTY("¦NOT_EMPTY¦"); + EMPTY("¦EMPTY¦"); private final String operatorString; diff --git a/json-path/src/test/java/com/jayway/jsonpath/FilterParseTest.java b/json-path/src/test/java/com/jayway/jsonpath/FilterParseTest.java index ff2d0c1f..ce24838a 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/FilterParseTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/FilterParseTest.java @@ -176,8 +176,8 @@ public class FilterParseTest { @Test public void a_not_empty_filter_can_be_serialized() { - String filter = filter(where("a").notEmpty()).toString(); - String parsed = parse("[?(@['a'] ¦NOT_EMPTY¦)]").toString(); + String filter = filter(where("a").empty(false)).toString(); + String parsed = parse("[?(@['a'] ¦EMPTY¦ false)]").toString(); assertThat(filter).isEqualTo(parsed); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/FilterTest.java b/json-path/src/test/java/com/jayway/jsonpath/FilterTest.java index f43b8fbe..d1aafce4 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/FilterTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/FilterTest.java @@ -370,7 +370,7 @@ public class FilterTest extends BaseTest { //---------------------------------------------------------------------------- // - // NOT_EMPTY + // EMPTY // //---------------------------------------------------------------------------- @Test