Smallest Lua-only single-file JSON parser

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Smallest Lua-only single-file JSON parser

Alexander Gladysh
Hi, list!

I need to parse JSON in Lua-only environment.

Is there some primitive single-file JSON parser that I can paste
directly into my script? (Doh, no multi-file support as well...)

Thanks,
Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Matthew Wild
On 26 October 2011 15:22, Alexander Gladysh <[hidden email]> wrote:
> Hi, list!
>
> I need to parse JSON in Lua-only environment.
>
> Is there some primitive single-file JSON parser that I can paste
> directly into my script? (Doh, no multi-file support as well...)
>

I thought you'd never ask.

MIT-licensed (see project root directory, though I can add a copyright
header if you require that):
http://hg.prosody.im/trunk/file/tip/util/json.lua

The decoder starts after line 132.

Regards,
Matthew

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Norbert Kiesel
On Wed, 2011-10-26 at 17:45 -0400, Matthew Wild wrote:

> On 26 October 2011 15:22, Alexander Gladysh <[hidden email]> wrote:
> > Hi, list!
> >
> > I need to parse JSON in Lua-only environment.
> >
> > Is there some primitive single-file JSON parser that I can paste
> > directly into my script? (Doh, no multi-file support as well...)
> >
>
> I thought you'd never ask.
>
> MIT-licensed (see project root directory, though I can add a copyright
> header if you require that):
> http://hg.prosody.im/trunk/file/tip/util/json.lua
>
> The decoder starts after line 132.
>
> Regards,
> Matthew
>

Shouldn't the * be a / in line 170?
168 if ch == "/" and peek == "*" then
169 skipstarcomment();
170 elseif ch == "/" and peek == "*" then
171 skiplinecomment();
172 else

</nk>



Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Matthew Wild
On 26 October 2011 18:49, Norbert Kiesel <[hidden email]> wrote:
>

> Shouldn't the * be a / in line 170?
> 168                     if ch == "/" and peek == "*" then
> 169                             skipstarcomment();
> 170                     elseif ch == "/" and peek == "*" then
> 171                             skiplinecomment();
> 172                     else
>

Fixed and pushed, thanks :)

Regards,
Matthew
Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Marc Balmer
In reply to this post by Alexander Gladysh




Am 26.10.2011 um 21:22 schrieb Alexander Gladysh <[hidden email]>:

> Hi, list!
>
> I need to parse JSON in Lua-only environment.
>
> Is there some primitive single-file JSON parser that I can paste
> directly into my script? (Doh, no multi-file support as well...)

I have a JSON module written in C, so I think that is not what you are after, right?

>
> Thanks,
> Alexander.
>

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Egil Hjelmeland
In reply to this post by Alexander Gladysh
On 2011-10-26 21:22, Alexander Gladysh wrote:

> Hi, list!
>
> I need to parse JSON in Lua-only environment.
>
> Is there some primitive single-file JSON parser that I can paste
> directly into my script? (Doh, no multi-file support as well...)
>
> Thanks,
> Alexander.
>
>
I used dkjosn:
http://chiselapp.com/user/dhkolf/repository/dkjson/home
I chose it because it can encode with indenting

Egil


Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Matthias Diener
In reply to this post by Alexander Gladysh
On Wed, Oct 26, 2011 at 11:22:40PM +0400, Alexander Gladysh wrote:
> Hi, list!
>
> I need to parse JSON in Lua-only environment.
>
> Is there some primitive single-file JSON parser that I can paste
> directly into my script? (Doh, no multi-file support as well...)
>

There is a comparison in the wiki:
http://lua-users.org/wiki/JsonModules

@Matthew: yours is missing, maybe that's the reason I choosed dkjson ;)

Regards,
Matthias

--
Matthias Diener -- +49 171-5352704 -- http://macarony.de


Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Alexander Gladysh
In reply to this post by Marc Balmer
On Thu, Oct 27, 2011 at 09:58, Marc Balmer <[hidden email]> wrote:

> Am 26.10.2011 um 21:22 schrieb Alexander Gladysh <[hidden email]>:
>
>> Hi, list!
>>
>> I need to parse JSON in Lua-only environment.
>>
>> Is there some primitive single-file JSON parser that I can paste
>> directly into my script? (Doh, no multi-file support as well...)
>
> I have a JSON module written in C, so I think that is not what you are after, right?

