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$ { lindex $data 0 }
64 ^num|str|obj|list$ { eval $data }
65 default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] }
70 proc deparse_json {data {indentIncr 2}} {
71 set deparse::indentBy 0
72 set deparse::indentIncr $indentIncr
73 if [expr $indentIncr == 0] {
78 deparse::deparse $data
81 namespace eval tclify {
83 proc str {str} { return $str }
85 proc num {num} { return $num }
92 switch -regexp [lindex $data 0] {
93 ^true|false|null$ { uplevel 1 return [lindex $data 0] }
95 default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] }
101 proc tclify_json {data} {
105 namespace eval parse {
111 set json [string trimleft $json]
114 proc eat_char {char} {
117 if {[string index $json 0] eq "$char"} {
124 set json [ string range $json 1 end ]
131 while {"$json" ne ""} {
132 if {[string index $json 0] eq "]"} {
136 lappend tcl [ parse ]
139 error "Ran out of JSON. Confused now."
146 while {"$json" ne ""} {
147 if {[string index $json 0] eq "]"} {
152 lappend tcl [ parse ]
155 error "Ran out of JSON. Confused now."
160 # like Text::Balanced except ugly (borrowed from tcvJSON's code)
161 set reStr {(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\"))}
162 if {![regexp $reStr $json string]} {
163 error [ concat "Invalid string: " [string range $json 0 32]
165 set json [string range $json [string length $string] end]
166 # chop off outer ""s and substitute backslashes
167 # This does more than the RFC-specified backslash sequences,
168 # but it does cover them all
169 list str [subst -nocommand -novariable [string range $string 1 end-1]]
174 string is double -failindex last $json
176 error "Saw number but wasn't - $json"
178 set num [string range $json 0 [expr {$last - 1}]]
179 set json [string range $json $last end]
189 switch -regexp [string index $json 0] {
194 {[-0-9]} { parse_num }
196 default { error "argh" }
201 proc parse_json {json} {
202 set parse::json [ string trim $json ]
203 set result [ parse::parse ]
205 if {$parse::json ne ""} {
206 error "Had JSON left over: $parse::json"
212 set ex_json { list {str foo} {num 0} {obj __remote_object__ {str 512}} {null} }
219 "__remote_object__": "512",
225 puts [ ten::json::deparse_json $ex_json 2 ]
227 dict for {k v} [ ten::json::tclify_json [
228 lindex [ ten::json::tclify_json $ex_json ] 2
229 ] ] { puts "$k: $v" }
231 #puts [ ten::json::parse_json $jtext ]
233 puts [ ten::json::parse_json {["foo",2345,["bar"]]} ]