Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ParseContext from JsonPath.using thread safe #187
- Loading branch information
1 parent
9cc10db
commit 257c36c
Showing
4 changed files
with
108 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
json-path/src/main/java/com/jayway/jsonpath/internal/ParseContextImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |