From: Matt S Trout Date: Tue, 19 Jun 2012 16:42:30 +0000 (+0000) Subject: tclify command X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a001e5b2a382980d9f453c38d83db367e15c27a5;p=scpubgit%2FTenDotTcl.git tclify command --- diff --git a/json.tcl b/json.tcl index 7b0e3c0..746a8df 100644 --- a/json.tcl +++ b/json.tcl @@ -76,10 +76,38 @@ namespace eval ten::json { } else { set deparse::nl "\n" } - namespace inscope deparse eval $data + deparse::deparse $data + } + + namespace eval tclify { + + proc str {str} { return $str } + + proc num {num} { return $num } + + proc obj {args} { + return $args + } + + proc tclify {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 $data ] + } + } + + proc tclify_json {data} { + tclify::tclify $data } } -puts [ ten::json::deparse_json { - list {str foo} {num 0} {obj __remote_object__ {str 512}} {null} -} 2 ] +set ex_json { list {str foo} {num 0} {obj __remote_object__ {str 512}} {null} } + +puts [ ten::json::deparse_json $ex_json 2 ] + +dict for {k v} [ ten::json::tclify_json [ + lindex [ ten::json::tclify_json $ex_json ] 2 +] ] { puts "$k: $v" }