lua-users home
lua-l archive

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

On 04/23/2017 12:54 AM, Sean Conner wrote:
> It was thus said that the Great Martin once stated:
>> Also there will be need to standartize behavior for bad data both
>> when decoding and coding. For example you can't serialize table
>> with table-type keys to JSON. Or table with cycles. Should error()
>> be raised or just (nil, err_msg) returned?
>   I actually prefer (nil, err_value).  First, the caller might be able to
> deal with the error (don't force me to pcall() just to capture an error I
> can handle), and an error value is easier to check than a string (especially
> given locales).
Me too. But error() is good stopping berserked user scripts that does
not bother to check success of called function.

>> Should standard provide .verify(t) function?
>   What should this do?

verify(data) should return true if codec fully understand given <data>.
Else it should return nil and error message. So it guaranties that
subsequent call to encode() or decode() will not raise data-dependent
errors. (In many cases checking input before actual work is cheaper in
resources than interrupting work due inconsistent data.)

But this conflicts with least-common-denominator philosophy, so
let's drop this function. (Else we should provide one verify() to check
string before decoding and other verify() to check table before

>> What if I want to serialize parts that can be serialized and omit
>> others? Should standard provide .align(t) function?
>   Least-common-demoninitor behavior is to assume error() and if it can't
> serialize what you give it, then error().

error() is too ridiculous. Suppose user calls json.encode(_G).
Can it be fully serialized? No. Can it be partly serialized? Yes.
What should be done to serialize it partly? align() data before

So user have options to call
  s = json.encode(_G)

which will return nil or raise error() but

  data = json.align(_G)
  s = json.encode(data)

will do job to some degree.

Similar problems will rise if we want standard for lua table
serializers, which encodes table to string with lua code.
AFAIK noone of them can handle table with cycles, with
metatables and with table keys.

-- Martin