This is a JavaCC-derived JSON parser. It is crazy fast and fault tolerant, but with no bells or whistles. The intent is for this to form the backbone for other JSON-consuming libraries like Resty.
None of the following is legitimate JSON code, yet you will find it in the wild. This library parses them 'correctly' (at least, insofar as standards-violating processing can be 'correct').
-
C-style and SH-style Comments - e.g.
/* ... */
,// ...
,# ...
-
Bareword Keys - Yup, that's invalid JSON. For reals.
-
Ruby Symbols - Because they're pervasive bugs across the internet.
-
Large Numerical Value Support - Not technically a fault, but most libraries parse integers and decimals using doubles or longs, which can be a problem if you have large values in your JSON. This library uses
BigDecimal
andBigInteger
by default for more accurate parsing. You can shift to using double and long values by setting theparser.nativeNumbers
property, but if you are thinking about setting that flag for performance reasons, you're almost certainly doing it wrong.
Here's a minimal example:
import org.fogbeam.json.parser.JSONParser;
public class Foo {
public static void main(String[] args) {
System.out.println("Parsed value: " + new JSONParser(args[0]).parse());
}
}
Also available on the JSONParser
class:
InputStream
andReader
constructors - For your consuming convenienceparseObject()
- Parses a JSON object into aLinkedHashMap
parseArray()
- Parses a JSON array into anArrayList
To the extent possible under law,
Robert Fischer
has waived all copyright and related or neighboring rights to
JSON Parser.
This work is published from:
United States.