Page 1 of 4

JSON 2.0 (and Jxon) - JSON lib for AutoHotkey

Posted: 13 Nov 2013, 14:51
by Coco
JSON and Jxon

License: WTFPL


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
Example reviver function definition. The function will receive the following parameters: the object/array that holds the key-value pair, the key and the value

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.Dump( value [ , replacer, space ] )
  • 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.
Example replacer function definition. The function will receive the following parameters: the object/array that holds the key-value pair, the key and the value

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
}
Requirement(s): Latest version of AutoHotkey v1.1 or v2.0-a

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 := Jxon_Dump( obj [ , indent := "" ] )
  • str, obj and indent - the same as above (JSON.stringify)
Requirement(s): AutoHotkey v1.1.17.00+ OR v2.0-a(latest version)

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)

Re: JSON [module]

Posted: 14 Apr 2014, 11:18
by Coco
Update: Added version for AHK v.2 (tested on 2.0-a046-692ef59). Link in OP.

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 15 Jun 2014, 16:00
by Coco
Modification(s):
  • parse(): Improved validation of JSON source. Most (if all)
    common format errors are detected. As before, an exception
    is thrown. Code refactored.
    Added OutputNormal class property to allow users to set
    whether returned object(s)/array(s) are sublclassed as
    JSON.object/JSON.array instance(s). Default is true which
    returns normal AHK object(s).
  • stringify(): A space is no longer added after a comma or colon
    if indent is not specified. Output is truly compact.

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 18 Jun 2014, 00:05
by joedf
Cool! I was interested for "beautify" in ahk also, so I wrote this :) https://github.com/joedf/JSON_BnU

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 18 Jun 2014, 01:49
by hoppfrosch
Here is my collection of json-functionality, based on ActiveX.

It's mainly a compilation of those two sources: Credits go to the two cited sources
Spoiler

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 18 Jun 2014, 10:44
by Coco
joedf wrote:Cool! I was interested for "beautify" in ahk also, so I wrote this :) https://github.com/joedf/JSON_BnU
I badly needed the ability to "pretty print" JSON output, hence, the integration. Yup, I've seen your JSON_BnU weeks ago, good job as always. :D
hoppfrosch wrote:Here is my collection of json-functionality, based on ActiveX.
I'm previously using a similar solution but opted to write a pure AHK solution to avoid dependencies and unwanted overhead. Plus, putting this in class would allow me to extend its functionality. e.g.:

Code: Select all

#Include <JSON>

class JSON2 extends JSON
{
	parse(src) {
		/*
		some custom code here
		to do before actual parsing.
		Example: strip 'C' style
		comments, etc.
		*/
		try obj := base.parse(src) ;// parse
		catch e
			;// do something if there was an error
		/*
		additional code here to do with the returned
		object. e.g.: remove/alter some keys before
		returning the result to the user
		*/
		return obj
	}
}

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 18 Jun 2014, 12:13
by joedf
Hmm true, I haven't added the thingy for C-Style stream line comments... Hmm but is it in the Official Specification of JSON or is it more Defacto? Anyway, I'll add that in soon :)

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 18 Jun 2014, 13:35
by Coco
Nope it's not an official specification. Here's the statement from the creator himself: https://plus.google.com/+DouglasCrockfo ... K8qyGVaGSr
Also: RFC 4627 The application/json Media Type for JavaScript Object Notation (JSON)

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 18 Jun 2014, 16:13
by joedf
Hmm thanks ! :)

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 05 Aug 2014, 15:30
by geek
Other than having pretty print (irrelevant in my case), why should I use this over VxE's original Json_To/FromObj?

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 05 Aug 2014, 20:40
by Coco
GeekDude wrote:Other than having pretty print (irrelevant in my case), why should I use this over VxE's original Json_To/FromObj?
I dunno, it comes down to your requirement(s). If you just want to serialize/de-serialize JSON data and be done with it, I would go for VxE's as it is way way faster - I was actually inspired to write this because of VxE's lib. :-)
My version provides the following though(I added these because I needed these for a project):
  • Validation of JSON formatted data during the parsing process w/c might be useful to some if its crtitical to the functionality of their script.
  • Option to wrap the returned object(s)/array(s) into a pseudo-JS dictionary -> key-value pairs are enumerated in the order they are created, useful later on for structured dumping.
  • Pretty printing is useful if you expect the data to be editted manually by the user -> config/settings file(s). And also for readability.
If you don't require these, then I would go for VxE's

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 05 Aug 2014, 21:48
by joedf
@Coco You forgot one good reason... Coding is fun! :D :thumbup:

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 05 Aug 2014, 22:06
by vasili111
It will be nice if someone that is familiar with JSON will test these scripts and them to Awesome AutoHotkey list :)

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 06 Aug 2014, 09:04
by Coco
joedf wrote:@Coco You forgot one good reason... Coding is fun! :D :thumbup:
Definitely true :D

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 27 Aug 2014, 09:31
by dens20
@Coco, thx for sharing your json class, it helped me a lot.
I noticed in your example that second param for JSON.parse is using default value. Because of it, array stringify fails...just making sure that example runs flawlessly :D
To modify: JSON.parse(src,1)

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 28 Aug 2014, 06:48
by Coco
dens20 wrote:@Coco, thx for sharing your json class, it helped me a lot.
I noticed in your example that second param for JSON.parse is using default value. Because of it, array stringify fails...just making sure that example runs flawlessly :D
To modify: JSON.parse(src,1)
Thanks. Yes the examples need some updating - since I've also added Json2() which works for both v1.1 and A_AhkVersion >= v2.0-a049. I would probably need to add a README for the repo as well.

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 31 Aug 2014, 17:08
by geek
License? If you can come up with a looser license than VxE's Json_ToObj, such as public domain, I think I'd switch.

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 01 Sep 2014, 08:46
by Coco
GeekDude wrote:License? If you can come up with a looser license than VxE's Json_ToObj, such as public domain, I think I'd switch.
Feel free to do whatever you want :D ! I guess I'll just go with something like WTFPL, do i need to add a document / comment about this?

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 01 Sep 2014, 09:19
by joedf
Just add in the readme/OP : "License: WTFPL" with a link! ;)

Re: JSON [module] for AHK v.1.1+ and AHK v2

Posted: 02 Sep 2014, 08:43
by Coco
joedf wrote:Just add in the readme/OP : "License: WTFPL" with a link! ;)
Thanks joedf, I'll try to update everything today ...