peteroupc / CBOR

A C# implementation of Concise Binary Object Representation (RFC 8949).
The Unlicense
206 stars 29 forks source link

Expected double header, but found F9 #47

Closed slipdef closed 4 years ago

slipdef commented 4 years ago

Describe the bug

Can't deserialize CBOR bytes using https://github.com/Kotlin/kotlinx.serialization if serialized object has a field with double value without fractional part.

To Reproduce

C#

  public class Data2
  {
      public double Field { get; set; }
  }

Kotlin

@Serializable
data class Data2(val field: Double)

Works:

var works = CBORObject.FromObject(new Data2 { Field = 66.77 }).EncodeToBytes()
var res1 = Cbor.Default.decodeFromByteArray<Data2>(works)

Exception 'Expected double header, but found F9':
var doesntWork = CBORObject.FromObject(new Data2 { Field = 66 }).EncodeToBytes()
var res2 = Cbor.Default.decodeFromByteArray<Data2>(doesntWork)

However it worked fine with 4.0 beta1. Tested on all versions after 4.0 beta1 - crashes with that exception.

Environment

peteroupc commented 4 years ago

The CBOR library encodes certain double values in a shorter format, namely the half-float format supported by CBOR, if that can be done without loss. This seems to be the case here. However, my CBOR library works correctly here; the issue is with your Kotlin serialization library, which appears not to support decoding the half-float format. You should file an issue for that library.