Browse Source

consistently use setArrayIndex for arrays instead of setProperty

pull/117/head
Jochen Berger 9 years ago
parent
commit
a048edb1cb
  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. 24
      json-path/src/main/java/com/jayway/jsonpath/spi/json/AbstractJsonProvider.java
  5. 7
      json-path/src/main/java/com/jayway/jsonpath/spi/json/GsonJsonProvider.java
  6. 8
      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);
if(optAlwaysReturnList && path.isDefinite()){
Object array = configuration.jsonProvider().createArray();
configuration.jsonProvider().setProperty(array, 0, res);
configuration.jsonProvider().setArrayIndex(array, 0, res);
return (T)array;
} else {
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
public void add(Object newVal, Configuration configuration) {
if(configuration.jsonProvider().isArray(parent)){
configuration.jsonProvider().setProperty(parent, null, newVal);
configuration.jsonProvider().setArrayIndex(parent, configuration.jsonProvider().length(parent), newVal);
} else {
throw new InvalidModificationException("Invalid add operation. $ is not an array");
}
@ -185,7 +185,7 @@ public abstract class PathRef implements Comparable<PathRef> {
return;
}
if(configuration.jsonProvider().isArray(target)){
configuration.jsonProvider().setProperty(target, null, value);
configuration.jsonProvider().setArrayIndex(target, configuration.jsonProvider().length(target), value);
} else {
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);
}
configuration.jsonProvider().setProperty(valueResult, resultIndex, model);
configuration.jsonProvider().setProperty(pathResult, resultIndex, path);
configuration.jsonProvider().setArrayIndex(valueResult, resultIndex, model);
configuration.jsonProvider().setArrayIndex(pathResult, resultIndex, path);
resultIndex++;
if(!configuration().getEvaluationListeners().isEmpty()){
int idx = resultIndex - 1;

24
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)) {
throw new UnsupportedOperationException();
} 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 key a String key or a numerical index
* @param obj an object
* @param key a String key
* @param value the value to set
*/
@SuppressWarnings("unchecked")
@ -89,14 +94,7 @@ public abstract class AbstractJsonProvider implements JsonProvider {
if (isMap(obj))
((Map) obj).put(key.toString(), value);
else {
List list = (List) obj;
int index;
if (key != null) {
index = key instanceof Integer ? (Integer) key : Integer.parseInt(key.toString());
} else {
index = list.size();
}
list.add(index, value);
throw new JsonPathException("setProperty operation cannot be used with " + obj!=null?obj.getClass().getName():"null");
}
}
@ -159,7 +157,7 @@ public abstract class AbstractJsonProvider implements JsonProvider {
} else if(obj instanceof String){
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");
}
/**

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

@ -134,10 +134,11 @@ public class GsonJsonProvider extends AbstractJsonProvider {
@Override
public void setArrayIndex(Object array, int index, Object newValue) {
if (!isArray(array)) {
throw new UnsupportedOperationException();
JsonArray a = toJsonArray(array);
if (index == a.size()) {
a.add(createJsonElement(newValue));
} else {
toJsonArray(array).set (index, createJsonElement(newValue));
a.set(index, createJsonElement(newValue));
}
}

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

@ -113,7 +113,7 @@ public interface JsonProvider {
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 idx index
@ -131,10 +131,10 @@ public interface JsonProvider {
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 key a String key or a numerical index
* @param obj an object
* @param key a String key
* @param value the value to set
*/
void setProperty(Object obj, Object key, Object value);

Loading…
Cancel
Save