Skip to content

Commit

Permalink
change marshaller
Browse files Browse the repository at this point in the history
  • Loading branch information
talgendler committed Feb 8, 2021
1 parent 4db20c4 commit 8adbb83
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions utils/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package utils
import (
"encoding/json"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

// MarshalMessageBody convenience method to marshal different interfaces into JSON
func MarshalMessageBody(body interface{}) ([]byte, error) {
switch msg := body.(type) {
case proto.Message:
marshaller := jsonpb.Marshaler{} // perhaps inject a custom one ?
jsonString, err := marshaller.MarshalToString(msg)
return []byte(jsonString), err
marshaller := protojson.MarshalOptions{} // perhaps inject a custom one ?
jsonBytes, err := marshaller.Marshal(msg)
return jsonBytes, err
case []byte:
return msg, nil
default:
Expand Down

0 comments on commit 8adbb83

Please sign in to comment.