Closed bambr closed 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).
I added the ability to define a custom value that will map to/from CBOR null and undefined values.
Thank you, your solution is better than mine.
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.