[Library] cJson.ahk (version 0.4.1 pre-release)
Re: [Library] cJson.ahk (version 0.4.0 pre-release)
This limitation can be avoided by using Object::Invoke instead of IDispatch::Invoke.
Re: [Library] cJson.ahk (version 0.4.0 pre-release)
Guess it's time for me to write C headers for Object haha
Re: [Library] cJson.ahk (version 0.4.0 pre-release)
I faced Object and array issue after loading and dumping json
i.e. [{}] turn into [[]]
I don't know is this even a issue ? or am I doing something wrong?
i.e. [{}] turn into [[]]
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
Re: [Library] cJson.ahk (version 0.4.0 pre-release)
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.
Re: [Library] cJson.ahk (version 0.4.0 pre-release)
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.
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
it will be perfect if jsonPath is merged into
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
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.
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
viewtopic.php?f=6&t=64219&p=275313#p275313geek wrote: ↑21 Jul 2022, 13:55I 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.
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.
- oldbrother
- Posts: 275
- Joined: 23 Oct 2013, 05:08
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
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
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
I think in recent versions Loads() was renamed to Load().
- oldbrother
- Posts: 275
- Joined: 23 Oct 2013, 05:08
Re: [Library] cJson.ahk (version 0.4.0 pre-release)
cool and thank you both.
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
I just did the same thing. @geek I have taken the liberty to fix the example in the first post.oldbrother wrote: ↑24 Jul 2022, 16:21I tried the sample codes below. It didn't work. The message boxes showed nothing.
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
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:
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
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
}
Basically, my entire feature request is "help me be lazy" XD
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
May be it will be better to convert floats to strings when decoding?
Because now we will
1) loose some info:
2) get not needed info
Because now we will
1) loose some info:
Code: Select all
var = {"x": 0.7654321}
msgbox % JSON.load(var).x ; shows 0.765432
Code: Select all
var = {"x": 0.5}
msgbox % JSON.load(var).x ; shows 0.500000
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
@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).
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
@geek
When GoogleTranslate runs, CJson does not work, Not sure why, but GoogleTranslate works.
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
}
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
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.
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
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.
Re: [Library] cJson.ahk (version 0.4.1 pre-release)
@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