From e10c47b7f93c2a3f90037b408f65f91a3b000b85 Mon Sep 17 00:00:00 2001
From: Kalle Stenflo <kalle.stenflo@gmail.com>
Date: Tue, 28 Mar 2017 18:23:55 +0200
Subject: [PATCH] Accept URLs as input but deprecate URL methods #218.

---
 .../java/com/jayway/jsonpath/JsonPath.java    | 37 ++++++++++---------
 .../com/jayway/jsonpath/ParseContext.java     |  4 ++
 .../jayway/jsonpath/internal/JsonContext.java | 14 +++++++
 .../com/jayway/jsonpath/DeepScanTest.java     |  1 -
 4 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
index 8ce853d5..d96cfd2b 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
+++ b/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);
     }
diff --git a/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java b/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
index edbdf03e..12f3a34f 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
+++ b/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;
 }
diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java
index 46f1e65e..f3d11934 100644
--- a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java
+++ b/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;
diff --git a/json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java b/json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java
index 17994b11..867ea9bf 100644
--- a/json-path/src/test/java/com/jayway/jsonpath/DeepScanTest.java
+++ b/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);
     }
-
 }