[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: org.conman.cbor and true string type VS bytes type?
- From: Sean Conner <sean@...>
- Date: Tue, 29 Aug 2023 17:34:35 -0400
It was thus said that the Great Federico Ferri once stated:
> Hello,
Hello.
> I am facing a difficulty in differentiating binary buffers from true
> strings in my application with Lua 5.3 embedded.
>
> I use the org.conman.cbor library to send CBOR payloads over the network,
> one of whose fields is a binary buffer (Lua string). Due to Lua strings
> being just bytes buffers, the org.conman.cbor library will try to detect if
> the Lua string is actually a text (valid UTF-8) string, and in that case
> use the CBOR TEXT type, otherwise it will use the CBOR BYTES type.
> This is a problem, as the binary buffer can sometimes happen to be a valid
> UTF-8 string, therefore the CBOR type tag can randomly be BYTES or TEXT,
> which turns out to be a problem for clients whose languages are aware of
> the string VS. bytes difference (C++, Python, Java, ...).
I knew that could be a problem and I just went with the simplest
solution---to check the contents of the string.
> Are there any plans for future lua versions to natively support strings
> (e.g. by having the type for strings different from the type for bytes)?
Not that I am aware of (personally, I think Lua's way of handling the
situation is much better than Python3, for instance).
> Or perhaps is there a solution or workaround to deal with this in the
> current version of Lua?
You could try a workaround----either by including a metatable with a
__tocbor() method (on a table if that's how you are encoding, or perhaps on
string objects themselves to "do the right thing") or maybe by monkey
patching cbor.__ENCODE_MAP.string() to do the checks or encoding you think
are required. Or by calling the low level cbor.TYPE.*() methods to encode
each data type directly.
Another approach that could possibly work would to be wrap a binary string
in a table, with a __tocbor() method to force encoding the string as a CBOR
BIN type.
I hope this is enough to point you in a good direction to solving your
issue.
-spc