Add JSON unmarshallers for numeric types from util (#83)

Uint160, Uint256, Fixed8 now have UnmarshalJSON method.
This commit is contained in:
Evgenii Stratonikov 2018-05-09 08:20:16 +03:00 committed by Anthony De Meulemeester
parent 35551282b0
commit 1d9045877c
8 changed files with 110 additions and 4 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
)
const uint256Size = 32
@ -59,6 +60,19 @@ func (u Uint256) String() string {
return hex.EncodeToString(ArrayReverse(u.Bytes()))
}
// UnmarshalJSON implements the json unmarshaller interface.
func (u *Uint256) UnmarshalJSON(data []byte) (err error) {
var js string
if err = json.Unmarshal(data, &js); err != nil {
return err
}
if strings.HasPrefix(js, "0x") {
js = js[2:]
}
*u, err = Uint256DecodeString(js)
return err
}
// MarshalJSON implements the json marshaller interface.
func (u Uint256) MarshalJSON() ([]byte, error) {
return json.Marshal(fmt.Sprintf("0x%s", u.String()))