-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
73 lines (64 loc) · 1.57 KB
/
example_test.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package jsonrpc_test
import (
"fmt"
"github.com/sb-im/jsonrpc-lite"
)
func ExampleParse() {
rpc, err := jsonrpc.Parse([]byte(`
{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
`))
if err != nil {
panic(err)
}
if rpc.Type != jsonrpc.TypeRequest {
fmt.Println(rpc)
}
fmt.Println(rpc)
}
func ExampleParseObject() {
rpc := jsonrpc.ParseObject([]byte(`
{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}
`))
if rpc.Type != jsonrpc.TypeRequest {
fmt.Println(rpc)
}
fmt.Println(rpc)
}
func ExampleBatch() {
rpcs := jsonrpc.Batch([]byte(`
[
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
{"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"},
{"foo": "boo"},
{"jsonrpc": "2.0", "method": "foo.get", "params": {"name": "myself"}, "id": "5"},
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
]
`))
for i, rpc := range rpcs {
fmt.Println(i, rpc)
}
}
func ExampleNewRequest() {
data, err := jsonrpc.NewRequest("123", "test", []string{"sss", "zzz"}).ToJSON()
if err != nil {
panic(err)
}
fmt.Printf("%s\n", data)
}
func ExampleNewSuccess() {
data, err := jsonrpc.NewSuccess(1234, []string{"sss", "zzz"}).ToJSON()
if err != nil {
panic(err)
}
fmt.Printf("%s\n", data)
}
func ExampleNewErrors() {
res := jsonrpc.NewErrors(1234)
res.Errors.ParseError([]string{"data", "data2"})
data, err := res.ToJSON()
if err != nil {
panic(err)
}
fmt.Printf("%s\n", data)
}