-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
41 lines (35 loc) · 891 Bytes
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package rpc
import (
"encoding/json"
"io"
)
// clientResponse represents a JSON-RPC response returned to a client.
type ClientResponse struct {
Version string `json:"jsonrpc"`
Result *json.RawMessage `json:"result"`
Error *json.RawMessage `json:"error"`
Id *uint64 `json:"id"`
}
func (c ClientResponse) Decode(reply interface{}) error {
if c.Error != nil {
jsonErr := &Error{}
if err := json.Unmarshal(*c.Error, jsonErr); err != nil {
return &Error{
Code: E_SERVER,
Message: string(*c.Error),
}
}
return jsonErr
}
if c.Result == nil {
return ErrNullResult
}
return json.Unmarshal(*c.Result, reply)
}
func decodeResponse(r io.Reader, reply interface{}) error {
var c ClientResponse
if err := json.NewDecoder(r).Decode(&c); err != nil {
return err
}
return c.Decode(reply)
}