JSON Serializer - 2022/09/07 - beta.8

Post your working scripts, libraries and tools.
madsounds
Posts: 59
Joined: 31 May 2019, 08:14

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

Post by madsounds » 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: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

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

Post by TheArkive » 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: 31
Joined: 29 May 2018, 22:11
Location: West Virginia, USA
Contact:

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

Post by bmcclure » 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: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

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

Post by TheArkive » 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: 31
Joined: 29 May 2018, 22:11
Location: West Virginia, USA
Contact:

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

Post by bmcclure » 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: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

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

Post by TheArkive » 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.

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON - load and dump arrays to/from JSON - AHK v1/2 - 2021/03/08 - a128

Post by TheArkive » 08 Mar 2021, 07:14

2021/03/08
  • Changed ByRef src to just src for Jxon_load()
    Given the new VarRef, and how it is handled, it throws an error unless you explicitly pass a &VarRef. I didn't really see how passing a VarRef in this case would be beneficial so for now it is removed.
  • Removed some "code noise" that were remnants from the old code that no longer made sense or did anything, mostly commented stuff

User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: JSON Serializer - AHK v1/2 - 2021/04/19 - a131

Post by dd900 » 03 May 2021, 06:51

referring to the v1 script:
newlines, tabs, and carriage return all get an extra "\" prefixed to them. ie. \\n

Is this intentional behavior? It is caused from the ordering of your StrReplace calls in the Dump function.

Code: Select all

Else {
	obj := StrReplace(obj,"`t","\t")
	obj := StrReplace(obj,"`r","\r")
	obj := StrReplace(obj,"`n","\n")
	obj := StrReplace(obj,"`b","\b")
	obj := StrReplace(obj,"`f","\f")
	obj := StrReplace(obj,"\","\\")
	obj := StrReplace(obj,"/","\/")
	obj := StrReplace(obj,q,"\" q)
	return q obj q
}
changing the order will fix it if it is not intentional

Code: Select all

Else {
	obj := StrReplace(obj,"\","\\")
	obj := StrReplace(obj,"`t","\t")
	obj := StrReplace(obj,"`r","\r")
	obj := StrReplace(obj,"`n","\n")
	obj := StrReplace(obj,"`b","\b")
	obj := StrReplace(obj,"`f","\f")
	obj := StrReplace(obj,"/","\/")
	obj := StrReplace(obj,q,"\" q)
	return q obj q
}

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON Serializer - AHK v1/2 - 2021/04/19 - a131

Post by TheArkive » 03 May 2021, 07:03

@dd900
That is definitely not intentional. Thanks for the info.

I can't believe I didn't come across this issue before.

I'll fix it soon.

User avatar
dd900
Posts: 121
Joined: 27 Oct 2013, 16:03

Re: JSON Serializer - AHK v1/2 - 2021/04/19 - a131

Post by dd900 » 03 May 2021, 07:21

I actually just ran into another issue. If the value of a key is empty the script will throw an error when doing Jxon_Load because quotes are not being added to empty values.

ie.
coco's result
,"Genres":"",

your script result
,"Genres":, this throws an error

I can skip empty values in code, but I'm assuming this is also unintentional behavior.

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON Serializer - AHK v1/2 - 2021/04/19 - a131

Post by TheArkive » 03 May 2021, 07:30

@dd900
Yah that is also unintentional. Thanks again.

Adding that to the list.

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON Serializer - AHK v1/2 - 2021/04/19 - a131

Post by TheArkive » 03 May 2021, 10:03

Update: 2021/05/03
  • fixed incorrect additional escaping (ie. \n to \\n) ... (both AHK v1 and v2)
  • fixed zero-length strings showing up as blank instead of "" on jxon_dump() ... (only affected AHK v1)
Thanks to @dd900 for reporting these.

quaritexa
Posts: 32
Joined: 09 Nov 2017, 21:03

Re: JSON Serializer - 2021/06/21 - beta.1

Post by quaritexa » 27 Jul 2021, 04:57

I catch the error:
image.png
image.png (12.75 KiB) Viewed 3687 times
On the empty key name:
image.png
image.png (6.29 KiB) Viewed 3687 times

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON Serializer - 2021/06/21 - beta.1

Post by TheArkive » 27 Jul 2021, 05:02

Yes, it is written this way to throw an error on a blank key, or on any object that is not an [array] or a {Map}. This code was in the original script written by coco.

Code: Select all

Line #179-180

if IsObject(k) || (k == "")
    throw Error("Invalid object key.", -1, k ? Format("<Object at 0x{:p}>", ObjPtr(obj)) : "<blank>")

EDIT:
If you want to change it, remove the || (k == ""):

Code: Select all

if IsObject(k)
    throw Error("Invalid object key.", -1, k ? Format("<Object at 0x{:p}>", ObjPtr(obj)) : "<blank>")

quaritexa
Posts: 32
Joined: 09 Nov 2017, 21:03

Re: JSON Serializer - 2021/06/21 - beta.1

Post by quaritexa » 27 Jul 2021, 10:51

Thaks.

buliasz
Posts: 26
Joined: 10 Oct 2016, 14:31
Contact:

Re: JSON Serializer - 2021/06/21 - beta.1

Post by buliasz » 19 Oct 2021, 07:27

First of all thank you for your work @TheArkive! I have created a fork that is able to serialize/deserialize non-Map/non-Array Objects as well. The limitation is for Classes to have constructor that is able to be executed without parameters, which is used in deserializer to create it's objects.
You might find it useful. Here's the fork address: https://github.com/buliasz/JXON_ahk2

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON Serializer - 2021/06/21 - beta.1

Post by TheArkive » 19 Oct 2021, 08:21

@buliasz
Good stuff :) thanks for posting. I'll check it out.

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON Serializer - 2021/11/05 - beta.1

Post by TheArkive » 05 Nov 2021, 10:15

2021/11/05
  • Changed some "strings" to 'strings' so a separate var for Chr(34) (") is unnecessary.
  • Fixed an issue where "\" in an item name was not properly processed on dump causing an error on load.

Noblish
Posts: 28
Joined: 22 Mar 2020, 06:10

Re: JSON Serializer - 2021/11/05 - beta.1

Post by Noblish » 20 Dec 2021, 14:34

Hi, how I determine Lenght() ? I have a json array and i need to know the lenght of it to do a loop.. thanks.

User avatar
TheArkive
Posts: 1027
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: JSON Serializer - 2021/11/05 - beta.1

Post by TheArkive » 20 Dec 2021, 14:46

@Noblish
You must load the text into a var with var := FileRead("file.txt")

Then you use arr := json_load(&var) to load to an array.

Then you use normal ahk syntax ... msgbox "array length: " arr.Length

Code: Select all

var := FileRead("path\to\file.txt")
arr := json_load(&var)
msgbox "array length: " arr.Length

Post Reply

Return to “Scripts and Functions (v2)”