Browse Source

Fixed issue 36 - deep scan for arrays.

pull/10/merge
Kalle Stenflo 11 years ago
parent
commit
5f8beff812
  1. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  2. 2
      json-path/src/test/java/com/jayway/jsonpath/HttpProviderTest.java
  3. 21
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  4. 2
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java

7
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java

@ -54,9 +54,12 @@ public class FieldFilter extends PathTokenFilter {
if(split.length == 1){ if(split.length == 1){
if (keys.contains(condition)) { if (keys.contains(condition)) {
Object o = jsonProvider.getProperty(current, condition); Object o = jsonProvider.getProperty(current, condition);
if (jsonProvider.isArray(o)) {
boolean isArr = jsonProvider.isArray(o);
boolean isEnd = pathToken.isEndToken();
if (isArr && !isEnd) {
for(Object item : jsonProvider.toIterable(o)){ for(Object item : jsonProvider.toIterable(o)){
jsonProvider.setProperty(result, jsonProvider.length(result), item); jsonProvider.setProperty(result, jsonProvider.length(result), item);
} }
} else { } else {
jsonProvider.setProperty(result, jsonProvider.length(result), jsonProvider.getProperty(current, condition)); jsonProvider.setProperty(result, jsonProvider.length(result), jsonProvider.getProperty(current, condition));

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

@ -2,6 +2,7 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.IOUtils; import com.jayway.jsonpath.internal.IOUtils;
import com.jayway.jsonpath.spi.HttpProviderFactory; import com.jayway.jsonpath.spi.HttpProviderFactory;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.io.IOException;
@ -16,6 +17,7 @@ import static junit.framework.Assert.assertEquals;
* Date: 3/10/12 * Date: 3/10/12
* Time: 8:12 AM * Time: 8:12 AM
*/ */
@Ignore
public class HttpProviderTest { public class HttpProviderTest {

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

@ -25,6 +25,27 @@ import static org.junit.Assert.assertThat;
*/ */
public class IssuesTest { public class IssuesTest {
@Test
public void issue_36() {
String json = "{\n" +
"\n" +
" \"arrayOfObjectsAndArrays\" : [ { \"k\" : [\"json\"] }, { \"k\":[\"path\"] }, { \"k\" : [\"is\"] }, { \"k\" : [\"cool\"] } ],\n" +
"\n" +
" \"arrayOfObjects\" : [{\"k\" : \"json\"}, {\"k\":\"path\"}, {\"k\" : \"is\"}, {\"k\" : \"cool\"}]\n" +
"\n" +
" }";
Object o1 = JsonPath.read(json, "$.arrayOfObjectsAndArrays..k ");
Object o2 = JsonPath.read(json, "$.arrayOfObjects..k ");
assertEquals("[[\"json\"],[\"path\"],[\"is\"],[\"cool\"]]", o1.toString());
assertEquals("[\"json\",\"path\",\"is\",\"cool\"]", o2.toString());
}
@Test(expected = PathNotFoundException.class) @Test(expected = PathNotFoundException.class)
public void issue_11() throws Exception { public void issue_11() throws Exception {
String json = "{ \"foo\" : [] }"; String json = "{ \"foo\" : [] }";

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

@ -276,7 +276,7 @@ public class JsonPathTest {
//List<String> books = JsonPath.<List<String>>read(DOCUMENT, "$..book"); //List<String> books = JsonPath.<List<String>>read(DOCUMENT, "$..book");
Object books = JsonPath.<List<String>>read(DOCUMENT, "$..book"); Object books = JsonPath.<List<String>>read(DOCUMENT, "$..book");
System.out.println("test"); System.out.println(books);
} }

Loading…
Cancel
Save