Debian Codegraph @

jsonrpc2.go

raw symbols SCIP
1 package lsp
2
3 import (
4 "encoding/json"
5 "strconv"
6 )
7
8 // ID represents a JSON-RPC 2.0 request ID, which may be either a
9 // string or number (or null, which is unsupported).
10 type ID struct {
11 // At most one of Num or Str may be nonzero. If both are zero
12 // valued, then IsNum specifies which field's value is to be used
13 // as the ID.
14 Num uint64
15 Str string
16
17 // IsString controls whether the Num or Str field's value should be
18 // used as the ID, when both are zero valued. It must always be
19 // set to true if the request ID is a string.
20 IsString bool
21 }
22
23 func (id ID) String() string {
24 if id.IsString {
25 return strconv.Quote(id.Str)
26 }
27 return strconv.FormatUint(id.Num, 10)
28 }
29
30 // MarshalJSON implements json.Marshaler.
31 func (id ID) MarshalJSON() ([]byte, error) {
32 if id.IsString {
33 return json.Marshal(id.Str)
34 }
35 return json.Marshal(id.Num)
36 }
37
38 // UnmarshalJSON implements json.Unmarshaler.
39 func (id *ID) UnmarshalJSON(data []byte) error {
40 // Support both uint64 and string IDs.
41 var v uint64
42 if err := json.Unmarshal(data, &v); err == nil {
43 *id = ID{Num: v}
44 return nil
45 }
46 var v2 string
47 if err := json.Unmarshal(data, &v2); err != nil {
48 return err
49 }
50 *id = ID{Str: v2, IsString: true}
51 return nil
52 }