github.com/ava-labs/avalanchego@v1.11.11/vms/types/blob_data.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package types 5 6 import ( 7 "encoding/json" 8 9 "github.com/ava-labs/avalanchego/utils/formatting" 10 ) 11 12 const nullStr = "null" 13 14 // JSONByteSlice represents [[]byte] that is json marshalled to hex 15 type JSONByteSlice []byte 16 17 func (b JSONByteSlice) MarshalJSON() ([]byte, error) { 18 if b == nil { 19 return []byte(nullStr), nil 20 } 21 22 hexData, err := formatting.Encode(formatting.HexNC, b) 23 if err != nil { 24 return nil, err 25 } 26 return json.Marshal(hexData) 27 } 28 29 func (b *JSONByteSlice) UnmarshalJSON(jsonBytes []byte) error { 30 if string(jsonBytes) == nullStr { 31 return nil 32 } 33 34 var hexData string 35 if err := json.Unmarshal(jsonBytes, &hexData); err != nil { 36 return err 37 } 38 v, err := formatting.Decode(formatting.HexNC, hexData) 39 if err != nil { 40 return err 41 } 42 *b = v 43 return nil 44 }