Browse Source

Fixed bracket bug in array filters.

pull/6/head
Kalle Stenflo 13 years ago
parent
commit
2933d3be25
  1. 21
      json-path-assert/pom.xml
  2. 13
      json-path/pom.xml
  3. 23
      json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java
  4. 9
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java
  5. 9
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java
  6. 3
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  7. 21
      json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java
  8. 20
      pom.xml

21
json-path-assert/pom.xml

@ -14,7 +14,8 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>json-path-parent</artifactId>
@ -33,18 +34,6 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
</dependency>
-->
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
@ -54,5 +43,11 @@
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

13
json-path/pom.xml

@ -14,7 +14,8 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.jayway.jsonpath</groupId>
@ -48,15 +49,17 @@
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- test dependencies -->
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

23
json-path/src/main/java/com/jayway/jsonpath/reader/PathTokenizer.java

@ -33,7 +33,7 @@ public class PathTokenizer implements Iterable<PathToken> {
}
}
public List<String> getFragments(){
public List<String> getFragments() {
List<String> fragments = new LinkedList<String>();
for (PathToken pathToken : pathTokens) {
fragments.add(pathToken.getFragment());
@ -106,17 +106,28 @@ public class PathTokenizer implements Iterable<PathToken> {
private String extract(boolean includeSopChar, char... stopChars) {
StringBuilder sb = new StringBuilder();
while (!isEmpty() && (!isStopChar(peek(), stopChars))) {
char c = poll();
if (isStopChar(c, stopChars)) {
if (includeSopChar) {
if (peek() == '(') {
do {
sb.append(poll());
} while (peek() != ')');
sb.append(poll());
} else {
char c = poll();
if (isStopChar(c, stopChars)) {
if (includeSopChar) {
sb.append(c);
}
} else {
sb.append(c);
}
} else {
sb.append(c);
}
}
if (includeSopChar) {

9
json-path/src/main/java/com/jayway/jsonpath/reader/filter/ArrayEvalFilter.java

@ -29,7 +29,14 @@ public class ArrayEvalFilter extends Filter {
List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList();
String trimmedCondition = trim(condition, 5, 2);
String trimmedCondition = condition;
if(condition.contains("['")){
trimmedCondition = trimmedCondition.replace("['", ".");
trimmedCondition = trimmedCondition.replace("']", "");
}
trimmedCondition = trim(trimmedCondition, 5, 2);
ConditionStatement conditionStatement = createConditionStatement(trimmedCondition);

9
json-path/src/main/java/com/jayway/jsonpath/reader/filter/HasFieldFilter.java

@ -24,7 +24,14 @@ public class HasFieldFilter extends Filter {
List<Object> src = jsonProvider.toList(obj);
List<Object> result = jsonProvider.createList();
String trimmedCondition = trim(condition, 5, 2);
String trimmedCondition = condition;
if(condition.contains("['")){
trimmedCondition = trimmedCondition.replace("['", ".");
trimmedCondition = trimmedCondition.replace("']", "");
}
trimmedCondition = trim(trimmedCondition, 5, 2);
for (Object item : src) {
if(jsonProvider.isMap(item)){

3
json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

@ -1,5 +1,6 @@
package com.jayway.jsonpath;
import com.jayway.jsonpath.reader.PathTokenizer;
import com.jayway.jsonpath.util.ScriptEngineJsonPath;
import org.junit.Test;
@ -212,7 +213,9 @@ public class JsonPathTest {
@Test
public void all_books_cheaper_than_10() throws Exception {
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..book[?(@['display-price'] < 10)].title"), hasItems("Sayings of the Century", "Moby Dick"));
assertThat(JsonPath.<List<String>>read(DOCUMENT, "$..book[?(@.display-price < 10)].title"), hasItems("Sayings of the Century", "Moby Dick"));
}

21
json-path/src/test/java/com/jayway/jsonpath/SplitPathFragmentsTest.java

@ -19,6 +19,27 @@ public class SplitPathFragmentsTest {
private JsonProvider jsonProvider = JsonProvider.getInstance();
@Test
public void array_filter_bracket_test() throws Exception {
PathTokenizer tokenizer = new PathTokenizer("$.store.book[?(@['isbn'])].isbn", jsonProvider);
//PathTokenizer tokenizer = new PathTokenizer("$.store.book[?(@.isbn)].isbn", jsonProvider);
for (String fragment : tokenizer.getFragments()) {
System.out.println(fragment);
}
//assertThat(tokenizer.getFragments(), hasItems("$", "store", "[*]"));
}
@Test
public void valid_path_is_split_correctly() throws Exception {

20
pom.xml

@ -14,7 +14,8 @@
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.sonatype.oss</groupId>
@ -31,7 +32,7 @@
<inceptionYear>2011</inceptionYear>
<issueManagement>
<system>GitHub Issue Tracking</system>
<url />
<url/>
</issueManagement>
<licenses>
<license>
@ -165,12 +166,23 @@
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
<!--
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.1</version>
</dependency>
-->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
@ -181,7 +193,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<version>4.10</version>
<scope>test</scope>
</dependency>

Loading…
Cancel
Save