Browse Source

Fixed multi prop select.

pull/13/merge
Kalle Stenflo 12 years ago
parent
commit
2d5a17d045
  1. 46
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FieldFilter.java
  2. 102
      json-path/src/test/java/com/jayway/jsonpath/HelpTest.java
  3. 2
      json-path/src/test/java/com/jayway/jsonpath/IssuesTest.java
  4. 2
      json-path/src/test/java/com/jayway/jsonpath/JsonModelChainedCallsTest.java
  5. 4
      json-path/src/test/java/com/jayway/jsonpath/JsonPathFilterTest.java
  6. 1
      json-path/src/test/java/com/jayway/jsonpath/JsonPathTest.java
  7. 9
      json-path/src/test/java/com/jayway/jsonpath/PathTest.java
  8. 4
      pom.xml

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

@ -40,25 +40,55 @@ public class FieldFilter extends PathTokenFilter {
List<Object> result = jsonProvider.createList();
for (Object current : jsonProvider.toList(obj)) {
if (jsonProvider.isMap(current)) {
Map<String, Object> map = jsonProvider.toMap(current);
if (map.containsKey(condition)) {
Object o = map.get(condition);
if (jsonProvider.isList(o)) {
result.addAll(jsonProvider.toList(o));
} else {
result.add(map.get(condition));
String[] split = condition.split("','");
if(split.length == 1){
if (map.containsKey(condition)) {
Object o = map.get(condition);
if (jsonProvider.isList(o)) {
result.addAll(jsonProvider.toList(o));
} else {
result.add(map.get(condition));
}
}
} else {
Map<String, Object> res = jsonProvider.createMap();
for (String prop : split) {
if (map.containsKey(prop)) {
res.put(prop, map.get(prop));
}
}
result.add(res);
}
}
}
return result;
}
} else {
String[] split = condition.split("','");
Map<String, Object> map = jsonProvider.toMap(obj);
if(!map.containsKey(condition)){
if(!map.containsKey(condition) && split.length == 1){
throw new InvalidPathException("invalid path");
} else {
return map.get(condition);
if(split.length == 1){
return map.get(condition);
} else {
Map<String, Object> res = jsonProvider.createMap();
for (String prop : split) {
if(map.containsKey(prop)){
res.put(prop, map.get(prop));
} else {
throw new InvalidPathException("invalid path");
}
}
return res;
}
}
}
}

102
json-path/src/test/java/com/jayway/jsonpath/HelpTest.java

@ -0,0 +1,102 @@
package com.jayway.jsonpath;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.Matchers.hasItems;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
public class HelpTest {
public static final String JSON = "{\n" +
" \"destination_addresses\" : [\n" +
" \"Rua do Dom António José Cordeiro 48, 3800-012 Aveiro, Portugal\",\n" +
" \"Rua do Dom António José Cordeiro 48, 3800-012 Aveiro, Portugal\"\n" +
" ],\n" +
" \"origin_addresses\" : [ \"N109, 3800, Portugal\" ],\n" +
" \"rows\" : [\n" +
" {\n" +
" \"elements\" : [\n" +
" {\n" +
" \"distance\" : {\n" +
" \"text\" : \"0.4 km\",\n" +
" \"value\" : 427\n" +
" },\n" +
" \"duration\" : {\n" +
" \"text\" : \"1 min\",\n" +
" \"value\" : 58\n" +
" },\n" +
" \"status\" : \"OK\"\n" +
" },\n" +
" {\n" +
" \"distance\" : {\n" +
" \"text\" : \"5.0 km\",\n" +
" \"value\" : 427\n" +
" },\n" +
" \"duration\" : {\n" +
" \"text\" : \"1 min\",\n" +
" \"value\" : 58\n" +
" },\n" +
" \"status\" : \"OK\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ],\n" +
" \"status\" : \"OK\"\n" +
"}";
private static final String JSON2 = "{\n" +
" \"error\": null,\n" +
" \"contents\": [\n" +
" {\n" +
" \"groupType\": \"series\",\n" +
" \"instanceId\": \"grp://15\",\n" +
" \"id\": \"prg://16\",\n" +
" \"type\": \"group\",\n" +
" \"media\": [\n" +
" {\n" +
" \"classification\": \"urn:1.2.3\",\n" +
" \"uri\": \"http://yahoo.com/1.png\",\n" +
" \"mimeType\": \"application/octet-stream\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ],\n" +
" \"header\": {\n" +
" \"total\": 1\n" +
" }\n" +
"}";
@Test
public void sample_one_x(){
System.out.println(JsonPath.read(JSON2, "$.['error', 'header']"));
System.out.println(JsonPath.read(JSON2, "$.contents[*].['groupType', 'type']"));
}
@Test
public void sample_one(){
List<String> addresses = JsonPath.read(JSON, "$.destination_addresses[*]");
assertThat(addresses, hasItems("Rua do Dom António José Cordeiro 48, 3800-012 Aveiro, Portugal"));
}
@Test
public void sample_two(){
String text = JsonPath.read(JSON, "$.rows[0].elements[1].distance.text");
assertEquals("5.0 km", text);
}
@Test
public void sample_two_b(){
String text = JsonPath.read(JSON, "$.rows[0].elements[1].distance.text");
assertEquals("5.0 km", text);
}
@Test
public void sample_three(){
List<String> allDistanceTexts = JsonPath.read(JSON, "$.rows[0].elements[*].distance.text");
assertThat(allDistanceTexts, hasItems("0.4 km", "5.0 km"));
}
}

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

@ -40,4 +40,6 @@ public class IssuesTest {
assertTrue(result.isEmpty());
}
}

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

