Browse Source

Merge pull request #117 from jochenberger/simplify-json-provider

Be more consistent about when to use object and array methods
pull/93/merge
kallestenflo 9 years ago
parent
commit
febc2a587a
  1. 2
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java
  3. 4
      json-path/src/main/java/com/jayway/jsonpath/internal/token/EvaluationContextImpl.java
  4. 32
      json-path/src/main/java/com/jayway/jsonpath/spi/json/AbstractJsonProvider.java
  5. 26
      json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java
  6. 19
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JacksonJsonNodeJsonProvider.java
  7. 14
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

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

@ -178,7 +178,7 @@ public class JsonPath {
Object res = path.evaluate(jsonObject, jsonObject, configuration).getValue(false); Object res = path.evaluate(jsonObject, jsonObject, configuration).getValue(false);
if(optAlwaysReturnList && path.isDefinite()){ if(optAlwaysReturnList && path.isDefinite()){
Object array = configuration.jsonProvider().createArray(); Object array = configuration.jsonProvider().createArray();
configuration.jsonProvider().setProperty(array, 0, res); configuration.jsonProvider().setArrayIndex(array, 0, res);
return (T)array; return (T)array;
} else { } else {
return (T)res; return (T)res;

4
json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java

@ -89,7 +89,7 @@ public abstract class PathRef implements Comparable<PathRef> {
@Override @Override
public void add(Object newVal, Configuration configuration) { public void add(Object newVal, Configuration configuration) {
if(configuration.jsonProvider().isArray(parent)){ if(configuration.jsonProvider().isArray(parent)){
configuration.jsonProvider().setProperty(parent, null, newVal); configuration.jsonProvider().setArrayIndex(parent, configuration.jsonProvider().length(parent), newVal);
} else { } else {
throw new InvalidModificationException("Invalid add operation. $ is not an array"); throw new InvalidModificationException("Invalid add operation. $ is not an array");
} }
@ -185,7 +185,7 @@ public abstract class PathRef implements Comparable<PathRef> {
return; return;
} }
if(configuration.jsonProvider().isArray(target)){ if(configuration.jsonProvider().isArray(target)){
configuration.jsonProvider().setProperty(target, null, value); configuration.jsonProvider().setArrayIndex(target, configuration.jsonProvider().length(target), value);
} else { } else {
throw new InvalidModificationException("Can only add to an array"); throw new InvalidModificationException("Can only add to an array");
} }

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

@ -80,8 +80,8 @@ public class EvaluationContextImpl implements EvaluationContext {
updateOperations.add(operation); updateOperations.add(operation);
} }
configuration.jsonProvider().setProperty(valueResult, resultIndex, model); configuration.jsonProvider().setArrayIndex(valueResult, resultIndex, model);
configuration.jsonProvider().setProperty(pathResult, resultIndex, path); configuration.jsonProvider().setArrayIndex(pathResult, resultIndex, path);
resultIndex++; resultIndex++;
if(!configuration().getEvaluationListeners().isEmpty()){ if(!configuration().getEvaluationListeners().isEmpty()){
int idx = resultIndex - 1; int idx = resultIndex - 1;

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

@ -56,7 +56,12 @@ public abstract class AbstractJsonProvider implements JsonProvider {
if (!isArray(array)) { if (!isArray(array)) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();
} else { } else {
((List) array).set(index, newValue); List l = (List) array;
if (index == l.size()){
l.add(newValue);
}else {
l.set(index, newValue);
}
} }
} }
@ -78,10 +83,10 @@ public abstract class AbstractJsonProvider implements JsonProvider {
} }
/** /**
* Sets a value in an object or array * Sets a value in an object
* *
* @param obj an array or an object * @param obj an object
* @param key a String key or a numerical index * @param key a String key
* @param value the value to set * @param value the value to set
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -89,14 +94,7 @@ public abstract class AbstractJsonProvider implements JsonProvider {
if (isMap(obj)) if (isMap(obj))
((Map) obj).put(key.toString(), value); ((Map) obj).put(key.toString(), value);
else { else {
List list = (List) obj; throw new JsonPathException("setProperty operation cannot be used with " + obj!=null?obj.getClass().getName():"null");
int index;
if (key != null) {
index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
} else {
index = list.size();
}
list.add(index, value);
} }
} }
@ -159,21 +157,21 @@ public abstract class AbstractJsonProvider implements JsonProvider {
} else if(obj instanceof String){ } else if(obj instanceof String){
return ((String)obj).length(); return ((String)obj).length();
} }
throw new JsonPathException("length operation can not applied to " + obj!=null?obj.getClass().getName():"null"); throw new JsonPathException("length operation cannot be applied to " + obj!=null?obj.getClass().getName():"null");
} }
/** /**
* Converts given object to an {@link Iterable} * Converts given array to an {@link Iterable}
* *
* @param obj an array or an object * @param obj an array
* @return the entries for an array or the values for a map * @return an Iterable that iterates over the entries of an array
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Iterable<? extends Object> toIterable(Object obj) { public Iterable<? extends Object> toIterable(Object obj) {
if (isArray(obj)) if (isArray(obj))
return ((Iterable) obj); return ((Iterable) obj);
else else
return ((Map) obj).values(); throw new JsonPathException("Cannot iterate over " + obj!=null?obj.getClass().getName():"null");
} }
@Override @Override

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

@ -134,10 +134,11 @@ public class GsonJsonProvider extends AbstractJsonProvider {
@Override @Override
public void setArrayIndex(Object array, int index, Object newValue) { public void setArrayIndex(Object array, int index, Object newValue) {
if (!isArray(array)) { JsonArray a = toJsonArray(array);
throw new UnsupportedOperationException(); if (index == a.size()) {
a.add(createJsonElement(newValue));
} else { } else {
toJsonArray(array).set (index, createJsonElement(newValue)); a.set(index, createJsonElement(newValue));
} }
} }
@ -219,21 +220,12 @@ public class GsonJsonProvider extends AbstractJsonProvider {
@Override @Override
public Iterable<?> toIterable(Object obj) { public Iterable<?> toIterable(Object obj) {
if (isArray(obj)) { JsonArray arr = toJsonArray(obj);
JsonArray arr = toJsonArray(obj); List<Object> values = new ArrayList<Object>(arr.size());
List<Object> values = new ArrayList<Object>(arr.size()); for (Object o : arr) {
for (Object o : arr) { values.add(unwrap(o));
values.add(unwrap(o));
}
return values;
} else {
JsonObject jsonObject = toJsonObject(obj);
List<Object> values = new ArrayList<Object>();
for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
values.add(unwrap(entry.getValue()));
}
return values;
} }
return values;
} }
private JsonElement createJsonElement(Object o) { private JsonElement createJsonElement(Object o) {

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

@ -217,21 +217,12 @@ public class JacksonJsonNodeJsonProvider extends AbstractJsonProvider {
@Override @Override
public Iterable<?> toIterable(Object obj) { public Iterable<?> toIterable(Object obj) {
if (isArray(obj)) { ArrayNode arr = toJsonArray(obj);
ArrayNode arr = toJsonArray(obj); List<Object> values = new ArrayList<Object>(arr.size());
List<Object> values = new ArrayList<Object>(arr.size()); for (Object o : arr) {
for (Object o : arr) { values.add(unwrap(o));
values.add(unwrap(o));
}
return values;
} else {
List<Object> values = new ArrayList<Object>();
Iterator<JsonNode> iter = toJsonObject(obj).elements();
while (iter.hasNext()){
values.add(unwrap(iter.next()));
}
return values;
} }
return values;
} }
private JsonNode createJsonElement(Object o) { private JsonNode createJsonElement(Object o) {

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

@ -76,10 +76,10 @@ public interface JsonProvider {
int length(Object obj); int length(Object obj);
/** /**
* Converts given object to an {@link Iterable} * Converts given array to an {@link Iterable}
* *
* @param obj an array or an object * @param obj an array
* @return the entries for an array or the values for a map * @return an Iterable that iterates over the entries of an array
*/ */
Iterable<?> toIterable(Object obj); Iterable<?> toIterable(Object obj);
@ -113,7 +113,7 @@ public interface JsonProvider {
Object getArrayIndex(Object obj, int idx, boolean unwrap); Object getArrayIndex(Object obj, int idx, boolean unwrap);
/** /**
* Sets a value in an array * Sets a value in an array. If the array is too small, the provider is supposed to enlarge it.
* *
* @param array an array * @param array an array
* @param idx index * @param idx index
@ -131,10 +131,10 @@ public interface JsonProvider {
Object getMapValue(Object obj, String key); Object getMapValue(Object obj, String key);
/** /**
* Sets a value in an object or array * Sets a value in an object
* *
* @param obj an array or an object * @param obj an object
* @param key a String key or a numerical index * @param key a String key
* @param value the value to set * @param value the value to set
*/ */
void setProperty(Object obj, Object key, Object value); void setProperty(Object obj, Object key, Object value);

Loading…
Cancel
Save