JSON.ahk error Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
hisrRB57
Posts: 67
Joined: 13 Jan 2019, 11:43

JSON.ahk error

18 Apr 2024, 11:39

@thqby

I am using Chrome.ahk, websocket.ahk and JSON.ahk from https://github.com/thqby/ahk2_lib/blob/master
all the latest versions.

I get an error in this part of JSON.ahk:

Code: Select all

		UC(S, e := 1) {
			static m := Map(Ord('"'), '"', Ord("a"), "`a", Ord("b"), "`b", Ord("t"), "`t", Ord("n"), "`n", Ord("v"), "`v", Ord("f"), "`f", Ord("r"), "`r")
			local v := ""
			Loop Parse S, "\"
				if !((e := !e) && A_LoopField = "" ? v .= "\" : !e ? (v .= A_LoopField, 1) : 0)
					v .= (t := InStr("ux", SubStr(A_LoopField, 1, 1)) ? SubStr(A_LoopField, 1, RegExMatch(A_LoopField, "i)^[ux]?([\dA-F]{4})?([\dA-F]{2})?\K") - 1) : "") && RegexMatch(t, "i)^[ux][\da-f]+$") ? Chr(Abs("0x" SubStr(t, 2))) SubStr(A_LoopField, RegExMatch(A_LoopField, "i)^[ux]?([\dA-F]{4})?([\dA-F]{2})?\K")) : m.has(Ord(A_LoopField)) ? m[Ord(A_LoopField)] SubStr(A_LoopField, 2) : "\" A_LoopField, e := A_LoopField = "" ? e : !e
			return v
		}
e := "Viggo Johansen\n1851\u20131935\nDroogplaats voor visnetten op Skagen\n1890\nolieverf op doek"
the error message is: Error: Parameter #1 of Chr is invalid.
It goes wrong on the \u20131935 part. On the site it is displayed as -1935

Can you help?
hisrRB57
Posts: 67
Joined: 13 Jan 2019, 11:43

Re: JSON.ahk error

19 Apr 2024, 07:02

I figured out that \u2013 refers to a unicode. Since Autohotkey supports UTF-16 this code should be transformed easily to the right UTF-16 character with the following code:

Code: Select all

xChar := CHR("0x2013")
And to translate all \uxxxx codes to the UTF-16 characters in a string this routine should do:

Code: Select all

Convert_Unicode_SlashU(xInString){
	; Converts a string with \u unicode to a UTF-16 string
	; for Example :
	;	xInstring : = "Viggo Johansen\n1851\u20131935\nDroogplaats voor visnetten op Skagen\n1890\nolieverf op doek\n\n\u00A9Singer Museum Laren"
	;	xOutstring := "Viggo Johansen\n1851–1935\nDroogplaats voor visnetten op Skagen\n1890\nolieverf op doek\n\n©Singer Museum Laren"
	xOutString := ""
	rNeedle := "m)\\u[0-9A-F][0-9A-F][0-9A-F][0-9A-F]"
	iOcc := 1
	iStartPos := 1
	Loop {
		iPos := RegExMatch(xInstring,rNeedle,&oReg,iStartPos)
		if (iPos = 0) {
			xOutString .= substr(xInstring,iStartPos)
			break
		}
		xUnicode := substr(oReg.0,3)
		xChar := Chr("0x" xUnicode)
		xOutstring .= substr(xInstring,iStartPos,iPos - iStartPos) . xChar
		iStartPos := ipos + 6
		iOcc++ 
	}
	Return xOutstring
}
Now when I change the UC function in JSON.ahk to:

Code: Select all

		UC(S, e := 1) {
			static m := Map(Ord('"'), '"', Ord("a"), "`a", Ord("b"), "`b", Ord("t"), "`t", Ord("n"), "`n", Ord("v"), "`v", Ord("f"), "`f", Ord("r"), "`r")
			S := Convert_Unicode_SlashU(S)		; temporary addition, needs Convert_Unicode_SlashU function hisrRB57 19-4-2024
			local v := ""
			Loop Parse S, "\"
				if !((e := !e) && A_LoopField = "" ? v .= "\" : !e ? (v .= A_LoopField, 1) : 0)
					v .= (t := InStr("ux", SubStr(A_LoopField, 1, 1)) ? SubStr(A_LoopField, 1, RegExMatch(A_LoopField, "i)^[ux]?([\dA-F]{4})?([\dA-F]{2})?\K") - 1) : "") && RegexMatch(t, "i)^[ux][\da-f]+$") ? Chr(Abs("0x" SubStr(t, 2))) SubStr(A_LoopField, RegExMatch(A_LoopField, "i)^[ux]?([\dA-F]{4})?([\dA-F]{2})?\K")) : m.has(Ord(A_LoopField)) ? m[Ord(A_LoopField)] SubStr(A_LoopField, 2) : "\" A_LoopField, e := A_LoopField = "" ? e : !e
			return v
		}
	}
It has solved my problem.
I leave it up to @thqby to integrate this into JSON.ahk or come up with even more appropriate solutions.
User avatar
thqby
Posts: 417
Joined: 16 Apr 2021, 11:18
Contact:

Re: JSON.ahk error

20 Apr 2024, 00:50

I fixed it yesterday.
hisrRB57
Posts: 67
Joined: 13 Jan 2019, 11:43

Re: JSON.ahk error

29 Apr 2024, 23:42

Thanks bit I still receive an error:

Code: Select all

#Requires AutoHotkey v2.1-a
#SingleInstance Force

s1 := "Wanneer een groep jonge Franse kunstenaars in 1874 hun nieuwste werken in Parijs exposeert, levert hen dat de geuzennaam \u2018impressionisten\u2019 op. Ze hanteren nieuwe technieken en schilderen het buitenlicht in heldere kleuren. Eind 19de eeuw verspreidt het impressionisme zich razendsnel over Nederland, Duitsland en Denemarken. In ieder land krijgt de stijl een eigen gezicht."
v1 := UC(s1)



UC(S, e := 1) {
    static m := Map('"', '"', "a", "`a", "b", "`b", "t", "`t", "n", "`n", "v", "`v", "f", "`f", "r", "`r")
    local v := ""
    Loop Parse S, "\"
        if !((e := !e) && A_LoopField = "" ? v .= "\" : !e ? (v .= A_LoopField, 1) : 0)
            v .= (t := m.Get(SubStr(A_LoopField, 1, 1), 0)) ? t SubStr(A_LoopField, 2) :
                (t := RegExMatch(A_LoopField, "i)^(u[\da-f]{4}|x[\da-f]{2})\K")) ?
                    Chr("0x" SubStr(A_LoopField, 2, t - 1)) SubStr(A_LoopField, t) : "\" A_LoopField,
                    e := A_LoopField = "" ? e : !e
    return v
}

Code: Select all

Error: Parameter #1 of Chr requires a Number, but received a String.

Specifically: 0x2018i

	012: Loop Parse S, "\"
	013: If !((e := !e) && A_LoopField = "" ? v .= "\" : !e ? (v .= A_LoopField, 1) : 0)
▶	014: v .= (t := m.Get(SubStr(A_LoopField, 1, 1), 0)) ? t SubStr(A_LoopField, 2) : (t := RegExMatch(A_LoopField, "i)^(u[\da-f]{4}|x[\da-f]{2})\K")) ? Chr("0x" SubStr(A_LoopField, 2, t - 1)) SubStr(A_LoopField, t) : "\" A_LoopField, e := A_LoopField = "" ? e : !e
	018: Return v
	019: }

User avatar
thqby
Posts: 417
Joined: 16 Apr 2021, 11:18
Contact:

Re: JSON.ahk error  Topic is solved

30 Apr 2024, 08:05

Chr("0x" SubStr(A_LoopField, 2, t - 2)
hisrRB57
Posts: 67
Joined: 13 Jan 2019, 11:43

Re: JSON.ahk error

01 May 2024, 14:43

Now it works!. Excellent, thanks a lot.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Milincho and 40 guests