Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codec: Use official encoding/json instead of json-iterator. (#157) #158

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -84,46 +84,43 @@ Copyright (c) 2016 Go Playground
5. go-strftime
Copyright (c) 2012 Jehiah Czebotar

6. go
Copyright (c) 2016 json-iterator

7. file-rotatelogs
6. file-rotatelogs
Copyright (c) 2014 lestrrat

8. strftime
7. strftime
Copyright (c) 2016 lestrrat

9. mapstructure
8. mapstructure
Copyright (c) 2013 Mitchell Hashimoto

10. lumberjack
9. lumberjack
Copyright (c) 2014 Nate Finch

11. ants
10. ants
Copyright (c) 2018 Andy Pan

12. cast
11. cast
Copyright (c) 2014 Steve Francia

13. testify
12. testify
Copyright (c) 2012-2020 Mat Ryer, Tyler Bunnell and contributors.

14. fasthttp
13. fasthttp
Copyright (c) 2015-present Aliaksandr Valialkin, VertaMedia, Kirill Danshin, Erik Dubbelboer, FastHTTP Authors

15. atomic
14. atomic
Copyright (c) 2016 Uber Technologies, Inc.

16. automaxprocs
15. automaxprocs
Copyright (c) 2017 Uber Technologies, Inc.

17. zap
16. zap
Copyright (c) 2016-2017 Uber Technologies, Inc.

18. assert
17. assert
Copyright (c) 2015 Dean Karn

19. lumberjack
18. lumberjack
Copyright (c) 2014 Nate Finch


Expand Down
26 changes: 21 additions & 5 deletions codec/serialization_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,33 @@
package codec

import (
jsoniter "github.com/json-iterator/go"
"encoding/json"
)

// JSONAPI is json packing and unpacking object, users can change
// the internal parameter.
var JSONAPI = jsoniter.ConfigCompatibleWithStandardLibrary
var JSONAPI JSONSerializer = StandardJSONSerializer{}

// JSONSerializer is json packing and unpacking object interface
type JSONSerializer interface {
Unmarshal([]byte, interface{}) error
Marshal(interface{}) ([]byte, error)
}

// StandardJSONSerializer is a JSONSerializer using standard encoding/json.
type StandardJSONSerializer struct{}

// Unmarshal deserializes the in bytes into body.
func (StandardJSONSerializer) Unmarshal(in []byte, body interface{}) error {
return json.Unmarshal(in, body)
}

// Marshal returns the serialized bytes in json protocol.
func (StandardJSONSerializer) Marshal(body interface{}) ([]byte, error) {
return json.Marshal(body)
}

// JSONSerialization provides json serialization mode.
// golang official json package is implemented by reflection,
// and has very low performance. So trpc-go choose json-iterator package
// to implement json serialization.
type JSONSerialization struct{}

// Unmarshal deserializes the in bytes into body.
Expand Down
3 changes: 0 additions & 3 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ replace trpc.group/trpc-go/trpc-go => ../

require (
github.com/golang/protobuf v1.5.2
github.com/stretchr/testify v1.8.0
google.golang.org/protobuf v1.30.0
trpc.group/trpc-go/trpc-go v0.0.0-00010101000000-000000000000
trpc.group/trpc/trpc-protocol/pb/go/trpc v1.0.0
Expand All @@ -15,7 +14,6 @@ require (
require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-playground/form/v4 v4.2.0 // indirect
github.com/golang/mock v1.4.4 // indirect
Expand All @@ -31,7 +29,6 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/panjf2000/ants/v2 v2.4.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.43.0 // indirect
Expand Down
4 changes: 0 additions & 4 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.43.0 h1:Gy4sb32C98fbzVWZlTM1oTMdLWGyvxR03VhM6cBIU4g=
Expand Down Expand Up @@ -128,7 +125,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update the go.mod file in the /test directory at the same time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ require (
github.com/google/flatbuffers v2.0.0+incompatible
github.com/google/go-cmp v0.5.8
github.com/hashicorp/go-multierror v1.1.1
github.com/json-iterator/go v1.1.12
github.com/lestrrat-go/strftime v1.0.6
github.com/mitchellh/mapstructure v1.5.0
github.com/panjf2000/ants/v2 v2.4.6
Expand All @@ -37,8 +36,6 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand Down
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ github.com/google/flatbuffers v2.0.0+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY=
github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU=
Expand All @@ -48,10 +45,6 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205Ah
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/panjf2000/ants/v2 v2.4.6 h1:drmj9mcygn2gawZ155dRbo+NfXEfAssjZNU1qoIb4gQ=
github.com/panjf2000/ants/v2 v2.4.6/go.mod h1:f6F0NZVFsGCp5A7QW/Zj/m92atWwOkY0OIhFxRNFr4A=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
51 changes: 39 additions & 12 deletions restful/serialize_jsonpb.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ package restful

import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"strconv"

jsoniter "github.com/json-iterator/go"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
Expand All @@ -35,9 +35,36 @@ type JSONPBSerializer struct {
AllowUnmarshalNil bool // allow unmarshalling nil body
}

// JSONAPI is a copy of jsoniter.ConfigCompatibleWithStandardLibrary.
// github.com/json-iterator/go is faster than Go's standard json library.
var JSONAPI = jsoniter.ConfigCompatibleWithStandardLibrary
// JSONAPI is json packing and unpacking object, users can change
// the internal parameter.
var JSONAPI JSONSerializer = StandardJSONSerializer{}

// JSONSerializer is json packing and unpacking object interface
type JSONSerializer interface {
Unmarshal([]byte, interface{}) error
Marshal(interface{}) ([]byte, error)
MarshalIndent(v any, prefix, indent string) ([]byte, error)
}

// StandardJSONSerializer is a JSONSerializer using standard encoding/json.
type StandardJSONSerializer struct{}

// Unmarshal deserializes the in bytes into body.
func (StandardJSONSerializer) Unmarshal(in []byte, body interface{}) error {
return json.Unmarshal(in, body)
}

// Marshal returns the serialized bytes in json protocol.
func (StandardJSONSerializer) Marshal(body interface{}) ([]byte, error) {
return json.Marshal(body)
}

// MarshalIndent is like Marshal but applies Indent to format the output.
// Each JSON element in the output will begin on a new line beginning with prefix
// followed by one or more copies of indent according to the indentation nesting.
func (StandardJSONSerializer) MarshalIndent(v interface{}, prefix, indent string) ([]byte, error) {
return json.MarshalIndent(v, prefix, indent)
}

// Marshaller is a configurable protojson marshaler.
var Marshaller = protojson.MarshalOptions{EmitUnpopulated: true}
Expand Down Expand Up @@ -104,15 +131,15 @@ func marshalNonProtoField(v interface{}) ([]byte, error) {
// marshal map proto message
if rv.Kind() == reflect.Map {
// make map for marshalling
m := make(map[string]*jsoniter.RawMessage)
m := make(map[string]*json.RawMessage)
for _, key := range rv.MapKeys() { // range all keys
// marshal value
out, err := marshal(rv.MapIndex(key).Interface())
if err != nil {
return out, err
}
// assignment
m[fmt.Sprintf("%v", key.Interface())] = (*jsoniter.RawMessage)(&out)
m[fmt.Sprintf("%v", key.Interface())] = (*json.RawMessage)(&out)
if Marshaller.Indent != "" { // 指定 indent
return JSONAPI.MarshalIndent(v, "", Marshaller.Indent)
}
Expand Down Expand Up @@ -193,7 +220,7 @@ func unmarshalNonProtoField(data []byte, v interface{}) error {
// can only unmarshal numeric enum
if _, ok := rv.Interface().(wrappedEnum); ok {
var x interface{}
if err := jsoniter.Unmarshal(data, &x); err != nil {
if err := json.Unmarshal(data, &x); err != nil {
return err
}
switch t := x.(type) {
Expand All @@ -206,8 +233,8 @@ func unmarshalNonProtoField(data []byte, v interface{}) error {
}
// unmarshal to slice
if rv.Kind() == reflect.Slice {
// unmarshal to jsoniter.RawMessage first
var rms []jsoniter.RawMessage
// unmarshal to json.RawMessage first
var rms []json.RawMessage
if err := JSONAPI.Unmarshal(data, &rms); err != nil {
return err
}
Expand All @@ -229,8 +256,8 @@ func unmarshalNonProtoField(data []byte, v interface{}) error {
if rv.IsNil() { // rv MakeMap
rv.Set(reflect.MakeMap(rv.Type()))
}
// unmarshal to map[string]*jsoniter.RawMessage first
m := make(map[string]*jsoniter.RawMessage)
// unmarshal to map[string]*json.RawMessage first
m := make(map[string]*json.RawMessage)
if err := JSONAPI.Unmarshal(data, &m); err != nil {
return err
}
Expand All @@ -242,7 +269,7 @@ func unmarshalNonProtoField(data []byte, v interface{}) error {
}
// unmarshal value
if value == nil {
rm := jsoniter.RawMessage("null")
rm := json.RawMessage("null")
value = &rm
}
rn := reflect.New(rv.Type().Elem())
Expand Down
3 changes: 0 additions & 3 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ require (
github.com/google/flatbuffers v2.0.0+incompatible // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/lestrrat-go/strftime v1.0.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/panjf2000/ants/v2 v2.4.6 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
7 changes: 0 additions & 7 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,11 @@ github.com/google/flatbuffers v2.0.0+incompatible h1:dicJ2oXwypfwUGnB2/TYWYEKiuk
github.com/google/flatbuffers v2.0.0+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kavu/go_reuseport v1.5.0 h1:UNuiY2OblcqAtVDE8Gsg1kZz8zbBWg907sP1ceBV+bk=
github.com/kavu/go_reuseport v1.5.0/go.mod h1:CG8Ee7ceMFSMnx/xr25Vm0qXaj2Z4i5PWoUx+JZ5/CU=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand All @@ -43,10 +40,6 @@ github.com/lestrrat-go/strftime v1.0.6 h1:CFGsDEt1pOpFNU+TJB0nhz9jl+K0hZSLE205Ah
github.com/lestrrat-go/strftime v1.0.6/go.mod h1:f7jQKgV5nnJpYgdEasS+/y7EsTb8ykN2z68n3TtcTaw=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/panjf2000/ants/v2 v2.4.6 h1:drmj9mcygn2gawZ155dRbo+NfXEfAssjZNU1qoIb4gQ=
github.com/panjf2000/ants/v2 v2.4.6/go.mod h1:f6F0NZVFsGCp5A7QW/Zj/m92atWwOkY0OIhFxRNFr4A=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down