Skip to content

Commit ed2543a

Browse files
committed
types: add RawMessage struct type
1 parent 0e50c0c commit ed2543a

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

types.go

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
package jsonrpc2
66

77
import (
8+
"encoding/json"
9+
"errors"
10+
"io"
811
"strconv"
912

1013
"github.com/francoispqt/gojay"
@@ -54,6 +57,53 @@ type Message struct {
5457
JSONRPC string `json:"jsonrpc"`
5558
}
5659

60+
// RawMessage is the same as json.RawMessage.
61+
type RawMessage struct {
62+
*gojay.EmbeddedJSON
63+
}
64+
65+
// Read implements io.Reader.
66+
func (m *RawMessage) Read(p []byte) (n int, err error) {
67+
if len(p) == 0 || p == nil {
68+
return 0, nil
69+
}
70+
if m == nil {
71+
return 0, io.EOF
72+
}
73+
74+
n = copy(p, *m.EmbeddedJSON)
75+
76+
return n, nil
77+
}
78+
79+
// MarshalJSON implements json.Marshaler.
80+
//
81+
// MarshalJSON returns m as the JSON encoding of m.
82+
func (m RawMessage) MarshalJSON() ([]byte, error) {
83+
if m.EmbeddedJSON == nil {
84+
return []byte("null"), nil
85+
}
86+
87+
return *m.EmbeddedJSON, nil
88+
}
89+
90+
// UnmarshalJSON implements json.Unmarshaler.
91+
//
92+
// UnmarshalJSON sets *m to a copy of data.
93+
func (m *RawMessage) UnmarshalJSON(data []byte) error {
94+
if m == nil {
95+
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
96+
}
97+
98+
*m.EmbeddedJSON = append((*m.EmbeddedJSON)[0:0], data...)
99+
100+
return nil
101+
}
102+
103+
var _ io.Reader = (*RawMessage)(nil)
104+
var _ json.Marshaler = (*RawMessage)(nil)
105+
var _ json.Unmarshaler = (*RawMessage)(nil)
106+
57107
// Request is a request message to describe a request between the client and the server.
58108
//
59109
// Every processed request must send a response back to the sender of the request.
@@ -67,7 +117,7 @@ type Request struct {
67117
Method string `json:"method"`
68118

69119
// The method's params.
70-
Params *gojay.EmbeddedJSON `json:"params,omitempty"`
120+
Params *RawMessage `json:"params,omitempty"`
71121
}
72122

73123
// IsNotify returns true if this request is a notification.
@@ -91,7 +141,7 @@ type Response struct {
91141

92142
// The result of a request. This member is REQUIRED on success.
93143
// This member MUST NOT exist if there was an error invoking the method.
94-
Result *gojay.EmbeddedJSON `json:"result,omitempty"`
144+
Result *RawMessage `json:"result,omitempty"`
95145
}
96146

97147
// NotificationMessage is a notification message.
@@ -104,5 +154,5 @@ type NotificationMessage struct {
104154
Method string `json:"method"`
105155

106156
// Params is the notification's params.
107-
Params interface{} `json:"params,omitempty"`
157+
Params *RawMessage `json:"params,omitempty"`
108158
}

types_gen.go

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

0 commit comments

Comments
 (0)