Skip to content

Commit

Permalink
Fix math/big.Int Does not Marshal (#194)
Browse files Browse the repository at this point in the history
* fix mashalling of math/big.Int

* Revert BigIntType. It is not needed.
  • Loading branch information
jeffmitchell-sas authored Apr 26, 2024
1 parent caa4fab commit c017c29
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ion/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ func (m *Encoder) encodeStruct(v reflect.Value) error {
if t == decimalType {
return m.encodeDecimal(v)
}
if t == bigIntType {
return m.encodeBigInt(v)
}

if err := m.w.BeginStruct(); err != nil {
return err
Expand Down Expand Up @@ -445,12 +448,18 @@ func (m *Encoder) encodeTimeDate(v reflect.Value) error {
return m.w.WriteTimestamp(timestamp)
}

// EncodeDecimal encodes an ion.Decimal to the output writer as an Ion decimal.
// encodeDecimal encodes an ion.Decimal to the output writer as an Ion decimal.
func (m *Encoder) encodeDecimal(v reflect.Value) error {
d := v.Addr().Interface().(*Decimal)
return m.w.WriteDecimal(d)
}

// encodeBigInt encodes a math/big.Int to the output writer as a big.Int.
func (m *Encoder) encodeBigInt(v reflect.Value) error {
b := v.Addr().Interface().(*big.Int)
return m.w.WriteBigInt(b)
}

func (m *Encoder) encodeWithAnnotation(v reflect.Value, fields []field) error {
original := v
for _, field := range fields {
Expand Down
5 changes: 5 additions & 0 deletions ion/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ion
import (
"bytes"
"math"
"math/big"
"strings"
"testing"
"time"
Expand All @@ -42,6 +43,10 @@ func TestMarshalText(t *testing.T) {
test(byte(42), "42")
test(-42, "-42")
test(uint64(math.MaxUint64), "18446744073709551615")
bigIntPos, _ := new(big.Int).SetString("123456789012345678901234567890", 10)
test(bigIntPos, "123456789012345678901234567890")
bigIntNeg, _ := new(big.Int).SetString("-123456789012345678901234567890", 10)
test(bigIntNeg, "-123456789012345678901234567890")
test(math.MinInt64, "-9223372036854775808")

test(42.0, "4.2e+1")
Expand Down

0 comments on commit c017c29

Please sign in to comment.