webmcp
diff framework/env/encode/json.lua @ 1:985024b16520
Version 1.0.1
New feature: JSON requests
Changes in ui.paginate: Current page setting is directly fetched from CGI params, instead of view params
Changed behavior of load methods of atom library to accept nil as input
Bugfixes in mondelefant_atom_connector timestamp(tz) loaders
Added global constant _WEBMCP_VERSION containing a version string
New feature: JSON requests
Changes in ui.paginate: Current page setting is directly fetched from CGI params, instead of view params
Changed behavior of load methods of atom library to accept nil as input
Bugfixes in mondelefant_atom_connector timestamp(tz) loaders
Added global constant _WEBMCP_VERSION containing a version string
author | jbe |
---|---|
date | Tue Nov 17 12:00:00 2009 +0100 (2009-11-17) |
parents | 9fdfb27f8e67 |
children | e017c47d43b5 |
line diff
1.1 --- a/framework/env/encode/json.lua Sun Oct 25 12:00:00 2009 +0100 1.2 +++ b/framework/env/encode/json.lua Tue Nov 17 12:00:00 2009 +0100 1.3 @@ -5,9 +5,12 @@ 1.4 ) 1.5 1.6 This function encodes any native datatype or atom in JavaScript object notation (JSON). 1.7 +TODO: can't distinguish unambiguously between empty object and empty list! 1.8 1.9 --]]-- 1.10 1.11 +-- TODO: check if numeric representations are JSON compatible 1.12 + 1.13 function encode.json(obj) 1.14 if obj == nil then 1.15 return "null"; 1.16 @@ -15,20 +18,49 @@ 1.17 return tostring(obj) 1.18 elseif atom.has_type(obj, atom.number) then 1.19 return tostring(obj) 1.20 + elseif type(obj) == "table" then 1.21 + local parts = {} 1.22 + local first = true 1.23 + if #obj > 0 then 1.24 + parts[#parts+1] = "[" 1.25 + for idx, value in ipairs(obj) do 1.26 + if first then 1.27 + first = false 1.28 + else 1.29 + parts[#parts+1] = "," 1.30 + end 1.31 + parts[#parts+1] = tostring(value) 1.32 + end 1.33 + parts[#parts+1] = "]" 1.34 + else 1.35 + parts[#parts+1] = "{" 1.36 + for key, value in pairs(obj) do 1.37 + if first then 1.38 + first = false 1.39 + else 1.40 + parts[#parts+1] = "," 1.41 + end 1.42 + parts[#parts+1] = encode.json(key) 1.43 + parts[#parts+1] = ":" 1.44 + parts[#parts+1] = encode.json(value) 1.45 + end 1.46 + parts[#parts+1] = "}" 1.47 + end 1.48 + return table.concat(parts) 1.49 else 1.50 return 1.51 - "'" .. 1.52 + '"' .. 1.53 string.gsub(atom.dump(obj), ".", 1.54 function (char) 1.55 - if char == "\r" then return "\\r" end 1.56 - if char == "\n" then return "\\n" end 1.57 - if char == "\\" then return "\\\\" end 1.58 - if char == "'" then return "\\'" end 1.59 - if char == "/" then return "\\/" end -- allowed according to RFC4627, needed for </script> 1.60 + if char == '\r' then return '\\r' end 1.61 + if char == '\n' then return '\\n' end 1.62 + if char == '\\' then return '\\\\' end 1.63 + if char == '"' then return '\\"' end 1.64 + if char == '/' then return '\\/' end -- allowed according to RFC4627, needed for </script> 1.65 local byte = string.byte(char) 1.66 if byte < 32 then return string.format("\\u%04x", byte) end 1.67 end 1.68 ) .. 1.69 - "'" 1.70 + '"' 1.71 end 1.72 end