5
5
package jsonrpc2
6
6
7
7
import (
8
+ "encoding/json"
9
+ "errors"
10
+ "io"
8
11
"strconv"
9
12
10
13
"github.com/francoispqt/gojay"
@@ -54,6 +57,53 @@ type Message struct {
54
57
JSONRPC string `json:"jsonrpc"`
55
58
}
56
59
60
+ // RawMessage is the same as json.RawMessage.
61
+ type RawMessage struct {
62
+ * gojay.EmbeddedJSON
63
+ }
64
+
65
+ // Read implements io.Reader.
66
+ func (m * RawMessage ) Read (p []byte ) (n int , err error ) {
67
+ if len (p ) == 0 || p == nil {
68
+ return 0 , nil
69
+ }
70
+ if m == nil {
71
+ return 0 , io .EOF
72
+ }
73
+
74
+ n = copy (p , * m .EmbeddedJSON )
75
+
76
+ return n , nil
77
+ }
78
+
79
+ // MarshalJSON implements json.Marshaler.
80
+ //
81
+ // MarshalJSON returns m as the JSON encoding of m.
82
+ func (m RawMessage ) MarshalJSON () ([]byte , error ) {
83
+ if m .EmbeddedJSON == nil {
84
+ return []byte ("null" ), nil
85
+ }
86
+
87
+ return * m .EmbeddedJSON , nil
88
+ }
89
+
90
+ // UnmarshalJSON implements json.Unmarshaler.
91
+ //
92
+ // UnmarshalJSON sets *m to a copy of data.
93
+ func (m * RawMessage ) UnmarshalJSON (data []byte ) error {
94
+ if m == nil {
95
+ return errors .New ("json.RawMessage: UnmarshalJSON on nil pointer" )
96
+ }
97
+
98
+ * m .EmbeddedJSON = append ((* m .EmbeddedJSON )[0 :0 ], data ... )
99
+
100
+ return nil
101
+ }
102
+
103
+ var _ io.Reader = (* RawMessage )(nil )
104
+ var _ json.Marshaler = (* RawMessage )(nil )
105
+ var _ json.Unmarshaler = (* RawMessage )(nil )
106
+
57
107
// Request is a request message to describe a request between the client and the server.
58
108
//
59
109
// Every processed request must send a response back to the sender of the request.
@@ -67,7 +117,7 @@ type Request struct {
67
117
Method string `json:"method"`
68
118
69
119
// The method's params.
70
- Params * gojay. EmbeddedJSON `json:"params,omitempty"`
120
+ Params * RawMessage `json:"params,omitempty"`
71
121
}
72
122
73
123
// IsNotify returns true if this request is a notification.
@@ -91,7 +141,7 @@ type Response struct {
91
141
92
142
// The result of a request. This member is REQUIRED on success.
93
143
// This member MUST NOT exist if there was an error invoking the method.
94
- Result * gojay. EmbeddedJSON `json:"result,omitempty"`
144
+ Result * RawMessage `json:"result,omitempty"`
95
145
}
96
146
97
147
// NotificationMessage is a notification message.
@@ -104,5 +154,5 @@ type NotificationMessage struct {
104
154
Method string `json:"method"`
105
155
106
156
// Params is the notification's params.
107
- Params interface {} `json:"params,omitempty"`
157
+ Params * RawMessage `json:"params,omitempty"`
108
158
}
0 commit comments