types: add MarshalJSON funcs for Response types with a Code

This commit is contained in:
Ethan Buchman
2017-12-06 01:56:39 -05:00
parent 4b9cae8998
commit e1ee4d6bf5
2 changed files with 43 additions and 0 deletions

View File

@@ -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{

View File

@@ -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
}