mirror of
https://github.com/tendermint/tendermint.git
synced 2026-02-05 19:40:44 +00:00
types: add MarshalJSON funcs for Response types with a Code
This commit is contained in:
@@ -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{
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user