Merge pull request #825 from cosmos/bucky/marshal-indent
MarshalJSONIndent
This commit is contained in:
commit
e17f486928
|
@ -6,6 +6,7 @@ import (
|
|||
"io/ioutil"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/wire"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/tendermint/go-crypto/keys"
|
||||
|
@ -97,7 +98,7 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error {
|
|||
return err
|
||||
}
|
||||
testnetInfo.NodeID = nodeKey.ID()
|
||||
out, err := json.MarshalIndent(testnetInfo, "", " ")
|
||||
out, err := wire.MarshalJSONIndent(cdc, testnetInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -194,13 +195,13 @@ func addGenesisState(filename string, appState json.RawMessage) error {
|
|||
}
|
||||
|
||||
var doc GenesisDoc
|
||||
err = json.Unmarshal(bz, &doc)
|
||||
err = cdc.UnmarshalJSON(bz, &doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
doc["app_state"] = appState
|
||||
out, err := json.MarshalIndent(doc, "", " ")
|
||||
out, err := wire.MarshalJSONIndent(cdc, doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
17
wire/wire.go
17
wire/wire.go
|
@ -1,6 +1,9 @@
|
|||
package wire
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/tendermint/go-amino"
|
||||
"github.com/tendermint/go-crypto"
|
||||
)
|
||||
|
@ -15,3 +18,17 @@ func NewCodec() *Codec {
|
|||
func RegisterCrypto(cdc *Codec) {
|
||||
crypto.RegisterAmino(cdc)
|
||||
}
|
||||
|
||||
func MarshalJSONIndent(cdc *Codec, obj interface{}) ([]byte, error) {
|
||||
bz, err := cdc.MarshalJSON(obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var out bytes.Buffer
|
||||
err = json.Indent(&out, bz, "", " ")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue