Browse Source

Accept URLs as input but deprecate URL methods #218.

pull/305/merge
Kalle Stenflo 7 years ago
parent
commit
45a4e5e9ef
  1. 37
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 4
      json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
  3. 14
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java
  4. 1
      json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java

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

@ -174,36 +174,36 @@ public class JsonPath {
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
try {
if(path.isFunctionPath()){
if(optAsPathList || optAlwaysReturnList){
if (path.isFunctionPath()) {
if (optAsPathList || optAlwaysReturnList) {
throw new JsonPathException("Options " + AS_PATH_LIST + " and " + ALWAYS_RETURN_LIST + " are not allowed when using path functions!");
}
return path.evaluate(jsonObject, jsonObject, configuration).getValue(true);
} else if(optAsPathList){
return (T)path.evaluate(jsonObject, jsonObject, configuration).getPath();
} else if (optAsPathList) {
return (T) path.evaluate(jsonObject, jsonObject, configuration).getPath();
} else {
Object res = path.evaluate(jsonObject, jsonObject, configuration).getValue(false);
if(optAlwaysReturnList && path.isDefinite()){
if (optAlwaysReturnList && path.isDefinite()) {
Object array = configuration.jsonProvider().createArray();
configuration.jsonProvider().setArrayIndex(array, 0, res);
return (T)array;
return (T) array;
} else {
return (T)res;
return (T) res;
}
}
} catch (RuntimeException e){
if(!optSuppressExceptions){
} catch (RuntimeException e) {
if (!optSuppressExceptions) {
throw e;
} else {
if(optAsPathList){
return (T)configuration.jsonProvider().createArray();
if (optAsPathList) {
return (T) configuration.jsonProvider().createArray();
} else {
if(optAlwaysReturnList){
return (T)configuration.jsonProvider().createArray();
if (optAlwaysReturnList) {
return (T) configuration.jsonProvider().createArray();
} else {
return (T)(path.isDefinite() ? null : configuration.jsonProvider().createArray());
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
}
}
@ -232,9 +232,9 @@ public class JsonPath {
/**
* Replaces the value on the given path with the result of the {@link MapFunction}.
*
* @param jsonObject a json object
* @param mapFunction Converter object to be invoked
* @param configuration configuration to use
* @param jsonObject a json object
* @param mapFunction Converter object to be invoked
* @param configuration configuration to use
* @return the updated jsonObject or the path list to updated objects if option AS_PATH_LIST is set.
*/
public <T> T map(Object jsonObject, MapFunction mapFunction, Configuration configuration) {
@ -306,7 +306,7 @@ public class JsonPath {
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}
public <T> T renameKey(Object jsonObject, String oldKeyName, String newKeyName, Configuration configuration){
public <T> T renameKey(Object jsonObject, String oldKeyName, String newKeyName, Configuration configuration) {
notNull(jsonObject, "json can not be null");
notEmpty(newKeyName, "newKeyName can not be null or empty");
notNull(configuration, "configuration can not be null");
@ -513,6 +513,7 @@ public class JsonPath {
* @return list of objects matched by the given path
*/
@SuppressWarnings({"unchecked"})
@Deprecated
public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException {
return new JsonContext().parse(jsonURL).read(jsonPath, filters);
}

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

@ -17,6 +17,7 @@ package com.jayway.jsonpath;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public interface ParseContext {
@ -29,4 +30,7 @@ public interface ParseContext {
DocumentContext parse(InputStream json, String charset);
DocumentContext parse(File json) throws IOException;
@Deprecated
DocumentContext parse(URL json) throws IOException;
}

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

@ -33,6 +33,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
@ -113,6 +114,19 @@ public class JsonContext implements ParseContext, DocumentContext {
return this;
}
@Override
public DocumentContext parse(URL url) throws IOException {
notNull(url, "url can not be null");
InputStream fis = null;
try {
fis = url.openStream();
parse(fis);
} finally {
Utils.closeQuietly(fis);
}
return this;
}
@Override
public Configuration configuration() {
return configuration;

1
json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java

@ -333,5 +333,4 @@ public class DeepScanTest extends BaseTest {
.read("$..array[0]");
assertThat(result.get(0)).isEqualTo(expected);
}
}

Loading…
Cancel
Save