The tolerant JSON parser and encoder for Haxe, maintained by BrenninhoTools.
TJSON reads everything standard JSON accepts plus:
- Single quoted strings
- Unquoted keys and values
/* block */and// linecomments- Trailing and missing commas
- Hex numbers (
0x1F), exponents (1e3), leading+,NaNandInfinity - Escapes
\b,\f,\v,\0,\xHH,\uHHHHwith surrogate pairs, and line continuations - A UTF-8 byte order mark at the start
Errors report the file name, line and column.
haxelib git tjson https://github.com/BrenninhoTools/tjson
Add -lib tjson to your hxml or <haxelib name="tjson"/> to your Lime project.
import tjson.TJSON;
var object = TJSON.parse("{key:'value', list:[1, 2, 3,], // comment\n}");
var maybe = TJSON.tryParse(text, {});
var config = TJSON.parseFile("config.json");tryParse returns the fallback instead of throwing. parseFile and saveFile are available on sys targets.
TJSON.encode({myKey: 'myValue'});
TJSON.encode(data, 'fancy');
TJSON.encode(data, 'pretty');
TJSON.stringify(data);
TJSON.stringify(data, '\t');encode keeps class instances (_hxcls), Date values and shared references, so parse can rebuild them. Classes can implement TJ_noEncode():Array<String> to skip fields and TJ_unserialize() to run after loading.
stringify writes plain standard JSON: no class names, dates as text and an error on circular references. Pass an indent to pretty print it.
Maps of any key type, List, Bytes (as Base64) and enum values are supported. NaN and infinite numbers are written as null, and strings are fully escaped, including control characters.
Custom styles implement tjson.EncodeStyle. SimpleStyle, FancyStyle and PrettyStyle are included.
haxe test.hxml --interp
BSD, see the original project by martamius.