Skip to content

Commit

Permalink
Merge pull request #2 from bianjieai/update-protobuf
Browse files Browse the repository at this point in the history
`NonFungibleTokenPacketData` uses camel case json encoding
  • Loading branch information
aofengli authored Nov 7, 2022
2 parents f4228fb + 90552f0 commit 9fc2c86
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
26 changes: 26 additions & 0 deletions types/codec.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package types

import (
"bytes"

"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -39,3 +44,24 @@ func init() {
RegisterLegacyAminoCodec(amino)
amino.Seal()
}

func mustProtoMarshalJSON(msg proto.Message) []byte {
anyResolver := codectypes.NewInterfaceRegistry()

// EmitDefaults is set to false to prevent marshalling of unpopulated fields (memo)
// OrigName and the anyResovler match the fields the original SDK function would expect
// in order to minimize changes.
jm := &jsonpb.Marshaler{OrigName: false, EmitDefaults: false, AnyResolver: anyResolver}

err := codectypes.UnpackInterfaces(msg, codectypes.ProtoJSONPacker{JSONPBMarshaler: jm})
if err != nil {
panic(err)
}

buf := new(bytes.Buffer)
if err := jm.Marshal(buf, msg); err != nil {
panic(err)
}

return buf.Bytes()
}
2 changes: 1 addition & 1 deletion types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,5 @@ func (nftpd NonFungibleTokenPacketData) ValidateBasic() error {

// GetBytes is a helper for serializing
func (nftpd NonFungibleTokenPacketData) GetBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&nftpd))
return sdk.MustSortJSON(mustProtoMarshalJSON(&nftpd))
}
40 changes: 21 additions & 19 deletions types/packet.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9fc2c86

Please sign in to comment.