public class JsonSlurper
extends Object
This has the same interface as the original JsonSlurper written for version 1.8.0, but its implementation has completely changed. It is now up to 20x faster than before, and its speed competes and often substantially exceeds popular common JSON parsers circa Jan, 2014.
JSON slurper parses text or reader content into a data structure of lists and maps.
Example usage:
JsonSlurper can use several types of JSON parsers. Please read the documentation for
JsonParserType. There are relaxed mode parsers, large file parser, and index overlay parsers.
Don't worry, it is all groovy. JsonSlurper will just work, but understanding the different parser
types may allow you to drastically improve the performance of your JSON parsing.
def slurper = new groovy.json.JsonSlurper()
def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')
assert result.person.name == "Guillaume"
assert result.person.age == 33
assert result.person.pets.size() == 2
assert result.person.pets[0] == "dog"
assert result.person.pets[1] == "cat"
parser = new JsonSlurper().setType(JsonParserType.INDEX_OVERLAY);
| Type Params | Return Type | Name and description |
|---|---|---|
|
public int |
getMaxNestingDepth()The maximum nesting depth of arrays/objects the parser will accept before throwing a JsonException. |
|
public int |
getMaxSizeForInMemory()The threshold (in characters) that selects which parser strategy the File-based parse
methods use: documents smaller than this are parsed in memory, larger ones use a windowing
buffer parser. |
|
public JsonParserType |
getType()Parser type. |
|
public boolean |
isCheckDates()Determine if slurper will automatically parse strings it recognizes as dates. |
|
public boolean |
isChop()Turns on buffer chopping for index overlay. |
|
public boolean |
isLazyChop()Turns on buffer lazy chopping for index overlay. |
|
public Object |
parse(Reader reader)Parse a JSON data structure from content from a reader |
|
public Object |
parse(InputStream inputStream)Parse a JSON data structure from content from an inputStream |
|
public Object |
parse(InputStream inputStream, String charset)Parse a JSON data structure from content from an inputStream |
|
public Object |
parse(byte[] bytes, String charset)Parse a JSON data structure from content from a byte array. |
|
public Object |
parse(byte[] bytes)Parse a JSON data structure from content from a byte array. |
|
public Object |
parse(char[] chars)Parse a JSON data structure from content from a char array. |
|
public Object |
parse(Path path)Parse a JSON data structure from content within a given Path. |
|
public Object |
parse(Path path, String charset)Parse a JSON data structure from content within a given Path. |
|
public Object |
parse(File file)Parse a JSON data structure from content within a given File. |
|
public Object |
parse(File file, String charset)Parse a JSON data structure from content within a given File. |
|
public Object |
parse(URL url)Parse a JSON data structure from content at a given URL. |
|
public Object |
parse(URL url, Map params)Parse a JSON data structure from content at a given URL. |
|
public Object |
parse(Map<String, ?> params, URL url)Parse a JSON data structure from content at a given URL. |
|
public Object |
parse(URL url, String charset)Parse a JSON data structure from content at a given URL. |
|
public Object |
parse(URL url, Map params, String charset)Parse a JSON data structure from content at a given URL. |
|
public Object |
parse(Map params, URL url, String charset)Parse a JSON data structure from content at a given URL. |
|
public Object |
parseText(String text)Parse a text representation of a JSON data structure |
|
public JsonSlurper |
setCheckDates(boolean checkDates)Determine if slurper will automatically parse strings it recognizes as dates. |
|
public JsonSlurper |
setChop(boolean chop)Turns on buffer chopping for index overlay. |
|
public JsonSlurper |
setLazyChop(boolean lazyChop)Turns on buffer lazy chopping for index overlay. |
|
public JsonSlurper |
setMaxNestingDepth(int maxNestingDepth)Sets the maximum nesting depth of arrays/objects the parser will accept before throwing a JsonException. |
|
public JsonSlurper |
setMaxSizeForInMemory(int maxSizeForInMemory)Sets the threshold that selects which parser strategy the File-based parse methods use
(see getMaxSizeForInMemory()). |
|
public JsonSlurper |
setType(JsonParserType type)Parser type. |
The maximum nesting depth of arrays/objects the parser will accept before throwing a
JsonException. This guards the recursive-descent parsers against a small but
deeply-nested document driving a StackOverflowError. A value of 0 or less
disables the check (restoring the previous, unbounded behaviour). Defaults to
BaseJsonParser.DEFAULT_MAX_NESTING_DEPTH, and can
be overridden globally with the groovy.json.maxNestingDepth system property.
The threshold (in characters) that selects which parser strategy the File-based parse
methods use: documents smaller than this are parsed in memory, larger ones use a windowing
buffer parser.
This is a parser-strategy selector, not a size limit. It does not reject or
cap input — an oversized document is still parsed in full (by the windowing parser).
It is consulted only by the File-based parse methods; the
parse(Reader), parse(InputStream) and parse(URL) entry points ignore
it and buffer their entire input into memory regardless of this value. To bound untrusted
input, limit its size yourself before parsing (and cap nesting via
setMaxNestingDepth(int) or the groovy.json.maxNestingDepth system property).
Parser type.
Determine if slurper will automatically parse strings it recognizes as dates. Index overlay only.
Turns on buffer chopping for index overlay.
Turns on buffer lazy chopping for index overlay.
Parse a JSON data structure from content from a reader
reader - reader over a JSON contentParse a JSON data structure from content from an inputStream
inputStream - stream over a JSON contentParse a JSON data structure from content from an inputStream
inputStream - stream over a JSON contentcharset - charsetParse a JSON data structure from content from a byte array.
bytes - buffer of JSON contentcharset - charsetParse a JSON data structure from content from a byte array.
bytes - buffer of JSON contentParse a JSON data structure from content from a char array.
chars - buffer of JSON contentParse a JSON data structure from content within a given Path.
path - Path containing JSON contentParse a JSON data structure from content within a given Path.
path - Path containing JSON contentcharset - the charset for this FileParse a JSON data structure from content within a given File.
file - File containing JSON contentParse a JSON data structure from content within a given File.
file - File containing JSON contentcharset - the charset for this FileParse a JSON data structure from content at a given URL.
url - URL containing JSON contentParse a JSON data structure from content at a given URL.
url - URL containing JSON contentparams - connection parametersParse a JSON data structure from content at a given URL. Convenience variant when using Groovy named parameters for the connection params.
params - connection parametersurl - URL containing JSON contentParse a JSON data structure from content at a given URL.
url - URL containing JSON contentcharset - the charset for this FileParse a JSON data structure from content at a given URL.
url - URL containing JSON contentparams - connection parameterscharset - the charset for this FileParse a JSON data structure from content at a given URL. Convenience variant when using Groovy named parameters for the connection params.
params - connection parametersurl - URL containing JSON contentcharset - the charset for this FileParse a text representation of a JSON data structure
text - JSON text to parseDetermine if slurper will automatically parse strings it recognizes as dates. Index overlay only.
Turns on buffer chopping for index overlay.
Turns on buffer lazy chopping for index overlay.
Sets the maximum nesting depth of arrays/objects the parser will accept before throwing a
JsonException. A value of 0 or less disables the check.
maxNestingDepth - maximum number of nested arrays/objects to allowJsonSlurper Sets the threshold that selects which parser strategy the File-based parse methods use
(see getMaxSizeForInMemory()).
This selects a parser strategy; it does not limit or reject input. Setting it
gives no DoS protection: it is not consulted by parse(Reader),
parse(InputStream) or parse(URL), and even the File-based methods still
parse documents larger than the threshold. Bound untrusted input by its size before parsing,
and cap nesting via setMaxNestingDepth(int) or groovy.json.maxNestingDepth.
maxSizeForInMemory - the in-memory vs windowing-parser selection thresholdJsonSlurper instanceParser type.