Browse Source

unescape strings in comparison, to deal with tabs, etc.

pull/115/head
Elias Ross 10 years ago
parent
commit
fb9c621e5c
  1. 2
      json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java
  2. 40
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  3. 12
      json-path/src/main/java/com/jayway/jsonpath/ValueCompareException.java
  4. 1
      json-path/src/test/java/com/jayway/jsonpath/BaseTest.java
  5. 7
      json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java

2
json-path-assert/src/test/java/com/jayway/jsonassert/JsonAssertTest.java

@ -40,6 +40,7 @@ public class JsonAssertTest {
" \"price\": 19.95\n," +
" \"gears\": [23, 50]\n," +
" \"extra\": {\"x\": 0}\n," +
" \"escape\" : \"Esc\\b\\f\\n\\r\\t\\u002A\",\n" +
" \"nullValue\": null\n" +
" }\n" +
" }\n" +
@ -62,6 +63,7 @@ public class JsonAssertTest {
with(JSON).assertThat("$.store.bicycle[?(@.gears == [23, 50])]", is(collectionWithSize(equalTo(1))));
with(JSON).assertThat("$.store.bicycle[?(@.gears == [23, 77])]", is(collectionWithSize(equalTo(0))));
with(JSON).assertThat("$.store.bicycle[?(@.extra == {\"x\":0})]", is(collectionWithSize(equalTo(1))));
with(JSON).assertThat("$.store.bicycle[?(@.escape == 'Esc\\b\\f\\n\\r\\t\\u002A')]", is(collectionWithSize(equalTo(1))));
}
@Test(expected = AssertionError.class)

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

@ -49,6 +49,7 @@ public class Criteria implements Predicate {
CriteriaType.GT.toString(),
CriteriaType.REGEX.toString()
};
private static final char BS = '\\';
private Object left;
private CriteriaType criteriaType;
@ -844,6 +845,38 @@ public class Criteria implements Predicate {
return safeCompare(left, right, null);
}
private static String unescape(String s) {
if (s.indexOf(BS) == - 1)
return s;
StringBuilder sb = new StringBuilder(s.length());
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == BS) {
char c2 = s.charAt(++i);
switch (c2) {
case 'b': c2 = '\b'; break;
case 'f': c2 = '\f'; break;
case 'n': c2 = '\n'; break;
case 'r': c2 = '\r'; break;
case 't': c2 = '\t'; break;
case 'u':
try {
String hex = s.substring(i + 1, i + 5);
c2 = (char)Integer.parseInt(hex, 16);
i += 4;
} catch (Exception e) {
throw new ValueCompareException("\\u parse failed", e);
}
break;
}
sb.append(c2);
} else {
sb.append(c);
}
}
return sb.toString();
}
private static int safeCompare(Object left, Object right, PredicateContext ctx) throws ValueCompareException {
if (left == right) {
@ -860,11 +893,8 @@ public class Criteria implements Predicate {
} else if (leftNullish && rightNullish) {
return 0;
} else if (left instanceof String && right instanceof String) {
String exp = (String) left;
if (exp.contains("\'")) {
exp = exp.replace("\\'", "'");
}
return exp.compareTo((String) right);
String expected = unescape((String) left);
return expected.compareTo((String) right);
} else if (left instanceof Number && right instanceof Number) {
return new BigDecimal(left.toString()).compareTo(new BigDecimal(right.toString()));
} else if (left instanceof String && right instanceof Number) {

12
json-path/src/main/java/com/jayway/jsonpath/ValueCompareException.java

@ -15,4 +15,16 @@
package com.jayway.jsonpath;
public class ValueCompareException extends JsonPathException {
public ValueCompareException() {
}
public ValueCompareException(String message) {
super(message);
}
public ValueCompareException(String message, Throwable cause) {
super(message, cause);
}
}

1
json-path/src/test/java/com/jayway/jsonpath/BaseTest.java

@ -78,6 +78,7 @@ public class BaseTest {
" ],\n" +
" \"bicycle\" : {\n" +
" \"foo\" : \"baz\",\n" +
" \"escape\" : \"Esc\\b\\f\\n\\r\\t\\n\\t\\u002A\",\n" +
" \"color\" : \"red\",\n" +
" \"display-price\" : 19.95,\n" +
" \"foo:bar\" : \"fooBar\",\n" +

7
json-path/src/test/java/com/jayway/jsonpath/InlineFilterTest.java

@ -11,6 +11,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class InlineFilterTest extends BaseTest {
private static ReadContext reader = JsonPath.parse(JSON_DOCUMENT);
private static int bookCount = 4;
@Test
public void root_context_can_be_referred_in_predicate() {
@ -23,13 +24,13 @@ public class InlineFilterTest extends BaseTest {
public void multiple_context_object_can_be_refered() {
List all = reader.read("store.book[ ?(@.category == @.category) ]", List.class);
assertThat(all.size()).isEqualTo(4);
assertThat(all.size()).isEqualTo(bookCount);
List all2 = reader.read("store.book[ ?(@.category == @['category']) ]", List.class);
assertThat(all2.size()).isEqualTo(4);
assertThat(all2.size()).isEqualTo(bookCount);
List all3 = reader.read("store.book[ ?(@ == @) ]", List.class);
assertThat(all3.size()).isEqualTo(4);
assertThat(all3.size()).isEqualTo(bookCount);
List none = reader.read("store.book[ ?(@.category != @.category) ]", List.class);
assertThat(none.size()).isEqualTo(0);

Loading…
Cancel
Save