proc deparse {data} {
switch -regexp [lindex $data 0] {
- ^true|false|null$ { lindex $data 0 }
- ^num|str|obj|list$ { eval $data }
+ ^(true|false|null)$ { lindex $data 0 }
+ ^(num|str|obj|list)$ { eval $data }
default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] }
}
}
proc tclify {data} {
switch -regexp [lindex $data 0] {
- ^true|false|null$ { uplevel 1 return [lindex $data 0] }
- ^num|str|obj|list$ {}
+ ^(true|false|null)$ { uplevel 1 return [lindex $data 0] }
+ ^(num|str|obj|list)$ {}
default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] }
}
eval $data
eat_any
set tcl {list}
while {"$json" ne ""} {
+ eat_spaces
if {[string index $json 0] eq "]"} {
eat_any
return $tcl
proc parse_obj {} {
variable json
- eat_char
+ eat_any
set tcl {obj}
while {"$json" ne ""} {
- if {[string index $json 0] eq "]"} {
- eat_char
+ eat_spaces
+ if {[string index $json 0] eq "\}"} {
+ eat_any
return $tcl
}
- lappend tcl [
+ eat_spaces
+ lappend tcl [ parse_str ]
+ eat_spaces
+ eat_char :
+ eat_spaces
lappend tcl [ parse ]
eat_char ,
}
# like Text::Balanced except ugly (borrowed from tcvJSON's code)
set reStr {(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\"))}
if {![regexp $reStr $json string]} {
- error [ concat "Invalid string: " [string range $json 0 32]
+ error "Invalid string: $json"
}
set json [string range $json [string length $string] end]
# chop off outer ""s and substitute backslashes
list num $num
}
+ proc parse_bare {} {
+ variable json
+ if [regexp {^(true|false|null)} $json matched] {
+ set json [ string range $json [ string length $matched ] end ]
+ return $matched
+ } else {
+ error "Out of ideas parsing: $json"
+ }
+ }
+
proc parse {} {
variable json
eat_spaces
{[-0-9]} { parse_num }
- default { error "argh" }
+ default { parse_bare }
}
}
}
lindex [ ten::json::tclify_json $ex_json ] 2
] ] { puts "$k: $v" }
-#puts [ ten::json::parse_json $jtext ]
+puts [ ten::json::parse_json $jtext ]
puts [ ten::json::parse_json {["foo",2345,["bar"]]} ]