forked from c0mm4nd/go-jsonrpc2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.go
46 lines (36 loc) · 1.24 KB
/
new.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
44
45
46
package jsonrpc2
import json "encoding/json"
// NewJsonRpcRequest returns a JSON-RPC 2.0 request message structures. id must be string/int/nil type. params should be json marshaled
func NewJsonRpcRequest(id interface{}, method string, params []byte) *JsonRpcMessage {
paramsField := json.RawMessage(params)
p := &JsonRpcMessage{
Version: jsonRpcVersion,
Method: method,
Params: ¶msField,
ID: id,
}
return p
}
// NewJsonRpcNotification returns a JSON-RPC 2.0 notification message structures which doesnt have id. params should be json marshaled
func NewJsonRpcNotification(method string, params []byte) *JsonRpcMessage {
return NewJsonRpcRequest(nil, method, params)
}
// NewJsonRpcSuccess returns a JSON-RPC 2.0 success message structures. result should be json marshaled
func NewJsonRpcSuccess(id interface{}, result []byte) *JsonRpcMessage {
resultField := json.RawMessage(result)
p := &JsonRpcMessage{
Version: jsonRpcVersion,
Result: &resultField,
ID: id,
}
return p
}
// NewJsonRpcError returns a JSON-RPC 2.0 error message structures.
func NewJsonRpcError(id interface{}, errParams *Error) *JsonRpcMessage {
p := &JsonRpcMessage{
Version: jsonRpcVersion,
Error: errParams,
ID: id,
}
return p
}