Kotlin / kotlinx.serialization

Kotlin multiplatform / multi-format serialization
Apache License 2.0
5.41k stars 620 forks source link

CBOR : encoding a ByteArray as a byte string #929

Closed Foalyy closed 4 years ago

Foalyy commented 4 years ago

What is your use-case and why do you need this feature? I need to be able to deserialize/re-serialize some CBOR-encoded data which includes raw data encoded as byte strings (Major Type 2 in the RFC : https://tools.ietf.org/html/rfc7049#section-2.1). According to #52 and the absence of header definition for this type in https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/cbor/commonMain/src/kotlinx/serialization/cbor/Cbor.kt#L370, I believe it is not implemented for CBOR yet.

Example If I take the following CBOR data, which is the data 0x01 0x1A 0xC6 encoded as a byte string : h'011AC6' -> 43011AC6

Trying to decode it as a ByteArray doesn't work because ByteArraySerializer() expects an array (Major Type 4) : kotlinx.serialization.cbor.CborDecodingException: Expected start of array, but found 43

Indeed :

val data: ByteArray = listOf(0x01.toByte(), 0x1A.toByte(), 0xC6.toByte()).toByteArray() // h'011AC6'
val output = Cbor.dump(ByteArraySerializer(), data)

This generates 0x9F 0x01 0x18 0x1A 0x38 0x39 0xFF (an array of int's), instead of 0x43 0x01 0x1A 0xC6.

(Of course this is a simple example in which I could simply drop the first byte of the ByteArray, but my actual data is more complex, such as a Map<Int, ByteArray> and @ Serializable classes having properties of type ByteArray)

Describe the solution you'd like I'd like to be able to decode and encode byte strings from a ByteArray. I understand this conflicts with the current implementation that represent a ByteArray as a list. Would it be possible to offer both possibilities by encoding ByteArray as a byte string (MT2) and List<Byte> as a list (MT4) ?

sandwwraith commented 4 years ago

Am I correct that this is a duplicate of #842 ?

Foalyy commented 4 years ago

Indeed it absolutely is, sorry for missing it and creating a duplicate. I see on #898 that this feature is almost finished, I'll wait until it is merged on master. Thank you!