diff --git a/src/math/big/intmarsh.go b/src/math/big/intmarsh.go index c1422e27107..ce429ffc11e 100644 --- a/src/math/big/intmarsh.go +++ b/src/math/big/intmarsh.go @@ -67,7 +67,10 @@ func (z *Int) UnmarshalText(text []byte) error { // MarshalJSON implements the json.Marshaler interface. func (x *Int) MarshalJSON() ([]byte, error) { - return x.MarshalText() + if x == nil { + return []byte("null"), nil + } + return x.abs.itoa(x.neg, 10), nil } // UnmarshalJSON implements the json.Unmarshaler interface. diff --git a/src/math/big/intmarsh_test.go b/src/math/big/intmarsh_test.go index f82956ceaf2..936669b380b 100644 --- a/src/math/big/intmarsh_test.go +++ b/src/math/big/intmarsh_test.go @@ -97,6 +97,20 @@ func TestIntJSONEncoding(t *testing.T) { } } + +func TestIntJSONEncodingNil(t *testing.T) { + var x *Int + b, err := x.MarshalJSON() + if err != nil { + t.Fatalf("marshaling of nil failed: %s", err) + } + got := string(b) + want := "null" + if got != want { + t.Fatalf("marshaling of nil failed: got %s want %s", got, want) + } +} + func TestIntXMLEncoding(t *testing.T) { for _, test := range encodingTests { for _, sign := range []string{"", "+", "-"} {