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 4dcc3490..3b2567da 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -259,7 +259,7 @@ public class JsonPath { InputStream in = null; try { in = HttpProviderFactory.getProvider().get(jsonURL); - return read(configuration.jsonProvider().parse(in), configuration); + return read(in, "UTF-8", configuration); } finally { Utils.closeQuietly(in); } @@ -297,7 +297,7 @@ public class JsonPath { FileInputStream fis = null; try { fis = new FileInputStream(jsonFile); - return read(configuration.jsonProvider().parse(fis), configuration); + return read(fis, configuration); } finally { Utils.closeQuietly(fis); } @@ -331,7 +331,29 @@ public class JsonPath { notNull(configuration, "configuration can not be null"); try { - return read(configuration.jsonProvider().parse(jsonInputStream), configuration); + return read(jsonInputStream, "UTF-8", configuration); + } finally { + Utils.closeQuietly(jsonInputStream); + } + } + + /** + * Applies this JsonPath to the provided json input stream + * + * @param jsonInputStream input stream to read from + * @param configuration configuration to use + * @param expected return type + * @return list of objects matched by the given path + * @throws IOException + */ + @SuppressWarnings({"unchecked"}) + public T read(InputStream jsonInputStream, String charset, Configuration configuration) throws IOException { + notNull(jsonInputStream, "json input stream can not be null"); + notNull(charset, "charset can not be null"); + notNull(configuration, "configuration can not be null"); + + try { + return read(configuration.jsonProvider().parse(jsonInputStream, charset), configuration); } finally { Utils.closeQuietly(jsonInputStream); } 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 1e73648e..8d7df718 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java +++ b/json-path/src/main/java/com/jayway/jsonpath/ParseContext.java @@ -27,6 +27,8 @@ public interface ParseContext { ReadContext parse(InputStream json); + ReadContext parse(InputStream json, String charset); + ReadContext parse(File json) throws IOException; ReadContext parse(URL json) throws IOException; diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java index 344f0e1a..79834ee6 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java @@ -51,8 +51,14 @@ public class JsonReader implements ParseContext, ReadContext { @Override public ReadContext parse(InputStream json) { + return parse(json, "UTF-8"); + } + + @Override + public ReadContext parse(InputStream json, String charset) { notNull(json, "json input stream can not be null"); - this.json = configuration.jsonProvider().parse(json); + notNull(json, "charset can not be null"); + this.json = configuration.jsonProvider().parse(json, charset); return this; } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/GsonJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/GsonJsonProvider.java index 6c73b86e..7fb38d16 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/GsonJsonProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/GsonJsonProvider.java @@ -15,6 +15,7 @@ import org.slf4j.LoggerFactory; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Collection; @@ -83,8 +84,13 @@ public class GsonJsonProvider extends AbstractJsonProvider { } @Override - public Object parse(InputStream jsonStream) throws InvalidJsonException { - return parser.parse(new InputStreamReader(jsonStream)); + public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException { + + try { + return parser.parse(new InputStreamReader(jsonStream, charset)); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } } @Override diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonProvider.java index d989262d..68c11685 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonProvider.java @@ -23,6 +23,7 @@ import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.StringWriter; import java.util.LinkedList; import java.util.List; @@ -77,9 +78,9 @@ public class JacksonJsonProvider extends AbstractJsonProvider { } @Override - public Object parse(InputStream jsonStream) throws InvalidJsonException { + public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException { try { - return objectReader.readValue(jsonStream); + return objectReader.readValue(new InputStreamReader(jsonStream, charset)); } catch (IOException e) { throw new InvalidJsonException(e); } diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java index 85394137..77b9499a 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java @@ -25,6 +25,7 @@ import net.minidev.json.parser.ParseException; import java.io.InputStream; import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; import java.util.List; import java.util.Map; @@ -55,11 +56,13 @@ public class JsonSmartJsonProvider extends AbstractJsonProvider { } @Override - public Object parse(InputStream jsonStream) throws InvalidJsonException { + public Object parse(InputStream jsonStream, String charset) throws InvalidJsonException { try { - return createParser().parse(new InputStreamReader(jsonStream), orderedMapper); + return createParser().parse(new InputStreamReader(jsonStream, charset), orderedMapper); } catch (ParseException e) { throw new InvalidJsonException(e); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); } } diff --git a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java index e5ac92fc..e5c40ab8 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java +++ b/json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java @@ -25,7 +25,7 @@ public interface JsonProvider { Object parse(String json) throws InvalidJsonException; - Object parse(InputStream jsonStream) throws InvalidJsonException; + Object parse(InputStream jsonStream, String charset) throws InvalidJsonException; String toJson(Object obj);