lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

My primitive JSON decoder, which operates by lexically translating
a JSON code to a Lua table literal, now does three things:

1. Unicode codepoints translated, e.g. from \u00e9 to \u{00e9}.
2. List delimiters translated from […] to {…}.
3. Keys translated e.g. from "item": to ["item"]=.

local function json_decode (s)
  s = s:gsub("\\u(%d%d%d%d)","\\u{%1}")
  local function do_json_list(s,is_list)
    if is_list then s = s:sub(2,-2) end
    s = s:gsub("(%b[])()",do_json_list)
    if is_list then s = '{' .. s ..'}' end
    return s
  local t = load('return '..do_json_list(s):gsub('("[%w_-]-"):','[%1]='))
  if t then return t() else return s end

Please show me some sample JSON code that this decoder can't handle

I already know about:
  1. Integer overflow. (Thanks, Sean!) --> won't happen
  2. JSON null is not Lua nil. --> define a suitable global called 'null'