Browse Source

Merge pull request #62 from jochenberger/wrapped-value-support-for-criteria-evaluation

add an unwrap(Object) method to JsonProvider, use it when extracting values for Criteria evaluation
pull/63/head
kallestenflo 10 years ago
parent
commit
3457ccbc92
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/Criteria.java
  2. 12
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/AbstractJsonProvider.java
  3. 14
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/GsonJsonProvider.java
  4. 14
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonNodeJsonProvider.java
  5. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/token/EvaluationContextImpl.java
  6. 10
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

2
json-path/src/main/java/com/jayway/jsonpath/Criteria.java

@ -352,7 +352,7 @@ public class Criteria implements Predicate {
res = leftPath.evaluate(doc, ctx.root(), ctx.configuration()).getValue();
}
}
return res;
return res == null ? null : ctx.configuration().jsonProvider().unwrap(res);
}
private boolean eval(PredicateContext ctx) {

12
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/AbstractJsonProvider.java

@ -16,6 +16,7 @@ package com.jayway.jsonpath.internal.spi.json;
import com.jayway.jsonpath.JsonPathException;
import com.jayway.jsonpath.spi.json.JsonProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -46,11 +47,11 @@ public abstract class AbstractJsonProvider implements JsonProvider {
* @return the entry at the given index
*/
public Object getArrayIndex(Object obj, int idx) {
return getArrayIndex(obj, idx, true);
return ((List) obj).get(idx);
}
public Object getArrayIndex(Object obj, int idx, boolean unwrap){
return ((List) obj).get(idx);
public final Object getArrayIndex(Object obj, int idx, boolean unwrap){
return getArrayIndex(obj, idx);
}
public void setArrayIndex(Object array, int index, Object newValue) {
@ -177,4 +178,9 @@ public abstract class AbstractJsonProvider implements JsonProvider {
return ((Map) obj).values();
}
@Override
public Object unwrap(Object obj) {
return obj;
}
}

14
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/GsonJsonProvider.java

@ -44,7 +44,7 @@ public class GsonJsonProvider extends AbstractJsonProvider {
private static final Gson gson = new GsonBuilder().create();
public static Object unwrap(Object o) {
public Object unwrap(Object o) {
if (o == null) {
return null;
@ -129,17 +129,7 @@ public class GsonJsonProvider extends AbstractJsonProvider {
@Override
public Object getArrayIndex(Object obj, int idx) {
return getArrayIndex(obj, idx, true);
}
@Override
public Object getArrayIndex(Object obj, int idx, boolean unwrap){
if(unwrap){
return unwrap(toJsonArray(obj).get(idx));
}
else {
return toJsonArray(obj).get(idx);
}
return toJsonArray(obj).get(idx);
}
@Override

14
json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonNodeJsonProvider.java

@ -84,7 +84,7 @@ public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider {
return JsonNodeFactory.instance.objectNode();
}
public static Object unwrap(Object o) {
public Object unwrap(Object o) {
if (o == null) {
return null;
@ -128,17 +128,7 @@ public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider {
@Override
public Object getArrayIndex(Object obj, int idx) {
return getArrayIndex(obj, idx, true);
}
@Override
public Object getArrayIndex(Object obj, int idx, boolean unwrap){
if(unwrap){
return unwrap(toJsonArray(obj).get(idx));
}
else {
return toJsonArray(obj).get(idx);
}
return toJsonArray(obj).get(idx);
}
@Override

7
json-path/src/main/java/com/jayway/jsonpath/internal/token/EvaluationContextImpl.java

@ -23,6 +23,7 @@ import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.spi.json.JsonProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -134,7 +135,11 @@ public class EvaluationContextImpl implements EvaluationContext {
if(resultIndex == 0){
throw new PathNotFoundException("No results for path: " + path.toString());
}
return (T) jsonProvider().getArrayIndex(valueResult, 0, unwrap);
Object value = jsonProvider().getArrayIndex(valueResult, 0);
if (value != null && unwrap){
value = jsonProvider().unwrap(value);
}
return (T) value;
}
return (T)valueResult;
}

10
json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

@ -109,6 +109,7 @@ public interface JsonProvider {
* @param unwrap should provider specific data type be unwrapped
* @return the entry at the given index
*/
@Deprecated
Object getArrayIndex(Object obj, int idx, boolean unwrap);
/**
@ -154,5 +155,14 @@ public interface JsonProvider {
*/
boolean isMap(Object obj);
/**
* Extracts a value from a wrapper object. For JSON providers that to not wrap
* values, this will usually be the object itself.
*
* @param obj a value holder object
* @return the unwrapped value.
*/
Object unwrap(Object obj);
}

Loading…
Cancel
Save