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); boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
try { try {
if(path.isFunctionPath()){ if (path.isFunctionPath()) {
if(optAsPathList || optAlwaysReturnList){ if (optAsPathList || optAlwaysReturnList) {
throw new JsonPathException("Options " + AS_PATH_LIST + " and " + ALWAYS_RETURN_LIST + " are not allowed when using path functions!"); 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); return path.evaluate(jsonObject, jsonObject, configuration).getValue(true);
} else if(optAsPathList){ } else if (optAsPathList) {
return (T)path.evaluate(jsonObject, jsonObject, configuration).getPath(); return (T) path.evaluate(jsonObject, jsonObject, configuration).getPath();
} else { } else {
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().setArrayIndex(array, 0, res); configuration.jsonProvider().setArrayIndex(array, 0, res);
return (T)array; return (T) array;
} else { } else {
return (T)res; return (T) res;
} }
} }
} catch (RuntimeException e){ } catch (RuntimeException e) {
if(!optSuppressExceptions){ if (!optSuppressExceptions) {
throw e; throw e;
} else { } else {
if(optAsPathList){ if (optAsPathList) {
return (T)configuration.jsonProvider().createArray(); return (T) configuration.jsonProvider().createArray();
} else { } else {
if(optAlwaysReturnList){ if (optAlwaysReturnList) {
return (T)configuration.jsonProvider().createArray(); return (T) configuration.jsonProvider().createArray();
} else { } 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}. * Replaces the value on the given path with the result of the {@link MapFunction}.
* *
* @param jsonObject a json object * @param jsonObject a json object
* @param mapFunction Converter object to be invoked * @param mapFunction Converter object to be invoked
* @param configuration configuration to use * @param configuration configuration to use
* @return the updated jsonObject or the path list to updated objects if option AS_PATH_LIST is set. * @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) { public <T> T map(Object jsonObject, MapFunction mapFunction, Configuration configuration) {
@ -306,7 +306,7 @@ public class JsonPath {
return resultByConfiguration(jsonObject, configuration, evaluationContext); 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"); notNull(jsonObject, "json can not be null");
notEmpty(newKeyName, "newKeyName can not be null or empty"); notEmpty(newKeyName, "newKeyName can not be null or empty");
notNull(configuration, "configuration can not be null"); notNull(configuration, "configuration can not be null");
@ -513,6 +513,7 @@ public class JsonPath {
* @return list of objects matched by the given path * @return list of objects matched by the given path
*/ */
@SuppressWarnings({"unchecked"}) @SuppressWarnings({"unchecked"})
@Deprecated
public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException { public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException {
return new JsonContext().parse(jsonURL).read(jsonPath, filters); 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.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
public interface ParseContext { public interface ParseContext {
@ -29,4 +30,7 @@ public interface ParseContext {
DocumentContext parse(InputStream json, String charset); DocumentContext parse(InputStream json, String charset);
DocumentContext parse(File json) throws IOException; 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.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -113,6 +114,19 @@ public class JsonContext implements ParseContext, DocumentContext {
return this; 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 @Override
public Configuration configuration() { public Configuration configuration() {
return 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]"); .read("$..array[0]");
assertThat(result.get(0)).isEqualTo(expected); assertThat(result.get(0)).isEqualTo(expected);
} }
} }

Loading…
Cancel
Save