org.kopitubruk.util.json
Class JSONParser

java.lang.Object
  extended by org.kopitubruk.util.json.JSONParser

public class JSONParser
extends Object

This is a JSON parser. It accepts a fairly loose version of JSON. Essentially it tries to allow anything Javascript eval() allows (within reason) so it lets you use single quotes instead of double quotes if you want and all versions of Javascript numbers are allowed. Unquoted identifiers are also permitted. Escapes in strings are converted to their proper characters and all Javascript escapes are permitted. Identifiers which contain code points which are permitted by the JSON standard but not by the ECMAScript standard must be quoted.

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.

Since:
1.2
Author:
Bill Davidson

Method Summary
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

parseJSON

public static Object parseJSON(String json)
Parse a string of JSON data.

Parameters:
json - the string of JSON data.
Returns:
The object containing the parsed data.

parseJSON

public static Object parseJSON(String json,
                               JSONConfig cfg)
Parse a string of JSON data.

Parameters:
json - the string of JSON data.
cfg - The config object.
Returns:
The object containing the parsed data.

parseJSON

public static Object parseJSON(Reader json)
                        throws IOException
Parse JSON from an input stream.

Parameters:
json - The input stream.
Returns:
The object containing the parsed data.
Throws:
IOException - If there's a problem with I/O.
Since:
1.7

parseJSON

public static Object parseJSON(Reader json,
                               JSONConfig cfg)
                        throws IOException
Parse JSON from an input stream.

Parameters:
json - The input stream.
cfg - The config object.
Returns:
The object containing the parsed data.
Throws:
IOException - If there's a problem with I/O.
Since:
1.7


Copyright © 2016. All rights reserved.