Skip to content

Commit 81a01c1

Browse files
authored
all: use segmentio/encoding/json (#15)
1 parent c93221c commit 81a01c1

File tree

9 files changed

+39
-38
lines changed

9 files changed

+39
-38
lines changed

conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"sync"
1010
"sync/atomic"
1111

12-
json "github.com/goccy/go-json"
12+
"github.com/segmentio/encoding/json"
1313

1414
"go.lsp.dev/pkg/event"
1515
"go.lsp.dev/pkg/event/label"

errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"errors"
88
"fmt"
99

10-
json "github.com/goccy/go-json"
10+
"github.com/segmentio/encoding/json"
1111
)
1212

1313
// Error represents a JSON-RPC error.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ module go.lsp.dev/jsonrpc2
33
go 1.15
44

55
require (
6-
github.com/goccy/go-json v0.3.6-0.20210203145456-145da4ae20ce
6+
github.com/segmentio/encoding v0.2.7
77
go.lsp.dev/pkg v0.0.0-20210125030640-b6310ac75a91
88
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
github.com/goccy/go-json v0.3.6-0.20210203145456-145da4ae20ce h1:d/p7l6tkK/rNLoSt/BzBvKZlRoy/sAZgU/FKxM5nas0=
2-
github.com/goccy/go-json v0.3.6-0.20210203145456-145da4ae20ce/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
1+
github.com/segmentio/encoding v0.2.7 h1:TKxEiKbernCFCTFW5wnSlE21kIQpqcY/ABXjhc9YeJU=
2+
github.com/segmentio/encoding v0.2.7/go.mod h1:MJjRE6bMDocliO2FyFC2Dusp+uYdBfHWh5Bw7QyExto=
33
go.lsp.dev/pkg v0.0.0-20210125030640-b6310ac75a91 h1:JPKNt/RzBcOc89rhZ4Vl6U05Y1nN37FAc8PTKE3hssk=
44
go.lsp.dev/pkg v0.0.0-20210125030640-b6310ac75a91/go.mod h1:gtSHRuYfbCT0qnbLnovpie/WEmqyJ7T4n6VXiFMBtcw=

jsonrpc2_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ package jsonrpc2_test
66
import (
77
"context"
88
"fmt"
9+
"io"
910
"net"
1011
"path"
1112
"reflect"
1213
"testing"
1314

14-
json "github.com/goccy/go-json"
15+
"github.com/segmentio/encoding/json"
1516

1617
"go.lsp.dev/jsonrpc2"
1718
)
@@ -121,7 +122,7 @@ func prepare(ctx context.Context, t *testing.T) (a, b jsonrpc2.Conn, done func()
121122
return a, b, done
122123
}
123124

124-
func run(ctx context.Context, nc net.Conn) jsonrpc2.Conn {
125+
func run(ctx context.Context, nc io.ReadWriteCloser) jsonrpc2.Conn {
125126
stream := jsonrpc2.NewStream(nc)
126127
conn := jsonrpc2.NewConn(stream)
127128
conn.Go(ctx, testHandler())
@@ -140,21 +141,21 @@ func testHandler() jsonrpc2.Handler {
140141

141142
case methodOneString:
142143
var v string
143-
if err := json.UnmarshalNoEscape(req.Params(), &v); err != nil {
144+
if err := json.Unmarshal(req.Params(), &v); err != nil {
144145
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
145146
}
146147
return reply(ctx, "got:"+v, nil)
147148

148149
case methodOneNumber:
149150
var v int
150-
if err := json.UnmarshalNoEscape(req.Params(), &v); err != nil {
151+
if err := json.Unmarshal(req.Params(), &v); err != nil {
151152
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
152153
}
153154
return reply(ctx, fmt.Sprintf("got:%d", v), nil)
154155

155156
case methodJoin:
156157
var v []string
157-
if err := json.UnmarshalNoEscape(req.Params(), &v); err != nil {
158+
if err := json.Unmarshal(req.Params(), &v); err != nil {
158159
return reply(ctx, nil, fmt.Errorf("%s: %w", jsonrpc2.ErrParse, err))
159160
}
160161
return reply(ctx, path.Join(v...), nil)

message.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
package jsonrpc2
55

66
import (
7-
stdjson "encoding/json"
87
"errors"
98
"fmt"
109

11-
json "github.com/goccy/go-json"
10+
"github.com/segmentio/encoding/json"
1211
)
1312

1413
// Message is the interface to all JSON-RPC message types.
@@ -92,7 +91,7 @@ func (c Call) MarshalJSON() ([]byte, error) {
9291
Params: &c.params,
9392
ID: &c.id,
9493
}
95-
data, err := stdjson.Marshal(req)
94+
data, err := json.Marshal(req)
9695
if err != nil {
9796
return data, fmt.Errorf("marshaling call: %w", err)
9897
}
@@ -103,7 +102,7 @@ func (c Call) MarshalJSON() ([]byte, error) {
103102
// UnmarshalJSON implements json.Unmarshaler.
104103
func (c *Call) UnmarshalJSON(data []byte) error {
105104
var req wireRequest
106-
if err := json.UnmarshalNoEscape(data, &req); err != nil {
105+
if err := json.Unmarshal(data, &req); err != nil {
107106
return fmt.Errorf("unmarshaling call: %w", err)
108107
}
109108

@@ -171,7 +170,7 @@ func (r Response) MarshalJSON() ([]byte, error) {
171170
resp.Result = &r.result
172171
}
173172

174-
data, err := stdjson.Marshal(resp)
173+
data, err := json.Marshal(resp)
175174
if err != nil {
176175
return data, fmt.Errorf("marshaling notification: %w", err)
177176
}
@@ -182,7 +181,7 @@ func (r Response) MarshalJSON() ([]byte, error) {
182181
// UnmarshalJSON implements json.Unmarshaler.
183182
func (r *Response) UnmarshalJSON(data []byte) error {
184183
var resp wireResponse
185-
if err := json.UnmarshalNoEscape(data, &resp); err != nil {
184+
if err := json.Unmarshal(data, &resp); err != nil {
186185
return fmt.Errorf("unmarshaling jsonrpc response: %w", err)
187186
}
188187

@@ -266,7 +265,7 @@ func (n Notification) MarshalJSON() ([]byte, error) {
266265
Method: n.method,
267266
Params: &n.params,
268267
}
269-
data, err := stdjson.Marshal(req)
268+
data, err := json.Marshal(req)
270269
if err != nil {
271270
return data, fmt.Errorf("marshaling notification: %w", err)
272271
}
@@ -277,7 +276,7 @@ func (n Notification) MarshalJSON() ([]byte, error) {
277276
// UnmarshalJSON implements json.Unmarshaler.
278277
func (n *Notification) UnmarshalJSON(data []byte) error {
279278
var req wireRequest
280-
if err := json.UnmarshalNoEscape(data, &req); err != nil {
279+
if err := json.Unmarshal(data, &req); err != nil {
281280
return fmt.Errorf("unmarshaling notification: %w", err)
282281
}
283282

@@ -292,7 +291,7 @@ func (n *Notification) UnmarshalJSON(data []byte) error {
292291
// DecodeMessage decodes data to Message.
293292
func DecodeMessage(data []byte) (Message, error) {
294293
var msg combined
295-
if err := json.UnmarshalNoEscape(data, &msg); err != nil {
294+
if err := json.Unmarshal(data, &msg); err != nil {
296295
return nil, fmt.Errorf("unmarshaling jsonrpc message: %w", err)
297296
}
298297

@@ -342,7 +341,7 @@ func DecodeMessage(data []byte) (Message, error) {
342341

343342
// marshalInterface marshal obj to json.RawMessage.
344343
func marshalInterface(obj interface{}) (json.RawMessage, error) {
345-
data, err := json.MarshalNoEscape(obj)
344+
data, err := json.Marshal(obj)
346345
if err != nil {
347346
return json.RawMessage{}, fmt.Errorf("failed to marshal json: %w", err)
348347
}

stream.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ package jsonrpc2
66
import (
77
"bufio"
88
"context"
9+
stdjson "encoding/json"
910
"fmt"
1011
"io"
1112
"strconv"
1213
"strings"
1314

14-
json "github.com/goccy/go-json"
15+
"github.com/segmentio/encoding/json"
1516
)
1617

1718
const (
@@ -61,7 +62,7 @@ type Stream interface {
6162

6263
type rawStream struct {
6364
conn io.ReadWriteCloser
64-
in *json.Decoder
65+
in *stdjson.Decoder
6566
}
6667

6768
// NewRawStream returns a Stream built on top of a io.ReadWriteCloser.
@@ -71,7 +72,7 @@ type rawStream struct {
7172
func NewRawStream(conn io.ReadWriteCloser) Stream {
7273
return &rawStream{
7374
conn: conn,
74-
in: json.NewDecoder(conn),
75+
in: stdjson.NewDecoder(conn), // TODO(zchee): why test fail using segmentio json.Decoder?
7576
}
7677
}
7778

@@ -83,7 +84,7 @@ func (s *rawStream) Read(ctx context.Context) (Message, int64, error) {
8384
default:
8485
}
8586

86-
var raw json.RawMessage
87+
var raw stdjson.RawMessage
8788
if err := s.in.Decode(&raw); err != nil {
8889
return nil, 0, fmt.Errorf("decoding raw message: %w", err)
8990
}
@@ -100,7 +101,7 @@ func (s *rawStream) Write(ctx context.Context, msg Message) (int64, error) {
100101
default:
101102
}
102103

103-
data, err := json.MarshalNoEscape(msg)
104+
data, err := json.Marshal(msg)
104105
if err != nil {
105106
return 0, fmt.Errorf("marshaling message: %w", err)
106107
}
@@ -199,7 +200,7 @@ func (s *stream) Write(ctx context.Context, msg Message) (int64, error) {
199200
default:
200201
}
201202

202-
data, err := json.MarshalNoEscape(msg)
203+
data, err := json.Marshal(msg)
203204
if err != nil {
204205
return 0, fmt.Errorf("marshaling message: %w", err)
205206
}

wire.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package jsonrpc2
66
import (
77
"fmt"
88

9-
json "github.com/goccy/go-json"
9+
"github.com/segmentio/encoding/json"
1010
)
1111

1212
// Code is an int64 error code as defined in the JSON-RPC spec.
@@ -70,13 +70,13 @@ var (
7070

7171
// MarshalJSON implements json.Marshaler.
7272
func (version) MarshalJSON() ([]byte, error) {
73-
return json.MarshalNoEscape(Version)
73+
return json.Marshal(Version)
7474
}
7575

7676
// UnmarshalJSON implements json.Unmarshaler.
7777
func (version) UnmarshalJSON(data []byte) error {
7878
version := ""
79-
if err := json.UnmarshalNoEscape(data, &version); err != nil {
79+
if err := json.Unmarshal(data, &version); err != nil {
8080
return fmt.Errorf("failed to Unmarshal: %w", err)
8181
}
8282
if version != Version {
@@ -128,18 +128,18 @@ func (id ID) Format(f fmt.State, r rune) {
128128
// MarshalJSON implements json.Marshaler.
129129
func (id *ID) MarshalJSON() ([]byte, error) {
130130
if id.name != "" {
131-
return json.MarshalNoEscape(id.name)
131+
return json.Marshal(id.name)
132132
}
133-
return json.MarshalNoEscape(id.number)
133+
return json.Marshal(id.number)
134134
}
135135

136136
// UnmarshalJSON implements json.Unmarshaler.
137137
func (id *ID) UnmarshalJSON(data []byte) error {
138138
*id = ID{}
139-
if err := json.UnmarshalNoEscape(data, &id.number); err == nil {
139+
if err := json.Unmarshal(data, &id.number); err == nil {
140140
return nil
141141
}
142-
return json.UnmarshalNoEscape(data, &id.name)
142+
return json.Unmarshal(data, &id.name)
143143
}
144144

145145
// wireRequest is sent to a server to represent a Call or Notify operaton.

wire_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"reflect"
1010
"testing"
1111

12-
json "github.com/goccy/go-json"
12+
"github.com/segmentio/encoding/json"
1313

1414
"go.lsp.dev/jsonrpc2"
1515
)
@@ -67,7 +67,7 @@ func TestIDEncode(t *testing.T) {
6767
t.Run(tt.name, func(t *testing.T) {
6868
t.Parallel()
6969

70-
data, err := json.MarshalNoEscape(&tt.id)
70+
data, err := json.Marshal(&tt.id)
7171
if err != nil {
7272
t.Fatal(err)
7373
}
@@ -85,7 +85,7 @@ func TestIDDecode(t *testing.T) {
8585
t.Parallel()
8686

8787
var got *jsonrpc2.ID
88-
if err := json.UnmarshalNoEscape(tt.encoded, &got); err != nil {
88+
if err := json.Unmarshal(tt.encoded, &got); err != nil {
8989
t.Fatal(err)
9090
}
9191

@@ -103,7 +103,7 @@ func TestIDDecode(t *testing.T) {
103103
func TestErrorEncode(t *testing.T) {
104104
t.Parallel()
105105

106-
b, err := json.MarshalNoEscape(jsonrpc2.NewError(0, ""))
106+
b, err := json.Marshal(jsonrpc2.NewError(0, ""))
107107
if err != nil {
108108
t.Fatal(err)
109109
}
@@ -120,7 +120,7 @@ func TestErrorResponse(t *testing.T) {
120120
// originally reported in #39719, this checks that result is not present if
121121
// it is an error response
122122
r, _ := jsonrpc2.NewResponse(jsonrpc2.NewNumberID(3), nil, fmt.Errorf("computing fix edits"))
123-
data, err := json.MarshalNoEscape(r)
123+
data, err := json.Marshal(r)
124124
if err != nil {
125125
t.Fatal(err)
126126
}

0 commit comments

Comments
 (0)