common/hexutil: implement TextMarshaler, TextUnmarshaler
This commit makes the wrapper types more generally applicable. encoding.TextMarshaler is supported by most codec implementations (e.g. for yaml). The tests now ensure that package json actually recognizes the custom marshaler implementation irrespective of how it is implemented. The Uint type has new tests, too. These are tricky because uint size depends on the CPU word size. Turns out that there was one incorrect case where decoding returned ErrUint64Range instead of ErrUintRange.
This commit is contained in:
@ -62,14 +62,14 @@ func (n BlockNonce) Uint64() uint64 {
|
||||
return binary.BigEndian.Uint64(n[:])
|
||||
}
|
||||
|
||||
// MarshalJSON implements json.Marshaler
|
||||
func (n BlockNonce) MarshalJSON() ([]byte, error) {
|
||||
return hexutil.Bytes(n[:]).MarshalJSON()
|
||||
// MarshalText encodes n as a hex string with 0x prefix.
|
||||
func (n BlockNonce) MarshalText() ([]byte, error) {
|
||||
return hexutil.Bytes(n[:]).MarshalText()
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements json.Unmarshaler
|
||||
func (n *BlockNonce) UnmarshalJSON(input []byte) error {
|
||||
return hexutil.UnmarshalJSON("BlockNonce", input, n[:])
|
||||
// UnmarshalText implements encoding.TextUnmarshaler.
|
||||
func (n *BlockNonce) UnmarshalText(input []byte) error {
|
||||
return hexutil.UnmarshalFixedText("BlockNonce", input, n[:])
|
||||
}
|
||||
|
||||
// Header represents a block header in the Ethereum blockchain.
|
||||
|
@ -75,14 +75,14 @@ func (b Bloom) TestBytes(test []byte) bool {
|
||||
|
||||
}
|
||||
|
||||
// MarshalJSON encodes b as a hex string with 0x prefix.
|
||||
func (b Bloom) MarshalJSON() ([]byte, error) {
|
||||
return hexutil.Bytes(b[:]).MarshalJSON()
|
||||
// MarshalText encodes b as a hex string with 0x prefix.
|
||||
func (b Bloom) MarshalText() ([]byte, error) {
|
||||
return hexutil.Bytes(b[:]).MarshalText()
|
||||
}
|
||||
|
||||
// UnmarshalJSON b as a hex string with 0x prefix.
|
||||
func (b *Bloom) UnmarshalJSON(input []byte) error {
|
||||
return hexutil.UnmarshalJSON("Bloom", input, b[:])
|
||||
// UnmarshalText b as a hex string with 0x prefix.
|
||||
func (b *Bloom) UnmarshalText(input []byte) error {
|
||||
return hexutil.UnmarshalFixedText("Bloom", input, b[:])
|
||||
}
|
||||
|
||||
func CreateBloom(receipts Receipts) Bloom {
|
||||
|
Reference in New Issue
Block a user