Skip to content

Commit e0253a8

Browse files
committed
types: remove Read method on RawMessage
1 parent b5c5420 commit e0253a8

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

types.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ package jsonrpc2
77
import (
88
"encoding/json"
99
"errors"
10-
"io"
1110
"strconv"
11+
"unsafe"
1212

1313
"github.com/francoispqt/gojay"
1414
)
@@ -56,51 +56,62 @@ func (id *ID) UnmarshalJSON(data []byte) error {
5656
return json.Unmarshal(data, &id.Name)
5757
}
5858

59-
// RawMessage is the same as json.RawMessage.
59+
// RawMessage mimic json.RawMessage
60+
// RawMessage is a raw encoded JSON value.
61+
// It implements Marshaler and Unmarshaler and can
62+
// be used to delay JSON decoding or precompute a JSON encoding.
6063
type RawMessage gojay.EmbeddedJSON
6164

62-
var _ io.Reader = (*RawMessage)(nil)
63-
var _ json.Marshaler = (*RawMessage)(nil)
64-
var _ json.Unmarshaler = (*RawMessage)(nil)
65-
66-
// Read implements io.Reader.
67-
func (m *RawMessage) Read(p []byte) (n int, err error) {
68-
if len(p) == 0 || p == nil {
69-
return 0, nil
70-
}
65+
func (m RawMessage) String() string {
7166
if m == nil {
72-
return 0, io.EOF
67+
return ""
7368
}
7469

75-
n = copy(p, *m)
76-
77-
return n, nil
70+
return *(*string)(unsafe.Pointer(&m))
7871
}
7972

73+
// Read implements io.Reader.
74+
// func (m *RawMessage) Read(p []byte) (n int, err error) {
75+
// if len(p) == 0 || p == nil {
76+
// return 0, nil
77+
// }
78+
// if m == nil {
79+
// return 0, io.EOF
80+
// }
81+
//
82+
// n = copy(p, *m)
83+
//
84+
// return n, nil
85+
// }
86+
8087
// MarshalJSON implements json.Marshaler.
8188
//
82-
// MarshalJSON returns m as the JSON encoding of m.
89+
// The returns m as the JSON encoding of m.
8390
func (m RawMessage) MarshalJSON() ([]byte, error) {
84-
if &m == nil {
85-
return []byte("null"), nil
91+
if m == nil {
92+
return []byte{110, 117, 108, 108}, nil // null
8693
}
8794

8895
return m, nil
8996
}
9097

9198
// UnmarshalJSON implements json.Unmarshaler.
9299
//
93-
// UnmarshalJSON sets *m to a copy of data.
100+
// The sets *m to a copy of data.
94101
func (m *RawMessage) UnmarshalJSON(data []byte) error {
95102
if m == nil {
96-
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
103+
return errors.New("jsonrpc2.RawMessage: UnmarshalJSON on nil pointer")
97104
}
98105

99106
*m = append((*m)[0:0], data...)
100107

101108
return nil
102109
}
103110

111+
// var _ io.Reader = (*RawMessage)(nil)
112+
var _ json.Marshaler = (*RawMessage)(nil)
113+
var _ json.Unmarshaler = (*RawMessage)(nil)
114+
104115
// Request is a request message to describe a request between the client and the server.
105116
//
106117
// Every processed request must send a response back to the sender of the request.

0 commit comments

Comments
 (0)