github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/encoding/json/json.go (about) 1 package json 2 3 import ( 4 "encoding/hex" 5 "encoding/json" 6 ) 7 8 type HexBytes []byte 9 10 func (h HexBytes) MarshalText() ([]byte, error) { 11 return []byte(hex.EncodeToString(h)), nil 12 } 13 14 func (h *HexBytes) UnmarshalText(text []byte) error { 15 n := hex.DecodedLen(len(text)) 16 *h = make([]byte, n) 17 _, err := hex.Decode(*h, text) 18 return err 19 } 20 21 type Map []byte 22 23 func (m Map) MarshalJSON() ([]byte, error) { 24 return m, nil 25 } 26 27 func (m *Map) UnmarshalJSON(text []byte) error { 28 var check map[string]*json.RawMessage 29 err := json.Unmarshal(text, &check) 30 if err != nil { 31 return err 32 } 33 *m = text 34 return nil 35 }