lua-users home
lua-l archive

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

Jerome Vuarand schrieb:
> 2010/9/8 Brian Maher <>:
>> Thanks for posting the comparison wiki page.  I notice the "protection
>> against bloated arrays when encoding" section.  Does this mean that
>> people desire for this:
>>  {[1000] = "harhar"}
>> To be encoded as this?
>>  {"1000":"harhar"}
>> Instead of encoding it as:
>>  [null,null,...,"harhar"]
>> If this is desired, then I could add support for this in lua-yajl.
>>From a user point of view, that would seem weird to change the key
> type. Another way to handler the situation is to have a non-nil null
> value, and to only accept tables with non-sparse array part or
> otherwise trigger an error. That way the user has to make an explicit
> decision : either use "1000" to get a dictionary, or fill keys 1 to
> 999 with json.null to get an array.

I am still thinking about this. In practice the user shouldn't try to
convert such a sparse array to JSON as there is no exact representation
for it. My encode and decode functions however should be stable no
matter what input they get so they could be used inside a sandbox.
Perhaps returning an error similar to the reference cycle case is indeed
a cleaner solution.

> Another similar comparison point is how each implementation let users
> declare empty arrays vs. empty dictionaries. I think that when
> features overlap between implementations, a little consistency would
> help users decide which one is the best for their situation.

In my implementation empty tables are encoded as JSON arrays. As far as
I understood, a JavaScript array is an object with added functionality
(length and so on) so I thought that accidentally creating an array
instead of an object shouldn't hurt in JavaScript code while the other
case could hurt if functionality is missing for an array.

For creating explicit empty objects using my implementation you would
have to provide your own serializing function:
  emptyobject = setmetatable ({},  {__tojson =
         function () return "{}" end})
Maybe I should add the constants json.emptyobject and json.emptyarray to
the module.

- David