JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

Share the finished AutoHotkey v2 Scripts and libraries you made here. Please put the current version of AutoHotkey v2 you used in Square Brackets at the start of the topic title.
madsounds
Posts: 53
Joined: 31 May 2019, 08:14

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

25 Nov 2020, 20:57

a := Map(), b := Map(), c := Map(), d := Map(), e := Map(), f := Map() ; Object() is more technically correct than {} but both will work.
Sorry how description "Object() is more technically correct than {} but both will work." connected with the code a := Map(), b := Map(), c := Map(), d := Map(), e := Map(), f := Map()?
User avatar
TheArkive
Posts: 405
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

26 Nov 2020, 02:54

Object() is for AHK v1 (just a mistake in the comments). Map() is for AHK v2. Map() and Object() in this context are almost the same, but for different versions of AHK.
bmcclure
Posts: 11
Joined: 29 May 2018, 22:11
Google: ben.mcclure
GitHub: bmcclure

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

01 Dec 2020, 02:16

This script is really useful for me! On the latest v2 alpha, I am experiencing one minor issue: All boolean values are loaded as a string (either "true" or "false")

I've narrowed it down to the fact that AHK v2 does not return true when you call IsNumber on the strings "true" and "false". Additionally, using the expression

Code: Select all

%var% + 0
when var is the string "true" or "false" throws an exception because AHK thinks you're trying to assign a value to a protected variable.

I had to change that block of code to this:

Code: Select all

				if IsNumber(val) {
					if IsInteger(val)
						val += 0
					else if IsFloat(val)
						val += 0
					else if (val == "null")
						val := ""
					else if is_key {
						pos--, next := "#"
						continue
					}
				} else if (val == "true" || val == "false") {
					val := (val == "true")
				}
Edit: Come to think of it, the "null" check nested within IsNumber likely doesn't work either... my .json files just don't have null values AFAIK.
User avatar
TheArkive
Posts: 405
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

01 Dec 2020, 07:07

@bmcclure
Thanks for the report!

I'll make the change in the OP soon, and of course give you credit. I'm surprised i missed that one :P
bmcclure
Posts: 11
Joined: 29 May 2018, 22:11
Google: ben.mcclure
GitHub: bmcclure

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

01 Dec 2020, 12:40

Awesome, thank you!

Just to confirm, are you still working on the change or is it already done?

It looks like the

Code: Select all

val := (val == "true")
change is in the OP, however it (and the "null" string check) are still nested within the IsNumber() conditional. I don't think those two checks will work right until they're moved out to the same level as that IsNumber conditional.
User avatar
TheArkive
Posts: 405
Joined: 05 Aug 2016, 08:06
GitHub: TheArkive

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2020/10/10 - a122

01 Dec 2020, 14:33

@bmcclure
Yep you're right. Fixed.

I just removed the IsNumber() check, not needed since the other types are already checked.

Return to “AutoHotkey v2 Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 4 guests