Browse Source

Fixed EMPTY operator.

pull/183/merge
Kalle Stenflo 9 years ago
parent
commit
3a077886e9
  1. 15
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  2. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/EvaluatorFactory.java
  3. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java
  4. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java
  5. 4
      json-path/src/test/java/com/jayway/jsonpath/FilterParseTest.java
  6. 2
      json-path/src/test/java/com/jayway/jsonpath/FilterTest.java

15
json-path/src/main/java/com/jayway/jsonpath/Criteria.java

@ -350,9 +350,20 @@ public class Criteria implements Predicate {
* *
* @return the criteria * @return the criteria
*/ */
@Deprecated
public Criteria notEmpty() { public Criteria notEmpty() {
this.criteriaType = RelationalOperator.NOT_EMPTY; return empty(false);
this.right = ValueNode.TRUE; }
/**
* The <code>notEmpty</code> 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; return this;
} }

8
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.GTE, new GreaterThanEqualsEvaluator());
evaluators.put(RelationalOperator.REGEX, new RegexpEvaluator()); evaluators.put(RelationalOperator.REGEX, new RegexpEvaluator());
evaluators.put(RelationalOperator.SIZE, new SizeEvaluator()); 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.IN, new InEvaluator());
evaluators.put(RelationalOperator.NIN, new NotInEvaluator()); evaluators.put(RelationalOperator.NIN, new NotInEvaluator());
evaluators.put(RelationalOperator.ALL, new AllEvaluator()); 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 @Override
public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateContext ctx) { public boolean evaluate(ValueNode left, ValueNode right, Predicate.PredicateContext ctx) {
if(left.isStringNode()){ if(left.isStringNode()){
return !left.asStringNode().isEmpty(); return left.asStringNode().isEmpty() == right.asBooleanNode().getBoolean();
} else if(left.isJsonNode()){ } else if(left.isJsonNode()){
return !left.asJsonNode().isEmpty(ctx); return left.asJsonNode().isEmpty(ctx) == right.asBooleanNode().getBoolean();
} }
return false; return false;
} }

8
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java

@ -150,10 +150,10 @@ public class FilterCompiler {
} }
} }
RelationalOperator operator = readRelationalOperator(); RelationalOperator operator = readRelationalOperator();
ValueNode right = ValueNode.TRUE; //ValueNode right = ValueNode.TRUE;
if(operator != RelationalOperator.NOT_EMPTY) { //if(operator != RelationalOperator.EMPTY) {
right = readValueNode(); ValueNode right = readValueNode();
} //}
return new RelationalExpressionNode(left, operator, right); return new RelationalExpressionNode(left, operator, right);
} }

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java

@ -19,7 +19,7 @@ public enum RelationalOperator {
EXISTS("¦EXISTS¦"), EXISTS("¦EXISTS¦"),
TYPE("¦TYPE¦"), TYPE("¦TYPE¦"),
MATCHES("¦MATCHES¦"), MATCHES("¦MATCHES¦"),
NOT_EMPTY(NOT_EMPTY¦"); EMPTY("¦EMPTY¦");
private final String operatorString; private final String operatorString;

4
json-path/src/test/java/com/jayway/jsonpath/FilterParseTest.java

@ -176,8 +176,8 @@ public class FilterParseTest {
@Test @Test
public void a_not_empty_filter_can_be_serialized() { public void a_not_empty_filter_can_be_serialized() {
String filter = filter(where("a").notEmpty()).toString(); String filter = filter(where("a").empty(false)).toString();
String parsed = parse("[?(@['a'] ¦NOT_EMPTY¦)]").toString(); String parsed = parse("[?(@['a'] ¦EMPTY¦ false)]").toString();
assertThat(filter).isEqualTo(parsed); assertThat(filter).isEqualTo(parsed);
} }

2
json-path/src/test/java/com/jayway/jsonpath/FilterTest.java

@ -370,7 +370,7 @@ public class FilterTest extends BaseTest {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
// //
// NOT_EMPTY // EMPTY
// //
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@Test @Test

Loading…
Cancel
Save