public class JsonSlurper extends Object
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);
JsonParserType
Constructor and Description |
---|
JsonSlurper() |
Modifier and Type | Method and Description |
---|---|
int |
getMaxSizeForInMemory()
Max size before Slurper starts to use windowing buffer parser.
|
JsonParserType |
getType()
Parser type.
|
boolean |
isCheckDates()
Determine if slurper will automatically parse strings it recognizes as dates.
|
boolean |
isChop()
Turns on buffer chopping for index overlay.
|
boolean |
isLazyChop()
Turns on buffer lazy chopping for index overlay.
|
Object |
parse(byte[] bytes)
Parse a JSON data structure from content from a byte array.
|
Object |
parse(byte[] bytes,
String charset)
Parse a JSON data structure from content from a byte array.
|
Object |
parse(char[] chars)
Parse a JSON data structure from content from a char array.
|
Object |
parse(File file)
Parse a JSON data structure from content within a given File.
|
Object |
parse(File file,
String charset)
Parse a JSON data structure from content within a given File.
|
Object |
parse(InputStream inputStream)
Parse a JSON data structure from content from an inputStream
|
Object |
parse(InputStream inputStream,
String charset)
Parse a JSON data structure from content from an inputStream
|
Object |
parse(Map params,
URL url)
Parse a JSON data structure from content at a given URL.
|
Object |
parse(Map params,
URL url,
String charset)
Parse a JSON data structure from content at a given URL.
|
Object |
parse(Reader reader)
Parse a JSON data structure from content from a reader
|
Object |
parse(URL url)
Parse a JSON data structure from content at a given URL.
|
Object |
parse(URL url,
Map params)
Parse a JSON data structure from content at a given URL.
|
Object |
parse(URL url,
Map params,
String charset)
Parse a JSON data structure from content at a given URL.
|
Object |
parse(URL url,
String charset)
Parse a JSON data structure from content at a given URL.
|
Object |
parseText(String text)
Parse a text representation of a JSON data structure
|
JsonSlurper |
setCheckDates(boolean checkDates)
Determine if slurper will automatically parse strings it recognizes as dates.
|
JsonSlurper |
setChop(boolean chop)
Turns on buffer chopping for index overlay.
|
JsonSlurper |
setLazyChop(boolean lazyChop)
Turns on buffer lazy chopping for index overlay.
|
JsonSlurper |
setMaxSizeForInMemory(int maxSizeForInMemory)
Max size before Slurper starts to use windowing buffer parser.
|
JsonSlurper |
setType(JsonParserType type)
Parser type.
|
public int getMaxSizeForInMemory()
public JsonSlurper setMaxSizeForInMemory(int maxSizeForInMemory)
public JsonParserType getType()
JsonParserType
public JsonSlurper setType(JsonParserType type)
JsonParserType
public boolean isChop()
JsonParserType
public JsonSlurper setChop(boolean chop)
JsonParserType
public boolean isLazyChop()
JsonParserType
public JsonSlurper setLazyChop(boolean lazyChop)
JsonParserType
public boolean isCheckDates()
public JsonSlurper setCheckDates(boolean checkDates)
public Object parseText(String text)
text
- JSON text to parsepublic Object parse(Reader reader)
reader
- reader over a JSON contentpublic Object parse(InputStream inputStream)
inputStream
- stream over a JSON contentpublic Object parse(InputStream inputStream, String charset)
inputStream
- stream over a JSON contentcharset
- charsetpublic Object parse(byte[] bytes, String charset)
bytes
- buffer of JSON contentcharset
- charsetpublic Object parse(byte[] bytes)
bytes
- buffer of JSON contentpublic Object parse(char[] chars)
chars
- buffer of JSON contentpublic Object parse(File file)
file
- File containing JSON contentpublic Object parse(File file, String charset)
file
- File containing JSON contentcharset
- the charset for this Filepublic Object parse(URL url)
url
- URL containing JSON contentpublic Object parse(URL url, Map params)
url
- URL containing JSON contentparams
- connection parameterspublic Object parse(Map params, URL url)
params
- connection parametersurl
- URL containing JSON contentpublic Object parse(URL url, String charset)
url
- URL containing JSON contentcharset
- the charset for this Filepublic Object parse(URL url, Map params, String charset)
url
- URL containing JSON contentparams
- connection parameterscharset
- the charset for this Filepublic Object parse(Map params, URL url, String charset)
params
- connection parametersurl
- URL containing JSON contentcharset
- the charset for this File