-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_JSON_Diff.ahk
186 lines (163 loc) · 5.38 KB
/
class_JSON_Diff.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
; Link:
; Author:
; Date:
; for: AHK_L
/*
*/
class JSON
{
static JS := JSON._GetJScriptObject(), true := {}, false := {}, null := {}
Parse(sJson, js := false) {
if jsObj := this.VerifyJson(sJson)
Return js ? jsObj : this._CreateObject(jsObj)
}
Stringify(obj, js := false, indent := "") {
if js
Return this.JS.JSON.stringify(obj, "", indent)
else {
sObj := this._ObjToString(obj)
Return this.JS.eval("JSON.stringify(" . sObj . ",'','" . indent . "')")
}
}
GetKey(sJson, key, indent := "") {
if !this.VerifyJson(sJson)
Return
try Return this.JS.eval("JSON.stringify((" . sJson . ")" . (SubStr(key, 1, 1) = "[" ? "" : ".") . key . ",'','" . indent . "')")
catch
MsgBox, Bad key:`n`n%key%
}
SetKey(sJson, key, value, indent := "") {
if !this.VerifyJson(sJson)
Return
if !this.VerifyJson(value, true) {
MsgBox, % "Bad value: " . value . "`n"
. "Must be a valid JSON string." . "`n"
. "Enclose literal strings in quotes '' or """".`n"
. "As an empty string pass '' or """""
Return
}
try {
res := this.JS.eval( "var obj = (" . sJson . ");"
. "obj" . (SubStr(key, 1, 1) = "[" ? "" : ".") . key . "=" . value . ";"
. "JSON.stringify(obj,'','" . indent . "')" )
this.JS.eval("obj = ''")
Return res
}
catch
MsgBox, Bad key:`n`n%key%
}
RemoveKey(sJson, key, indent := "") {
if !this.VerifyJson(sJson)
Return
sign := SubStr(key, 1, 1) = "[" ? "" : "."
try {
if !RegExMatch(key, "(.*)\[(\d+)]$", match)
res := this.JS.eval("var obj = (" . sJson . "); delete obj" . sign . key . "; JSON.stringify(obj,'','" . indent . "')")
else
res := this.JS.eval( "var obj = (" . sJson . ");"
. "obj" . (match1 != "" ? sign . match1 : "") . ".splice(" . match2 . ", 1);"
. "JSON.stringify(obj,'','" . indent . "')" )
this.JS.eval("obj = ''")
Return res
}
catch
MsgBox, Bad key:`n`n%key%
}
Enum(sJson, key := "", indent := "") {
if !this.VerifyJson(sJson)
Return
conc := key ? (SubStr(key, 1, 1) = "[" ? "" : ".") . key : ""
try {
jsObj := this.JS.eval("(" sJson ")" . conc)
res := jsObj.IsArray()
if (res = "")
Return
obj := {}
if (res = -1) {
Loop % jsObj.length
obj[A_Index - 1] := this.JS.eval("JSON.stringify((" sJson ")" . conc . "[" . (A_Index - 1) . "],'','" . indent . "')")
}
else if (res = 0) {
keys := jsObj.GetKeys()
Loop % keys.length
k := keys[A_Index - 1], obj[k] := this.JS.eval("JSON.stringify((" sJson ")" . conc . "['" . k . "'],'','" . indent . "')")
}
Return obj
}
catch
MsgBox, Bad key:`n`n%key%
}
VerifyJson(sJson, silent := false) {
try jsObj := this.JS.eval("(" sJson ")")
catch {
if !silent
MsgBox, Bad JSON string:`n`n%sJson%
Return
}
Return IsObject(jsObj) ? jsObj : true
}
_ObjToString(obj) {
if IsObject( obj ) {
for k, v in ["true", "false", "null"]
if (obj = this[v])
Return v
isArray := true
for key in obj {
if IsObject(key)
throw Exception("Invalid key")
if !( key = A_Index || isArray := false )
break
}
for k, v in obj
str .= ( A_Index = 1 ? "" : "," ) . ( isArray ? "" : """" . k . """:" ) . this._ObjToString(v)
Return isArray ? "[" str "]" : "{" str "}"
}
else if !(obj*1 = "" || RegExMatch(obj, "\s"))
Return obj
for k, v in [["\", "\\"], [A_Tab, "\t"], ["""", "\"""], ["/", "\/"], ["`n", "\n"], ["`r", "\r"], [Chr(12), "\f"], [Chr(08), "\b"]]
obj := StrReplace( obj, v[1], v[2] )
Return """" obj """"
}
_GetJScriptObject() {
static doc
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv=""X-UA-Compatible"" content=""IE=9"">")
JS := doc.parentWindow
JSON._AddMethods(JS)
Return JS
}
_AddMethods(ByRef JS) {
JScript =
(
Object.prototype.GetKeys = function () {
var keys = []
for (var k in this)
if (this.hasOwnProperty(k))
keys.push(k)
return keys
}
Object.prototype.IsArray = function () {
var toStandardString = {}.toString
return toStandardString.call(this) == '[object Array]'
}
)
JS.eval(JScript)
}
_CreateObject(jsObj) {
res := jsObj.IsArray()
if (res = "")
Return jsObj
else if (res = -1) {
obj := []
Loop % jsObj.length
obj[A_Index] := this._CreateObject(jsObj[A_Index - 1])
}
else if (res = 0) {
obj := {}
keys := jsObj.GetKeys()
Loop % keys.length
k := keys[A_Index - 1], obj[k] := this._CreateObject(jsObj[k])
}
Return obj
}
}