|
|
@@ -1,12 +1,34 @@
|
|
|
package common
|
|
|
|
|
|
import (
|
|
|
+ "bytes"
|
|
|
"encoding/json"
|
|
|
|
|
|
+ "github.com/golang/protobuf/jsonpb"
|
|
|
"google.golang.org/protobuf/types/known/structpb"
|
|
|
+
|
|
|
+ "github.com/gogo/protobuf/proto"
|
|
|
)
|
|
|
|
|
|
+var jsonpbMarshaler *jsonpb.Marshaler
|
|
|
+
|
|
|
+func init() {
|
|
|
+ jsonpbMarshaler = &jsonpb.Marshaler{
|
|
|
+ EnumsAsInts: true,
|
|
|
+ EmitDefaults: true,
|
|
|
+ OrigName: true,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func MarshalJSON(v interface{}) []byte {
|
|
|
+ if _, ok := v.(proto.Message); ok {
|
|
|
+ var buf bytes.Buffer
|
|
|
+ err := jsonpbMarshaler.Marshal(&buf, v.(proto.Message))
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return buf.Bytes()
|
|
|
+ }
|
|
|
j, _ := json.Marshal(v)
|
|
|
return j
|
|
|
}
|