Skip to content

Commit 7d756fc

Browse files
committed
Change json libs to goccy/go-json from intel-go/fastjson
1 parent 86cb5ee commit 7d756fc

13 files changed

+66
-57
lines changed

context.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package jsonrpc
33
import (
44
"context"
55

6-
"github.com/intel-go/fastjson"
6+
"github.com/goccy/go-json"
77
)
88

99
type requestIDKey struct{}
1010
type metadataIDKey struct{}
1111
type methodNameKey struct{}
1212

1313
// RequestID takes request id from context.
14-
func RequestID(c context.Context) *fastjson.RawMessage {
15-
return c.Value(requestIDKey{}).(*fastjson.RawMessage)
14+
func RequestID(c context.Context) *json.RawMessage {
15+
return c.Value(requestIDKey{}).(*json.RawMessage)
1616
}
1717

1818
// WithRequestID adds request id to context.
19-
func WithRequestID(c context.Context, id *fastjson.RawMessage) context.Context {
19+
func WithRequestID(c context.Context, id *json.RawMessage) context.Context {
2020
return context.WithValue(c, requestIDKey{}, id)
2121
}
2222

context_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/intel-go/fastjson"
7+
"github.com/goccy/go-json"
88
"github.com/stretchr/testify/require"
99
)
1010

1111
func TestRequestID(t *testing.T) {
1212

1313
c := context.Background()
14-
id := fastjson.RawMessage("1")
14+
id := json.RawMessage("1")
1515
c = WithRequestID(c, &id)
16-
var pick *fastjson.RawMessage
16+
var pick *json.RawMessage
1717
require.NotPanics(t, func() {
1818
pick = RequestID(c)
1919
})

debug.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"reflect"
66

77
"github.com/alecthomas/jsonschema"
8-
"github.com/intel-go/fastjson"
8+
"github.com/goccy/go-json"
99
)
1010

1111
// A MethodReference is a reference of JSON-RPC method.
@@ -28,7 +28,7 @@ func (mr *MethodRepository) ServeDebug(w http.ResponseWriter, r *http.Request) {
2828
l = append(l, makeMethodReference(k, md))
2929
}
3030
w.Header().Set(contentTypeKey, contentTypeValue)
31-
if err := fastjson.NewEncoder(w).Encode(l); err != nil {
31+
if err := json.NewEncoder(w).Encode(l); err != nil {
3232
w.WriteHeader(http.StatusInternalServerError)
3333
return
3434
}

example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"net/http/httptest"
1010
"os"
1111

12-
"github.com/intel-go/fastjson"
12+
"github.com/goccy/go-json"
1313
)
1414

1515
type (
@@ -28,7 +28,7 @@ type (
2828
}
2929
)
3030

31-
func (h EchoHandler) ServeJSONRPC(c context.Context, params *fastjson.RawMessage) (interface{}, *Error) {
31+
func (h EchoHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *Error) {
3232

3333
var p EchoParams
3434
if err := Unmarshal(params, &p); err != nil {
@@ -40,7 +40,7 @@ func (h EchoHandler) ServeJSONRPC(c context.Context, params *fastjson.RawMessage
4040
}, nil
4141
}
4242

43-
func (h PositionalHandler) ServeJSONRPC(c context.Context, params *fastjson.RawMessage) (interface{}, *Error) {
43+
func (h PositionalHandler) ServeJSONRPC(c context.Context, params *json.RawMessage) (interface{}, *Error) {
4444

4545
var p PositionalParams
4646
if err := Unmarshal(params, &p); err != nil {

go.mod

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ module github.com/osamingo/jsonrpc/v2
33
go 1.13
44

55
require (
6-
github.com/alecthomas/jsonschema v0.0.0-20190626084004-00dfc6288dec
6+
github.com/alecthomas/jsonschema v0.0.0-20200530073317-71f438968921
77
github.com/davecgh/go-spew v1.1.1 // indirect
8-
github.com/intel-go/fastjson v0.0.0-20170329170629-f846ae58a1ab
9-
github.com/stretchr/testify v1.4.0
8+
github.com/goccy/go-json v0.1.15
9+
github.com/iancoleman/orderedmap v0.1.0 // indirect
10+
github.com/stretchr/testify v1.6.1
11+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
1012
)

go.sum

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
github.com/alecthomas/jsonschema v0.0.0-20190626084004-00dfc6288dec h1:aQdFubFhjKH9sH8sbfzy2G2qIY68WEzDopaW8CjfaRU=
2-
github.com/alecthomas/jsonschema v0.0.0-20190626084004-00dfc6288dec/go.mod h1:Juc2PrI3wtNfUwptSvAIeNx+HrETwHQs6nf+TkOJlOA=
1+
github.com/alecthomas/jsonschema v0.0.0-20200530073317-71f438968921 h1:T3+cD5fYvuH36h7EZq+TDpm+d8a6FSD4pQsbmuGGQ8o=
2+
github.com/alecthomas/jsonschema v0.0.0-20200530073317-71f438968921/go.mod h1:/n6+1/DWPltRLWL/VKyUxg6tzsl5kHUCcraimt4vr60=
33
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
6-
github.com/intel-go/fastjson v0.0.0-20170329170629-f846ae58a1ab h1:K7WJJ5AnrQV/6tEh0Qqs19KLzvsq5V15f9CifKii6aU=
7-
github.com/intel-go/fastjson v0.0.0-20170329170629-f846ae58a1ab/go.mod h1:xr9Svf97gkxlW+ZDxs47vReKp7m9EUzNhEGOLyBHR+8=
6+
github.com/goccy/go-json v0.1.15 h1:DC3M3UNYrTkSL1GHo2Bx9POxBnFxonFPa1qqwMbkXk0=
7+
github.com/goccy/go-json v0.1.15/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
8+
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
9+
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
10+
github.com/iancoleman/orderedmap v0.1.0 h1:2orAxZBJsvimgEBmMWfXaFlzSG2fbQil5qzP3F6cCkg=
11+
github.com/iancoleman/orderedmap v0.1.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
812
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
913
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
14+
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
1015
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1116
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
12-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
13-
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
17+
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
18+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1419
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1520
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
16-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
17-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
21+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
22+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
24+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import (
55
"fmt"
66
"net/http"
77

8-
"github.com/intel-go/fastjson"
8+
"github.com/goccy/go-json"
99
)
1010

1111
// Handler links a method of JSON-RPC request.
1212
type Handler interface {
13-
ServeJSONRPC(c context.Context, params *fastjson.RawMessage) (result interface{}, err *Error)
13+
ServeJSONRPC(c context.Context, params *json.RawMessage) (result interface{}, err *Error)
1414
}
1515

1616
// HandlerFunc type is an adapter to allow the use of
1717
// ordinary functions as JSONRPC handlers. If f is a function
1818
// with the appropriate signature, HandlerFunc(f) is a
1919
// jsonrpc.Handler that calls f.
20-
type HandlerFunc func(c context.Context, params *fastjson.RawMessage) (result interface{}, err *Error)
20+
type HandlerFunc func(c context.Context, params *json.RawMessage) (result interface{}, err *Error)
2121

2222
// ServeJSONRPC calls f(w, r).
23-
func (f HandlerFunc) ServeJSONRPC(c context.Context, params *fastjson.RawMessage) (result interface{}, err *Error) {
23+
func (f HandlerFunc) ServeJSONRPC(c context.Context, params *json.RawMessage) (result interface{}, err *Error) {
2424
return f(c, params)
2525
}
2626

handler_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"net/http/httptest"
88
"testing"
99

10-
"github.com/intel-go/fastjson"
10+
"github.com/goccy/go-json"
1111
"github.com/stretchr/testify/assert"
1212
"github.com/stretchr/testify/require"
1313
)
@@ -23,7 +23,7 @@ func TestHandler(t *testing.T) {
2323
mr.ServeHTTP(rec, r)
2424

2525
res := Response{}
26-
err = fastjson.NewDecoder(rec.Body).Decode(&res)
26+
err = json.NewDecoder(rec.Body).Decode(&res)
2727
require.NoError(t, err)
2828
assert.NotNil(t, res.Error)
2929

@@ -34,15 +34,15 @@ func TestHandler(t *testing.T) {
3434

3535
mr.ServeHTTP(rec, r)
3636
res = Response{}
37-
err = fastjson.NewDecoder(rec.Body).Decode(&res)
37+
err = json.NewDecoder(rec.Body).Decode(&res)
3838
require.NoError(t, err)
3939
assert.NotNil(t, res.Error)
4040

41-
h1 := HandlerFunc(func(c context.Context, params *fastjson.RawMessage) (interface{}, *Error) {
41+
h1 := HandlerFunc(func(c context.Context, params *json.RawMessage) (interface{}, *Error) {
4242
return "hello", nil
4343
})
4444
require.NoError(t, mr.RegisterMethod("hello", h1, nil, nil))
45-
h2 := HandlerFunc(func(c context.Context, params *fastjson.RawMessage) (interface{}, *Error) {
45+
h2 := HandlerFunc(func(c context.Context, params *json.RawMessage) (interface{}, *Error) {
4646
return nil, ErrInternal()
4747
})
4848
require.NoError(t, mr.RegisterMethod("bye", h2, nil, nil))
@@ -54,7 +54,7 @@ func TestHandler(t *testing.T) {
5454

5555
mr.ServeHTTP(rec, r)
5656
res = Response{}
57-
err = fastjson.NewDecoder(rec.Body).Decode(&res)
57+
err = json.NewDecoder(rec.Body).Decode(&res)
5858
require.NoError(t, err)
5959
assert.Nil(t, res.Error)
6060
assert.Equal(t, "hello", res.Result)
@@ -66,7 +66,7 @@ func TestHandler(t *testing.T) {
6666

6767
mr.ServeHTTP(rec, r)
6868
res = Response{}
69-
err = fastjson.NewDecoder(rec.Body).Decode(&res)
69+
err = json.NewDecoder(rec.Body).Decode(&res)
7070
require.NoError(t, err)
7171
assert.NotNil(t, res.Error)
7272
}

jsonrpc.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/http"
66
"strings"
77

8-
"github.com/intel-go/fastjson"
8+
"github.com/goccy/go-json"
99
)
1010

1111
const (
@@ -20,18 +20,18 @@ const (
2020
type (
2121
// A Request represents a JSON-RPC request received by the server.
2222
Request struct {
23-
Version string `json:"jsonrpc"`
24-
Method string `json:"method"`
25-
Params *fastjson.RawMessage `json:"params"`
26-
ID *fastjson.RawMessage `json:"id"`
23+
Version string `json:"jsonrpc"`
24+
Method string `json:"method"`
25+
Params *json.RawMessage `json:"params"`
26+
ID *json.RawMessage `json:"id"`
2727
}
2828

2929
// A Response represents a JSON-RPC response returned by the server.
3030
Response struct {
31-
Version string `json:"jsonrpc"`
32-
Result interface{} `json:"result,omitempty"`
33-
Error *Error `json:"error,omitempty"`
34-
ID *fastjson.RawMessage `json:"id,omitempty"`
31+
Version string `json:"jsonrpc"`
32+
Result interface{} `json:"result,omitempty"`
33+
Error *Error `json:"error,omitempty"`
34+
ID *json.RawMessage `json:"id,omitempty"`
3535
}
3636
)
3737

@@ -70,13 +70,13 @@ func ParseRequest(r *http.Request) ([]*Request, bool, *Error) {
7070
var rs []*Request
7171
if f != batchRequestKey {
7272
var req *Request
73-
if err := fastjson.NewDecoder(buf).Decode(&req); err != nil {
73+
if err := json.NewDecoder(buf).Decode(&req); err != nil {
7474
return nil, false, ErrParse()
7575
}
7676
return append(rs, req), false, nil
7777
}
7878

79-
if err := fastjson.NewDecoder(buf).Decode(&rs); err != nil {
79+
if err := json.NewDecoder(buf).Decode(&rs); err != nil {
8080
return nil, false, ErrParse()
8181
}
8282

@@ -95,9 +95,9 @@ func NewResponse(r *Request) *Response {
9595
func SendResponse(w http.ResponseWriter, resp []*Response, batch bool) error {
9696
w.Header().Set(contentTypeKey, contentTypeValue)
9797
if batch || len(resp) > 1 {
98-
return fastjson.NewEncoder(w).Encode(resp)
98+
return json.NewEncoder(w).Encode(resp)
9999
} else if len(resp) == 1 {
100-
return fastjson.NewEncoder(w).Encode(resp[0])
100+
return json.NewEncoder(w).Encode(resp[0])
101101
}
102102
return nil
103103
}

jsonrpc_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/http/httptest"
77
"testing"
88

9-
"github.com/intel-go/fastjson"
9+
"github.com/goccy/go-json"
1010
"github.com/stretchr/testify/assert"
1111
"github.com/stretchr/testify/require"
1212
)
@@ -77,7 +77,7 @@ func TestParseRequest(t *testing.T) {
7777
}
7878

7979
func TestNewResponse(t *testing.T) {
80-
id := fastjson.RawMessage("test")
80+
id := json.RawMessage("test")
8181
r := NewResponse(&Request{
8282
Version: "2.0",
8383
ID: &id,
@@ -93,7 +93,7 @@ func TestSendResponse(t *testing.T) {
9393
require.NoError(t, err)
9494
assert.Empty(t, rec.Body.String())
9595

96-
id := fastjson.RawMessage([]byte(`"test"`))
96+
id := json.RawMessage([]byte(`"test"`))
9797
r := &Response{
9898
ID: &id,
9999
Version: "2.0",

method_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/intel-go/fastjson"
7+
"github.com/goccy/go-json"
88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
1010
)
@@ -63,7 +63,7 @@ func TestMethods(t *testing.T) {
6363

6464
func SampleHandler() Handler {
6565

66-
h := HandlerFunc(func(c context.Context, params *fastjson.RawMessage) (result interface{}, err *Error) {
66+
h := HandlerFunc(func(c context.Context, params *json.RawMessage) (result interface{}, err *Error) {
6767
return nil, nil
6868
})
6969
return &h

unmarshal.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package jsonrpc
22

3-
import "github.com/intel-go/fastjson"
3+
import "github.com/goccy/go-json"
44

55
// Unmarshal decodes JSON-RPC params.
6-
func Unmarshal(params *fastjson.RawMessage, dst interface{}) *Error {
6+
func Unmarshal(params *json.RawMessage, dst interface{}) *Error {
77
if params == nil {
88
return ErrInvalidParams()
99
}
10-
if err := fastjson.Unmarshal(*params, dst); err != nil {
10+
if err := json.Unmarshal(*params, dst); err != nil {
1111
return ErrInvalidParams()
1212
}
1313
return nil

unmarshal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package jsonrpc
33
import (
44
"testing"
55

6-
"github.com/intel-go/fastjson"
6+
"github.com/goccy/go-json"
77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
99
)
@@ -14,7 +14,7 @@ func TestUnmarshal(t *testing.T) {
1414
require.IsType(t, &Error{}, err)
1515
assert.Equal(t, ErrorCodeInvalidParams, err.Code)
1616

17-
src := fastjson.RawMessage([]byte(`{"name":"john"}`))
17+
src := json.RawMessage([]byte(`{"name":"john"}`))
1818

1919
err = Unmarshal(&src, nil)
2020
require.IsType(t, &Error{}, err)

0 commit comments

Comments
 (0)