Mercurial > repos > tabletprog
diff modules/string.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 | b74956a2196f |
children | 9a30510f6e52 |
line wrap: on
line diff
--- a/modules/string.tp Fri Jul 18 20:45:50 2014 -0700 +++ b/modules/string.tp Sat Jul 19 19:59:51 2014 -0700 @@ -310,6 +310,35 @@ } } + jsonEncode <- { + i <- 0 + start <- 0 + parts <- #["\""] + q <- "\"" byte: 0 + s <- "\\" byte: 0 + while: { i < byte_length } do: { + b <- byte: i + if: b = q { + parts append: (from: start withLength: i - start) + start <- i + 1 + parts append: "\\\"" + } else: { + if: b = s { + parts append: (from: start withLength: i - start) + start <- i + 1 + parts append: "\\\\" + } + } + + i <- i + 1 + } + if: start < byte_length { + parts append: (from: start) + } + parts append: "\"" + parts join: "" + } + isInteger? <- { false } isString? <- { true } isBasicString? <- { true }