diff --git a/types/messages_test.go b/types/messages_test.go index 607215be1..32f09623f 100644 --- a/types/messages_test.go +++ b/types/messages_test.go @@ -2,12 +2,20 @@ package types import ( "bytes" + "encoding/json" + "strings" "testing" "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" ) +func TestMarshalJSON(t *testing.T) { + b, err := json.Marshal(&ResponseDeliverTx{}) + assert.Nil(t, err) + assert.True(t, strings.Contains(string(b), "code")) +} + func TestWriteReadMessage(t *testing.T) { cases := []proto.Message{ &Header{ diff --git a/types/result.go b/types/result.go index cb2fb7a38..1963520da 100644 --- a/types/result.go +++ b/types/result.go @@ -2,6 +2,8 @@ package types import ( "fmt" + + "github.com/gogo/protobuf/jsonpb" ) const ( @@ -71,3 +73,36 @@ func (r ResponseQuery) Error() string { func fmtError(code uint32, log string) string { return fmt.Sprintf("Error code (%d): %s", code, log) } + +//--------------------------------------------------------------------------- +// override JSON marshalling so we dont emit defaults (ie. disable omitempty) + +func (r *ResponseSetOption) MarshalJSON() ([]byte, error) { + m := jsonpb.Marshaler{EmitDefaults: true} + s, err := m.MarshalToString(r) + return []byte(s), err +} + +func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) { + m := jsonpb.Marshaler{EmitDefaults: true} + s, err := m.MarshalToString(r) + return []byte(s), err +} + +func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) { + m := jsonpb.Marshaler{EmitDefaults: true} + s, err := m.MarshalToString(r) + return []byte(s), err +} + +func (r *ResponseQuery) MarshalJSON() ([]byte, error) { + m := jsonpb.Marshaler{EmitDefaults: true} + s, err := m.MarshalToString(r) + return []byte(s), err +} + +func (r *ResponseCommit) MarshalJSON() ([]byte, error) { + m := jsonpb.Marshaler{EmitDefaults: true} + s, err := m.MarshalToString(r) + return []byte(s), err +}