Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package ethrpc
import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"strconv"
"unsafe"
)

Expand Down Expand Up @@ -232,6 +234,13 @@ func (i *hexInt) UnmarshalJSON(data []byte) error {
return err
}

func (i *hexInt) MarshalJSON() ([]byte, error) {
// Convert the hexInt value to a base 16 string
str := strconv.FormatInt(int64(*i), 16)

return []byte(fmt.Sprintf(`"0x%s"`, str)), nil
}

type hexBig big.Int

func (i *hexBig) UnmarshalJSON(data []byte) error {
Expand Down
14 changes: 14 additions & 0 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ func TestHexIntUnmarshal(t *testing.T) {
require.Equal(t, hexInt(1885000), test.ID)
}

func TestHexIntMarshal(t *testing.T) {
test := struct {
ID hexInt `json:"id"`
}{
ID: hexInt(1885000),
}

data, err := json.Marshal(&test)
require.Nil(t, err)

expected := []byte(`{"id":"0x1cc348"}`)
require.Equal(t, expected, data)
}

func TestHexBigUnmarshal(t *testing.T) {
test := struct {
ID hexBig `json:"id"`
Expand Down