good morning!!!!

Skip to content
Snippets Groups Projects
Commit 1c88d0e7 authored by a's avatar a
Browse files

use jsoniter

parent 32aea540
Branches
Tags
No related merge requests found
......@@ -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
}
}
......
......@@ -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
}
......
......@@ -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))
......
......@@ -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,
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment