github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/codec/json.go (about)

     1  package codec
     2  
     3  import (
     4  	"bytes"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec/types"
     7  	"github.com/gogo/protobuf/jsonpb"
     8  	"github.com/gogo/protobuf/proto"
     9  )
    10  
    11  // ProtoMarshalJSON provides an auxiliary function to return Proto3 JSON encoded
    12  // bytes of a message.
    13  func ProtoMarshalJSON(msg proto.Message, resolver jsonpb.AnyResolver) ([]byte, error) {
    14  	// We use the OrigName because camel casing fields just doesn't make sense.
    15  	// EmitDefaults is also often the more expected behavior for CLI users
    16  	jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: resolver}
    17  	err := types.UnpackInterfaces(msg, types.ProtoJSONPacker{JSONPBMarshaler: jm})
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	buf := new(bytes.Buffer)
    23  
    24  	if err := jm.Marshal(buf, msg); err != nil {
    25  		return nil, err
    26  	}
    27  
    28  	return buf.Bytes(), nil
    29  }