Mercurial > repos > tabletprog
comparison modules/dict.tp @ 271:bb4723fec05e
Support for encoding objects, dictionaries, lists and arrays to JSON in json module
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 19 Jul 2014 19:59:51 -0700 |
parents | 697c2c562af2 |
children | eef8a5cea812 |
comparison
equal
deleted
inserted
replaced
270:b74956a2196f | 271:bb4723fec05e |
---|---|
1 { | 1 { |
2 _jsonEncode <- :dict { | |
3 parts <- #[] | |
4 foreach: dict :key val { | |
5 //TODO: escape field names | |
6 parts append: (key jsonEncode) . ":" . (json encode: val) | |
7 } | |
8 "{" . (parts join: ",") . "}" | |
9 } | |
2 linearWithEls <- :els { | 10 linearWithEls <- :els { |
3 key:val <- :k v { | 11 key:val <- :k v { |
4 #{ | 12 #{ |
5 key <- k | 13 key <- k |
6 val <- v | 14 val <- v |
75 } | 83 } |
76 linearWithEls: newels | 84 linearWithEls: newels |
77 } | 85 } |
78 | 86 |
79 length <- { els length } | 87 length <- { els length } |
88 | |
89 jsonEncode <- { | |
90 _jsonEncode: self | |
91 } | |
80 } | 92 } |
81 } | 93 } |
82 _empty <- #{ | 94 _empty <- #{ |
83 empty? <- { true } | 95 empty? <- { true } |
84 } | 96 } |
200 if: (not: (el empty?)) { | 212 if: (not: (el empty?)) { |
201 fun: (el k) (el v) | 213 fun: (el k) (el v) |
202 } | 214 } |
203 } | 215 } |
204 } | 216 } |
217 | |
218 jsonEncode <- { | |
219 _jsonEncode: self | |
220 } | |
205 } | 221 } |
206 } | 222 } |
207 | 223 |
208 main <- { | 224 main <- { |
209 d <- hash | 225 d <- hash |