Browse Source

Overloaded write ops with compiled path.

pull/60/head
Kalle Stenflo 10 years ago
parent
commit
0e997e6260
  1. 36
      json-path/src/main/java/com/jayway/jsonpath/WriteContext.java
  2. 31
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

36
json-path/src/main/java/com/jayway/jsonpath/WriteContext.java

@ -40,6 +40,15 @@ public interface WriteContext {
*/ */
DocumentContext set(String path, Object newValue, Predicate... filters); DocumentContext set(String path, Object newValue, Predicate... filters);
/**
* Set the value a the given path
*
* @param path path to set
* @param newValue new value
* @return a document context
*/
DocumentContext set(JsonPath path, Object newValue);
/** /**
* Deletes the given path * Deletes the given path
* *
@ -49,6 +58,14 @@ public interface WriteContext {
*/ */
DocumentContext delete(String path, Predicate... filters); DocumentContext delete(String path, Predicate... filters);
/**
* Deletes the given path
*
* @param path path to delete
* @return a document context
*/
DocumentContext delete(JsonPath path);
/** /**
* Add value to array at the given path * Add value to array at the given path
* *
@ -59,6 +76,15 @@ public interface WriteContext {
*/ */
DocumentContext add(String path, Object value, Predicate... filters); DocumentContext add(String path, Object value, Predicate... filters);
/**
* Add value to array at the given path
*
* @param path path to array
* @param value value to add
* @return a document context
*/
DocumentContext add(JsonPath path, Object value);
/** /**
* Add or update the key with a the given value at the given path * Add or update the key with a the given value at the given path
* *
@ -70,4 +96,14 @@ public interface WriteContext {
*/ */
DocumentContext put(String path, String key, Object value, Predicate... filters); DocumentContext put(String path, String key, Object value, Predicate... filters);
/**
* Add or update the key with a the given value at the given path
*
* @param path path to array
* @param key key to add
* @param value value of key
* @return a document context
*/
DocumentContext put(JsonPath path, String key, Object value);
} }

31
json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java

@ -29,6 +29,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import static com.jayway.jsonpath.JsonPath.compile;
import static com.jayway.jsonpath.internal.Utils.notEmpty; import static com.jayway.jsonpath.internal.Utils.notEmpty;
import static com.jayway.jsonpath.internal.Utils.notNull; import static com.jayway.jsonpath.internal.Utils.notNull;
@ -127,7 +128,7 @@ public class JsonReader implements ParseContext, DocumentContext {
@Override @Override
public <T> T read(String path, Predicate... filters) { public <T> T read(String path, Predicate... filters) {
notEmpty(path, "path can not be null or empty"); notEmpty(path, "path can not be null or empty");
return read(JsonPath.compile(path, filters)); return read(compile(path, filters));
} }
@Override @Override
@ -161,25 +162,45 @@ public class JsonReader implements ParseContext, DocumentContext {
@Override @Override
public DocumentContext set(String path, Object newValue, Predicate... filters) { public DocumentContext set(String path, Object newValue, Predicate... filters) {
Object modifiedJson = JsonPath.compile(path, filters).set(json, newValue, configuration); return set(compile(path, filters), newValue);
}
@Override
public DocumentContext set(JsonPath path, Object newValue){
Object modifiedJson = path.set(json, newValue, configuration);
return new JsonReader(modifiedJson, configuration); return new JsonReader(modifiedJson, configuration);
} }
@Override @Override
public DocumentContext delete(String path, Predicate... filters) { public DocumentContext delete(String path, Predicate... filters) {
Object modifiedJson = JsonPath.compile(path, filters).delete(json, configuration); return delete(compile(path, filters));
}
@Override
public DocumentContext delete(JsonPath path) {
Object modifiedJson = path.delete(json, configuration);
return new JsonReader(modifiedJson, configuration); return new JsonReader(modifiedJson, configuration);
} }
@Override @Override
public DocumentContext add(String path, Object value, Predicate... filters){ public DocumentContext add(String path, Object value, Predicate... filters){
Object modifiedJson = JsonPath.compile(path, filters).add(json, value, configuration); return add(compile(path, filters), value);
}
@Override
public DocumentContext add(JsonPath path, Object value){
Object modifiedJson = path.add(json, value, configuration);
return new JsonReader(modifiedJson, configuration); return new JsonReader(modifiedJson, configuration);
} }
@Override @Override
public DocumentContext put(String path, String key, Object value, Predicate... filters){ public DocumentContext put(String path, String key, Object value, Predicate... filters){
Object modifiedJson = JsonPath.compile(path, filters).put(json, key, value, configuration); return put(compile(path, filters), key, value);
}
@Override
public DocumentContext put(JsonPath path, String key, Object value){
Object modifiedJson = path.put(json, key, value, configuration);
return new JsonReader(modifiedJson, configuration); return new JsonReader(modifiedJson, configuration);
} }

Loading…
Cancel
Save