ugorji / go

idiomatic codec and rpc lib for msgpack, cbor, json, etc. msgpack.org[Go]
MIT License
1.83k stars 294 forks source link

Unable to Decode bytes | CborHandle not working as expected #373

Closed HardCodeProgrammer closed 1 year ago

HardCodeProgrammer commented 2 years ago

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.

ugorji commented 1 year ago

Hard for me to reproduce this and know if this is a valid error or user error.

Can you please post a reproducer (main.go or main_test.go) that I can just run with "go run main.go" or "go test".

Thanks.

ugorji commented 1 year ago

if you post something here, just tag me using @ugorji and I will take another look.

ugorji commented 1 year ago

@HardCodeProgrammer ^^