msgpack
msgpack copied to clipboard
int64 aren't properly decoded
Isn't int64 supposed to be properly decoded ?
code:
package main
import (
"bytes"
"encoding/json"
"github.com/vmihailenco/msgpack/v5"
)
type Test struct {
ID int64 `json:"id"`
}
func main() {
s := `{"id":10}`
var obj map[string]interface{}
err := json.Unmarshal([]byte(s), &obj)
if err != nil {
panic(err)
}
res, err := msgpack.Marshal(&obj)
if err != nil {
panic(err)
}
var publisher Test
var buf bytes.Buffer
buf.Write(res)
dec := msgpack.NewDecoder(&buf)
dec.SetCustomStructTag("json")
err = dec.Decode(&publisher)
if err != nil {
panic(err)
}
}
output:
panic: msgpack: invalid code=cb decoding int64
goroutine 1 [running]:
main.main()
/wherever/main.go:32 +0x108
exit status 2
version:
require github.com/vmihailenco/msgpack/v5 v5.4.1
Hi jney, I think the panic happens because encoding/json turns numbers into float64, link: https://pkg.go.dev/encoding/json#Unmarshal:~:text=for%20JSON%20booleans-,float64%2C%20for%20JSON%20numbers,-string%2C%20for%20JSON then the decoder won’t accept it for an int64 field. It’s much better to encode and decode using the same schema