Browse Source

Overloaded read operations with 'charset'

pull/56/head
Kalle Stenflo 10 years ago
parent
commit
a8a50dc05a
  1. 28
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
  3. 8
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonReader.java
  4. 10
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/GsonJsonProvider.java
  5. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JacksonJsonProvider.java
  6. 7
      json-path/src/main/java/com/jayway/jsonpath/internal/spi/json/JsonSmartJsonProvider.java
  7. 2
      json-path/src/main/java/com/jayway/jsonpath/spi/json/JsonProvider.java

28
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 <T> expected return type
* @return list of objects matched by the given path
* @throws IOException
*/
@SuppressWarnings({"unchecked"})
public <T> 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);
}

2
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;

8
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;
}

10
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

5
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);
}

7
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);
}
}

2
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);

Loading…
Cancel
Save