From a1990356bdebbf7599c97e644dc9482d7a0d00d1 Mon Sep 17 00:00:00 2001 From: Jeremy Wei Date: Fri, 12 Jul 2024 09:54:38 -0400 Subject: [PATCH] add back MarshalJSON and UnmarshalJSON --- libs/bytes/bytes.go | 42 +++++++++++++++++++----------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/libs/bytes/bytes.go b/libs/bytes/bytes.go index 5f1382758..86d5d965e 100644 --- a/libs/bytes/bytes.go +++ b/libs/bytes/bytes.go @@ -70,27 +70,23 @@ func (bz HexBytes) Format(s fmt.State, verb rune) { } } -// // This is the point of Bytes. -// func (bz HexBytes) MarshalJSON() ([]byte, error) { -// fmt.Println("In MarshalJSON") -// s := strings.ToUpper(hex.EncodeToString(bz)) -// jbz := make([]byte, len(s)+2) -// jbz[0] = '"' -// copy(jbz[1:], s) -// jbz[len(jbz)-1] = '"' -// return jbz, nil -// } +func (bz HexBytes) MarshalJSON() ([]byte, error) { + s := strings.ToUpper(hex.EncodeToString(bz)) + jbz := make([]byte, len(s)+2) + jbz[0] = '"' + copy(jbz[1:], s) + jbz[len(jbz)-1] = '"' + return jbz, nil +} -// // This is the point of Bytes. -// func (bz *HexBytes) UnmarshalJSON(data []byte) error { -// fmt.Println("In UnmarshalJSON, data: ", string(data)) -// if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { -// return fmt.Errorf("invalid hex string: %s", data) -// } -// bz2, err := hex.DecodeString(string(data[1 : len(data)-1])) -// if err != nil { -// return err -// } -// *bz = bz2 -// return nil -// } +func (bz *HexBytes) UnmarshalJSON(data []byte) error { + if len(data) < 2 || data[0] != '"' || data[len(data)-1] != '"' { + return fmt.Errorf("invalid hex string: %s", data) + } + bz2, err := hex.DecodeString(string(data[1 : len(data)-1])) + if err != nil { + return err + } + *bz = bz2 + return nil +}