-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.tcl
230 lines (213 loc) · 7.45 KB
/
json.tcl
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# JSON parser / encoder.
# Copyright (C) 2014 Danyil Bohdan.
# License: MIT
# Parse the string $str containing JSON into nested Tcl dictionaries.
# numberDictArrays: decode arrays as dictionaries with sequential integers
# starting with zero as keys; otherwise decode them as lists.
namespace eval ::docker::json {}
proc ::docker::json::parse {str {numberDictArrays 0}} {
set result [::docker::json::decode-value $str $numberDictArrays]
if {[lindex $result 1] eq ""} {
return [lindex $result 0]
} else {
error "trailing garbage after JSON data: $str"
}
}
# Serialize nested Tcl dictionaries as JSON.
# numberDictArrays: encode dictionaries with keys {0 1 2 3 ...} as arrays,
# e.g., {0 a 1 b} to ["a", "b"].
proc ::docker::json::stringify {dictionaryOrValue {numberDictArrays 1} {allstring 0}} {
set result {}
if {[llength $dictionaryOrValue] <= 1} {
if {[string is integer $dictionaryOrValue] || \
[string is double $dictionaryOrValue]} {
# Number.
if { $allstring } {
set result "\"$dictionaryOrValue\""
} else {
set result $dictionaryOrValue
}
} else {
# String.
set result "\"$dictionaryOrValue\""
}
} else {
# Dict.
set allNumeric 1
if {$numberDictArrays} {
set values {}
set i 0
foreach {key value} $dictionaryOrValue {
set allNumeric [expr {$allNumeric && ($key == $i)}]
if {!$allNumeric} {
break
}
lappend values $value
incr i
}
}
if {$numberDictArrays && $allNumeric} {
# Produce array.
set arrayElements {}
foreach x $values {
lappend arrayElements [::docker::json::stringify $x 1 $allstring]
}
set result "\[[join $arrayElements {, }]\]"
} else {
# Produce object.
set objectDict {}
foreach {key value} $dictionaryOrValue {
foreach {k type} [split $key "!"] break
if { $type eq "string" } {
lappend objectDict "\"$k\": [::docker::json::stringify $value 0 1]"
} else {
lappend objectDict "\"$k\": [::docker::json::stringify $value 0 $allstring]"
}
}
set result "{[join $objectDict {, }]}"
}
}
return $result
}
# Choose how to decode a JSON value. Return a list consisting of the result of
# parsing the initial part of $str and the remainder of $str that was not
# parsed. E.g., ::docker::json::decode-value {"string", 55} returns {{string} {, 55}}.
proc ::docker::json::decode-value {str {numberDictArrays 0}} {
set str [string trimleft $str]
switch -regexp -- $str {
{^\".*} {
return [::docker::json::decode-string $str]
}
{^[0-9-].*} {
return [::docker::json::decode-number $str]
}
{^\{.*} {
return [::docker::json::decode-object $str $numberDictArrays]
}
{^\[.*} {
return [::docker::json::decode-array $str $numberDictArrays]
}
{^(true|false|null)} {
return [::docker::json::decode-boolean-or-null $str]
}
default {
error "cannot decode value as JSON: \"$str\""
}
}
}
# Return a list of two elements: the initial part of $str parsed as "true",
# "false" or "null" and the remainder of $str that wasn't parsed.
proc ::docker::json::decode-boolean-or-null {str} {
regexp {^(true|false|null)} $str value
return [list $value [string range $str [string length $value] end]]
}
# Return a list of two elements: the initial part of $str parsed as a JSON
# string and the remainder of $str that wasn't parsed.
proc ::docker::json::decode-string {str} {
if {[regexp {^"((?:[^"\\]|\\.)*)"} $str _ result]} {
return [list \
[subst -nocommands -novariables $result] \
[string range $str [expr {2 + [string length $result]}] end]]
# Add two to result length to account for the double quotes
# around the string.
} else {
error "can't parse JSON string: $str"
}
}
# Return a list of two elements: the initial part of $str parsed as a JSON
# number and the remainder of $str that wasn't parsed.
proc ::docker::json::decode-number {str} {
if {[regexp -- {^-?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(:?(?:e|E)[+-]?[0-9]*)?} \
$str result]} {
# [][ integer part ][ optional ][ optional exponent ]
# ^ sign [ frac. part]
return [list $result [string range $str [string length $result] end]]
} else {
error "can't parse JSON number: $str"
}
}
# Return a list of two elements: the initial part of $str parsed as a JSON array
# and the remainder of $str that wasn't parsed. Arrays are parsed into
# dictionaries with numbers {0 1 2 ...} as keys if $numberDictArrays is true
# or lists if it is false. E.g., if $numberDictArrays == 1 then
# ["Hello, World" 2048] is converted to {0 {Hello, World!} 1 2048}; otherwise
# it is converted to {{Hello, World!} 2048}.
proc ::docker::json::decode-array {str {numberDictArrays 0}} {
set strInitial $str
set result {}
set value {}
set i 0
if {[string index $str 0] ne "\["} {
error "can't parse JSON array: $strInitial"
} else {
set str [string range $str 1 end]
}
while 1 {
# Empty array, get out from here.
if { [string index [string trimleft $str] 0] eq "\]" } {
set str [string range [string trimleft $str] 1 end]
break
}
# Value.
lassign [::docker::json::decode-value $str $numberDictArrays] value str
set str [string trimleft $str]
if {$numberDictArrays} {
lappend result $i
}
lappend result $value
# ","
set sep [string index $str 0]
set str [string range $str 1 end]
if {$sep eq "\]"} {
break
} elseif {$sep ne ","} {
error "can't parse JSON array: $strInitial"
}
incr i
}
return [list $result $str]
}
# Return a list of two elements: the initial part of $str parsed as a JSON
# object and the remainder of $str that wasn't parsed.
proc ::docker::json::decode-object {str {numberDictArrays 0}} {
set strInitial $str
set result {}
set value {}
if {[string index $str 0] ne "\{"} {
error "can't parse JSON object: $strInitial"
} else {
set str [string range $str 1 end]
}
while 1 {
# Key string.
set str [string trimleft $str]
# Empty array, get out from here.
if { [string index $str 0] eq "\}" } {
set str [string range $str 1 end]
break
}
lassign [::docker::json::decode-string $str] value str
set str [string trimleft $str]
lappend result $value
# ":"
set sep [string index $str 0]
set str [string range $str 1 end]
if {$sep ne ":"} {
error "can't parse JSON object: $strInitial"
}
# Value.
lassign [::docker::json::decode-value $str $numberDictArrays] value str
set str [string trimleft $str]
lappend result $value
# ","
set sep [string index $str 0]
set str [string range $str 1 end]
if {$sep eq "\}"} {
break
} elseif {$sep ne ","} {
error "can't parse JSON object: $str"
}
}
return [list $result $str]
}
package provide docker::json 1.0