go
go copied to clipboard
Unable to Decode bytes | CborHandle not working as expected
Did you read the documentation?
Yes
What are you trying to do?
I'm reading a file that has data encoded in CBOR format and passing the bytes to decode More specifically I'm
Decoding without knowing what is in the stream
as said in the primer
Code
// Deserialize the data
//
// Converts CBOR byte array from a file to a Data container
// d []byte - `The data read from a file`
//
// Returns
// data interface{} - `The deserialized data`
func Decode(d []byte) (data any) {
var _d any
if d == nil {
return nil
}
fmt.Println(d)
err := codec.NewDecoderBytes(d, new(codec.CborHandle)).Decode(&_d)
if CheckErr(err) {
panic(err)
}
println(_d)
return _d
}
Input passed (d []byte
)
[163 102 100 97 116 97 95 51 161 102 100 101 112 116 104 50 161 102 100 101 112 116 104 51 111 86 97 108 117 101 32 111 102 32 100 97 116 97 32 51 102 100 97 116 97 95 49 161 102 100 101 112 116 104 50 161 102 100 101 112 116 104 51 111 86 97 108 117 101 32 111 102 32 100 97 116 97 32 49 102 100 97 116 97 95 50 161 102 100 101 112 116 104 50 161 102 100 101 112 116 104 51 111 86 97 108 117 101 32 111 102 32 100 97 116 97 32 50]
Output
$ go run .
> [163 102 100 97 116 97 95 51 161 102 100 101 112 116 104 50 161 102 100 101 112 116 104 51 111 86 97 108 117 101 32 111 102 32 100 97 116 97 32 51 102 100 97 116 97 95 49 161 102 100 101 112 116 104 50 161 102 100 101 112 116 104 51 111 86 97 108 117 101 32 111 102 32 100 97 116 97 32 49 102 100 97 116 97 95 50 161 102 100 101 112 116 104 50 161 102 100 101 112 116 104 51 111 86 97 108 117 101 32 111 102 32 100 97 116 97 32 50]
(0x8fbf60,0xc000089110)
Expected Output CBOR Playground
Please Help 🙏 @ugorji
EDIT: I tried to decode a sample json data using JsonHandle and it works perfectly.