@@ -11,15 +11,16 @@ import (
11
11
"strconv"
12
12
13
13
"github.com/francoispqt/gojay"
14
- jsoniter "github.com/json-iterator/go"
15
14
)
16
15
16
+ const Version = "2.0"
17
+
17
18
// ID is a Request identifier.
18
19
// Only one of either the Name or Number members will be set, using the
19
20
// number form if the Name is the empty string.
20
21
type ID struct {
21
- Name string
22
- Number int64
22
+ Name string `json:"name,omitempty"`
23
+ Number int64 `json:"number,omitempty"`
23
24
}
24
25
25
26
// String returns a string representation of the ID.
@@ -32,35 +33,35 @@ func (id *ID) String() string {
32
33
if id .Name != "" {
33
34
return strconv .Quote (id .Name )
34
35
}
36
+
35
37
return "#" + strconv .FormatInt (id .Number , 10 )
36
38
}
37
39
38
40
// MarshalJSON implements json.MarshalJSON.
39
41
func (id * ID ) MarshalJSON () ([]byte , error ) {
40
42
if id .Name != "" {
41
- return jsoniter . ConfigFastest .Marshal (id .Name )
43
+ return json .Marshal (id .Name )
42
44
}
43
- return jsoniter .ConfigFastest .Marshal (id .Number )
45
+
46
+ return json .Marshal (id .Number )
44
47
}
45
48
46
49
// UnmarshalJSON implements json.UnmarshalJSON.
47
50
func (id * ID ) UnmarshalJSON (data []byte ) error {
48
51
* id = ID {}
49
- if err := jsoniter . ConfigFastest .Unmarshal (data , & id .Number ); err == nil {
52
+ if err := json .Unmarshal (data , & id .Number ); err == nil {
50
53
return nil
51
54
}
52
- return jsoniter .ConfigFastest .Unmarshal (data , & id .Name )
53
- }
54
55
55
- // Message is a general message as defined by JSON-RPC. The language server protocol always uses "2.0" as the jsonrpc version.
56
- type Message struct {
57
- JSONRPC string `json:"jsonrpc"`
56
+ return json .Unmarshal (data , & id .Name )
58
57
}
59
58
60
59
// RawMessage is the same as json.RawMessage.
61
- type RawMessage struct {
62
- * gojay.EmbeddedJSON
63
- }
60
+ type RawMessage gojay.EmbeddedJSON
61
+
62
+ var _ io.Reader = (* RawMessage )(nil )
63
+ var _ json.Marshaler = (* RawMessage )(nil )
64
+ var _ json.Unmarshaler = (* RawMessage )(nil )
64
65
65
66
// Read implements io.Reader.
66
67
func (m * RawMessage ) Read (p []byte ) (n int , err error ) {
@@ -71,7 +72,7 @@ func (m *RawMessage) Read(p []byte) (n int, err error) {
71
72
return 0 , io .EOF
72
73
}
73
74
74
- n = copy (p , * m . EmbeddedJSON )
75
+ n = copy (p , * m )
75
76
76
77
return n , nil
77
78
}
@@ -80,11 +81,11 @@ func (m *RawMessage) Read(p []byte) (n int, err error) {
80
81
//
81
82
// MarshalJSON returns m as the JSON encoding of m.
82
83
func (m RawMessage ) MarshalJSON () ([]byte , error ) {
83
- if m . EmbeddedJSON == nil {
84
+ if & m == nil {
84
85
return []byte ("null" ), nil
85
86
}
86
87
87
- return * m . EmbeddedJSON , nil
88
+ return m , nil
88
89
}
89
90
90
91
// UnmarshalJSON implements json.Unmarshaler.
@@ -95,20 +96,17 @@ func (m *RawMessage) UnmarshalJSON(data []byte) error {
95
96
return errors .New ("json.RawMessage: UnmarshalJSON on nil pointer" )
96
97
}
97
98
98
- * m . EmbeddedJSON = append ((* m . EmbeddedJSON )[0 :0 ], data ... )
99
+ * m = append ((* m )[0 :0 ], data ... )
99
100
100
101
return nil
101
102
}
102
103
103
- var _ io.Reader = (* RawMessage )(nil )
104
- var _ json.Marshaler = (* RawMessage )(nil )
105
- var _ json.Unmarshaler = (* RawMessage )(nil )
106
-
107
104
// Request is a request message to describe a request between the client and the server.
108
105
//
109
106
// Every processed request must send a response back to the sender of the request.
110
107
type Request struct {
111
- Message
108
+ // JSONRPC is a general message as defined by JSON-RPC.
109
+ JSONRPC string `json:"jsonrpc"`
112
110
113
111
// The request id.
114
112
ID * ID `json:"id"`
@@ -131,7 +129,8 @@ func (r *Request) IsNotify() bool {
131
129
// conform to the JSON RPC specification.
132
130
// The result property of the ResponseMessage should be set to null in this case to signal a successful request.
133
131
type Response struct {
134
- Message
132
+ // JSONRPC is a general message as defined by JSON-RPC.
133
+ JSONRPC string `json:"jsonrpc"`
135
134
136
135
// The request id.
137
136
ID * ID `json:"id"`
@@ -144,11 +143,34 @@ type Response struct {
144
143
Result * RawMessage `json:"result,omitempty"`
145
144
}
146
145
146
+ // Combined represents a all the fields of both Request and Response.
147
+ type Combined struct {
148
+ // JSONRPC is a general message as defined by JSON-RPC.
149
+ JSONRPC string `json:"jsonrpc"`
150
+
151
+ // The request id.
152
+ ID * ID `json:"id,omitempty"`
153
+
154
+ // The method to be invoked.
155
+ Method string `json:"method"`
156
+
157
+ // The method's params.
158
+ Params * RawMessage `json:"params,omitempty"`
159
+
160
+ // The error object in case a request fails.
161
+ Error * Error `json:"error,omitempty"`
162
+
163
+ // The result of a request. This member is REQUIRED on success.
164
+ // This member MUST NOT exist if there was an error invoking the method.
165
+ Result * RawMessage `json:"result,omitempty"`
166
+ }
167
+
147
168
// NotificationMessage is a notification message.
148
169
//
149
170
// A processed notification message must not send a response back. They work like events.
150
171
type NotificationMessage struct {
151
- Message
172
+ // JSONRPC is a general message as defined by JSON-RPC.
173
+ JSONRPC string `json:"jsonrpc"`
152
174
153
175
// Method is the method to be invoked.
154
176
Method string `json:"method"`
0 commit comments