JSON.ahk (Class)
Methods:
value := JSON.Load( text [ , reviver ] )
- value - an AutoHotkey value(object/array, string, number)
- text - JSON formatted string
- reviver - function object(Func, BoundFunc, or custom function object), prescribes how the value originally produced by parsing is transformed, before being returned. Similar to JavaScript's JSON.parse() reviver parameter
Code: Select all
reviver(this, key, value)
{
; return value as is if you don't want to alter it
return [value][1] ; for numeric values, preserve internally cached number
}
- str - JSON formatted string
- value - any value(object/array, string, number)
- replacer - function object(Func, BoundFunc, or custom function object), alters the behavior of the stringification process. Similar to JavaScript's JSON.stringify() replacer parameter
- space - if space is a non-negative integer or string, then array elements and object members will be pretty-printed with that indent level. Blank("") (the default) or 0 selects the most compact representation. Using a positive integer space indents that many spaces per level; this number is capped at 10 if it's larger than that. If space is a string (such as `t), the string(or the first 10 characters of the string, if it's longer than that) is used to indent each level.
Code: Select all
replacer(this, key, value)
{
; return value as is if you don't want to alter it
return [value][1] ; for numeric values, preserve internally cached number
}
Edit: Updated (11/07/2015)
Jxon.ahk (Lib function)
Functions:
obj := Jxon_Load( ByRef src [ , object_base , array_base ] )
- obj and src - the same as above (JSON.parse)
- object_base - base object for objects({}) created during parsing
- array_base - base object for arrays([]) created during parsing
- str, obj and indent - the same as above (JSON.stringify)
Remarks:
This was previously Json2.ahk but I opted for a name change since previously the user would just need to call Json2(arg .. ) and it will parse or stringify based on whether arg is on object or a string. I don't really prefer to separate the functions as Json2_Load and Json2_Dump.
Exception Handling
An exception is thrown when invalid data/token is encountered during parsing (same goes when dumping). Error messages are descriptive and will also report the (one-based) row number, column number and character position of the culprit(char/token). This is helpful especially for large or non-prettified JSON document(s).
Edit: Updated OP to reflect changes (02/17/2015)