From: Matt S Trout Date: Tue, 19 Jun 2012 16:34:43 +0000 (+0000) Subject: via deparse function X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ced6e98eb073039f471c06906403ec49d1f90059;p=scpubgit%2FTenDotTcl.git via deparse function --- diff --git a/json.tcl b/json.tcl index 659c152..7b0e3c0 100644 --- a/json.tcl +++ b/json.tcl @@ -39,7 +39,7 @@ namespace eval ten::json { indent_one foreach el [lrange $args 0 end] { append out $nl$indent - append out [ eval $el ], + append out [ deparse $el ], } outdent_one append out $nl$indent\] @@ -52,18 +52,19 @@ namespace eval ten::json { set out \{ indent_one dict for {k v} $args { - append out $nl$indent[ str $k ]:\ [ eval $v ], + append out $nl$indent[ str $k ]:\ [ deparse $v ], } outdent_one append out $nl$indent\} } - proc deparse {args} { - switch -regexp [lindex $args 0] { - ^num|str|obj|list$ - - default { error "Invalid JSON type [lindex $args 0]" } + proc deparse {data} { + switch -regexp [lindex $data 0] { + ^true|false|null$ { uplevel 1 return [lindex $data 0] } + ^num|str|obj|list$ {} + default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] } } - return [ eval $args ] + return [ eval $data ] } } @@ -80,5 +81,5 @@ namespace eval ten::json { } puts [ ten::json::deparse_json { - list {str foo} {num 0} {obj __remote_object__ {str 512}} -} 0 ] + list {str foo} {num 0} {obj __remote_object__ {str 512}} {null} +} 2 ]