Skip to content

Commit f86f048

Browse files
authored
common/math: allow HexOrDecimal to accept unquoted decimals too (#26758)
1 parent 4034c67 commit f86f048

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

common/math/big.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ func NewHexOrDecimal256(x int64) *HexOrDecimal256 {
4949
return &h
5050
}
5151

52+
// UnmarshalJSON implements json.Unmarshaler.
53+
//
54+
// It is similar to UnmarshalText, but allows parsing real decimals too, not just
55+
// quoted decimal strings.
56+
func (i *HexOrDecimal256) UnmarshalJSON(input []byte) error {
57+
if len(input) > 0 && input[0] == '"' {
58+
input = input[1 : len(input)-1]
59+
}
60+
return i.UnmarshalText(input)
61+
}
62+
5263
// UnmarshalText implements encoding.TextUnmarshaler.
5364
func (i *HexOrDecimal256) UnmarshalText(input []byte) error {
5465
bigint, ok := ParseBig256(string(input))

common/math/integer.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ const (
4141
// HexOrDecimal64 marshals uint64 as hex or decimal.
4242
type HexOrDecimal64 uint64
4343

44+
// UnmarshalJSON implements json.Unmarshaler.
45+
//
46+
// It is similar to UnmarshalText, but allows parsing real decimals too, not just
47+
// quoted decimal strings.
48+
func (i *HexOrDecimal64) UnmarshalJSON(input []byte) error {
49+
if len(input) > 0 && input[0] == '"' {
50+
input = input[1 : len(input)-1]
51+
}
52+
return i.UnmarshalText(input)
53+
}
54+
4455
// UnmarshalText implements encoding.TextUnmarshaler.
4556
func (i *HexOrDecimal64) UnmarshalText(input []byte) error {
4657
int, ok := ParseUint64(string(input))

0 commit comments

Comments
 (0)