diff --git a/cmd/abci-cli/abci-cli.go b/cmd/abci-cli/abci-cli.go index 18437ff71..47ffd6bc1 100644 --- a/cmd/abci-cli/abci-cli.go +++ b/cmd/abci-cli/abci-cli.go @@ -311,9 +311,7 @@ func or(err1 error, err2 error) error { func cmdTest(cmd *cobra.Command, args []string) error { fmt.Println("Running tests") - var err error - - err = servertest.InitChain(client) + err := servertest.InitChain(client) fmt.Println("") err = or(err, servertest.SetOption(client, "serial", "on")) fmt.Println("") diff --git a/types/messages_test.go b/types/messages_test.go index 32f09623f..15e84b624 100644 --- a/types/messages_test.go +++ b/types/messages_test.go @@ -14,6 +14,20 @@ func TestMarshalJSON(t *testing.T) { b, err := json.Marshal(&ResponseDeliverTx{}) assert.Nil(t, err) assert.True(t, strings.Contains(string(b), "code")) + + r1 := ResponseCheckTx{ + Code: 1, + Data: []byte("hello"), + Gas: 43, + Fee: 12, + } + b, err = json.Marshal(&r1) + assert.Nil(t, err) + + var r2 ResponseCheckTx + err = json.Unmarshal(b, &r2) + assert.Nil(t, err) + assert.Equal(t, r1, r2) } func TestWriteReadMessage(t *testing.T) { diff --git a/types/result.go b/types/result.go index 1963520da..782dbac6a 100644 --- a/types/result.go +++ b/types/result.go @@ -1,6 +1,7 @@ package types import ( + "bytes" "fmt" "github.com/gogo/protobuf/jsonpb" @@ -76,33 +77,63 @@ func fmtError(code uint32, log string) string { //--------------------------------------------------------------------------- // override JSON marshalling so we dont emit defaults (ie. disable omitempty) +// note we need Unmarshal functions too because protobuf had the bright idea +// to marshal int64->string. cool. cool, cool, cool: https://developers.google.com/protocol-buffers/docs/proto3#json + +var ( + jsonpbMarshaller = jsonpb.Marshaler{ + EnumsAsInts: true, + EmitDefaults: true, + } + jsonpbUnmarshaller = jsonpb.Unmarshaler{} +) func (r *ResponseSetOption) MarshalJSON() ([]byte, error) { - m := jsonpb.Marshaler{EmitDefaults: true} - s, err := m.MarshalToString(r) + s, err := jsonpbMarshaller.MarshalToString(r) return []byte(s), err } +func (r *ResponseSetOption) UnmarshalJSON(b []byte) error { + reader := bytes.NewBuffer(b) + return jsonpbUnmarshaller.Unmarshal(reader, r) +} + func (r *ResponseCheckTx) MarshalJSON() ([]byte, error) { - m := jsonpb.Marshaler{EmitDefaults: true} - s, err := m.MarshalToString(r) + s, err := jsonpbMarshaller.MarshalToString(r) return []byte(s), err } +func (r *ResponseCheckTx) UnmarshalJSON(b []byte) error { + reader := bytes.NewBuffer(b) + return jsonpbUnmarshaller.Unmarshal(reader, r) +} + func (r *ResponseDeliverTx) MarshalJSON() ([]byte, error) { - m := jsonpb.Marshaler{EmitDefaults: true} - s, err := m.MarshalToString(r) + s, err := jsonpbMarshaller.MarshalToString(r) return []byte(s), err } +func (r *ResponseDeliverTx) UnmarshalJSON(b []byte) error { + reader := bytes.NewBuffer(b) + return jsonpbUnmarshaller.Unmarshal(reader, r) +} + func (r *ResponseQuery) MarshalJSON() ([]byte, error) { - m := jsonpb.Marshaler{EmitDefaults: true} - s, err := m.MarshalToString(r) + s, err := jsonpbMarshaller.MarshalToString(r) return []byte(s), err } +func (r *ResponseQuery) UnmarshalJSON(b []byte) error { + reader := bytes.NewBuffer(b) + return jsonpbUnmarshaller.Unmarshal(reader, r) +} + func (r *ResponseCommit) MarshalJSON() ([]byte, error) { - m := jsonpb.Marshaler{EmitDefaults: true} - s, err := m.MarshalToString(r) + s, err := jsonpbMarshaller.MarshalToString(r) return []byte(s), err } + +func (r *ResponseCommit) UnmarshalJSON(b []byte) error { + reader := bytes.NewBuffer(b) + return jsonpbUnmarshaller.Unmarshal(reader, r) +}