-
Notifications
You must be signed in to change notification settings - Fork 3
/
codec.go
43 lines (37 loc) · 1005 Bytes
/
codec.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
42
43
package codec
import (
"encoding/json"
"fmt"
)
// Request is a jsonrpc request
type Request struct {
JsonRPC string `json:"jsonrpc"`
ID uint64 `json:"id"`
Method string `json:"method"`
Params json.RawMessage `json:"params"`
}
// Response is a jsonrpc response
type Response struct {
ID uint64 `json:"id"`
Result json.RawMessage `json:"result"`
Error *ErrorObject `json:"error,omitempty"`
}
// ErrorObject is a jsonrpc error
type ErrorObject struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
// Subscription is a jsonrpc subscription
type Subscription struct {
ID string `json:"subscription"`
Result json.RawMessage `json:"result"`
}
// Error implements error interface
func (e *ErrorObject) Error() string {
data, err := json.Marshal(e)
if err != nil {
return fmt.Sprintf("jsonrpc.internal marshal error: %v", err)
}
return string(data)
}