[Library] cJson.ahk (version 0.4.1 pre-release)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
thqby
Posts: 431
Joined: 16 Apr 2021, 11:18
Contact:

Re: [Library] cJson.ahk (version 0.4.0 pre-release)

22 Feb 2022, 01:12

This limitation can be avoided by using Object::Invoke instead of IDispatch::Invoke.
geek
Posts: 1054
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: [Library] cJson.ahk (version 0.4.0 pre-release)

22 Feb 2022, 10:20

thqby wrote:
22 Feb 2022, 01:12
This limitation can be avoided by using Object::Invoke instead of IDispatch::Invoke.
Guess it's time for me to write C headers for Object haha
User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: [Library] cJson.ahk (version 0.4.0 pre-release)

12 Mar 2022, 01:26

I faced Object and array issue after loading and dumping json
i.e. [{}] turn into [[]]
:think: I don't know is this even a issue ? or am I doing something wrong?

Code: Select all

test = {"match":[{}]}
testload := Json.load(test)
msgbox, % json.dump(testload) ; returns>> {"match": [[]]}
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory
geek
Posts: 1054
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: [Library] cJson.ahk (version 0.4.0 pre-release)

17 Mar 2022, 09:49

AuroHotkey v1 has no distinct data types differentiating [] and {}, both are just objects. The library guesses whether you mean [] or {} based on the contents of the object. An object with no contents is assumed []. Some other libraries assume {}. Neither is correct in all cases. It's planned to add an override for this functionality, but I've yet to find the time to do so.
geek
Posts: 1054
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: [Library] cJson.ahk (version 0.4.0 pre-release)

29 May 2022, 22:12

tuzi wrote:
18 Feb 2022, 21:30
@geek

here is a bug.

the key should be char, but it shows CHAR.
Apologies for a delay in the response on this one, but Lexikos has fixed the underlying issue in v1.1.33.11. Updating to the latest AHK will make the problem go away.
hughman
Posts: 18
Joined: 17 Jun 2014, 09:39

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

21 Jul 2022, 07:09

it will be perfect if jsonPath is merged into :D
geek
Posts: 1054
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

21 Jul 2022, 13:55

hughman wrote:
21 Jul 2022, 07:09
it will be perfect if jsonPath is merged into :D
I have plans to implement something like it, but as far as I can tell a full jsonPath implementation would require many more times as much code to implement than everything else I've already implemented. My supported subset will most likely just let you pick out a single fully qualified key.
hughman
Posts: 18
Joined: 17 Jun 2014, 09:39

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

22 Jul 2022, 05:56

geek wrote:
21 Jul 2022, 13:55
I have plans to implement something like it, but as far as I can tell a full jsonPath implementation would require many more times as much code to implement than everything else I've already implemented. My supported subset will most likely just let you pick out a single fully qualified key.
viewtopic.php?f=6&t=64219&p=275313#p275313

Here is a implemention by DuyMinh, but for jsObj. It's so handy and so cool.
You can first implement it for your js version easily. :lol:
User avatar
oldbrother
Posts: 275
Joined: 23 Oct 2013, 05:08

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

24 Jul 2022, 16:21

I tried the sample codes below. It didn't work. The message boxes showed nothing. Why?

Code: Select all

#Include, JSON.ahk

; Create some JSON
str = ["abc", 123, {"true": 1, "false": 0, "null": ""}, [true, false, null]]
obj := JSON.Loads(str)

MsgBox, % obj[1] ; abc
MsgBox, % obj[2] ; 123

MsgBox, % obj[3].true ; 1
MsgBox, % obj[3].false ; 0
MsgBox, % obj[3].null ; *nothing*

MsgBox, % obj[4, 1] ; 1
MsgBox, % obj[4, 2] ; 0
MsgBox, % obj[4, 3] == JSON.Null ; 1

return
gregster
Posts: 9095
Joined: 30 Sep 2013, 06:48

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

