JSON 2.0 (and Jxon) - JSON lib for AutoHotkey

Post your working scripts, libraries and tools
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

JSON 2.0 (and Jxon) - JSON lib for AutoHotkey

13 Nov 2013, 14:51

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] [Download] GeSHi © Codebox Plus

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] [Download] GeSHi © Codebox Plus

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)
Last edited by Coco on 07 Nov 2015, 06:44, edited 7 times in total.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

Re: JSON [module]

14 Apr 2014, 11:18

Update: Added version for AHK v.2 (tested on 2.0-a046-692ef59). Link in OP.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

15 Jun 2014, 16:00

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.
User avatar
joedf
Posts: 5675
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

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

18 Jun 2014, 00:05

Cool! I was interested for "beautify" in ahk also, so I wrote this :) https://github.com/joedf/JSON_BnU
User avatar
hoppfrosch
Posts: 271
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

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

18 Jun 2014, 01:49

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
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

18 Jun 2014, 10:44

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] [Expand] [Download] (Script.ahk)GeSHi © Codebox Plus

User avatar
joedf
Posts: 5675
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

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

18 Jun 2014, 12:13

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 :)
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

18 Jun 2014, 13:35

Nope it's not an official specification. Here's the statement from the creator himself: https://plus.google.com/+DouglasCrockfordEsq/posts/RK8qyGVaGSr
Also: RFC 4627 The application/json Media Type for JavaScript Object Notation (JSON)
User avatar
joedf
Posts: 5675
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

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

18 Jun 2014, 16:13

Hmm thanks ! :)
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

05 Aug 2014, 20:40

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
User avatar
joedf
Posts: 5675
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

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

05 Aug 2014, 21:48

@Coco You forgot one good reason... Coding is fun! :D :thumbup:
vasili111
Posts: 726
Joined: 21 Jan 2014, 02:04
Location: Georgia

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

05 Aug 2014, 22:06

It will be nice if someone that is familiar with JSON will test these scripts and them to Awesome AutoHotkey list :)
DRAKON-AutoHotkey: Visual programming for AutoHotkey.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

06 Aug 2014, 09:04

joedf wrote:@Coco You forgot one good reason... Coding is fun! :D :thumbup:

Definitely true :D
dens20
Posts: 4
Joined: 26 Jul 2014, 17:15

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

27 Aug 2014, 09:31

@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)
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

28 Aug 2014, 06:48

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.
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

01 Sep 2014, 08:46

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?
User avatar
joedf
Posts: 5675
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

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

01 Sep 2014, 09:19

Just add in the readme/OP : "License: WTFPL" with a link! ;)
Coco
Posts: 771
Joined: 29 Sep 2013, 20:37
GitHub: cocobelgica

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

02 Sep 2014, 08:43

joedf wrote:Just add in the readme/OP : "License: WTFPL" with a link! ;)

Thanks joedf, I'll try to update everything today ...

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 21 guests