comparison modules/json.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 9d8ae39e8e67
children bb2b4613fdc8
comparison
equal deleted inserted replaced
270:b74956a2196f 271:bb4723fec05e
161 } 161 }
162 #{ 162 #{
163 decode <- :text { 163 decode <- :text {
164 (_decode: text at: 0) value 164 (_decode: text at: 0) value
165 } 165 }
166
167 encode <- :value {
168 if: (object does: value understand?: "jsonEncode") {
169 value jsonEncode
170 } else: {
171 toEncode <- #[]
172 if: (object does: value understand?: "serializeFields") {
173 toEncode <- value serializeFields
174 } else: {
175 toEncode <- object propertiesOf: value
176 }
177 parts <- #[]
178 foreach: toEncode :idx field {
179 fieldVal <- object sendMessage: field to: value
180 parts append: (field jsonEncode) . ":" . (encode: fieldVal)
181 }
182 "{" . (parts join: ",") . "}"
183 }
184 }
185
186 main <- {
187 o <- #{
188 foo <- "bar"
189 baz <- ["fizz" "buzz" "buzzzz"]
190 qux <- ((dict hash) set: "fo" "shizzle") set: "my" "nizzle"
191 arr <- #["pirate" "booty"]
192 }
193 print: (encode: o) . "\n"
194 0
195 }
166 } 196 }
167 } 197 }