-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonrpc2.go
52 lines (41 loc) · 934 Bytes
/
jsonrpc2.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
package lsp
import (
"encoding/json"
"strconv"
)
// ID is a JSON-RPC 2.0 request ID. It can be either a string or a number.
type ID struct {
AsInteger uint64
AsString string
IsString bool
}
func (id ID) String() string {
if id.IsString {
return strconv.Quote(id.AsString)
}
return strconv.FormatUint(id.AsInteger, 10)
}
// MarshalJSON will turn the ID into a JSON string.
func (id ID) MarshalJSON() ([]byte, error) {
if id.IsString {
return json.Marshal(id.AsString)
}
return json.Marshal(id.AsInteger)
}
// UnmarshalJSON will turn the passed data into an ID struct.
func (id *ID) UnmarshalJSON(data []byte) error {
var asInteger uint64
if err := json.Unmarshal(data, &asInteger); err == nil {
*id = ID{AsInteger: asInteger}
return nil
}
var asString string
if err := json.Unmarshal(data, &asString); err != nil {
return err
}
*id = ID{
AsString: asString,
IsString: true,
}
return nil
}