Skip to content

Commit

Permalink
optimize: use sonic instead of encoding/json
Browse files Browse the repository at this point in the history
  • Loading branch information
jkskj committed Jul 10, 2024
1 parent 9a7e600 commit 6479360
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions json.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package websocket

import (
"encoding/json"
"github.com/bytedance/sonic"
"io"
)

Expand All @@ -21,7 +21,7 @@ func (c *Conn) WriteJSON(v interface{}) error {
if err != nil {
return err
}
err1 := json.NewEncoder(w).Encode(v)
err1 := sonic.ConfigDefault.NewEncoder(w).Encode(v)
err2 := w.Close()
if err1 != nil {
return err1
Expand All @@ -39,7 +39,7 @@ func (c *Conn) ReadJSON(v interface{}) error {
if err != nil {
return err
}
err = json.NewDecoder(r).Decode(v)
err = sonic.ConfigDefault.NewDecoder(r).Decode(v)
if err == io.EOF {
// One value is expected in the message.
err = io.ErrUnexpectedEOF
Expand Down
8 changes: 5 additions & 3 deletions json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package websocket

import (
"bytes"
"encoding/json"
"errors"
"github.com/bytedance/sonic"
"github.com/bytedance/sonic/decoder"
"io"
"reflect"
"testing"
Expand Down Expand Up @@ -56,7 +58,7 @@ func TestPartialJSONRead(t *testing.T) {

// Partial JSON values.

data, err := json.Marshal(v)
data, err := sonic.Marshal(v)
if err != nil {
t.Fatal(err)
}
Expand All @@ -82,7 +84,7 @@ func TestPartialJSONRead(t *testing.T) {

for i := 0; i < messageCount; i++ {
err := rc.ReadJSON(&v)
if err != io.ErrUnexpectedEOF {
if err != io.ErrUnexpectedEOF && !errors.As(err, &decoder.SyntaxError{}) {
t.Error("read", i, err)
}
}
Expand Down

0 comments on commit 6479360

Please sign in to comment.