webmcp
annotate framework/env/encode/json.lua @ 11:d76a8857ba62
Added ui.partial and other functions, which allow partial content replacement using XMLHttpRequests; Image support for ui.link
Also includes following changes:
- Fix for rocketcgi library to accept POST data content-types, which contain additional charset information.
- Support arrays passed as params to encode.url (only for keys ending with "[]")
- Version information changed to "1.0.7"
Documentation for added functions is not yet complete.
Also includes following changes:
- Fix for rocketcgi library to accept POST data content-types, which contain additional charset information.
- Support arrays passed as params to encode.url (only for keys ending with "[]")
- Version information changed to "1.0.7"
Documentation for added functions is not yet complete.
author | jbe/bsw |
---|---|
date | Fri Feb 12 18:40:22 2010 +0100 (2010-02-12) |
parents | e017c47d43b5 |
children | 32ec28229bb5 |
rev | line source |
---|---|
jbe/bsw@0 | 1 --[[-- |
jbe/bsw@0 | 2 json_string = -- JavaScript code representing the given datum (with quotes, if needed) |
jbe/bsw@0 | 3 encode.json( |
jbe/bsw@0 | 4 obj -- true, false, nil or a number or string |
jbe/bsw@0 | 5 ) |
jbe/bsw@0 | 6 |
jbe@10 | 7 This function encodes any native datatype or atom in JavaScript object notation (JSON). It ensures that the returned string can be safely included in inline scripts both in HTML and XHTML (within CDATA section). |
jbe@10 | 8 |
jbe@1 | 9 TODO: can't distinguish unambiguously between empty object and empty list! |
jbe/bsw@0 | 10 |
jbe/bsw@0 | 11 --]]-- |
jbe/bsw@0 | 12 |
jbe@1 | 13 -- TODO: check if numeric representations are JSON compatible |
jbe@1 | 14 |
jbe/bsw@0 | 15 function encode.json(obj) |
jbe/bsw@0 | 16 if obj == nil then |
jbe/bsw@0 | 17 return "null"; |
jbe/bsw@0 | 18 elseif atom.has_type(obj, atom.boolean) then |
jbe/bsw@0 | 19 return tostring(obj) |
jbe/bsw@0 | 20 elseif atom.has_type(obj, atom.number) then |
jbe/bsw@0 | 21 return tostring(obj) |
jbe@1 | 22 elseif type(obj) == "table" then |
jbe@1 | 23 local parts = {} |
jbe@1 | 24 local first = true |
jbe@1 | 25 if #obj > 0 then |
jbe@1 | 26 parts[#parts+1] = "[" |
jbe@1 | 27 for idx, value in ipairs(obj) do |
jbe@1 | 28 if first then |
jbe@1 | 29 first = false |
jbe@1 | 30 else |
jbe@1 | 31 parts[#parts+1] = "," |
jbe@1 | 32 end |
jbe@1 | 33 parts[#parts+1] = tostring(value) |
jbe@1 | 34 end |
jbe@1 | 35 parts[#parts+1] = "]" |
jbe@1 | 36 else |
jbe@1 | 37 parts[#parts+1] = "{" |
jbe@1 | 38 for key, value in pairs(obj) do |
jbe@1 | 39 if first then |
jbe@1 | 40 first = false |
jbe@1 | 41 else |
jbe@1 | 42 parts[#parts+1] = "," |
jbe@1 | 43 end |
jbe@1 | 44 parts[#parts+1] = encode.json(key) |
jbe@1 | 45 parts[#parts+1] = ":" |
jbe@1 | 46 parts[#parts+1] = encode.json(value) |
jbe@1 | 47 end |
jbe@1 | 48 parts[#parts+1] = "}" |
jbe@1 | 49 end |
jbe@1 | 50 return table.concat(parts) |
jbe/bsw@0 | 51 else |
jbe@10 | 52 local str = atom.dump(obj) |
jbe@10 | 53 str = string.gsub(str, ".", |
jbe@10 | 54 function (char) |
jbe@10 | 55 if char == '\r' then return '\\r' end |
jbe@10 | 56 if char == '\n' then return '\\n' end |
jbe@10 | 57 if char == '\\' then return '\\\\' end |
jbe@10 | 58 if char == '"' then return '\\"' end |
jbe@10 | 59 local byte = string.byte(char) |
jbe@10 | 60 if byte < 32 then return string.format("\\u%04x", byte) end |
jbe@10 | 61 end |
jbe@10 | 62 ) |
jbe@10 | 63 str = string.gsub(str, "</", "<\\/") |
jbe@10 | 64 str = string.gsub(str, "<!%[CDATA%[", "\\u003c![CDATA[") |
jbe@10 | 65 str = string.gsub(str, "]]>", "]]\\u003e") |
jbe@10 | 66 return '"' .. str .. '"' |
jbe/bsw@0 | 67 end |
jbe/bsw@0 | 68 end |