github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/std/utils.go (about) 1 package std 2 3 import "encoding/json" 4 5 // sortJSON takes any JSON and returns it sorted by keys. Also, all white-spaces 6 // are removed. 7 // This method can be used to canonicalize JSON to be returned by GetSignBytes, 8 // e.g. for the ledger integration. 9 // If the passed JSON isn't valid it will return an error 10 func sortJSON(toSortJSON []byte) ([]byte, error) { 11 var c any 12 13 if err := json.Unmarshal(toSortJSON, &c); err != nil { 14 return nil, err 15 } 16 17 js, err := json.Marshal(c) 18 if err != nil { 19 return nil, err 20 } 21 22 return js, nil 23 } 24 25 // MustSortJSON is like sortJSON but panic if an error occurs, e.g., if 26 // the passed JSON isn't valid. 27 func MustSortJSON(toSortJSON []byte) []byte { 28 js, err := sortJSON(toSortJSON) 29 if err != nil { 30 panic(err) 31 } 32 return js 33 }