webmcp

changeset 285:aacddd07e471

encode.json{...} does not support atoms anymore but uses the JSON library
author jbe
date Sun Mar 22 02:09:49 2015 +0100 (2015-03-22)
parents 851452af0c36
children fc2aba7d5db9
files framework/env/encode/json.lua
line diff
     1.1 --- a/framework/env/encode/json.lua	Sun Mar 22 01:45:51 2015 +0100
     1.2 +++ b/framework/env/encode/json.lua	Sun Mar 22 02:09:49 2015 +0100
     1.3 @@ -1,70 +1,19 @@
     1.4  --[[--
     1.5 -json_string =  -- JavaScript code representing the given datum (with quotes, if needed)
     1.6 +json_string =  -- JavaScript code
     1.7  encode.json(
     1.8 -  obj          -- true, false, nil or a number or string
     1.9 +  value        -- nil, false, true, a number, a string, or json.array{...} or json.object{...}
    1.10  )
    1.11  
    1.12 -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).
    1.13 +This function encodes any native datatype or table structure to JavaScript object notation (JSON). In order to distinguish between the empty array and the empty object, use json.array{} and json.object{} respectively.
    1.14  
    1.15 -TODO: can't distinguish unambiguously between empty object and empty list!
    1.16 -
    1.17 -TODO: replace with JSON library
    1.18 +The return value of this function is additionally escaped in such way that it can be safely included in inline scripts both in HTML and XHTML (within CDATA section).
    1.19  
    1.20  --]]--
    1.21  
    1.22 --- TODO: check if numeric representations are JSON compatible
    1.23 -
    1.24  function encode.json(obj)
    1.25 -  if obj == nil then
    1.26 -    return "null";
    1.27 -  elseif atom.has_type(obj, atom.boolean) then
    1.28 -    return tostring(obj)
    1.29 -  elseif atom.has_type(obj, atom.number) then
    1.30 -    return tostring(obj)
    1.31 -  elseif type(obj) == "table" then
    1.32 -    local parts = {}
    1.33 -    local first = true
    1.34 -    if #obj > 0 then
    1.35 -      parts[#parts+1] = "["
    1.36 -      for idx, value in ipairs(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] = tostring(value)
    1.43 -      end
    1.44 -      parts[#parts+1] = "]"
    1.45 -    else
    1.46 -      parts[#parts+1] = "{"
    1.47 -      for key, value in pairs(obj) do
    1.48 -        if first then
    1.49 -          first = false
    1.50 -        else
    1.51 -          parts[#parts+1] = ","
    1.52 -        end
    1.53 -        parts[#parts+1] = encode.json(key)
    1.54 -        parts[#parts+1] = ":"
    1.55 -        parts[#parts+1] = encode.json(value)
    1.56 -      end
    1.57 -      parts[#parts+1] = "}"
    1.58 -    end
    1.59 -    return table.concat(parts)
    1.60 -  else
    1.61 -    local str = atom.dump(obj)
    1.62 -    str = string.gsub(str, ".",
    1.63 -      function (char)
    1.64 -        if char == '\r' then return '\\r'  end
    1.65 -        if char == '\n' then return '\\n'  end
    1.66 -        if char == '\\' then return '\\\\' end
    1.67 -        if char == '"'  then return '\\"'  end
    1.68 -        local byte = string.byte(char)
    1.69 -        if byte < 32 then return string.format("\\u%04x", byte) end
    1.70 -      end
    1.71 -    )
    1.72 -    str = string.gsub(str, "</", "<\\/")
    1.73 -    str = string.gsub(str, "<!%[CDATA%[", "\\u003c![CDATA[")
    1.74 -    str = string.gsub(str, "]]>", "]]\\u003e")
    1.75 -    return '"' .. str .. '"'
    1.76 -  end
    1.77 +  str = json.export(obj)
    1.78 +  str = string.gsub(str, "</", "<\\/")
    1.79 +  str = string.gsub(str, "<!%[CDATA%[", "\\u003c![CDATA[")
    1.80 +  str = string.gsub(str, "]]>", "]]\\u003e")
    1.81 +  return str
    1.82  end

Impressum / About Us