From 1c88d0e79cf62ff07e581d7439c627be9ff76124 Mon Sep 17 00:00:00 2001 From: a <a@a.a> Date: Wed, 14 Sep 2022 11:12:49 -0500 Subject: [PATCH] use jsoniter --- client.go | 9 +++++---- http.go | 4 +++- json.go | 4 ++-- protocol.go | 2 +- wire.go | 18 ++++++++++-------- 5 files changed, 21 insertions(+), 16 deletions(-) diff --git a/client.go b/client.go index 9e77cfa..9d1151b 100644 --- a/client.go +++ b/client.go @@ -27,6 +27,7 @@ import ( "time" "git.tuxpa.in/a/zlog/log" + jsoniter "github.com/json-iterator/go" ) var ( @@ -285,7 +286,7 @@ func (c *Client) call(ctx context.Context, result any, msg *jsonrpcMessage) erro case len(resp.Result) == 0: return ErrNoResult default: - return json.Unmarshal(resp.Result, &result) + return jsoniter.Unmarshal(resp.Result, &result) } } @@ -402,7 +403,7 @@ func (c *Client) BatchCallContext(ctx context.Context, b []BatchElem) error { elem.Error = ErrNoResult continue } - elem.Error = json.Unmarshal(resp.Result, elem.Result) + elem.Error = jsoniter.Unmarshal(resp.Result, elem.Result) } return err @@ -427,7 +428,7 @@ func (c *Client) newMessage(method string, paramsIn ...any) (*jsonrpcMessage, er msg := &jsonrpcMessage{ID: c.nextID(), Method: method} if paramsIn != nil { // prevent sending "params":null var err error - if msg.Params, err = json.Marshal(paramsIn); err != nil { + if msg.Params, err = jsoniter.Marshal(paramsIn); err != nil { return nil, err } } @@ -437,7 +438,7 @@ func (c *Client) newMessageP(method string, paramIn any) (*jsonrpcMessage, error msg := &jsonrpcMessage{ID: c.nextID(), Method: method} if paramIn != nil { // prevent sending "params":null var err error - if msg.Params, err = json.Marshal(paramIn); err != nil { + if msg.Params, err = jsoniter.Marshal(paramIn); err != nil { return nil, err } } diff --git a/http.go b/http.go index cbfa3f5..ba29c09 100644 --- a/http.go +++ b/http.go @@ -29,6 +29,8 @@ import ( "net/url" "sync" "time" + + jsoniter "github.com/json-iterator/go" ) const ( @@ -177,7 +179,7 @@ func (c *Client) sendBatchHTTP(ctx context.Context, op *requestOp, msgs []*jsonr } func (hc *httpConn) doRequest(ctx context.Context, msg any) (io.ReadCloser, error) { - body, err := json.Marshal(msg) + body, err := jsoniter.Marshal(msg) if err != nil { return nil, err } diff --git a/json.go b/json.go index a22414b..f73fc32 100644 --- a/json.go +++ b/json.go @@ -92,7 +92,7 @@ func (msg *jsonrpcMessage) namespace() string { } func (msg *jsonrpcMessage) String() string { - b, _ := json.Marshal(msg) + b, _ := jsoniter.Marshal(msg) return string(b) } @@ -267,7 +267,7 @@ func (c *jsonCodec) closed() <-chan any { func parseMessage(raw json.RawMessage) ([]*jsonrpcMessage, bool) { if !isBatch(raw) { msgs := []*jsonrpcMessage{{}} - json.Unmarshal(raw, &msgs[0]) + jsoniter.Unmarshal(raw, &msgs[0]) return msgs, false } dec := json.NewDecoder(bytes.NewReader(raw)) diff --git a/protocol.go b/protocol.go index 9fa8c7e..a8fd134 100644 --- a/protocol.go +++ b/protocol.go @@ -31,7 +31,7 @@ type Request struct { func NewRequest(ctx context.Context, id string, method string, params any) *Request { r := &Request{ctx: ctx} - pms, _ := json.Marshal(params) + pms, _ := jsoniter.Marshal(params) r.msg = jsonrpcMessage{ ID: NewStringIDPtr(id), Method: method, diff --git a/wire.go b/wire.go index 9d2c282..08957b5 100644 --- a/wire.go +++ b/wire.go @@ -3,6 +3,8 @@ package jrpc import ( "encoding/json" "fmt" + + jsoniter "github.com/json-iterator/go" ) // Version represents a JSON-RPC version. @@ -21,13 +23,13 @@ var ( // MarshalJSON implements json.Marshaler. func (version) MarshalJSON() ([]byte, error) { - return json.Marshal(Version) + return jsoniter.Marshal(Version) } // UnmarshalJSON implements json.Unmarshaler. func (version) UnmarshalJSON(data []byte) error { version := "" - if err := json.Unmarshal(data, &version); err != nil { + if err := jsoniter.Unmarshal(data, &version); err != nil { return fmt.Errorf("failed to Unmarshal: %w", err) } if version != Version { @@ -93,12 +95,12 @@ func (id *ID) RawMessage() json.RawMessage { return null } if id.name != "" { - ans, err := json.Marshal(id.name) + ans, err := jsoniter.Marshal(id.name) if err == nil { return ans } } - ans, err := json.Marshal(id.number) + ans, err := jsoniter.Marshal(id.number) if err == nil { return ans } @@ -114,18 +116,18 @@ func (id *ID) MarshalJSON() ([]byte, error) { return null, nil } if id.name != "" { - return json.Marshal(id.name) + return jsoniter.Marshal(id.name) } - return json.Marshal(id.number) + return jsoniter.Marshal(id.number) } // UnmarshalJSON implements json.Unmarshaler. func (id *ID) UnmarshalJSON(data []byte) error { *id = ID{} - if err := json.Unmarshal(data, &id.number); err == nil { + if err := jsoniter.Unmarshal(data, &id.number); err == nil { return nil } - if err := json.Unmarshal(data, &id.name); err == nil { + if err := jsoniter.Unmarshal(data, &id.name); err == nil { return nil } id.null = true -- GitLab