1 namespace eval ten::json {
3 namespace eval deparse {
6 list "\"" "\\\"" / \\/ \\ \\\\ \b \\b \f \\f \n \\n \r \\r \t \\t
16 variable indentBy [ expr $indentBy + $indentIncr ]
17 variable indent [ string repeat " " $indentBy ]
22 variable indentBy [ expr $indentBy - $indentIncr ]
23 variable indent [ string repeat " " $indentBy ]
28 return \"[ string map $quotes $str ]\"
40 foreach el [lrange $args 0 end] {
42 append out [ deparse $el ],
45 append out $nl$indent\]
54 dict for {k v} $args {
55 append out $nl$indent[ str $k ]:\ [ deparse $v ],
58 append out $nl$indent\}
62 switch -regexp [lindex $data 0] {
63 ^true|false|null$ { uplevel 1 return [lindex $data 0] }
65 default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] }
71 proc deparse_json {data {indentIncr 2}} {
72 set deparse::indentBy 0
73 set deparse::indentIncr $indentIncr
74 if [expr $indentIncr == 0] {
79 deparse::deparse $data
82 namespace eval tclify {
84 proc str {str} { return $str }
86 proc num {num} { return $num }
93 switch -regexp [lindex $data 0] {
94 ^true|false|null$ { uplevel 1 return [lindex $data 0] }
96 default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] }
102 proc tclify_json {data} {
107 set ex_json { list {str foo} {num 0} {obj __remote_object__ {str 512}} {null} }
109 puts [ ten::json::deparse_json $ex_json 2 ]
111 dict for {k v} [ ten::json::tclify_json [
112 lindex [ ten::json::tclify_json $ex_json ] 2
113 ] ] { puts "$k: $v" }