Skip to content

Commit dfee05f

Browse files
committed
types: fix ID methods to MarshalJSON and UnmarshalJSON
1 parent 33e9882 commit dfee05f

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

types.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
package jsonrpc2
66

7+
import (
8+
"strconv"
9+
)
10+
711
// ID is a Request identifier.
812
// Only one of either the Name or Number members will be set, using the
913
// number form if the Name is the empty string.
@@ -12,6 +16,36 @@ type ID struct {
1216
Number int64
1317
}
1418

19+
// String returns a string representation of the ID.
20+
// The representation is non ambiguous, string forms are quoted, number forms
21+
// are preceded by a #.
22+
func (id *ID) String() string {
23+
if id == nil {
24+
return ""
25+
}
26+
if id.Name != "" {
27+
return strconv.Quote(id.Name)
28+
}
29+
return "#" + strconv.FormatInt(id.Number, 10)
30+
}
31+
32+
// // MarshalJSON implements json.MarshalJSON.
33+
// func (id *ID) MarshalJSON() ([]byte, error) {
34+
// if id.Name != "" {
35+
// return json.Marshal(id.Name)
36+
// }
37+
// return json.Marshal(id.Number)
38+
// }
39+
//
40+
// // MarshalJSON implements json.UnmarshalJSON.
41+
// func (id *ID) UnmarshalJSON(data []byte) error {
42+
// *id = ID{}
43+
// if err := json.Unmarshal(data, &id.Number); err == nil {
44+
// return nil
45+
// }
46+
// return json.Unmarshal(data, &id.Name)
47+
// }
48+
1549
// Message is a general message as defined by JSON-RPC. The language server protocol always uses "2.0" as the jsonrpc version.
1650
type Message struct {
1751
JSONRPC string `json:"jsonrpc"`

types_gen.go

Lines changed: 14 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)