fix old proto
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
9ea3149b60
commit
5bfca99627
@ -1,14 +1,18 @@
|
|||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
|
oldjsonpb "github.com/golang/protobuf/jsonpb"
|
||||||
|
oldproto "github.com/golang/protobuf/proto"
|
||||||
"github.com/oxtoacart/bpool"
|
"github.com/oxtoacart/bpool"
|
||||||
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
jsonpb "google.golang.org/protobuf/encoding/protojson"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
var jsonpbMarshaler = &jsonpb.MarshalOptions{}
|
var jsonpbMarshaler = &jsonpb.MarshalOptions{}
|
||||||
|
var oldjsonpbMarshaler = &oldjsonpb.Marshaler{}
|
||||||
|
|
||||||
// create buffer pool with 16 instances each preallocated with 256 bytes
|
// create buffer pool with 16 instances each preallocated with 256 bytes
|
||||||
var bufferPool = bpool.NewSizedBufferPool(16, 256)
|
var bufferPool = bpool.NewSizedBufferPool(16, 256)
|
||||||
@ -16,19 +20,22 @@ var bufferPool = bpool.NewSizedBufferPool(16, 256)
|
|||||||
type Marshaler struct{}
|
type Marshaler struct{}
|
||||||
|
|
||||||
func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
|
func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
|
||||||
if pb, ok := v.(proto.Message); ok {
|
switch m := v.(type) {
|
||||||
buf, err := jsonpbMarshaler.Marshal(pb)
|
case proto.Message:
|
||||||
if err != nil {
|
return jsonpbMarshaler.Marshal(m)
|
||||||
return nil, err
|
case oldproto.Message:
|
||||||
}
|
buf, err := oldjsonpbMarshaler.MarshalToString(m)
|
||||||
return buf, nil
|
return []byte(buf), err
|
||||||
}
|
}
|
||||||
return json.Marshal(v)
|
return json.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j Marshaler) Unmarshal(d []byte, v interface{}) error {
|
func (j Marshaler) Unmarshal(d []byte, v interface{}) error {
|
||||||
if pb, ok := v.(proto.Message); ok {
|
switch m := v.(type) {
|
||||||
return jsonpb.Unmarshal(d, pb)
|
case proto.Message:
|
||||||
|
return jsonpb.Unmarshal(d, m)
|
||||||
|
case oldproto.Message:
|
||||||
|
return oldjsonpb.Unmarshal(bytes.NewReader(d), m)
|
||||||
}
|
}
|
||||||
return json.Unmarshal(d, v)
|
return json.Unmarshal(d, v)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user