Package groovy.json
Class JsonSlurper
java.lang.Object
groovy.json.JsonSlurper
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);
- Since:
- 1.8.0
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Max size before Slurper starts to use windowing buffer parser.getType()
Parser type.boolean
Determine if slurper will automatically parse strings it recognizes as dates.boolean
isChop()
Turns on buffer chopping for index overlay.boolean
Turns on buffer lazy chopping for index overlay.parse
(byte[] bytes) Parse a JSON data structure from content from a byte array.Parse a JSON data structure from content from a byte array.parse
(char[] chars) Parse a JSON data structure from content from a char array.Parse a JSON data structure from content within a given File.Parse a JSON data structure from content within a given File.parse
(InputStream inputStream) Parse a JSON data structure from content from an inputStreamparse
(InputStream inputStream, String charset) Parse a JSON data structure from content from an inputStreamParse a JSON data structure from content from a readerParse a JSON data structure from content at a given URL.Parse a JSON data structure from content at a given URL.Parse a JSON data structure from content at a given URL.Parse a JSON data structure from content at a given URL.Parse a JSON data structure from content within a given Path.Parse a JSON data structure from content within a given Path.Parse a JSON data structure from content at a given URL.Parse a JSON data structure from content at a given URL.Parse a text representation of a JSON data structuresetCheckDates
(boolean checkDates) Determine if slurper will automatically parse strings it recognizes as dates.setChop
(boolean chop) Turns on buffer chopping for index overlay.setLazyChop
(boolean lazyChop) Turns on buffer lazy chopping for index overlay.setMaxSizeForInMemory
(int maxSizeForInMemory) Max size before Slurper starts to use windowing buffer parser.setType
(JsonParserType type) Parser type.
-
Constructor Details
-
JsonSlurper
public JsonSlurper()
-
-
Method Details
-
getMaxSizeForInMemory
public int getMaxSizeForInMemory()Max size before Slurper starts to use windowing buffer parser.- Returns:
- size of file/buffer
- Since:
- 2.3
-
setMaxSizeForInMemory
Max size before Slurper starts to use windowing buffer parser.- Returns:
- JsonSlurper
- Since:
- 2.3
-
getType
Parser type.- Returns:
- type
- Since:
- 2.3
- See Also:
-
setType
Parser type.- Returns:
- JsonSlurper
- Since:
- 2.3
- See Also:
-
isChop
public boolean isChop()Turns on buffer chopping for index overlay.- Returns:
- chop on or off
- Since:
- 2.3
- See Also:
-
setChop
Turns on buffer chopping for index overlay.- Returns:
- JsonSlurper
- Since:
- 2.3
- See Also:
-
isLazyChop
public boolean isLazyChop()Turns on buffer lazy chopping for index overlay.- Returns:
- on or off
- Since:
- 2.3
- See Also:
-
setLazyChop
Turns on buffer lazy chopping for index overlay.- Returns:
- JsonSlurper
- Since:
- 2.3
- See Also:
-
isCheckDates
public boolean isCheckDates()Determine if slurper will automatically parse strings it recognizes as dates. Index overlay only.- Returns:
- on or off
- Since:
- 2.3
-
setCheckDates
Determine if slurper will automatically parse strings it recognizes as dates. Index overlay only.- Returns:
- on or off
- Since:
- 2.3
-
parseText
Parse a text representation of a JSON data structure- Parameters:
text
- JSON text to parse- Returns:
- a data structure of lists and maps
-
parse
Parse a JSON data structure from content from a reader- Parameters:
reader
- reader over a JSON content- Returns:
- a data structure of lists and maps
-
parse
Parse a JSON data structure from content from an inputStream- Parameters:
inputStream
- stream over a JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content from an inputStream- Parameters:
inputStream
- stream over a JSON contentcharset
- charset- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content from a byte array.- Parameters:
bytes
- buffer of JSON contentcharset
- charset- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content from a byte array.- Parameters:
bytes
- buffer of JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content from a char array.- Parameters:
chars
- buffer of JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.3
-
parse
Parse a JSON data structure from content within a given Path.- Parameters:
path
-Path
containing JSON content- Returns:
- a data structure of lists and maps
- Throws:
IOException
-
parse
Parse a JSON data structure from content within a given Path.- Parameters:
path
-Path
containing JSON contentcharset
- the charset for this File- Returns:
- a data structure of lists and maps
- Throws:
IOException
-
parse
Parse a JSON data structure from content within a given File.- Parameters:
file
- File containing JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content within a given File.- Parameters:
file
- File containing JSON contentcharset
- the charset for this File- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL.- Parameters:
url
- URL containing JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL.- Parameters:
url
- URL containing JSON contentparams
- connection parameters- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL. Convenience variant when using Groovy named parameters for the connection params.- Parameters:
params
- connection parametersurl
- URL containing JSON content- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL.- Parameters:
url
- URL containing JSON contentcharset
- the charset for this File- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL.- Parameters:
url
- URL containing JSON contentparams
- connection parameterscharset
- the charset for this File- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-
parse
Parse a JSON data structure from content at a given URL. Convenience variant when using Groovy named parameters for the connection params.- Parameters:
params
- connection parametersurl
- URL containing JSON contentcharset
- the charset for this File- Returns:
- a data structure of lists and maps
- Since:
- 2.2.0
-