Fix proto3 JSON (#6345)

* Fix jsonpb

* linting

* cleanup

Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
Aaron Craelius
2020-06-05 14:20:41 +00:00
committed by GitHub
co-authored by Alexander Bezobchuk
parent e248693ac8
commit 1e6953ccdf
5 changed files with 111 additions and 29 deletions
+12
View File
@@ -4,6 +4,8 @@ import (
"bytes"
"encoding/json"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
)
@@ -25,6 +27,11 @@ func MarshalIndentFromJSON(bz []byte) ([]byte, error) {
// bytes of a message.
func ProtoMarshalJSON(msg proto.Message) ([]byte, error) {
jm := &jsonpb.Marshaler{EmitDefaults: false, OrigName: false}
err := types.UnpackInterfaces(msg, types.ProtoJSONPacker{JSONPBMarshaler: jm})
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := jm.Marshal(buf, msg); err != nil {
@@ -38,6 +45,11 @@ func ProtoMarshalJSON(msg proto.Message) ([]byte, error) {
// JSON encoded bytes of a message.
func ProtoMarshalJSONIndent(msg proto.Message) ([]byte, error) {
jm := &jsonpb.Marshaler{EmitDefaults: false, OrigName: false, Indent: " "}
err := types.UnpackInterfaces(msg, types.ProtoJSONPacker{JSONPBMarshaler: jm})
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := jm.Marshal(buf, msg); err != nil {