Browse Source

json通用库适配

marion 5 years ago
parent
commit
3cf664fc39

+ 1 - 0
go.mod

@@ -14,6 +14,7 @@ require (
 	github.com/json-iterator/go v1.1.9
 	github.com/lestrrat-go/file-rotatelogs v2.3.0+incompatible
 	github.com/lestrrat-go/strftime v1.0.1 // indirect
+	github.com/mailru/easyjson v0.7.1
 	github.com/nats-io/nats-server/v2 v2.1.6 // indirect
 	github.com/nats-io/nats.go v1.9.2
 	github.com/onsi/ginkgo v1.12.0 // indirect

+ 2 - 3
utils/http_middleware/gin_recovery.go

@@ -6,7 +6,6 @@ import (
 	"git.aionnect.com/aionnect/go-common/utils/date"
 	"git.aionnect.com/aionnect/go-common/utils/logger"
 	"github.com/gin-gonic/gin"
-	"github.com/json-iterator/go"
 	"net/http"
 	"strings"
 )
@@ -69,7 +68,7 @@ func Recovery(hostPrefix string, logger *logger.Logger) gin.HandlerFunc {
 					}
 					if i, ok := ctxt.Get(IsLogHTTPResponse); ok {
 						if isLogHttpResponse, ok := i.(bool); ok && isLogHttpResponse {
-							respB, _ := jsoniter.Marshal(res)
+							respB, _ := res.MarshalJSON()
 							logger = logger.WithField("responseBody", string(respB))
 						}
 					}
@@ -86,7 +85,7 @@ func Recovery(hostPrefix string, logger *logger.Logger) gin.HandlerFunc {
 					res := utils.E(9999, "未定义", nil)
 					if i, ok := ctxt.Get(IsLogHTTPResponse); ok {
 						if isLogHttpResponse, ok := i.(bool); ok && isLogHttpResponse {
-							respB, _ := jsoniter.Marshal(res)
+							respB, _ := res.MarshalJSON()
 							logger = logger.WithField("responseBody", string(respB))
 						}
 					}

+ 2 - 0
utils/http_utils.go

@@ -27,11 +27,13 @@ type IErrorCode interface {
 	ErrMsg(args ...interface{}) string
 }
 
+//easyjson:json
 type Res struct {
 	Head ResHead     `json:"head"`
 	Data interface{} `json:"data,omitempty"`
 }
 
+//easyjson:json
 type ResHead struct {
 	ErrCode    int    `json:"errcode"`
 	ErrMsg     string `json:"errmsg,omitempty"`

+ 236 - 0
utils/http_utils_easyjson.go

@@ -0,0 +1,236 @@
+// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.
+
+package utils
+
+import (
+	json "encoding/json"
+	easyjson "github.com/mailru/easyjson"
+	jlexer "github.com/mailru/easyjson/jlexer"
+	jwriter "github.com/mailru/easyjson/jwriter"
+)
+
+// suppress unused package warning
+var (
+	_ *json.RawMessage
+	_ *jlexer.Lexer
+	_ *jwriter.Writer
+	_ easyjson.Marshaler
+)
+
+func easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils(in *jlexer.Lexer, out *ResHead) {
+	isTopLevel := in.IsStart()
+	if in.IsNull() {
+		if isTopLevel {
+			in.Consumed()
+		}
+		in.Skip()
+		return
+	}
+	in.Delim('{')
+	for !in.IsDelim('}') {
+		key := in.UnsafeString()
+		in.WantColon()
+		if in.IsNull() {
+			in.Skip()
+			in.WantComma()
+			continue
+		}
+		switch key {
+		case "errcode":
+			out.ErrCode = int(in.Int())
+		case "errmsg":
+			out.ErrMsg = string(in.String())
+		default:
+			in.SkipRecursive()
+		}
+		in.WantComma()
+	}
+	in.Delim('}')
+	if isTopLevel {
+		in.Consumed()
+	}
+}
+func easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils(out *jwriter.Writer, in ResHead) {
+	out.RawByte('{')
+	first := true
+	_ = first
+	{
+		const prefix string = ",\"errcode\":"
+		out.RawString(prefix[1:])
+		out.Int(int(in.ErrCode))
+	}
+	if in.ErrMsg != "" {
+		const prefix string = ",\"errmsg\":"
+		out.RawString(prefix)
+		out.String(string(in.ErrMsg))
+	}
+	out.RawByte('}')
+}
+
+// MarshalJSON supports json.Marshaler interface
+func (v ResHead) MarshalJSON() ([]byte, error) {
+	w := jwriter.Writer{}
+	easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils(&w, v)
+	return w.Buffer.BuildBytes(), w.Error
+}
+
+// MarshalEasyJSON supports easyjson.Marshaler interface
+func (v ResHead) MarshalEasyJSON(w *jwriter.Writer) {
+	easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils(w, v)
+}
+
+// UnmarshalJSON supports json.Unmarshaler interface
+func (v *ResHead) UnmarshalJSON(data []byte) error {
+	r := jlexer.Lexer{Data: data}
+	easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils(&r, v)
+	return r.Error()
+}
+
+// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
+func (v *ResHead) UnmarshalEasyJSON(l *jlexer.Lexer) {
+	easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils(l, v)
+}
+func easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils1(in *jlexer.Lexer, out *Res) {
+	isTopLevel := in.IsStart()
+	if in.IsNull() {
+		if isTopLevel {
+			in.Consumed()
+		}
+		in.Skip()
+		return
+	}
+	in.Delim('{')
+	for !in.IsDelim('}') {
+		key := in.UnsafeString()
+		in.WantColon()
+		if in.IsNull() {
+			in.Skip()
+			in.WantComma()
+			continue
+		}
+		switch key {
+		case "head":
+			(out.Head).UnmarshalEasyJSON(in)
+		case "data":
+			if m, ok := out.Data.(easyjson.Unmarshaler); ok {
+				m.UnmarshalEasyJSON(in)
+			} else if m, ok := out.Data.(json.Unmarshaler); ok {
+				_ = m.UnmarshalJSON(in.Raw())
+			} else {
+				out.Data = in.Interface()
+			}
+		default:
+			in.SkipRecursive()
+		}
+		in.WantComma()
+	}
+	in.Delim('}')
+	if isTopLevel {
+		in.Consumed()
+	}
+}
+func easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils1(out *jwriter.Writer, in Res) {
+	out.RawByte('{')
+	first := true
+	_ = first
+	{
+		const prefix string = ",\"head\":"
+		out.RawString(prefix[1:])
+		(in.Head).MarshalEasyJSON(out)
+	}
+	if in.Data != nil {
+		const prefix string = ",\"data\":"
+		out.RawString(prefix)
+		if m, ok := in.Data.(easyjson.Marshaler); ok {
+			m.MarshalEasyJSON(out)
+		} else if m, ok := in.Data.(json.Marshaler); ok {
+			out.Raw(m.MarshalJSON())
+		} else {
+			out.Raw(json.Marshal(in.Data))
+		}
+	}
+	out.RawByte('}')
+}
+
+// MarshalJSON supports json.Marshaler interface
+func (v Res) MarshalJSON() ([]byte, error) {
+	w := jwriter.Writer{}
+	easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils1(&w, v)
+	return w.Buffer.BuildBytes(), w.Error
+}
+
+// MarshalEasyJSON supports easyjson.Marshaler interface
+func (v Res) MarshalEasyJSON(w *jwriter.Writer) {
+	easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils1(w, v)
+}
+
+// UnmarshalJSON supports json.Unmarshaler interface
+func (v *Res) UnmarshalJSON(data []byte) error {
+	r := jlexer.Lexer{Data: data}
+	easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils1(&r, v)
+	return r.Error()
+}
+
+// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
+func (v *Res) UnmarshalEasyJSON(l *jlexer.Lexer) {
+	easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils1(l, v)
+}
+func easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils2(in *jlexer.Lexer, out *RequestPromise) {
+	isTopLevel := in.IsStart()
+	if in.IsNull() {
+		if isTopLevel {
+			in.Consumed()
+		}
+		in.Skip()
+		return
+	}
+	in.Delim('{')
+	for !in.IsDelim('}') {
+		key := in.UnsafeString()
+		in.WantColon()
+		if in.IsNull() {
+			in.Skip()
+			in.WantComma()
+			continue
+		}
+		switch key {
+		default:
+			in.SkipRecursive()
+		}
+		in.WantComma()
+	}
+	in.Delim('}')
+	if isTopLevel {
+		in.Consumed()
+	}
+}
+func easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils2(out *jwriter.Writer, in RequestPromise) {
+	out.RawByte('{')
+	first := true
+	_ = first
+	out.RawByte('}')
+}
+
+// MarshalJSON supports json.Marshaler interface
+func (v RequestPromise) MarshalJSON() ([]byte, error) {
+	w := jwriter.Writer{}
+	easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils2(&w, v)
+	return w.Buffer.BuildBytes(), w.Error
+}
+
+// MarshalEasyJSON supports easyjson.Marshaler interface
+func (v RequestPromise) MarshalEasyJSON(w *jwriter.Writer) {
+	easyjsonD579c6ceEncodeGitAionnectComAionnectGoCommonUtils2(w, v)
+}
+
+// UnmarshalJSON supports json.Unmarshaler interface
+func (v *RequestPromise) UnmarshalJSON(data []byte) error {
+	r := jlexer.Lexer{Data: data}
+	easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils2(&r, v)
+	return r.Error()
+}
+
+// UnmarshalEasyJSON supports easyjson.Unmarshaler interface
+func (v *RequestPromise) UnmarshalEasyJSON(l *jlexer.Lexer) {
+	easyjsonD579c6ceDecodeGitAionnectComAionnectGoCommonUtils2(l, v)
+}

+ 18 - 0
utils/jsonutil/jsoniter_adapter.go

@@ -0,0 +1,18 @@
+// 设置jsoniter为默认库 // +build jsoniter // 仅当编译时带 -tags=jsoniter 参数时才生效
+
+package jsonutil
+
+import (
+	"github.com/json-iterator/go"
+)
+
+var (
+	json                = jsoniter.ConfigCompatibleWithStandardLibrary
+	Marshal             = json.Marshal
+	Unmarshal           = json.Unmarshal
+	MarshalToString     = json.MarshalToString
+	UnmarshalFromString = json.UnmarshalFromString
+	MarshalIndent       = json.MarshalIndent
+	NewDecoder          = json.NewDecoder
+	NewEncoder          = json.NewEncoder
+)

+ 32 - 0
utils/jsonutil/official_adapter.go

@@ -0,0 +1,32 @@
+// +build json // 仅当编译时带 -tags=json 参数时才生效
+
+package jsonutil
+
+import (
+	"encoding/json"
+	"strings"
+)
+
+var (
+	Marshal         = json.Marshal
+	Unmarshal       = json.Unmarshal
+	MarshalToString = func(v interface{}) (string, error) {
+		data, err := json.Marshal(v)
+		if nil != err {
+			return "", err
+		}
+		return string(data), nil
+	}
+	UnmarshalFromString = func(str string, v interface{}) error {
+		str = strings.TrimSpace(str)
+		if str == "" {
+			return nil
+		}
+
+		data := []byte(str)
+		return json.Unmarshal(data, v)
+	}
+	MarshalIndent = json.MarshalIndent
+	NewDecoder    = json.NewDecoder
+	NewEncoder    = json.NewEncoder
+)

+ 2 - 2
utils/mq/kafka/consumer.go

@@ -3,10 +3,10 @@ package kafka
 import (
 	"fmt"
 	"git.aionnect.com/aionnect/go-common/utils"
+	"git.aionnect.com/aionnect/go-common/utils/jsonutil"
 	"git.aionnect.com/aionnect/go-common/utils/logger"
 	"github.com/Shopify/sarama"
 	"github.com/bsm/sarama-cluster"
-	"github.com/json-iterator/go"
 	"github.com/spf13/viper"
 	"reflect"
 )
@@ -82,7 +82,7 @@ func (c *Consumer) BindJSONChan(channel interface{}) {
 					} else {
 						oPtr = reflect.New(argType.Elem())
 					}
-					_ = jsoniter.Unmarshal(msg.Value, oPtr.Interface())
+					_ = jsonutil.Unmarshal(msg.Value, oPtr.Interface())
 					if argType.Kind() != reflect.Ptr {
 						oPtr = reflect.Indirect(oPtr)
 					}

+ 2 - 2
utils/mq/kafka/producer.go

@@ -3,10 +3,10 @@ package kafka
 import (
 	"fmt"
 	"git.aionnect.com/aionnect/go-common/utils"
+	"git.aionnect.com/aionnect/go-common/utils/jsonutil"
 	"git.aionnect.com/aionnect/go-common/utils/logger"
 	"git.aionnect.com/aionnect/go-common/utils/mq"
 	"github.com/Shopify/sarama"
-	"github.com/json-iterator/go"
 	"github.com/spf13/viper"
 )
 
@@ -16,7 +16,7 @@ func NewMsg(topic string, value interface{}) (msg *sarama.ProducerMessage, err e
 	if nil == value {
 		bytes = []byte{}
 	} else {
-		bytes, err = jsoniter.Marshal(value)
+		bytes, err = jsonutil.Marshal(value)
 		if nil != err {
 			return
 		}

+ 7 - 2
utils/string_utils.go

@@ -16,13 +16,18 @@ const (
 	GBK     = Charset("GBK")
 )
 
+var (
+	GB18030Decoder = simplifiedchinese.GB18030.NewDecoder()
+	GBKDecoder     = simplifiedchinese.GBK.NewDecoder()
+)
+
 func ConvertToEncodingBytes(bytes []byte, charset Charset) []byte {
 	switch charset {
 	case GB18030:
-		var decodeBytes, _ = simplifiedchinese.GB18030.NewDecoder().Bytes(bytes)
+		var decodeBytes, _ = GB18030Decoder.Bytes(bytes)
 		return decodeBytes
 	case GBK:
-		var decodeBytes, _ = simplifiedchinese.GBK.NewDecoder().Bytes(bytes)
+		var decodeBytes, _ = GBKDecoder.Bytes(bytes)
 		return decodeBytes
 	case UTF8:
 		fallthrough