Sadly, no. :-(

Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Alexander Gladysh
In reply to this post by Egil Hjelmeland
On Thu, Oct 27, 2011 at 10:55, Egil Hjelmeland
<[hidden email]> wrote:
> On 2011-10-26 21:22, Alexander Gladysh wrote:

>> I need to parse JSON in Lua-only environment.

>> Is there some primitive single-file JSON parser that I can paste
>> directly into my script? (Doh, no multi-file support as well...)

> I used dkjosn:
> http://chiselapp.com/user/dhkolf/repository/dkjson/home
> I chose it because it can encode with indenting

Thanks! I'll give it a try.

Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Alexander Gladysh
In reply to this post by Matthew Wild
On Thu, Oct 27, 2011 at 01:45, Matthew Wild <[hidden email]> wrote:
> On 26 October 2011 15:22, Alexander Gladysh <[hidden email]> wrote:

>> I need to parse JSON in Lua-only environment.
>>
>> Is there some primitive single-file JSON parser that I can paste
>> directly into my script? (Doh, no multi-file support as well...)

> I thought you'd never ask.
>
> MIT-licensed (see project root directory, though I can add a copyright
> header if you require that):
> http://hg.prosody.im/trunk/file/tip/util/json.lua

> The decoder starts after line 132.

Thank you! I'll give it a try as well!

Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Alexander Gladysh
On Thu, Oct 27, 2011 at 13:50, Alexander Gladysh <[hidden email]> wrote:

> On Thu, Oct 27, 2011 at 01:45, Matthew Wild <[hidden email]> wrote:
>> On 26 October 2011 15:22, Alexander Gladysh <[hidden email]> wrote:
>
>>> I need to parse JSON in Lua-only environment.
>>>
>>> Is there some primitive single-file JSON parser that I can paste
>>> directly into my script? (Doh, no multi-file support as well...)
>
>> I thought you'd never ask.
>>
>> MIT-licensed (see project root directory, though I can add a copyright
>> header if you require that):
>> http://hg.prosody.im/trunk/file/tip/util/json.lua
>
>> The decoder starts after line 132.
>
> Thank you! I'll give it a try as well!

For the record: ended up using this one, as it is the shortest.

Thanks again,
Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Duncan Cross
On Thu, Oct 27, 2011 at 7:34 PM, Alexander Gladysh <[hidden email]> wrote:

> On Thu, Oct 27, 2011 at 13:50, Alexander Gladysh <[hidden email]> wrote:
>> On Thu, Oct 27, 2011 at 01:45, Matthew Wild <[hidden email]> wrote:
>>> On 26 October 2011 15:22, Alexander Gladysh <[hidden email]> wrote:
>>
>>>> I need to parse JSON in Lua-only environment.
>>>>
>>>> Is there some primitive single-file JSON parser that I can paste
>>>> directly into my script? (Doh, no multi-file support as well...)
>>
>>> I thought you'd never ask.
>>>
>>> MIT-licensed (see project root directory, though I can add a copyright
>>> header if you require that):
>>> http://hg.prosody.im/trunk/file/tip/util/json.lua
>>
>>> The decoder starts after line 132.
>>
>> Thank you! I'll give it a try as well!
>
> For the record: ended up using this one, as it is the shortest.
>
> Thanks again,
> Alexander.
>
>

I'm not seriously advocating this approach, but I was inspired to
write this proof-of-concept minimal module that uses Lua's own parser:

http://pastebin.com/H3gPsEeC

It didn't end up much shorter than the one you already have, but I
hope it's interesting to someone, at least. :)

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Duncan Cross
On Thu, Oct 27, 2011 at 11:11 PM, Duncan Cross <[hidden email]> wrote:

> On Thu, Oct 27, 2011 at 7:34 PM, Alexander Gladysh <[hidden email]> wrote:
>> On Thu, Oct 27, 2011 at 13:50, Alexander Gladysh <[hidden email]> wrote:
>>> On Thu, Oct 27, 2011 at 01:45, Matthew Wild <[hidden email]> wrote:
>>>> On 26 October 2011 15:22, Alexander Gladysh <[hidden email]> wrote:
>>>
>>>>> I need to parse JSON in Lua-only environment.
>>>>>
>>>>> Is there some primitive single-file JSON parser that I can paste
>>>>> directly into my script? (Doh, no multi-file support as well...)
>>>
>>>> I thought you'd never ask.
>>>>
>>>> MIT-licensed (see project root directory, though I can add a copyright
>>>> header if you require that):
>>>> http://hg.prosody.im/trunk/file/tip/util/json.lua
>>>
>>>> The decoder starts after line 132.
>>>
>>> Thank you! I'll give it a try as well!
>>
>> For the record: ended up using this one, as it is the shortest.
>>
>> Thanks again,
>> Alexander.
>>
>>
>
> I'm not seriously advocating this approach, but I was inspired to
> write this proof-of-concept minimal module that uses Lua's own parser:
>
> http://pastebin.com/H3gPsEeC
>
> It didn't end up much shorter than the one you already have, but I
> hope it's interesting to someone, at least. :)
>
> -Duncan
>

Sorry, I accidentally put up the wrong (broken) version. This is the
correct one:

http://pastebin.com/ZsE9BvQM

-Duncan

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Patrick Rapin
As an exercise, I tried to wrote the really minimum JSON parser.
It is below (only 10 lines of code)...
It first converts the JSON snipped into a valid Lua expression using
string patterns, then evaluates it.
Bugs apart, the only missing feature is the \uXXXX string escape.

function decode_json(json)
        local str = {}
        local escapes = { r='\r', n='\n', b='\b', f='\f', t='\t', Q='"',
['\\'] = '\\', ['/']='/' }
        json = json:gsub('([^\\])\\"', '%1\\Q'):gsub('"(.-)"', function(s)
                str[#str+1] = s:gsub("\\(.)", function(c) return escapes[c] end)
                return "$"..#str
        end):gsub("%s", ""):gsub("%[","{"):gsub("%]","}"):gsub("null", "nil")
        json = json:gsub("(%$%d+):", "[%1]="):gsub("%$(%d+)", function(s)
                return ("%q"):format(str[tonumber(s)])
        end)
        return assert(loadstring("return "..json))()
end

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Alexander Gladysh
Duncan, Patrick,

That's an interesting approach!

On Fri, Oct 28, 2011 at 16:14, Patrick Rapin <[hidden email]> wrote:
> As an exercise, I tried to wrote the really minimum JSON parser.
> It is below (only 10 lines of code)...
> It first converts the JSON snipped into a valid Lua expression using
> string patterns, then evaluates it.
> Bugs apart, the only missing feature is the \uXXXX string escape.

Is that hard to add?

> function decode_json(json)
>        local str = {}
>        local escapes = { r='\r', n='\n', b='\b', f='\f', t='\t', Q='"',
> ['\\'] = '\\', ['/']='/' }
>        json = json:gsub('([^\\])\\"', '%1\\Q'):gsub('"(.-)"', function(s)
>                str[#str+1] = s:gsub("\\(.)", function(c) return escapes[c] end)

I believe that you o not need a function here, just pass `escapes`
here. Or even put the table directly, to shorten further.

>                return "$"..#str
>        end):gsub("%s", ""):gsub("%[","{"):gsub("%]","}"):gsub("null", "nil")
>        json = json:gsub("(%$%d+):", "[%1]="):gsub("%$(%d+)", function(s)
>                return ("%q"):format(str[tonumber(s)])
>        end)
>        return assert(loadstring("return "..json))()
> end

Anyone knows a good JSON compatibility test suite for the parser?

Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Patrick Rapin
> That's an interesting approach!

I already used such a trick to implement little parsers, but that was in Perl !

>> Bugs apart, the only missing feature is the \uXXXX string escape.
> Is that hard to add?

Not really, but this will probably double the code size... This is
merely an additional gsub(...,function), encoding the numeric value
into UTF-8.

> I believe that you o not need a function here, just pass `escapes`
> here. Or even put the table directly, to shorten further.

You are right. I forgot about that feature of string.gsub.

> Anyone knows a good JSON compatibility test suite for the parser?

I have not searched for that, and only performed a minimum test.
If you seriously think about using that code, you will have to test it
more thoroughly.

Reply | Threaded
Open this post in threaded view
|

Re: Smallest Lua-only single-file JSON parser

Mark Pulford
In reply to this post by Alexander Gladysh
Hi,

On Fri, Oct 28, 2011 at 09:42:02PM +0400, Alexander Gladysh wrote:
> Anyone knows a good JSON compatibility test suite for the parser?

I don't claim that is it particularly good, but I include a bunch of
tests for Lua CJSON.

        http://www.kyne.com.au/~mark/software/lua-cjson.php

It should be fairly simple to use/adapt them to a different parser. Feel
free to suggest/provide improvements :).

Regards,
Mark