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.JsonException;
import com.jayway.jsonpath.json.JsonFactory;
import com.jayway.jsonpath.json.JsonPathResultList;
import static java.lang.String.format;
@ -16,40 +15,52 @@ import static java.lang.String.format;
* Date: 2/9/11
* Time: 12:28 PM
*/
public class FilterOutput extends ArrayList<JsonElement> {
public class FilterOutput {
private final List<JsonElement> result;
public FilterOutput(JsonElement root) {
super();
this.add(root);
}
public FilterOutput(){
super();
this.result = new ArrayList<JsonElement>();
result.add(root);
}
public JsonArray getResultAsJsonArray() throws JsonException {
return this.get(0).toJsonArray();
public FilterOutput(List<JsonElement> result) {
this.result = result;
}
public JsonElement getResultAsJson() throws JsonException {
if(this.size()>1){
JsonArray ja = JsonFactory.getInstance().createJsonArray();
for(JsonElement je:this){
ja.add(je);
public FilterOutput() {
this.result = new ArrayList<JsonElement>();
}
return ja;
public JsonElement getResult() throws JsonException {
if(result.size()==0){
return null;
}
else if(this.size()==1){
return this.get(0);
else if(result.size()==1){
return result.get(0);
}
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 FilterOutput apply(FilterOutput element) throws JsonException{
FilterOutput result = new FilterOutput();
for(JsonElement el : element){
List<JsonElement> result = new ArrayList<JsonElement>();
for(JsonElement el : element.getList()){
List<JsonElement> out = apply(el);
if(out != null)
result.addAll(out);
}
return result;
return new FilterOutput(result);
}
public abstract List<JsonElement> apply(JsonElement element) 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 {
FilterOutput out = new FilterOutput(root);
log.info(out.getResultAsJson().toString());
log.info(out.getResult().toString());
for (JsonPathFilterBase filter : filters) {
if (filter == null) {
throw new InvalidPathException();
}
if(out.getResultAsJson().isJsonNull()){
break;
if(out.getList() == null){
return null;
}
out = filter.apply(out);
log.info(out.getResultAsJson().toString());
if(out.getResult()!=null)
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.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
import com.google.gson.JsonNull;
import com.jayway.jsonpath.json.JsonArray;
import com.jayway.jsonpath.json.JsonElement;
import com.jayway.jsonpath.json.JsonException;
@ -37,31 +35,19 @@ public class ListIndexFilter extends JsonPathFilterBase {
@Override
public List<JsonElement> apply(JsonElement element) throws JsonException {
List<JsonElement> result = new ArrayList<JsonElement>();
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()){
for (int i : index) {
if (indexIsInRange(element.toJsonArray(), 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) {

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)) {
JsonElement o = element.toJsonObject().getProperty(pathFragment);
if(o != null){
result.add(o);
}
else{
result.add(factory.createJsonNull(pathFragment,element));
}
}
else if(element.isJsonObject()){
result.add(factory.createJsonNull(pathFragment,element));
}

Loading…
Cancel
Save