Skip to content

Commit c8ee79c

Browse files
committed
types: re-add {Un}MarshalJSON methods uses jsoniter
1 parent cf2927e commit c8ee79c

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

types.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ package jsonrpc2
66

77
import (
88
"strconv"
9+
10+
jsoniter "github.com/json-iterator/go"
11+
)
12+
13+
var (
14+
json = jsoniter.ConfigFastest
915
)
1016

1117
// ID is a Request identifier.
@@ -29,29 +35,30 @@ func (id *ID) String() string {
2935
return "#" + strconv.FormatInt(id.Number, 10)
3036
}
3137

32-
// // MarshalJSON implements json.MarshalJSON.
33-
// func (id *ID) MarshalJSON() ([]byte, error) {
34-
// if id.Name != "" {
35-
// return json.Marshal(id.Name)
36-
// }
37-
// return json.Marshal(id.Number)
38-
// }
39-
//
40-
// // MarshalJSON implements json.UnmarshalJSON.
41-
// func (id *ID) UnmarshalJSON(data []byte) error {
42-
// *id = ID{}
43-
// if err := json.Unmarshal(data, &id.Number); err == nil {
44-
// return nil
45-
// }
46-
// return json.Unmarshal(data, &id.Name)
47-
// }
38+
// MarshalJSON implements json.MarshalJSON.
39+
func (id *ID) MarshalJSON() ([]byte, error) {
40+
if id.Name != "" {
41+
return json.Marshal(id.Name)
42+
}
43+
return json.Marshal(id.Number)
44+
}
45+
46+
// MarshalJSON implements json.UnmarshalJSON.
47+
func (id *ID) UnmarshalJSON(data []byte) error {
48+
*id = ID{}
49+
if err := json.Unmarshal(data, &id.Number); err == nil {
50+
return nil
51+
}
52+
return json.Unmarshal(data, &id.Name)
53+
}
4854

4955
// Message is a general message as defined by JSON-RPC. The language server protocol always uses "2.0" as the jsonrpc version.
5056
type Message struct {
5157
JSONRPC string `json:"jsonrpc"`
5258
}
5359

5460
// Request is a request message to describe a request between the client and the server.
61+
//
5562
// Every processed request must send a response back to the sender of the request.
5663
type Request struct {
5764
Message
@@ -72,6 +79,7 @@ func (r *Request) IsNotify() bool {
7279
}
7380

7481
// Response is a response ressage sent as a result of a request.
82+
//
7583
// If a request doesn't provide a result value the receiver of a request still needs to return a response message to
7684
// conform to the JSON RPC specification.
7785
// The result property of the ResponseMessage should be set to null in this case to signal a successful request.
@@ -89,6 +97,9 @@ type Response struct {
8997
Result []byte `json:"result,omitempty"`
9098
}
9199

100+
// NotificationMessage is a notification message.
101+
//
102+
// A processed notification message must not send a response back. They work like events.
92103
type NotificationMessage struct {
93104
Message
94105

0 commit comments

Comments
 (0)