This commit is contained in:
crusader 2018-03-23 12:22:16 +09:00
parent b77b975069
commit 4fe1fa640d
7 changed files with 23 additions and 25 deletions

View File

@ -8,8 +8,6 @@ import (
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Codec // Codec
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -42,7 +40,7 @@ func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface
ID: id, 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 { if err := encoder.Encode(req); nil != err {
return err return err
} }

View File

@ -1,14 +1,14 @@
package json package json
import ( import (
"encoding/json"
"git.loafle.net/commons_go/rpc/codec" "git.loafle.net/commons_go/rpc/codec"
crp "git.loafle.net/commons_go/rpc/protocol" crp "git.loafle.net/commons_go/rpc/protocol"
cuej "git.loafle.net/commons_go/util/encoding/json" cuej "git.loafle.net/commons_go/util/encoding/json"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// ClientNotificationCodec // ClientNotificationCodec
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -56,7 +56,7 @@ func (cnc *ClientNotificationCodec) Params() ([]string, error) {
if cnc.err == nil && cnc.noti.Params != nil { if cnc.err == nil && cnc.noti.Params != nil {
var values []string 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{ cnc.err = &Error{
Code: crp.E_INVALID_REQ, Code: crp.E_INVALID_REQ,
Message: err.Error(), Message: err.Error(),

View File

@ -1,6 +1,7 @@
package json package json
import ( import (
"encoding/json"
"fmt" "fmt"
"io" "io"
@ -10,8 +11,6 @@ import (
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// ClientResponseCodec // ClientResponseCodec
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -36,7 +35,7 @@ func (crc *ClientResponseCodec) ID() interface{} {
func (crc *ClientResponseCodec) Result(result interface{}) error { func (crc *ClientResponseCodec) Result(result interface{}) error {
if nil == crc.err && nil != crc.res.Result { 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{ crc.err = &Error{
Code: crp.E_PARSE, Code: crp.E_PARSE,
Message: err.Error(), Message: err.Error(),
@ -58,7 +57,7 @@ func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec
} }
noti := &clientNotification{} noti := &clientNotification{}
err := json.Unmarshal(*crc.res.Result, noti) err := jsoniter.Unmarshal(*crc.res.Result, noti)
if nil != err { if nil != err {
return nil, err return nil, err
} }
@ -69,7 +68,7 @@ func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec
// newClientMessageCodec returns a new ClientMessageCodec. // newClientMessageCodec returns a new ClientMessageCodec.
func newClientResponseCodec(r io.Reader, codec codec.Codec) (protocol.ClientResponseCodec, error) { func newClientResponseCodec(r io.Reader, codec codec.Codec) (protocol.ClientResponseCodec, error) {
decoder := json.NewDecoder(r) decoder := jsoniter.NewDecoder(r)
if nil == r { if nil == r {
return nil, io.EOF return nil, io.EOF
} }

View File

@ -1,5 +1,11 @@
package json package json
// import (
// jsoniter "github.com/json-iterator/go"
// )
// var json = jsoniter.ConfigCompatibleWithStandardLibrary
const ( const (
Name = "jsonrpc" Name = "jsonrpc"
Version = "2.0" Version = "2.0"

View File

@ -1,6 +1,7 @@
package json package json
import ( import (
"encoding/json"
"io" "io"
"git.loafle.net/commons_go/rpc/codec" "git.loafle.net/commons_go/rpc/codec"
@ -8,8 +9,6 @@ import (
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary
var null = json.RawMessage([]byte("null")) 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} noti := &serverNotification{Method: method, Params: params}
res := &serverResponse{Version: Version, Result: noti} 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. // Not sure in which case will this happen. But seems harmless.
if err := encoder.Encode(res); nil != err { if err := encoder.Encode(res); nil != err {
return err return err

View File

@ -1,6 +1,7 @@
package json package json
import ( import (
"encoding/json"
"io" "io"
"git.loafle.net/commons_go/rpc/codec" "git.loafle.net/commons_go/rpc/codec"
@ -10,8 +11,6 @@ import (
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Request // Request
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -40,7 +39,7 @@ type serverRequest struct {
// newRequestCodec returns a new ServerRequestCodec. // newRequestCodec returns a new ServerRequestCodec.
func newServerRequestCodec(r io.Reader, codec codec.Codec) (protocol.ServerRequestCodec, error) { func newServerRequestCodec(r io.Reader, codec codec.Codec) (protocol.ServerRequestCodec, error) {
// Decode the request body and check if RPC method is valid. // Decode the request body and check if RPC method is valid.
decoder := json.NewDecoder(r) decoder := jsoniter.NewDecoder(r)
if nil == r { if nil == r {
return nil, io.EOF return nil, io.EOF
} }
@ -120,7 +119,7 @@ func (src *ServerRequestCodec) Params() ([]string, error) {
if src.err == nil && src.req.Params != nil { if src.err == nil && src.req.Params != nil {
var values []string 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{ src.err = &Error{
Code: crp.E_INVALID_REQ, Code: crp.E_INVALID_REQ,
Message: err.Error(), 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 { func (src *ServerRequestCodec) writeServerResponse(w io.Writer, res *serverResponse) error {
// ID is null for notifications and they don't have a response. // ID is null for notifications and they don't have a response.
if src.req.ID != nil { 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. // Not sure in which case will this happen. But seems harmless.
if err := encoder.Encode(res); nil != err { if err := encoder.Encode(res); nil != err {
return err return err

View File

@ -4,13 +4,10 @@ import (
"fmt" "fmt"
"reflect" "reflect"
jsoniter "github.com/json-iterator/go"
cur "git.loafle.net/commons_go/util/reflect" cur "git.loafle.net/commons_go/util/reflect"
jsoniter "github.com/json-iterator/go"
) )
var json = jsoniter.ConfigCompatibleWithStandardLibrary
func convertParamsToStringArray(params []interface{}) ([]string, error) { func convertParamsToStringArray(params []interface{}) ([]string, error) {
var values []string var values []string
if nil == params || 0 == len(params) { if nil == params || 0 == len(params) {
@ -23,7 +20,7 @@ func convertParamsToStringArray(params []interface{}) ([]string, error) {
case reflect.String: case reflect.String:
values = append(values, param.(string)) values = append(values, param.(string))
case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct: case reflect.Array, reflect.Slice, reflect.Map, reflect.Struct:
b, err := json.Marshal(param) b, err := jsoniter.Marshal(param)
if nil != err { if nil != err {
return nil, err return nil, err
} }
@ -32,7 +29,7 @@ func convertParamsToStringArray(params []interface{}) ([]string, error) {
if t.Elem().Kind() != reflect.Struct { if t.Elem().Kind() != reflect.Struct {
return nil, fmt.Errorf("Pointer of param[%d] is permitted only Struct type", i) 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 { if nil != err {
return nil, err return nil, err
} }