-
Notifications
You must be signed in to change notification settings - Fork 11
/
json.applescript
101 lines (83 loc) · 2.02 KB
/
json.applescript
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
on encode(value)
set type to class of value
if type = integer or type = boolean
return value as text
else if type = text
return encodeString(value)
else if type = list
return encodeList(value)
else if type = script
return value's toJson()
else
error "Unknown type " & type
end
end
on encodeList(value_list)
set out_list to {}
repeat with value in value_list
copy encode(value) to end of out_list
end
return "[" & join(out_list, ", ") & "]"
end
on encodeString(value)
set rv to ""
set codepoints to id of value
if (class of codepoints) is not list
set codepoints to {codepoints}
end
repeat with codepoint in codepoints
set codepoint to codepoint as integer
if codepoint = 34
set quoted_ch to "\\\""
else if codepoint = 92 then
set quoted_ch to "\\\\"
else if codepoint >= 32 and codepoint < 127
set quoted_ch to character id codepoint
else
set quoted_ch to "\\u" & hex4(codepoint)
end
set rv to rv & quoted_ch
end
return "\"" & rv & "\""
end
on join(value_list, delimiter)
set original_delimiter to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set rv to value_list as text
set AppleScript's text item delimiters to original_delimiter
return rv
end
on hex4(n)
set digit_list to "0123456789abcdef"
set rv to ""
repeat until length of rv = 4
set digit to (n mod 16)
set n to (n - digit) / 16 as integer
set rv to (character (1+digit) of digit_list) & rv
end
return rv
end
on createDictWith(item_pairs)
set item_list to {}
script Dict
on setkv(key, value)
copy {key, value} to end of item_list
end
on toJson()
set item_strings to {}
repeat with kv in item_list
set key_str to encodeString(item 1 of kv)
set value_str to encode(item 2 of kv)
copy key_str & ": " & value_str to end of item_strings
end
return "{" & join(item_strings, ", ") & "}"
end
end
repeat with pair in item_pairs
Dict's setkv(item 1 of pair, item 2 of pair)
end
return Dict
end
on createDict()
return createDictWith({})
end