24 Jul 2022, 16:32

I think in recent versions Loads() was renamed to Load().
User avatar
oldbrother
Posts: 275
Joined: 23 Oct 2013, 05:08

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

24 Jul 2022, 18:20

gregster wrote:
24 Jul 2022, 16:32
I think in recent versions Loads() was renamed to Load().
:headwall:

Thank you!
tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

Re: [Library] cJson.ahk (version 0.4.0 pre-release)

07 Sep 2022, 05:32

geek wrote:
29 May 2022, 22:12
tuzi wrote:
18 Feb 2022, 21:30
@geek

here is a bug.

the key should be char, but it shows CHAR.
Apologies for a delay in the response on this one, but Lexikos has fixed the underlying issue in v1.1.33.11. Updating to the latest AHK will make the problem go away.
cool and thank you both. :D
lexikos
Posts: 9665
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

09 Sep 2022, 18:24

oldbrother wrote:
24 Jul 2022, 16:21
I tried the sample codes below. It didn't work. The message boxes showed nothing.
I just did the same thing. @geek I have taken the liberty to fix the example in the first post.
Qriist
Posts: 82
Joined: 11 Sep 2016, 04:02

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

27 Sep 2022, 20:35

Heya geek! Phenomenal work you've put into this! ^_^

Small feature request: an optional flag in json.load() to render the sub-objects "flat". The sub-object behavior, while certainly useful moving forward, deviates from coco's and affects the ability to do drop-in replacements if a key value is true/false/null.

The most common examples look something like this:

Code: Select all

obj := ""	;as coco would render a null
if !obj
	msgbox % "triggered by empty string"


obj := []	;as cJson would render a false/null
if !obj 
	msgbox % "never triggered by false/null" ;nulls and falses will have unexpected positives



obj := [ "" , [] ]
for k,v in obj{
	if !v
		msgbox % a_index	;the object doesn't trigger this msgbox
}
It's also handy to sometimes print an unfamiliar json array and the extra objects can make things more difficult to read.

Basically, my entire feature request is "help me be lazy" XD
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

25 Oct 2022, 17:57

May be it will be better to convert floats to strings when decoding?
Because now we will
1) loose some info:

Code: Select all

var = {"x": 0.7654321}
msgbox % JSON.load(var).x   ; shows 0.765432
2) get not needed info

Code: Select all

var = {"x": 0.5}
msgbox % JSON.load(var).x   ; shows 0.500000
lexikos
Posts: 9665
Joined: 30 Sep 2013, 04:07
Contact:

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

27 Oct 2022, 02:41

@malcev I would assume that JSON.load() is not gaining or losing information as much as you think. That happens when you convert the floating-point number to a string with 6 digits of precision (the default for SetFormat).
sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

06 Nov 2022, 09:40

@geek
When GoogleTranslate runs, CJson does not work, Not sure why, but GoogleTranslate works.

Code: Select all

#NoEnv
#Include <CJson.ahk>
arr := {"1" : {"100" : {"k1" : "v1", "k2" : "v2"}}}

f1::  ;normal
text := JSON.Dump(arr)
MsgBox, % text
Return

f2::  ;error
str := GoogleTranslate("Hello, World!", "en", "zh-cn")
text := JSON.Dump(arr)
MsgBox, % text "`n" str
Return

; by teadrinker 
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=63835
GoogleTranslate(str, from:="auto", to:="en") {
   static JS := CreateScriptObj(), _ := JS.( GetJScript() ) := JS.("delete ActiveXObject; delete GetObject;")
   
   json := SendRequest(JS, str, to, from, proxy := "127.0.0.1:10809")
   oJSON := JS.("(" . json . ")")

   if !IsObject(oJSON[1]) {
      Loop % oJSON[0].length
         trans .= oJSON[0][A_Index - 1][0]
   }
   else {
      MainTransText := oJSON[0][0][0]
      Loop % oJSON[1].length {
         trans .= "`n+"
         obj := oJSON[1][A_Index-1][1]
         Loop % obj.length {
            txt := obj[A_Index - 1]
            trans .= (MainTransText = txt ? "" : "`n" txt)
         }
      }
   }
   if !IsObject(oJSON[1])
      MainTransText := trans := Trim(trans, ",+`n ")
   else
      trans := MainTransText . "`n+`n" . Trim(trans, ",+`n ")

   from := oJSON[2]
   trans := Trim(trans, ",+`n ")
   Return trans
}

