go icon indicating copy to clipboard operation
go copied to clipboard

Unable to Decode bytes | CborHandle not working as expected

Open HardCodeProgrammer opened this issue 2 years ago • 0 comments

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 image

Please Help 🙏 @ugorji

EDIT: I tried to decode a sample json data using JsonHandle and it works perfectly.

HardCodeProgrammer avatar Jul 06 '22 17:07 HardCodeProgrammer