1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-24 08:02:32 +02:00

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:
Vasiliy Tolstov 2020-03-21 12:40:58 +03:00 committed by GitHub
parent 9501512219
commit d559587807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package grpc
import (
"encoding/json"
"fmt"
"strings"
@ -8,7 +9,6 @@ import (
"github.com/golang/protobuf/jsonpb"
"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/bytes"
"google.golang.org/grpc"
@ -21,6 +21,7 @@ type bytesCodec struct{}
type wrapCodec struct{ encoding.Codec }
var jsonpbMarshaler = &jsonpb.Marshaler{}
var useNumber bool
var (
defaultGRPCCodecs = map[string]encoding.Codec{
@ -33,18 +34,11 @@ var (
"application/grpc+proto": protoCodec{},
"application/grpc+bytes": bytesCodec{},
}
json = jsoniter.ConfigCompatibleWithStandardLibrary
)
// UseNumber fix unmarshal Number(8234567890123456789) to interface(8.234567890123457e+18)
func UseNumber() {
json = jsoniter.Config{
UseNumber: true,
EscapeHTML: true,
SortMapKeys: true,
ValidateJsonRawMessage: true,
}.Froze()
useNumber = true
}
func (w wrapCodec) String() string {
@ -134,7 +128,12 @@ func (jsonCodec) Unmarshal(data []byte, v interface{}) error {
if pb, ok := v.(proto.Message); ok {
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 {