diff --git a/protocol/json/client.go b/protocol/json/client.go index 63dfb3c..15e9eb4 100644 --- a/protocol/json/client.go +++ b/protocol/json/client.go @@ -8,8 +8,6 @@ import ( jsoniter "github.com/json-iterator/go" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary - // ---------------------------------------------------------------------------- // Codec // ---------------------------------------------------------------------------- @@ -42,7 +40,7 @@ func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface ID: id, } - encoder := json.NewEncoder(cc.codecSel.SelectByWriter(w).Encode(w)) + encoder := jsoniter.NewEncoder(cc.codecSel.SelectByWriter(w).Encode(w)) if err := encoder.Encode(req); nil != err { return err } diff --git a/protocol/json/client_notification.go b/protocol/json/client_notification.go index 0065a85..18b4c54 100644 --- a/protocol/json/client_notification.go +++ b/protocol/json/client_notification.go @@ -1,14 +1,14 @@ package json import ( + "encoding/json" + "git.loafle.net/commons_go/rpc/codec" crp "git.loafle.net/commons_go/rpc/protocol" cuej "git.loafle.net/commons_go/util/encoding/json" jsoniter "github.com/json-iterator/go" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary - // ---------------------------------------------------------------------------- // ClientNotificationCodec // ---------------------------------------------------------------------------- @@ -56,7 +56,7 @@ func (cnc *ClientNotificationCodec) Params() ([]string, error) { if cnc.err == nil && cnc.noti.Params != nil { var values []string - if err := json.Unmarshal(*cnc.noti.Params, &values); err != nil { + if err := jsoniter.Unmarshal(*cnc.noti.Params, &values); err != nil { cnc.err = &Error{ Code: crp.E_INVALID_REQ, Message: err.Error(), diff --git a/protocol/json/client_response.go b/protocol/json/client_response.go index 130c365..c4fd65d 100644 --- a/protocol/json/client_response.go +++ b/protocol/json/client_response.go @@ -1,6 +1,7 @@ package json import ( + "encoding/json" "fmt" "io" @@ -10,8 +11,6 @@ import ( jsoniter "github.com/json-iterator/go" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary - // ---------------------------------------------------------------------------- // ClientResponseCodec // ---------------------------------------------------------------------------- @@ -36,7 +35,7 @@ func (crc *ClientResponseCodec) ID() interface{} { func (crc *ClientResponseCodec) Result(result interface{}) error { if nil == crc.err && nil != crc.res.Result { - if err := json.Unmarshal(*crc.res.Result, result); nil != err { + if err := jsoniter.Unmarshal(*crc.res.Result, result); nil != err { crc.err = &Error{ Code: crp.E_PARSE, Message: err.Error(), @@ -58,7 +57,7 @@ func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec } noti := &clientNotification{} - err := json.Unmarshal(*crc.res.Result, noti) + err := jsoniter.Unmarshal(*crc.res.Result, noti) if nil != err { return nil, err } @@ -69,7 +68,7 @@ func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec // newClientMessageCodec returns a new ClientMessageCodec. func newClientResponseCodec(r io.Reader, codec codec.Codec) (protocol.ClientResponseCodec, error) { - decoder := json.NewDecoder(r) + decoder := jsoniter.NewDecoder(r) if nil == r { return nil, io.EOF } diff --git a/protocol/json/constants.go b/protocol/json/constants.go index b5e4742..524e171 100644 --- a/protocol/json/constants.go +++ b/protocol/json/constants.go @@ -1,5 +1,11 @@ package json +// import ( +// jsoniter "github.com/json-iterator/go" +// ) + +// var json = jsoniter.ConfigCompatibleWithStandardLibrary + const ( Name = "jsonrpc" Version = "2.0" diff --git a/protocol/json/server.go b/protocol/json/server.go index 78d259e..af5135d 100644 --- a/protocol/json/server.go +++ b/protocol/json/server.go @@ -1,6 +1,7 @@ package json import ( + "encoding/json" "io" "git.loafle.net/commons_go/rpc/codec" @@ -8,8 +9,6 @@ import ( jsoniter "github.com/json-iterator/go" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary - var null = json.RawMessage([]byte("null")) // ---------------------------------------------------------------------------- @@ -46,7 +45,7 @@ func (sc *ServerCodec) WriteNotification(w io.Writer, method string, args []inte noti := &serverNotification{Method: method, Params: params} res := &serverResponse{Version: Version, Result: noti} - encoder := json.NewEncoder(sc.codecSel.SelectByWriter(w).Encode(w)) + encoder := jsoniter.NewEncoder(sc.codecSel.SelectByWriter(w).Encode(w)) // Not sure in which case will this happen. But seems harmless. if err := encoder.Encode(res); nil != err { return err diff --git a/protocol/json/server_request.go b/protocol/json/server_request.go index 865f443..3bec25a 100644 --- a/protocol/json/server_request.go +++ b/protocol/json/server_request.go @@ -1,6 +1,7 @@ package json import ( + "encoding/json" "io" "git.loafle.net/commons_go/rpc/codec" @@ -10,8 +11,6 @@ import ( jsoniter "github.com/json-iterator/go" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary - // ---------------------------------------------------------------------------- // Request // ---------------------------------------------------------------------------- @@ -40,7 +39,7 @@ type serverRequest struct { // newRequestCodec returns a new ServerRequestCodec. func newServerRequestCodec(r io.Reader, codec codec.Codec) (protocol.ServerRequestCodec, error) { // Decode the request body and check if RPC method is valid. - decoder := json.NewDecoder(r) + decoder := jsoniter.NewDecoder(r) if nil == r { return nil, io.EOF } @@ -120,7 +119,7 @@ func (src *ServerRequestCodec) Params() ([]string, error) { if src.err == nil && src.req.Params != nil { var values []string - if err := json.Unmarshal(*src.req.Params, &values); err != nil { + if err := jsoniter.Unmarshal(*src.req.Params, &values); err != nil { src.err = &Error{ Code: crp.E_INVALID_REQ, Message: err.Error(), @@ -156,7 +155,7 @@ func (src *ServerRequestCodec) WriteError(w io.Writer, status int, err error) er func (src *ServerRequestCodec) writeServerResponse(w io.Writer, res *serverResponse) error { // ID is null for notifications and they don't have a response. if src.req.ID != nil { - encoder := json.NewEncoder(src.codec.Encode(w)) + encoder := jsoniter.NewEncoder(src.codec.Encode(w)) // Not sure in which case will this happen. But seems harmless. if err := encoder.Encode(res); nil != err { return err diff --git a/protocol/json/util.go b/protocol/json/util.go index dd20139..d31ee5d 100644 --- a/protocol/json/util.go +++ b/protocol/json/util.go @@ -4,13 +4,10 @@ import ( "fmt" "reflect" - jsoniter "github.com/json-iterator/go" - cur "git.loafle.net/commons_go/util/reflect" + jsoniter "github.com/json-iterator/go" ) -var json = jsoniter.ConfigCompatibleWithStandardLibrary - func convertParamsToStringArray(params []interface{}) ([]string, error) { var values []string if nil == params || 0 == len(params) { @@ -23,7 +20,7 @@ func convertParamsToStringArray(params []interface{}) ([]string, error) { case reflect.String: values = append(values, param.(string)) case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct: - b, err := json.Marshal(param) + b, err := jsoniter.Marshal(param) if nil != err { return nil, err } @@ -32,7 +29,7 @@ func convertParamsToStringArray(params []interface{}) ([]string, error) { if t.Elem().Kind() != reflect.Struct { return nil, fmt.Errorf("Pointer of param[%d] is permitted only Struct type", i) } - b, err := json.Marshal(param) + b, err := jsoniter.Marshal(param) if nil != err { return nil, err }