Skip to content

Commit

Permalink
Make ParseContext from JsonPath.using thread safe #187
Browse files Browse the repository at this point in the history
  • Loading branch information
kallestenflo committed Mar 29, 2017
1 parent 9cc10db commit 257c36c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 97 deletions.
38 changes: 20 additions & 18 deletions json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Expand Up @@ -16,7 +16,7 @@


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;
Expand Down Expand Up @@ -499,7 +499,7 @@ public static <T> T read(Object json, String jsonPath, Predicate... filters) {
*/
@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);
}


Expand All @@ -515,7 +515,7 @@ public static <T> T read(String json, String jsonPath, Predicate... filters) {
@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);
}

/**
Expand All @@ -529,7 +529,7 @@ public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) thr
*/
@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);
}

/**
Expand All @@ -543,7 +543,7 @@ public static <T> T read(File jsonFile, String jsonPath, Predicate... filters) t
*/
@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);
}


Expand All @@ -555,13 +555,15 @@ public static <T> T read(InputStream jsonInputStream, String jsonPath, Predicate


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

/**
Expand All @@ -572,7 +574,7 @@ public static ParseContext using(Configuration configuration) {
*/
@Deprecated
public static ParseContext using(JsonProvider provider) {
return new JsonContext(Configuration.builder().jsonProvider(provider).build());
return new ParseContextImpl(Configuration.builder().jsonProvider(provider).build());
}

/**
Expand All @@ -583,7 +585,7 @@ public static ParseContext using(JsonProvider provider) {
* @return a read context
*/
public static DocumentContext parse(Object json) {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}

/**
Expand All @@ -594,7 +596,7 @@ public static DocumentContext parse(Object json) {
* @return a read context
*/
public static DocumentContext parse(String json) {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}

/**
Expand All @@ -605,7 +607,7 @@ public static DocumentContext parse(String json) {
* @return a read context
*/
public static DocumentContext parse(InputStream json) {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}

/**
Expand All @@ -616,7 +618,7 @@ public static DocumentContext parse(InputStream json) {
* @return a read context
*/
public static DocumentContext parse(File json) throws IOException {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}

/**
Expand All @@ -627,7 +629,7 @@ public static DocumentContext parse(File json) throws IOException {
* @return a read context
*/
public static DocumentContext parse(URL json) throws IOException {
return new JsonContext().parse(json);
return new ParseContextImpl().parse(json);
}

/**
Expand All @@ -638,7 +640,7 @@ public static DocumentContext parse(URL json) throws IOException {
* @return a read context
*/
public static DocumentContext parse(Object json, Configuration configuration) {
return new JsonContext(configuration).parse(json);
return new ParseContextImpl(configuration).parse(json);
}

/**
Expand All @@ -649,7 +651,7 @@ public static DocumentContext parse(Object json, Configuration configuration) {
* @return a read context
*/
public static DocumentContext parse(String json, Configuration configuration) {
return new JsonContext(configuration).parse(json);
return new ParseContextImpl(configuration).parse(json);
}

/**
Expand All @@ -660,7 +662,7 @@ public static DocumentContext parse(String json, Configuration configuration) {
* @return a read context
*/
public static DocumentContext parse(InputStream json, Configuration configuration) {
return new JsonContext(configuration).parse(json);
return new ParseContextImpl(configuration).parse(json);
}

/**
Expand All @@ -671,7 +673,7 @@ public static DocumentContext parse(InputStream json, Configuration configuratio
* @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);
}

/**
Expand All @@ -682,7 +684,7 @@ public static DocumentContext parse(File json, Configuration configuration) thro
* @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) {
Expand Down
3 changes: 3 additions & 0 deletions json-path/src/main/java/com/jayway/jsonpath/ParseContext.java
Expand Up @@ -19,6 +19,9 @@
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);
Expand Down
Expand Up @@ -20,7 +20,6 @@
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;
Expand All @@ -29,11 +28,6 @@
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;

Expand All @@ -42,90 +36,20 @@
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() {
Expand Down
@@ -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);
}
}

}

0 comments on commit 257c36c

Please sign in to comment.