Browse Source

Added method to honor backwards compatibility.

pull/183/merge
Kalle Stenflo 9 years ago
parent
commit
8775a1f100
  1. 29
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  2. 10
      json-path/src/test/java/com/jayway/jsonpath/FilterTest.java

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

@ -388,9 +388,34 @@ public class Criteria implements Predicate {
*/ */
@Deprecated @Deprecated
public static Criteria parse(String criteria) { public static Criteria parse(String criteria) {
//TODO: fix if(criteria == null){
return null; throw new InvalidPathException("Criteria can not be null");
} }
String[] split = criteria.trim().split(" ");
if(split.length == 3){
return create(split[0], split[1], split[2]);
} else if(split.length == 1){
return create(split[0], "EXISTS", "true");
} else {
throw new InvalidPathException("Could not parse criteria");
}
}
/**
* Creates a new criteria
* @param left path to evaluate in criteria
* @param operator operator
* @param right expected value
* @return a new Criteria
*/
@Deprecated
public static Criteria create(String left, String operator, String right) {
Criteria criteria = new Criteria(ValueNode.toValueNode(left));
criteria.criteriaType = RelationalOperator.fromString(operator);
criteria.right = ValueNode.toValueNode(right);
return criteria;
}
private static String prefixPath(String key){ private static String prefixPath(String key){
if (!key.startsWith("$") && !key.startsWith("@")) { if (!key.startsWith("$") && !key.startsWith("@")) {

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

@ -469,4 +469,14 @@ public class FilterTest extends BaseTest {
Object json = Configuration.defaultConfiguration().jsonProvider().parse("{\"firstname\":\"Bob\",\"surname\":\"Smith\",\"age\":30}"); Object json = Configuration.defaultConfiguration().jsonProvider().parse("{\"firstname\":\"Bob\",\"surname\":\"Smith\",\"age\":30}");
assertThat(Filter.parse("[?((@.firstname == 'Bob' || @.firstname == 'Jane') && @.surname == 'Smith')]").apply(createPredicateContext(json))).isTrue(); assertThat(Filter.parse("[?((@.firstname == 'Bob' || @.firstname == 'Jane') && @.surname == 'Smith')]").apply(createPredicateContext(json))).isTrue();
} }
@Test
public void criteria_can_be_parsed() {
Criteria criteria = Criteria.parse("@.foo == 'baar'");
assertThat(criteria.toString()).isEqualTo("@['foo'] == 'baar'");
criteria = Criteria.parse("@.foo");
assertThat(criteria.toString()).isEqualTo("@['foo']");
}
} }

Loading…
Cancel
Save