forked from CG-it/CG-it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_tcl.tcl
290 lines (250 loc) · 8.54 KB
/
json_tcl.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#
# JSON parser for Tcl.
#
# See http://www.json.org/ && http://www.ietf.org/rfc/rfc4627.txt
#
# Total rework of the code published with version number 1.0 by
# Thomas Maeder, Glue Software Engineering AG
#
# $Id: json.tcl,v 1.7 2011/11/10 21:05:58 andreas_kupries Exp $
#
if {![package vsatisfies [package provide Tcl] 8.5]} {
package require dict
}
# Parse JSON text into a dict
# @param jsonText JSON text
# @return dict (or list) containing the object represented by $jsonText
proc ::json::json2dict_tcl {jsonText} {
variable tokenRE
set tokens [regexp -all -inline -- $tokenRE $jsonText]
set nrTokens [llength $tokens]
set tokenCursor 0
#puts T:\t[join $tokens \nT:\t]
return [parseValue $tokens $nrTokens tokenCursor]
}
# Parse multiple JSON entities in a string into a list of dictionaries
# @param jsonText JSON text to parse
# @param max Max number of entities to extract.
# @return list of (dict (or list) containing the objects) represented by $jsonText
proc ::json::many-json2dict_tcl {jsonText {max -1}} {
variable tokenRE
if {$max == 0} {
return -code error -errorCode {JSON BAD-LIMIT ZERO} \
"Bad limit 0 of json entities to extract."
}
set tokens [regexp -all -inline -- $tokenRE $jsonText]
set nrTokens [llength $tokens]
set tokenCursor 0
set result {}
set found 0
set n $max
while {$n != 0} {
if {$tokenCursor >= $nrTokens} break
lappend result [parseValue $tokens $nrTokens tokenCursor]
incr found
if {$n > 0} {incr n -1}
}
if {$n > 0} {
return -code error -errorCode {JSON BAD-LIMIT TOO LARGE} \
"Bad limit $max of json entities to extract, found only $found."
}
return $result
}
# Throw an exception signaling an unexpected token
proc ::json::unexpected {tokenCursor token expected} {
return -code error -errorcode [list JSON UNEXPECTED $tokenCursor $expected] \
"unexpected token \"$token\" at position $tokenCursor; expecting $expected"
}
# Get rid of the quotes surrounding a string token and substitute the
# real characters for escape sequences within it
# @param token
# @return unquoted unescaped value of the string contained in $token
proc ::json::unquoteUnescapeString {tokenCursor token} {
variable stringREv
set unquoted [string range $token 1 end-1]
if {![regexp $stringREv $token]} {
unexpected $tokenCursor $token STRING
}
set res [subst -nocommands -novariables $unquoted]
return $res
}
# Parse an object member
# @param tokens list of tokens
# @param nrTokens length of $tokens
# @param tokenCursorName name (in caller's context) of variable
# holding current position in $tokens
# @param objectDictName name (in caller's context) of dict
# representing the JSON object of which to
# parse the next member
proc ::json::parseObjectMember {tokens nrTokens tokenCursorName objectDictName} {
upvar $tokenCursorName tokenCursor
upvar $objectDictName objectDict
set token [lindex $tokens $tokenCursor]
set tc $tokenCursor
incr tokenCursor
set leadingChar [string index $token 0]
if {$leadingChar eq "\""} {
set memberName [unquoteUnescapeString $tc $token]
if {$tokenCursor == $nrTokens} {
unexpected $tokenCursor "END" "\":\""
} else {
set token [lindex $tokens $tokenCursor]
incr tokenCursor
if {$token eq ":"} {
set memberValue [parseValue $tokens $nrTokens tokenCursor]
dict set objectDict $memberName $memberValue
} else {
unexpected $tokenCursor $token "\":\""
}
}
} else {
unexpected $tokenCursor $token "STRING"
}
}
# Parse the members of an object
# @param tokens list of tokens
# @param nrTokens length of $tokens
# @param tokenCursorName name (in caller's context) of variable
# holding current position in $tokens
# @param objectDictName name (in caller's context) of dict
# representing the JSON object of which to
# parse the next member
proc ::json::parseObjectMembers {tokens nrTokens tokenCursorName objectDictName} {
upvar $tokenCursorName tokenCursor
upvar $objectDictName objectDict
while true {
parseObjectMember $tokens $nrTokens tokenCursor objectDict
set token [lindex $tokens $tokenCursor]
incr tokenCursor
switch -exact $token {
"," {
# continue
}
"\}" {
break
}
default {
unexpected $tokenCursor $token "\",\"|\"\}\""
}
}
}
}
# Parse an object
# @param tokens list of tokens
# @param nrTokens length of $tokens
# @param tokenCursorName name (in caller's context) of variable
# holding current position in $tokens
# @return parsed object (Tcl dict)
proc ::json::parseObject {tokens nrTokens tokenCursorName} {
upvar $tokenCursorName tokenCursor
if {$tokenCursor == $nrTokens} {
unexpected $tokenCursor "END" "OBJECT"
} else {
set result [dict create]
set token [lindex $tokens $tokenCursor]
if {$token eq "\}"} {
# empty object
incr tokenCursor
} else {
parseObjectMembers $tokens $nrTokens tokenCursor result
}
return $result
}
}
# Parse the elements of an array
# @param tokens list of tokens
# @param nrTokens length of $tokens
# @param tokenCursorName name (in caller's context) of variable
# holding current position in $tokens
# @param resultName name (in caller's context) of the list
# representing the JSON array
proc ::json::parseArrayElements {tokens nrTokens tokenCursorName resultName} {
upvar $tokenCursorName tokenCursor
upvar $resultName result
while true {
lappend result [parseValue $tokens $nrTokens tokenCursor]
if {$tokenCursor == $nrTokens} {
unexpected $tokenCursor "END" "\",\"|\"\]\""
} else {
set token [lindex $tokens $tokenCursor]
incr tokenCursor
switch -exact $token {
"," {
# continue
}
"\]" {
break
}
default {
unexpected $tokenCursor $token "\",\"|\"\]\""
}
}
}
}
}
# Parse an array
# @param tokens list of tokens
# @param nrTokens length of $tokens
# @param tokenCursorName name (in caller's context) of variable
# holding current position in $tokens
# @return parsed array (Tcl list)
proc ::json::parseArray {tokens nrTokens tokenCursorName} {
upvar $tokenCursorName tokenCursor
if {$tokenCursor == $nrTokens} {
unexpected $tokenCursor "END" "ARRAY"
} else {
set result {}
set token [lindex $tokens $tokenCursor]
set leadingChar [string index $token 0]
if {$leadingChar eq "\]"} {
# empty array
incr tokenCursor
} else {
parseArrayElements $tokens $nrTokens tokenCursor result
}
return $result
}
}
# Parse a value
# @param tokens list of tokens
# @param nrTokens length of $tokens
# @param tokenCursorName name (in caller's context) of variable
# holding current position in $tokens
# @return parsed value (dict, list, string, number)
proc ::json::parseValue {tokens nrTokens tokenCursorName} {
upvar $tokenCursorName tokenCursor
if {$tokenCursor == $nrTokens} {
unexpected $tokenCursor "END" "VALUE"
} else {
set token [lindex $tokens $tokenCursor]
set tc $tokenCursor
incr tokenCursor
set leadingChar [string index $token 0]
switch -exact -- $leadingChar {
"\{" {
return [parseObject $tokens $nrTokens tokenCursor]
}
"\[" {
return [parseArray $tokens $nrTokens tokenCursor]
}
"\"" {
# quoted string
return [unquoteUnescapeString $tc $token]
}
"t" -
"f" -
"n" {
# bare word: true, false, null (return as is)
return $token
}
default {
# number?
if {[string is double -strict $token]} {
return $token
} else {
unexpected $tokenCursor $token "VALUE"
}
}
}
}
}