Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CBOR @ByteString should work with value classes #2187

Closed
steffandroid opened this issue Feb 8, 2023 · 0 comments
Closed

CBOR @ByteString should work with value classes #2187

steffandroid opened this issue Feb 8, 2023 · 0 comments
Labels
feature up for grabs A good issue for external contribution

Comments

@steffandroid
Copy link

What is your use-case and why do you need this feature?

According to the documentation, CBOR's @ByteString annotation can be used in data classes to encode or decode a ByteArray as a byte string (major type 2). This is fine if you're dealing with CBOR data that can be represented as a data class, but there cases where a data class is not suitable (one of which is covered in #2037).

My use-case is to decode a single raw byte string, without any kind of wrapper. Other primitive types can be serialized directly (e.g. Cbor.decodeFromByteArray<Int>(data)) or using a value class:

@JvmInline
@Serializable
value class Value(val data: Int)

which can then be decoded with Cbor.decodeFromByteArray<Value>(data).

This is not possible with byte strings.

Describe the solution you'd like

I would like to be able to define a value class as such:

@JvmInline
@Serializable
value class Value(@ByteString val data: ByteArray)

Currently, if I try using this to decode the byte string 4401020304, I get the following error: kotlinx.serialization.cbor.internal.CborDecodingException: Expected start of array, but found 44.

@sandwwraith sandwwraith added the up for grabs A good issue for external contribution label Feb 15, 2023
the-eater added a commit to the-eater/kotlinx.serialization that referenced this issue Oct 6, 2023
the-eater added a commit to the-eater/kotlinx.serialization that referenced this issue Oct 6, 2023
the-eater added a commit to the-eater/kotlinx.serialization that referenced this issue Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature up for grabs A good issue for external contribution
Projects
None yet
Development

No branches or pull requests

2 participants