lua-users home
lua-l archive

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


On 04/16/2017 11:47 PM, Dirk Laurie wrote:
> 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
>   end
>   local t = load('return '..do_json_list(s):gsub('("[%w_-]-"):','[%1]='))
>   if t then return t() else return s end
> end
> 
> Please show me some sample JSON code that this decoder can't handle
> properly.
> 
> 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'

I've done some years ago similar thing (>1). Problems with such
implementation are probably same: security and format incompatibilities.
(But this is a nice hack, usually it is faster than all other
JSON-loading tools except lua-cjson. And yes, practically it works
in most cases. So using it is potentially risky but easy.)

Regarding format incompatibilities:

  In JSON strings both "\/" and "/" means "/". Also "\\" means "\".
  So we can't just replace "\/" to "/" as it converts "\\/" (means
  "\/") to "\/" (means "/").

[1]:
https://github.com/martin-eden/workshop/blob/master/formats/json/load/via_hack.lua#L9

-- Martin