From 3ecc4613850e4bb620a9b0c55235e4200cbb39d7 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 25 Mar 2020 13:03:14 -0400 Subject: [PATCH] Implement and use MarshalIndentFromJSON --- client/context/context.go | 8 +------- types/utils.go | 13 +++++++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/client/context/context.go b/client/context/context.go index 8b26da6e1..f3746e31c 100644 --- a/client/context/context.go +++ b/client/context/context.go @@ -1,7 +1,6 @@ package context import ( - "encoding/json" "fmt" "io" "os" @@ -261,12 +260,7 @@ func (ctx CLIContext) Print(toPrint interface{}) error { // error. The re-encoded JSON uses the standard library as the initial encoded // JSON should have the correct output produced by ctx.Marshaler. if ctx.Indent && err == nil { - var generic interface{} - - err = json.Unmarshal(out, &generic) - if err == nil { - out, err = json.MarshalIndent(generic, "", " ") - } + out, err = sdk.MarshalIndentFromJSON(out) } } diff --git a/types/utils.go b/types/utils.go index be3316026..56639e636 100644 --- a/types/utils.go +++ b/types/utils.go @@ -14,6 +14,19 @@ var ( DBBackend = "" ) +// MarshalIndentFromJSON returns indented JSON-encoded bytes from already encoded +// JSON bytes. The output encoding will adhere to the original input's encoding +// (e.g. Proto3). +func MarshalIndentFromJSON(bz []byte) ([]byte, error) { + var generic interface{} + + if err := json.Unmarshal(bz, &generic); err == nil { + return nil, err + } + + return json.MarshalIndent(generic, "", " ") +} + // SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces // are removed. // This method can be used to canonicalize JSON to be returned by GetSignBytes,