9 Feb 20:19
Re: Lua and JSON
Fabio Mascarenhas <mascarenhas <at> acm.org>
2010-02-09 19:19:23 GMT
2010-02-09 19:19:23 GMT
On Tue, Feb 9, 2010 at 4:52 PM, Thomas Harning Jr. <harningt <at> gmail.com> wrote:
Hm... that is a good idea. I would probably have it be something of the sort:On Tue, Feb 9, 2010 at 1:34 PM, Fabio Mascarenhas <mascarenhas <at> acm.org> wrote:
> On Tue, Feb 9, 2010 at 4:17 PM, Thomas Harning Jr. <harningt <at> gmail.com>
> wrote:
>>
>> My LuaJSON projects uses LPeg for parsing and the strict mode passes
>> all tests provided by the JSON validator project from JSON.org The
>> non-strict mode permits some more loose handling of constructs.
>>
>
> +1 for Thomas' module, it works great. Thomas, have you thought about having
> json.encode honor a "__tojson" metamethod (along with a rawencode function),
> so it is easier to extend the encoder to understand app-specific data types?
__tojson(self, rawencode)
rawencode passed in so that you can take advantage of some of the
infinite recursion avoidance, as well as encoding options. If I get
it right, I'd expect that rawencode would skip the __tojson field of
the immediate object and handle inner objects.
My idea was to json.rawencode just what json.encode does right now and ignore __tojson metamethods, just like rawget/rawset/raw*. The __json metamethod could receive the options the user passed to json.encode.
But it is important that __json is a metamethod instead of a regular field, just to keep things idiomatic, even though having to work with metatables can be a drag sometimes. 

--
Fabio
RSS Feed