number parsing and nested array
[scpubgit/TenDotTcl.git] / json.tcl
1 namespace eval ten::json {
2
3   namespace eval deparse {
4
5     variable quotes [
6       list "\"" "\\\"" / \\/ \\ \\\\ \b \\b \f \\f \n \\n \r \\r \t \\t
7     ]
8
9     variable indent
10     variable indentBy 0
11     variable indentIncr
12     variable nl
13
14     proc indent_one {} {
15       variable indentIncr
16       variable indentBy [ expr $indentBy + $indentIncr ]
17       variable indent [ string repeat " " $indentBy ]
18     }
19
20     proc outdent_one {} {
21       variable indentIncr
22       variable indentBy [ expr $indentBy - $indentIncr ]
23       variable indent [ string repeat " " $indentBy ]
24     }
25
26     proc str {str} {
27       variable quotes
28       return \"[ string map $quotes $str ]\"
29     }
30
31     proc num {num} {
32       return $num
33     }
34
35     proc list {args} {
36       variable indent
37       variable nl
38       set out \[
39       indent_one
40       foreach el [lrange $args 0 end] {
41         append out $nl$indent
42         append out [ deparse $el ],
43       }
44       outdent_one
45       append out $nl$indent\]
46       return $out
47     }
48
49     proc obj {args} {
50       variable indent
51       variable nl
52       set out \{
53       indent_one
54       dict for {k v} $args {
55         append out $nl$indent[ str $k ]:\ [ deparse $v ],
56       }
57       outdent_one
58       append out $nl$indent\}
59     }
60
61     proc deparse {data} {
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] ] }
66       }
67     }
68   }
69
70   proc deparse_json {data {indentIncr 2}} {
71     set deparse::indentBy 0
72     set deparse::indentIncr $indentIncr
73     if [expr $indentIncr == 0] {
74       set deparse::nl ""
75     } else {
76       set deparse::nl "\n"
77     }
78     deparse::deparse $data
79   }
80
81   namespace eval tclify {
82
83     proc str {str} { return $str }
84
85     proc num {num} { return $num }
86
87     proc obj {args} {
88       return $args
89     }
90
91     proc tclify {data} {
92       switch -regexp [lindex $data 0] {
93         ^true|false|null$ { uplevel 1 return [lindex $data 0] }
94         ^num|str|obj|list$ {}
95         default { error [ concat "Invalid JSON type " [lindex $data 0 0] ] }
96       }
97       eval $data
98     }
99   }
100
101   proc tclify_json {data} {
102     tclify::tclify $data
103   }
104
105   namespace eval parse {
106
107     variable json
108
109     proc eat_comma {} {
110       variable json
111       set json [string trimleft $json]
112       if {[string index $json 0] eq ","} {
113         set json [string range $json 1 end]
114       }
115     }
116
117     proc eat_char {} {
118       variable json
119       set json [ string range $json 1 end ]
120     }
121
122     proc parse_list {} {
123       variable json
124       eat_char
125       set tcl {list}
126       while {"$json" ne ""} {
127         if {[string index $json 0] eq "]"} {
128           eat_char
129           return $tcl
130         }
131         lappend tcl [ parse ]
132         eat_comma
133       }
134       error "Ran out of JSON. Confused now."
135     }
136
137     proc parse_str {} {
138       variable json
139       # like Text::Balanced except ugly (borrowed from tcvJSON's code)
140       set reStr {(?:(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\"))}
141       if {![regexp $reStr $json string]} {
142         error [ concat "Invalid string: " [string range $json 0 32]
143       }
144       set json [string range $json [string length $string] end]
145       # chop off outer ""s and substitute backslashes
146       # This does more than the RFC-specified backslash sequences,
147       # but it does cover them all
148       list str [subst -nocommand -novariable [string range $string 1 end-1]]
149     }
150
151     proc parse_num {} {
152       variable json
153       string is double -failindex last $json
154       if {$last == 0} {
155         error "Saw number but wasn't - $json"
156       }
157       set num [string range $json 0 [expr {$last - 1}]]
158       set json [string range $json $last end]
159       list num $num
160     }
161
162     proc parse {} {
163       variable json
164       set json [string trimleft $json]
165       if {$json eq ""} {
166         return
167       }
168       switch -regexp [string index $json 0] {
169         {\{} { parse_obj }
170         {\[} { parse_list }
171         {\"} { parse_str }
172
173         {[-0-9]} { parse_num }
174
175         default { error "argh" }
176       }
177     }
178   }
179
180   proc parse_json {json} {
181     set parse::json [ string trim $json ]
182     set result [ parse::parse ]
183     if {[string trimleft $parse::json] ne ""} {
184       error "Had JSON left over: $parse::json"
185     }
186     return $result
187   }
188 }
189
190 set ex_json { list {str foo} {num 0} {obj __remote_object__ {str 512}} {null} }
191
192 set jtext {
193   [
194     "foo",
195     0,
196     {
197       "__remote_object__": "512",
198     },
199     null,
200   ]
201 }
202
203 puts [ ten::json::deparse_json $ex_json 2 ]
204
205 dict for {k v} [ ten::json::tclify_json [
206   lindex [ ten::json::tclify_json $ex_json ] 2
207 ] ] { puts "$k: $v" }
208
209 #puts [ ten::json::parse_json $jtext ]
210
211 puts [ ten::json::parse_json {["foo",2345,["bar"]]} ]