lua-users home
lua-l archive

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


On Tue, Feb 9, 2010 at 4:52 PM, Thomas Harning Jr. <harningt@gmail.com> wrote:
On Tue, Feb 9, 2010 at 1:34 PM, Fabio Mascarenhas <mascarenhas@acm.org> wrote:
> On Tue, Feb 9, 2010 at 4:17 PM, Thomas Harning Jr. <harningt@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?
Hm... that is a good idea.  I would probably have it be something of the sort:

 __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