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"
)
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
}

View File

@ -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(),

View File

@ -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
}

View File

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

View File

@ -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

View File

@ -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

View File

@ -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
}