public class JSONParser extends Object
Javascript objects are converted to LinkedHashMaps with the
identifiers being the keys.
Javascript arrays are converted to ArrayLists. If
JSONConfig.isUsePrimitiveArrays() returns true, then the list
will be examined and if it contains only wrappers for primitives and
those primitives are compatible with each other, then the list will be
converted to an array of primitives. This works for booleans and
all primitive numbers. It also works if the list only contains strings
that consist of a single character each, which get converted to an
array of chars. For primitive numbers, it uses smallest type that does
not lose information though if doubles or floats are used, they could
lose information from longs or ints that are in the same list. This
can cut down on memory use considerably for large arrays.
Literal null is just a null value and boolean values are converted to
Booleans.
Floating point numbers are converted to Double and integers are
converted to Long. If a floating point number loses precision when
converted to Double, then BigDecimal will be used instead
in order to retain all of the precision of the original number depicted by
the string. Likewise, if an integer number is too big to fit in a
Long, then a BigInteger will be used in order to retain the
original number depicted by the string. If
JSONConfig.isSmallNumbers() returns true then the parser will attempt
to use smaller types if they don't lose information including bytes for
small magnitude integers.
If JSONConfig.isEncodeNumericStringsAsNumbers() returns true, then
strings which look like numbers will be encoded as numbers in the result.
If the JSONConfig.isEncodeDatesAsObjects() or
JSONConfig.isEncodeDatesAsStrings() returns true, then strings that
look like dates will be converted to Date objects. By default,
parsing formats support ISO 8601 extended format that include data down to
seconds. Fractions of seconds and time zone offsets are optional. Other
formats can be added with calls to
JSONConfig.addDateParseFormat(DateFormat) or its variants and passing
the config object to the parser. Custom formats that you add will be tried
before the default ISO 8601 formats.
Calls to the new Date(String) constructor from Javascript are converted to
Dates.
JSON input can be fed to this class either as a String or as a
and object that extends Reader, which may be useful and save memory
when reading from files or other input sources. Common objects that extend
Reader include InputStreamReader, FileReader and
BufferedReader.
| Modifier and Type | Method and Description |
|---|---|
static Object |
parseJSON(Reader json)
Parse JSON from an input stream.
|
static Object |
parseJSON(Reader json,
JSONConfig cfg)
Parse JSON from an input stream.
|
static Object |
parseJSON(String json)
Parse a string of JSON data.
|
static Object |
parseJSON(String json,
JSONConfig cfg)
Parse a string of JSON data.
|
public static Object parseJSON(String json)
json - the string of JSON data.public static Object parseJSON(String json, JSONConfig cfg)
json - the string of JSON data.cfg - The config object.public static Object parseJSON(Reader json) throws IOException
json - The input stream.IOException - If there's a problem with I/O.public static Object parseJSON(Reader json, JSONConfig cfg) throws IOException
json - The input stream.cfg - The config object.IOException - If there's a problem with I/O.Copyright © 2016. All rights reserved.