spc476 / CBOR

The most comprehensive CBOR module in the Lua universe.
GNU Lesser General Public License v3.0
22 stars 3 forks source link

add cbor.null as possible drop-in replacement for lua "nil" values #7

Closed bambr closed 4 years ago

bambr commented 4 years ago

Lua can't create array tables with trailing nil values, they are dropped away. So, you can't decode CBOR with such data without loosing information. Moreover, you can't encode such arrays at all. Special "cbor.null" value can be used in lua structures instead of "nil" to solve this problem, such structures are encoded as expected. Also you can decode CBOR with "cbor.null" instead of "nil" values when this mode is enabled. This technique is implemented in lua-cjson package and works well. Value cbor.null is the same as cjson.null, so data extracted by one module can be used in other one directly and (I hope) without loosing data.

spc476 commented 4 years ago

I thought I solved this, but apparently not (someone came up with a decent approach but it looks like I never got around to it). Your approach is similar to what I was thinking, only without the special userdata. The method I was planning was to use cbor.null and cbor.undefined when decoding those values, and checking values against those two for encoding. They won't be defined, so they default to nil (current behavior) but if you need sentinel values, you can define them (any value except for NaN which for reasons, isn't even equal to itself).

spc476 commented 4 years ago

I added the ability to define a custom value that will map to/from CBOR null and undefined values.

bambr commented 4 years ago

Thank you, your solution is better than mine.