@ -58,7 +58,6 @@ public class JsonModelChainedCallsTest {
JsonModel model = JsonModel.model(DOCUMENT);
Transformer<Map<String, Object>> transformer = new Transformer<Map<String, Object>>() {
@Override
public Object transform(Map<String, Object> map) {
map.remove("isbn");
map.put("author", "kalle");
@ -78,7 +77,6 @@ public class JsonModelChainedCallsTest {
JsonModel model = JsonModel.model(DOCUMENT);
Transformer<Object> transformer = new Transformer<Object>() {
@Override
public Object transform(Object obj) {
Map<String, Object> map = (Map<String, Object>) obj;
map.remove("isbn");

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

@ -101,6 +101,10 @@ public class JsonPathFilterTest {
Filter rootChildFilter = filter(where("name").regex(Pattern.compile("rootChild_[A|B]")));
Filter rootGrandChildFilter = filter(where("name").regex(Pattern.compile("rootGrandChild_[A|B]")));
List read = JsonPath.read(root, "children[?].children[?][?]", rootChildFilter, rootGrandChildFilter, customFilter);

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

@ -72,7 +72,6 @@ public class JsonPathTest {
private final static String ARRAY_EXPAND = "[{\"parent\": \"ONE\", \"child\": {\"name\": \"NAME_ONE\"}}, [{\"parent\": \"TWO\", \"child\": {\"name\": \"NAME_TWO\"}}]]";
@Test
public void array_start_expands() throws Exception {
//assertThat(JsonPath.<List<String>>read(ARRAY_EXPAND, "$[?(@.parent = 'ONE')].child.name"), hasItems("NAME_ONE"));

9
json-path/src/test/java/com/jayway/jsonpath/PathTest.java

@ -111,6 +111,15 @@ public class PathTest {
assertPathInvalid("$...*");
}
@Test
public void multi_field_select(){
PathTokenizer tokenizer = new PathTokenizer("$.contents[*].['groupType', 'type']");
for (String fragment : tokenizer.getFragments()) {
System.out.println(fragment);
}
}
//----------------------------------------------------------------
//

4
pom.xml

@ -112,8 +112,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

Loading…
Cancel
Save