Skip to content

Commit b29af9b

Browse files
committed
jsonrpc2: add marshalToEmbedded function
1 parent 00b35d5 commit b29af9b

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

jsonrpc2.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,16 @@ func NewConn(ctx context.Context, s Stream, options ...Options) *Conn {
153153

154154
// Call sends a request over the connection and then waits for a response.
155155
func (c *Conn) Call(ctx context.Context, method string, params, result interface{}) error {
156-
rawdata, err := gojay.Marshal(params)
156+
jsonParams, err := marshalToEmbedded(params)
157157
if err != nil {
158158
return xerrors.Errorf("failed to marshalling call parameters: %v", err)
159159
}
160-
jsonParams := gojay.EmbeddedJSON(rawdata)
161160
id := ID{Number: c.seq.Add(1)}
162161

163162
req := &Request{
164163
ID: &id,
165164
Method: method,
166-
Params: &jsonParams,
165+
Params: jsonParams,
167166
}
168167

169168
// marshal the request now it is complete
@@ -231,18 +230,14 @@ func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err
231230
}
232231

233232
elapsed := time.Since(handling.start)
234-
var jsonParams gojay.EmbeddedJSON
233+
var jsonParams *gojay.EmbeddedJSON
235234
if err == nil {
236-
rawdata, err := gojay.Marshal(result)
237-
if err != nil {
238-
return xerrors.Errorf("failed to marshalling call parameters: %v", err)
239-
}
240-
jsonParams = gojay.EmbeddedJSON(rawdata)
235+
jsonParams, err = marshalToEmbedded(result)
241236
}
242237

243238
resp := &Response{
244239
ID: req.ID,
245-
Result: &jsonParams,
240+
Result: jsonParams,
246241
}
247242

248243
if err != nil {
@@ -301,3 +296,13 @@ func (d Direction) String() string {
301296
panic("unreachable")
302297
}
303298
}
299+
300+
func marshalToEmbedded(obj interface{}) (*gojay.EmbeddedJSON, error) {
301+
data, err := gojay.Marshal(obj)
302+
if err != nil {
303+
return nil, err
304+
}
305+
raw := gojay.EmbeddedJSON(data)
306+
307+
return &raw, nil
308+
}

0 commit comments

Comments
 (0)