Browse Source

Make ParseContext from JsonPath.using thread safe #187

pull/346/head
Kalle Stenflo 7 years ago
parent
commit
257c36c00a
  1. 38
      json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
  2. 3
      json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
  3. 82
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java
  4. 82
      json-path/src/main/java/com/jayway/jsonpath/internal/ParseContextImpl.java

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

@ -16,7 +16,7 @@ package com.jayway.jsonpath;
import com.jayway.jsonpath.internal.EvaluationContext;
import com.jayway.jsonpath.internal.JsonContext;
import com.jayway.jsonpath.internal.ParseContextImpl;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.PathRef;
import com.jayway.jsonpath.internal.Utils;
@ -499,7 +499,7 @@ public class JsonPath {
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(String json, String jsonPath, Predicate... filters) {
return new JsonContext().parse(json).read(jsonPath, filters);
return new ParseContextImpl().parse(json).read(jsonPath, filters);
}
@ -515,7 +515,7 @@ public class JsonPath {
@SuppressWarnings({"unchecked"})
@Deprecated
public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException {
return new JsonContext().parse(jsonURL).read(jsonPath, filters);
return new ParseContextImpl().parse(jsonURL).read(jsonPath, filters);
}
/**
@ -529,7 +529,7 @@ public class JsonPath {
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(File jsonFile, String jsonPath, Predicate... filters) throws IOException {
return new JsonContext().parse(jsonFile).read(jsonPath, filters);
return new ParseContextImpl().parse(jsonFile).read(jsonPath, filters);
}
/**
@ -543,7 +543,7 @@ public class JsonPath {
*/
@SuppressWarnings({"unchecked"})
public static <T> T read(InputStream jsonInputStream, String jsonPath, Predicate... filters) throws IOException {
return new JsonContext().parse(jsonInputStream).read(jsonPath, filters);
return new ParseContextImpl().parse(jsonInputStream).read(jsonPath, filters);
}
@ -555,13 +555,15 @@ public class JsonPath {
/**
* Creates a {@link ParseContext} that can be used to parse a given JSON input.
* Creates a {@link ParseContext} that can be used to parse JSON input. The parse context
* is as thread safe as the underlying {@link JsonProvider}. Note that not all JsonProvider are
* thread safe.
*
* @param configuration configuration to use when parsing JSON
* @return a parsing context based on given configuration
*/
public static ParseContext using(Configuration configuration) {
return new JsonContext(configuration);
return new ParseContextImpl(configuration);
}
/**
@ -572,7 +574,7 @@ public class JsonPath {
*/
@Deprecated
public static ParseContext using(JsonProvider provider) {
return new JsonContext(Configuration.builder().jsonProvider(provider).build());
return new ParseContextImpl(Configuration.builder().jsonProvider(provider).build());
}
/**
@ -583,7 +585,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(Object json) {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}
/**
@ -594,7 +596,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(String json) {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}
/**
@ -605,7 +607,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(InputStream json) {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}
/**
@ -616,7 +618,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(File json) throws IOException {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}
/**
@ -627,7 +629,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(URL json) throws IOException {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}
/**
@ -638,7 +640,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(Object json, Configuration configuration) {
return new JsonContext(configuration).parse(json);
return new ParseContextImpl(configuration).parse(json);
}
/**
@ -649,7 +651,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(String json, Configuration configuration) {
return new JsonContext(configuration).parse(json);
return new ParseContextImpl(configuration).parse(json);
}
/**
@ -660,7 +662,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(InputStream json, Configuration configuration) {
return new JsonContext(configuration).parse(json);
return new ParseContextImpl(configuration).parse(json);
}
/**
@ -671,7 +673,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(File json, Configuration configuration) throws IOException {
return new JsonContext(configuration).parse(json);
return new ParseContextImpl(configuration).parse(json);
}
/**
@ -682,7 +684,7 @@ public class JsonPath {
* @return a read context
*/
public static DocumentContext parse(URL json, Configuration configuration) throws IOException {
return new JsonContext(configuration).parse(json);
return new ParseContextImpl(configuration).parse(json);
}
private <T> T resultByConfiguration(Object jsonObject, Configuration configuration, EvaluationContext evaluationContext) {

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

@ -19,6 +19,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* Parses JSON as specified by the used {@link com.jayway.jsonpath.spi.json.JsonProvider}.
*/
public interface ParseContext {
DocumentContext parse(String json);

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

@ -20,7 +20,6 @@ import com.jayway.jsonpath.EvaluationListener;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.MapFunction;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.ReadContext;
import com.jayway.jsonpath.TypeRef;
@ -29,11 +28,6 @@ import com.jayway.jsonpath.spi.cache.CacheProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
@ -42,90 +36,20 @@ import static com.jayway.jsonpath.internal.Utils.notEmpty;
import static com.jayway.jsonpath.internal.Utils.notNull;
import static java.util.Arrays.asList;
public class JsonContext implements ParseContext, DocumentContext {
public class JsonContext implements DocumentContext {
private static final Logger logger = LoggerFactory.getLogger(JsonContext.class);
private final Configuration configuration;
private Object json;
private final Object json;
public JsonContext() {
this(Configuration.defaultConfiguration());
}
public JsonContext(Configuration configuration) {
notNull(configuration, "configuration can not be null");
this.configuration = configuration;
}
private JsonContext(Object json, Configuration configuration) {
JsonContext(Object json, Configuration configuration) {
notNull(json, "json can not be null");
notNull(configuration, "configuration can not be null");
this.configuration = configuration;
this.json = json;
}
//------------------------------------------------
//
// ParseContext impl
//
//------------------------------------------------
@Override
public DocumentContext parse(Object json) {
notNull(json, "json object can not be null");
this.json = json;
return this;
}
@Override
public DocumentContext parse(String json) {
notEmpty(json, "json string can not be null or empty");
this.json = configuration.jsonProvider().parse(json);
return this;
}
@Override
public DocumentContext parse(InputStream json) {
return parse(json, "UTF-8");
}
@Override
public DocumentContext parse(InputStream json, String charset) {
notNull(json, "json input stream can not be null");
notNull(json, "charset can not be null");
try {
this.json = configuration.jsonProvider().parse(json, charset);
return this;
} finally {
Utils.closeQuietly(json);
}
}
@Override
public DocumentContext parse(File json) throws IOException {
notNull(json, "json file can not be null");
FileInputStream fis = null;
try {
fis = new FileInputStream(json);
parse(fis);
} finally {
Utils.closeQuietly(fis);
}
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() {

82
json-path/src/main/java/com/jayway/jsonpath/internal/ParseContextImpl.java

@ -0,0 +1,82 @@
package com.jayway.jsonpath.internal;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.ParseContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import static com.jayway.jsonpath.internal.Utils.notEmpty;
import static com.jayway.jsonpath.internal.Utils.notNull;
public class ParseContextImpl implements ParseContext {
private final Configuration configuration;
public ParseContextImpl() {
this(Configuration.defaultConfiguration());
}
public ParseContextImpl(Configuration configuration) {
this.configuration = configuration;
}
@Override
public DocumentContext parse(Object json) {
notNull(json, "json object can not be null");
return new JsonContext(json, configuration);
}
@Override
public DocumentContext parse(String json) {
notEmpty(json, "json string can not be null or empty");
Object obj = configuration.jsonProvider().parse(json);
return new JsonContext(obj, configuration);
}
@Override
public DocumentContext parse(InputStream json) {
return parse(json, "UTF-8");
}
@Override
public DocumentContext parse(InputStream json, String charset) {
notNull(json, "json input stream can not be null");
notNull(json, "charset can not be null");
try {
Object obj = configuration.jsonProvider().parse(json, charset);
return new JsonContext(obj, configuration);
} finally {
Utils.closeQuietly(json);
}
}
@Override
public DocumentContext parse(File json) throws IOException {
notNull(json, "json file can not be null");
FileInputStream fis = null;
try {
fis = new FileInputStream(json);
return parse(fis);
} finally {
Utils.closeQuietly(fis);
}
}
@Override
public DocumentContext parse(URL url) throws IOException {
notNull(url, "url can not be null");
InputStream fis = null;
try {
fis = url.openStream();
return parse(fis);
} finally {
Utils.closeQuietly(fis);
}
}
}
Loading…
Cancel
Save