Native JSON-parsing implementations

317 views
Skip to first unread message

Mikhail T.

unread,
Jan 14, 2014, 2:00:46 PM1/14/14
to
The existing json2dict is painfully slow -- perhaps, because it is implemented
in pure Tcl. Needing to parse large (20-60Mb) JSON files I wrote my own... First
as simply a wrapper around the json-c library:

http://aldan.algebra.com/~mi/tcljson.c

This implementation uses json-c for actual parsing (which creates a tree of
json-c's own objects) and then lazily translates chunks of that tree into proper
Tcl_Objects. This is about 15 times faster than json2dict found in tcllib can do.

But it requires a 3rd-party library and uses additional memory (and time) -- for
that library's own object-tree, which then needs to be translated... So I
implemented my own parser from scratch -- using the grammar posted on json.org

Here is my tcljson.y (requires a modern yacc or bison -- no lex). Maybe, some
day it will become part of tcllib, but in the mean time you are welcome to test
it and/or use it. Made available under BSD-license:

http://aldan.algebra.com/~mi/tcljson.y

With this, my own scripts become even faster than their equivalents written in
PHP (where JSON-parsing is native).

-mi

Sp...@controlq.com

unread,
Jan 14, 2014, 4:25:57 PM1/14/14
to
On Tue, 14 Jan 2014, Mikhail T. wrote:

> Date: Tue, 14 Jan 2014 14:00:46 -0500
> From: Mikhail T. <mi+use...@aldan.algebra.com>
> Newsgroups: comp.lang.tcl
> Subject: Native JSON-parsing implementations
This is a nice bit of tcl extending 8-) ...

Cheers,
Rob.

Andreas Kupries

unread,
Jan 22, 2014, 2:37:48 AM1/22/14
to
"Mikhail T." <mi+use...@aldan.algebra.com> writes:

> The existing json2dict is painfully slow -- perhaps, because it is
> implemented in pure Tcl. Needing to parse large (20-60Mb) JSON files
> I wrote my own... First as simply a wrapper around the json-c
> library:
> ... So I implemented my own parser from scratch -- using
> the grammar posted on json.org
>
> Here is my tcljson.y (requires a modern yacc or bison -- no
> lex). Maybe, some day it will become part of tcllib,

http://core.tcl.tk/tcllib/info/11390a7baa

(plus fixes for errors I made in the integration above)
http://core.tcl.tk/tcllib/info/2d0b61da55
http://core.tcl.tk/tcllib/info/09ed31fbfc

> but in the mean time you are welcome to test
> it and/or use it. Made available under BSD-license:
>
> http://aldan.algebra.com/~mi/tcljson.y
>
> With this, my own scripts become even faster than their equivalents
> written in PHP (where JSON-parsing is native).
>
> -mi

--
So long,
Andreas Kupries <akup...@shaw.ca>
<http://www.purl.org/NET/akupries/>
Developer @ <http://www.activestate.com/>

EuroTcl 2014, July 12-13, Munich/DE, http://www.eurotcl.tcl3d.org/
GSoC 2014, add your ideas to http://wiki.tcl.tk/36464
-------------------------------------------------------------------------------

haze...@gmail.com

unread,
Apr 19, 2015, 3:43:00 AM4/19/15
to
The url http://aldan.algebra.com/~mi/tcljson.y is not available, Can you please share the code with me, or fix the link please.

Thank you
Reply all
Reply to author
Forward
0 new messages