-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
math/big: Big.Int inconsistently marshals to JSON #28946
Comments
You're ignoring the errors:
|
Yes, that happens because it does not marshal correctly. Why does it marshal correctly when it is in a slice of structs but not alone or simply in the struct? |
For example, neither of these throw errors but fail to encode correctly. package main
import (
"fmt"
"math/big"
"encoding/json"
)
type Simple struct {
I big.Int
}
type List struct {
S []Simple
}
func main() {
i := big.Int{}
b := []byte{
0x55, 0x55, 0x55, 0x55, 0x55,
0x55, 0x55, 0x55, 0x55, 0x55}
i.SetBytes(b)
fmt.Println("b", b)
fmt.Println("i", i)
// marshal and unmarshal big int
j, err := json.Marshal(i)
if err != nil {
fmt.Println(err)
}
fmt.Println("ji", j)
// marshal and unmarshal big int in struct
s := Simple{
I: i}
j, err = json.Marshal(s)
if err != nil {
fmt.Println(err)
}
fmt.Println("js", j)
// marshal and unmarshal big int in struct in list
l := List{}
l.S = append(l.S, s)
j, _ = json.Marshal(l)
fmt.Println("jl", j)
l2 := List{}
_ = json.Unmarshal(j, &l2)
fmt.Println("l2", l2)
}
|
https://golang.org/pkg/math/big/#Int.MarshalJSON It is failing to marshal in those situations because the See https://golang.org/pkg/reflect/#Value.CanAddr
|
Thanks! |
Move serialization concerns into the type big.Int -> *big.Int for JSON marshalling (golang/go#28946 (comment))
Move serialization concerns into the type big.Int -> *big.Int for JSON marshalling (golang/go#28946 (comment))
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Reproduces on go playground.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Marshal a big.Int to JSON and it doesn't work.
Marshal a big.Int to JSON when inside a struct and it doesn't work.
Marshal a big.Int to JSON when it's inside a slice of structs and it does work.
https://play.golang.org/p/1pI0OCtLQDa
What did you expect to see?
Either it to fail consistently or work consistently.
What did you see instead?
The big.Int sometimes marshals correctly and sometimes does not.
What do you want out of this issue?
I'm mostly just curious why it works sometimes.
The text was updated successfully, but these errors were encountered: