Skip to content

Commit 257c36c

Browse files
committed
Make ParseContext from JsonPath.using thread safe #187
1 parent 9cc10db commit 257c36c

File tree

4 files changed

+108
-97
lines changed

4 files changed

+108
-97
lines changed

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717

1818
import com.jayway.jsonpath.internal.EvaluationContext;
19-
import com.jayway.jsonpath.internal.JsonContext;
19+
import com.jayway.jsonpath.internal.ParseContextImpl;
2020
import com.jayway.jsonpath.internal.Path;
2121
import com.jayway.jsonpath.internal.PathRef;
2222
import com.jayway.jsonpath.internal.Utils;
@@ -499,7 +499,7 @@ public static <T> T read(Object json, String jsonPath, Predicate... filters) {
499499
*/
500500
@SuppressWarnings({"unchecked"})
501501
public static <T> T read(String json, String jsonPath, Predicate... filters) {
502-
return new JsonContext().parse(json).read(jsonPath, filters);
502+
return new ParseContextImpl().parse(json).read(jsonPath, filters);
503503
}
504504

505505

@@ -515,7 +515,7 @@ public static <T> T read(String json, String jsonPath, Predicate... filters) {
515515
@SuppressWarnings({"unchecked"})
516516
@Deprecated
517517
public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) throws IOException {
518-
return new JsonContext().parse(jsonURL).read(jsonPath, filters);
518+
return new ParseContextImpl().parse(jsonURL).read(jsonPath, filters);
519519
}
520520

521521
/**
@@ -529,7 +529,7 @@ public static <T> T read(URL jsonURL, String jsonPath, Predicate... filters) thr
529529
*/
530530
@SuppressWarnings({"unchecked"})
531531
public static <T> T read(File jsonFile, String jsonPath, Predicate... filters) throws IOException {
532-
return new JsonContext().parse(jsonFile).read(jsonPath, filters);
532+
return new ParseContextImpl().parse(jsonFile).read(jsonPath, filters);
533533
}
534534

535535
/**
@@ -543,7 +543,7 @@ public static <T> T read(File jsonFile, String jsonPath, Predicate... filters) t
543543
*/
544544
@SuppressWarnings({"unchecked"})
545545
public static <T> T read(InputStream jsonInputStream, String jsonPath, Predicate... filters) throws IOException {
546-
return new JsonContext().parse(jsonInputStream).read(jsonPath, filters);
546+
return new ParseContextImpl().parse(jsonInputStream).read(jsonPath, filters);
547547
}
548548

549549

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

556556

557557
/**
558-
* Creates a {@link ParseContext} that can be used to parse a given JSON input.
558+
* Creates a {@link ParseContext} that can be used to parse JSON input. The parse context
559+
* is as thread safe as the underlying {@link JsonProvider}. Note that not all JsonProvider are
560+
* thread safe.
559561
*
560562
* @param configuration configuration to use when parsing JSON
561563
* @return a parsing context based on given configuration
562564
*/
563565
public static ParseContext using(Configuration configuration) {
564-
return new JsonContext(configuration);
566+
return new ParseContextImpl(configuration);
565567
}
566568

567569
/**
@@ -572,7 +574,7 @@ public static ParseContext using(Configuration configuration) {
572574
*/
573575
@Deprecated
574576
public static ParseContext using(JsonProvider provider) {
575-
return new JsonContext(Configuration.builder().jsonProvider(provider).build());
577+
return new ParseContextImpl(Configuration.builder().jsonProvider(provider).build());
576578
}
577579

578580
/**
@@ -583,7 +585,7 @@ public static ParseContext using(JsonProvider provider) {
583585
* @return a read context
584586
*/
585587
public static DocumentContext parse(Object json) {
586-
return new JsonContext().parse(json);
588+
return new ParseContextImpl().parse(json);
587589
}
588590

589591
/**
@@ -594,7 +596,7 @@ public static DocumentContext parse(Object json) {
594596
* @return a read context
595597
*/
596598
public static DocumentContext parse(String json) {
597-
return new JsonContext().parse(json);
599+
return new ParseContextImpl().parse(json);
598600
}
599601

600602
/**
@@ -605,7 +607,7 @@ public static DocumentContext parse(String json) {
605607
* @return a read context
606608
*/
607609
public static DocumentContext parse(InputStream json) {
608-
return new JsonContext().parse(json);
610+
return new ParseContextImpl().parse(json);
609611
}
610612

611613
/**
@@ -616,7 +618,7 @@ public static DocumentContext parse(InputStream json) {
616618
* @return a read context
617619
*/
618620
public static DocumentContext parse(File json) throws IOException {
619-
return new JsonContext().parse(json);
621+
return new ParseContextImpl().parse(json);
620622
}
621623

622624
/**
@@ -627,7 +629,7 @@ public static DocumentContext parse(File json) throws IOException {
627629
* @return a read context
628630
*/
629631
public static DocumentContext parse(URL json) throws IOException {
630-
return new JsonContext().parse(json);
632+
return new ParseContextImpl().parse(json);
631633
}
632634

633635
/**
@@ -638,7 +640,7 @@ public static DocumentContext parse(URL json) throws IOException {
638640
* @return a read context
639641
*/
640642
public static DocumentContext parse(Object json, Configuration configuration) {
641-
return new JsonContext(configuration).parse(json);
643+
return new ParseContextImpl(configuration).parse(json);
642644
}
643645

644646
/**
@@ -649,7 +651,7 @@ public static DocumentContext parse(Object json, Configuration configuration) {
649651
* @return a read context
650652
*/
651653
public static DocumentContext parse(String json, Configuration configuration) {
652-
return new JsonContext(configuration).parse(json);
654+
return new ParseContextImpl(configuration).parse(json);
653655
}
654656

655657
/**
@@ -660,7 +662,7 @@ public static DocumentContext parse(String json, Configuration configuration) {
660662
* @return a read context
661663
*/
662664
public static DocumentContext parse(InputStream json, Configuration configuration) {
663-
return new JsonContext(configuration).parse(json);
665+
return new ParseContextImpl(configuration).parse(json);
664666
}
665667

666668
/**
@@ -671,7 +673,7 @@ public static DocumentContext parse(InputStream json, Configuration configuratio
671673
* @return a read context
672674
*/
673675
public static DocumentContext parse(File json, Configuration configuration) throws IOException {
674-
return new JsonContext(configuration).parse(json);
676+
return new ParseContextImpl(configuration).parse(json);
675677
}
676678

677679
/**
@@ -682,7 +684,7 @@ public static DocumentContext parse(File json, Configuration configuration) thro
682684
* @return a read context
683685
*/
684686
public static DocumentContext parse(URL json, Configuration configuration) throws IOException {
685-
return new JsonContext(configuration).parse(json);
687+
return new ParseContextImpl(configuration).parse(json);
686688
}
687689

688690
private <T> T resultByConfiguration(Object jsonObject, Configuration configuration, EvaluationContext evaluationContext) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import java.io.InputStream;
2020
import java.net.URL;
2121

22+
/**
23+
* Parses JSON as specified by the used {@link com.jayway.jsonpath.spi.json.JsonProvider}.
24+
*/
2225
public interface ParseContext {
2326

2427
DocumentContext parse(String json);

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

Lines changed: 3 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.jayway.jsonpath.JsonPath;
2121
import com.jayway.jsonpath.MapFunction;
2222
import com.jayway.jsonpath.Option;
23-
import com.jayway.jsonpath.ParseContext;
2423
import com.jayway.jsonpath.Predicate;
2524
import com.jayway.jsonpath.ReadContext;
2625
import com.jayway.jsonpath.TypeRef;
@@ -29,11 +28,6 @@
2928
import org.slf4j.Logger;
3029
import org.slf4j.LoggerFactory;
3130

32-
import java.io.File;
33-
import java.io.FileInputStream;
34-
import java.io.IOException;
35-
import java.io.InputStream;
36-
import java.net.URL;
3731
import java.util.LinkedList;
3832
import java.util.List;
3933

@@ -42,90 +36,20 @@
4236
import static com.jayway.jsonpath.internal.Utils.notNull;
4337
import static java.util.Arrays.asList;
4438

45-
public class JsonContext implements ParseContext, DocumentContext {
39+
public class JsonContext implements DocumentContext {
4640

4741
private static final Logger logger = LoggerFactory.getLogger(JsonContext.class);
4842

4943
private final Configuration configuration;
50-
private Object json;
44+
private final Object json;
5145

52-
public JsonContext() {
53-
this(Configuration.defaultConfiguration());
54-
}
55-
56-
public JsonContext(Configuration configuration) {
57-
notNull(configuration, "configuration can not be null");
58-
this.configuration = configuration;
59-
}
60-
61-
private JsonContext(Object json, Configuration configuration) {
46+
JsonContext(Object json, Configuration configuration) {
6247
notNull(json, "json can not be null");
6348
notNull(configuration, "configuration can not be null");
6449
this.configuration = configuration;
6550
this.json = json;
6651
}
6752

68-
//------------------------------------------------
69-
//
70-
// ParseContext impl
71-
//
72-
//------------------------------------------------
73-
@Override
74-
public DocumentContext parse(Object json) {
75-
notNull(json, "json object can not be null");
76-
this.json = json;
77-
return this;
78-
}
79-
80-
@Override
81-
public DocumentContext parse(String json) {
82-
notEmpty(json, "json string can not be null or empty");
83-
this.json = configuration.jsonProvider().parse(json);
84-
return this;
85-
}
86-
87-
@Override
88-
public DocumentContext parse(InputStream json) {
89-
return parse(json, "UTF-8");
90-
}
91-
92-
@Override
93-
public DocumentContext parse(InputStream json, String charset) {
94-
notNull(json, "json input stream can not be null");
95-
notNull(json, "charset can not be null");
96-
try {
97-
this.json = configuration.jsonProvider().parse(json, charset);
98-
return this;
99-
} finally {
100-
Utils.closeQuietly(json);
101-
}
102-
}
103-
104-
@Override
105-
public DocumentContext parse(File json) throws IOException {
106-
notNull(json, "json file can not be null");
107-
FileInputStream fis = null;
108-
try {
109-
fis = new FileInputStream(json);
110-
parse(fis);
111-
} finally {
112-
Utils.closeQuietly(fis);
113-
}
114-
return this;
115-
}
116-
117-
@Override
118-
public DocumentContext parse(URL url) throws IOException {
119-
notNull(url, "url can not be null");
120-
InputStream fis = null;
121-
try {
122-
fis = url.openStream();
123-
parse(fis);
124-
} finally {
125-
Utils.closeQuietly(fis);
126-
}
127-
return this;
128-
}
12953

13054
@Override
13155
public Configuration configuration() {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.jayway.jsonpath.internal;
2+
3+
import com.jayway.jsonpath.Configuration;
4+
import com.jayway.jsonpath.DocumentContext;
5+
import com.jayway.jsonpath.ParseContext;
6+
7+
import java.io.File;
8+
import java.io.FileInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.net.URL;
12+
13+
import static com.jayway.jsonpath.internal.Utils.notEmpty;
14+
import static com.jayway.jsonpath.internal.Utils.notNull;
15+
16+
public class ParseContextImpl implements ParseContext {
17+
18+
private final Configuration configuration;
19+
20+
public ParseContextImpl() {
21+
this(Configuration.defaultConfiguration());
22+
}
23+
24+
public ParseContextImpl(Configuration configuration) {
25+
this.configuration = configuration;
26+
}
27+
28+
@Override
29+
public DocumentContext parse(Object json) {
30+
notNull(json, "json object can not be null");
31+
return new JsonContext(json, configuration);
32+
}
33+
34+
@Override
35+
public DocumentContext parse(String json) {
36+
notEmpty(json, "json string can not be null or empty");
37+
Object obj = configuration.jsonProvider().parse(json);
38+
return new JsonContext(obj, configuration);
39+
}
40+
41+
@Override
42+
public DocumentContext parse(InputStream json) {
43+
return parse(json, "UTF-8");
44+
}
45+
46+
@Override
47+
public DocumentContext parse(InputStream json, String charset) {
48+
notNull(json, "json input stream can not be null");
49+
notNull(json, "charset can not be null");
50+
try {
51+
Object obj = configuration.jsonProvider().parse(json, charset);
52+
return new JsonContext(obj, configuration);
53+
} finally {
54+
Utils.closeQuietly(json);
55+
}
56+
}
57+
58+
@Override
59+
public DocumentContext parse(File json) throws IOException {
60+
notNull(json, "json file can not be null");
61+
FileInputStream fis = null;
62+
try {
63+
fis = new FileInputStream(json);
64+
return parse(fis);
65+
} finally {
66+
Utils.closeQuietly(fis);
67+
}
68+
}
69+
70+
@Override
71+
public DocumentContext parse(URL url) throws IOException {
72+
notNull(url, "url can not be null");
73+
InputStream fis = null;
74+
try {
75+
fis = url.openStream();
76+
return parse(fis);
77+
} finally {
78+
Utils.closeQuietly(fis);
79+
}
80+
}
81+
82+
}

0 commit comments

Comments
 (0)