Browse Source

more strag

pull/1/head
David Baldwin 14 years ago
parent
commit
041bd03e58
  1. 47
      json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java
  2. 6
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java
  3. 10
      json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java
  4. 22
      json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java
  5. 5
      json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java

47
json-path/src/main/java/com/jayway/jsonpath/filter/FilterOutput.java

@ -7,7 +7,6 @@ import com.jayway.jsonpath.json.JsonArray;
import com.jayway.jsonpath.json.JsonElement; import com.jayway.jsonpath.json.JsonElement;
import com.jayway.jsonpath.json.JsonException; import com.jayway.jsonpath.json.JsonException;
import com.jayway.jsonpath.json.JsonFactory; import com.jayway.jsonpath.json.JsonFactory;
import com.jayway.jsonpath.json.JsonPathResultList;
import static java.lang.String.format; import static java.lang.String.format;
@ -16,40 +15,52 @@ import static java.lang.String.format;
* Date: 2/9/11 * Date: 2/9/11
* Time: 12:28 PM * Time: 12:28 PM
*/ */
public class FilterOutput extends ArrayList<JsonElement> { public class FilterOutput {
private final List<JsonElement> result;
public FilterOutput(JsonElement root) { public FilterOutput(JsonElement root) {
super(); this.result = new ArrayList<JsonElement>();
this.add(root); result.add(root);
}
public FilterOutput(){
super();
} }
public JsonArray getResultAsJsonArray() throws JsonException { public FilterOutput(List<JsonElement> result) {
return this.get(0).toJsonArray(); this.result = result;
} }
public JsonElement getResultAsJson() throws JsonException {
if(this.size()>1){ public FilterOutput() {
JsonArray ja = JsonFactory.getInstance().createJsonArray(); this.result = new ArrayList<JsonElement>();
for(JsonElement je:this){
ja.add(je);
} }
return ja;
public JsonElement getResult() throws JsonException {
if(result.size()==0){
return null;
} }
else if(this.size()==1){ else if(result.size()==1){
return this.get(0); return result.get(0);
} }
else{ else{
return JsonFactory.getInstance().createJsonNull(null, null); JsonFactory fact = JsonFactory.getInstance();
JsonArray ja = fact.createJsonArray();
for(JsonElement ele:result)
ja.add(ele);
return ja;
}
} }
public JsonArray getResultAsList() throws JsonException {
return getResult().toJsonArray();
} }
public List<JsonElement> getList() throws JsonException {
return result;
}
} }

6
json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterBase.java

@ -14,13 +14,13 @@ import com.jayway.jsonpath.json.JsonException;
*/ */
public abstract class JsonPathFilterBase { public abstract class JsonPathFilterBase {
public FilterOutput apply(FilterOutput element) throws JsonException{ public FilterOutput apply(FilterOutput element) throws JsonException{
FilterOutput result = new FilterOutput(); List<JsonElement> result = new ArrayList<JsonElement>();
for(JsonElement el : element){ for(JsonElement el : element.getList()){
List<JsonElement> out = apply(el); List<JsonElement> out = apply(el);
if(out != null) if(out != null)
result.addAll(out); result.addAll(out);
} }
return result; return new FilterOutput(result);
} }
public abstract List<JsonElement> apply(JsonElement element) throws JsonException; public abstract List<JsonElement> apply(JsonElement element) throws JsonException;
public abstract String getPathSegment() throws JsonException;; public abstract String getPathSegment() throws JsonException;;

10
json-path/src/main/java/com/jayway/jsonpath/filter/JsonPathFilterChain.java

@ -43,17 +43,17 @@ public class JsonPathFilterChain {
public FilterOutput filter(JsonElement root) throws JsonException { public FilterOutput filter(JsonElement root) throws JsonException {
FilterOutput out = new FilterOutput(root); FilterOutput out = new FilterOutput(root);
log.info(out.getResultAsJson().toString()); log.info(out.getResult().toString());
for (JsonPathFilterBase filter : filters) { for (JsonPathFilterBase filter : filters) {
if (filter == null) { if (filter == null) {
throw new InvalidPathException(); throw new InvalidPathException();
} }
if(out.getResultAsJson().isJsonNull()){ if(out.getList() == null){
break; return null;
} }
out = filter.apply(out); out = filter.apply(out);
if(out.getResult()!=null)
log.info(out.getResultAsJson().toString()); log.info(out.getResult().toString());
} }

22
json-path/src/main/java/com/jayway/jsonpath/filter/ListIndexFilter.java

@ -3,12 +3,10 @@ package com.jayway.jsonpath.filter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.google.gson.JsonNull;
import com.jayway.jsonpath.json.JsonArray; import com.jayway.jsonpath.json.JsonArray;
import com.jayway.jsonpath.json.JsonElement; import com.jayway.jsonpath.json.JsonElement;
import com.jayway.jsonpath.json.JsonException; import com.jayway.jsonpath.json.JsonException;
@ -37,31 +35,19 @@ public class ListIndexFilter extends JsonPathFilterBase {
@Override @Override
public List<JsonElement> apply(JsonElement element) throws JsonException { public List<JsonElement> apply(JsonElement element) throws JsonException {
List<JsonElement> result = new ArrayList<JsonElement>();
Integer[] index = getArrayIndex(); Integer[] index = getArrayIndex();
ArrayList<JsonElement> result = new ArrayList<JsonElement>();
filterRange(result,element,index);
return result;
}
private <T extends Collection> void filterRange(T result,JsonElement element,Integer[] index) throws JsonException{
if(element.isJsonArray()){ if(element.isJsonArray()){
for (int i : index) { for (int i : index) {
if (indexIsInRange(element.toJsonArray(), i)) { if (indexIsInRange(element.toJsonArray(), i)) {
result.add(element.toJsonArray().get(i)); result.add(element.toJsonArray().get(i));
} }
else{
com.jayway.jsonpath.json.JsonNull jn = factory.createJsonNull(i,element);
result.add(jn);
}
} }
} }
return result;
} }
private boolean indexIsInRange(List list, int index) { private boolean indexIsInRange(List list, int index) {

5
json-path/src/main/java/com/jayway/jsonpath/filter/PropertyFilter.java

@ -43,13 +43,8 @@ public class PropertyFilter extends JsonPathFilterBase {
if (element.isJsonObject() && element.toJsonObject().hasProperty(pathFragment)) { if (element.isJsonObject() && element.toJsonObject().hasProperty(pathFragment)) {
JsonElement o = element.toJsonObject().getProperty(pathFragment); JsonElement o = element.toJsonObject().getProperty(pathFragment);
if(o != null){
result.add(o); result.add(o);
} }
else{
result.add(factory.createJsonNull(pathFragment,element));
}
}
else if(element.isJsonObject()){ else if(element.isJsonObject()){
result.add(factory.createJsonNull(pathFragment,element)); result.add(factory.createJsonNull(pathFragment,element));
} }

Loading…
Cancel
Save