lua-users home
lua-l archive

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



Am 17.04.17 um 08:47 schrieb Dirk Laurie:
> 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.

Does it decode numbers to lua numbers?  Does it handle numeric subtypes?

I have an extensive JSON test suite lying around somewhere, ping me off
list if I should try to find it.

> 
> 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 let the used choose how JSON null values are to be handled (see
https://github.com/arcapos/luajson/blob/master/luajson.c), it can either
map to a json-null object (which has a json-null metatable), to nil, or
to an empty string.  You could reuse that idea in above code.