client/grpc: remove json-iterator usage (#1387)
* minimize external deps and binary size * if user wants json-iterator codec it must be used in server and client code. so best to use it via go-plugins Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
84a19c5dff
commit
1969a92b6e
19
codec.go
19
codec.go
@ -1,6 +1,7 @@
|
|||||||
package grpc
|
package grpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -8,7 +9,6 @@ import (
|
|||||||
|
|
||||||
"github.com/golang/protobuf/jsonpb"
|
"github.com/golang/protobuf/jsonpb"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
jsoniter "github.com/json-iterator/go"
|
|
||||||
"github.com/micro/go-micro/v2/codec"
|
"github.com/micro/go-micro/v2/codec"
|
||||||
"github.com/micro/go-micro/v2/codec/bytes"
|
"github.com/micro/go-micro/v2/codec/bytes"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
@ -21,6 +21,7 @@ type bytesCodec struct{}
|
|||||||
type wrapCodec struct{ encoding.Codec }
|
type wrapCodec struct{ encoding.Codec }
|
||||||
|
|
||||||
var jsonpbMarshaler = &jsonpb.Marshaler{}
|
var jsonpbMarshaler = &jsonpb.Marshaler{}
|
||||||
|
var useNumber bool
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultGRPCCodecs = map[string]encoding.Codec{
|
defaultGRPCCodecs = map[string]encoding.Codec{
|
||||||
@ -33,18 +34,11 @@ var (
|
|||||||
"application/grpc+proto": protoCodec{},
|
"application/grpc+proto": protoCodec{},
|
||||||
"application/grpc+bytes": bytesCodec{},
|
"application/grpc+bytes": bytesCodec{},
|
||||||
}
|
}
|
||||||
|
|
||||||
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// UseNumber fix unmarshal Number(8234567890123456789) to interface(8.234567890123457e+18)
|
// UseNumber fix unmarshal Number(8234567890123456789) to interface(8.234567890123457e+18)
|
||||||
func UseNumber() {
|
func UseNumber() {
|
||||||
json = jsoniter.Config{
|
useNumber = true
|
||||||
UseNumber: true,
|
|
||||||
EscapeHTML: true,
|
|
||||||
SortMapKeys: true,
|
|
||||||
ValidateJsonRawMessage: true,
|
|
||||||
}.Froze()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w wrapCodec) String() string {
|
func (w wrapCodec) String() string {
|
||||||
@ -134,7 +128,12 @@ func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
|
|||||||
if pb, ok := v.(proto.Message); ok {
|
if pb, ok := v.(proto.Message); ok {
|
||||||
return jsonpb.Unmarshal(b.NewReader(data), pb)
|
return jsonpb.Unmarshal(b.NewReader(data), pb)
|
||||||
}
|
}
|
||||||
return json.Unmarshal(data, v)
|
|
||||||
|
dec := json.NewDecoder(b.NewReader(data))
|
||||||
|
if useNumber {
|
||||||
|
dec.UseNumber()
|
||||||
|
}
|
||||||
|
return dec.Decode(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (jsonCodec) Name() string {
|
func (jsonCodec) Name() string {
|
||||||
|
Loading…
Reference in New Issue
Block a user