[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Simple Lua-only JSON decoder
- From: Dirk Laurie <dirk.laurie@...>
- Date: Mon, 17 Apr 2017 17:53:19 +0200
I2017-04-17 16:22 GMT+02:00 Soni L. <fakedme@gmail.com>:
>
>
> On 2017-04-17 03:47 AM, 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 don't think { "\":": {} } works... Haven't tested it tho.
It also defeats rapidjson, so at least I am in good company :-)