SendRequest(JS, str, tl, sl, proxy) {
   static http
   ComObjError(false)
   if !http
   {
      http := ComObjCreate("WinHttp.WinHttpRequest.5.1")
      ( proxy && http.SetProxy(2, proxy) )
      http.open("GET", "https://translate.google.com", true)
      http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
      http.send()
      http.WaitForResponse(-1)
   }
   http.open("POST", "https://translate.googleapis.com/translate_a/single?client=gtx"
                ; or "https://clients5.google.com/translate_a/t?client=dict-chrome-ex"
      . "&sl=" . sl . "&tl=" . tl . "&hl=" . tl
      . "&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=0&ssel=0&tsel=0&pc=1&kc=1"
      . "&tk=" . JS.("tk").(str), true)

   http.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
   http.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0")
   http.send("q=" . URIEncode(str))
   http.WaitForResponse(-1)
   Return http.responsetext
}

URIEncode(str, encoding := "UTF-8")  {
   VarSetCapacity(var, StrPut(str, encoding))
   StrPut(str, &var, encoding)

   while code := NumGet(Var, A_Index - 1, "UChar")  {
      bool := (code > 0x7F || code < 0x30 || code = 0x3D)
      UrlStr .= bool ? "%" . Format("{:02X}", code) : Chr(code)
   }
   Return UrlStr
}

GetJScript()
{
   script =
   (
      var TKK = ((function() {
        var a = 561666268;
        var b = 1526272306;
        return 406398 + '.' + (a + b);
      })());

      function b(a, b) {
        for (var d = 0; d < b.length - 2; d += 3) {
            var c = b.charAt(d + 2),
                c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
                c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
            a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
        }
        return a
      }

      function tk(a) {
          for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
              var c = a.charCodeAt(f);
              128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ?
              (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
              g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
          }
          a = h;
          for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
          a = b(a, "+-3^+b+-f");
          a ^= Number(e[1]) || 0;
          0 > a && (a = (a & 2147483647) + 2147483648);
          a `%= 1E6;
          return a.toString() + "." + (a ^ h)
      }
   )
   Return script
}

CreateScriptObj() {
   static doc, JS, _JS
   if !doc {
      doc := ComObjCreate("htmlfile")
      doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
      JS := doc.parentWindow
      if (doc.documentMode < 9)
         JS.execScript()
      _JS := ObjBindMethod(JS, "eval")
   }
   Return _JS
}
geek
Posts: 1054
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

06 Nov 2022, 17:02

sanmaodo wrote:
06 Nov 2022, 09:40
@geek
When GoogleTranslate runs, CJson does not work, Not sure why, but GoogleTranslate works.
The top of your GoogleTranslate function overwrites the JSON global class object. Change GoogleTranslate to use a different variable name and the problem should go away.
User avatar
kczx3
Posts: 1648
Joined: 06 Oct 2015, 21:39

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

06 Nov 2022, 18:10

Wouldn’t that be a local json variable though? Or can you not shadow a class name? I don’t know v1 semantics very well anymore so could have that totally wrong.
sanmaodo
Posts: 45
Joined: 28 Aug 2020, 01:39

Re: [Library] cJson.ahk (version 0.4.1 pre-release)

06 Nov 2022, 21:44

@geek Thanks for giving me the answer so quickly, everything works fine now.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 34 guests