Skip to content

Commit bbff146

Browse files
committed
types: move RawMessage to types_gojay.go
1 parent 1af33bc commit bbff146

File tree

2 files changed

+44
-45
lines changed

2 files changed

+44
-45
lines changed

types.go

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ package jsonrpc2
66

77
import (
88
"encoding/json"
9-
"errors"
109
"strconv"
11-
"unsafe"
12-
13-
"github.com/francoispqt/gojay"
1410
)
1511

1612
// Version represents a JSONRPC version.
@@ -62,47 +58,6 @@ func (id *ID) UnmarshalJSON(data []byte) error {
6258
var _ json.Marshaler = (*ID)(nil)
6359
var _ json.Unmarshaler = (*ID)(nil)
6460

65-
// RawMessage mimic json.RawMessage
66-
// RawMessage is a raw encoded JSON value.
67-
// It implements Marshaler and Unmarshaler and can
68-
// be used to delay JSON decoding or precompute a JSON encoding.
69-
type RawMessage gojay.EmbeddedJSON
70-
71-
func (m RawMessage) String() string {
72-
if m == nil {
73-
return ""
74-
}
75-
76-
return *(*string)(unsafe.Pointer(&m))
77-
}
78-
79-
// MarshalJSON implements json.Marshaler.
80-
//
81-
// The returns m as the JSON encoding of m.
82-
func (m RawMessage) MarshalJSON() ([]byte, error) {
83-
if m == nil {
84-
return []byte{110, 117, 108, 108}, nil // null
85-
}
86-
87-
return m, nil
88-
}
89-
90-
// UnmarshalJSON implements json.Unmarshaler.
91-
//
92-
// The sets *m to a copy of data.
93-
func (m *RawMessage) UnmarshalJSON(data []byte) error {
94-
if m == nil {
95-
return errors.New("jsonrpc2.RawMessage: UnmarshalJSON on nil pointer")
96-
}
97-
98-
*m = append((*m)[0:0], data...)
99-
100-
return nil
101-
}
102-
103-
var _ json.Marshaler = (*RawMessage)(nil)
104-
var _ json.Unmarshaler = (*RawMessage)(nil)
105-
10661
// Request is a request message to describe a request between the client and the server.
10762
//
10863
// Every processed request must send a response back to the sender of the request.

types_gojay.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,54 @@ package jsonrpc2
88

99
import (
1010
"encoding/json"
11+
"errors"
12+
"unsafe"
1113

1214
"github.com/francoispqt/gojay"
1315
)
1416

17+
// RawMessage mimic json.RawMessage.
18+
//
19+
// RawMessage is a raw encoded JSON value.
20+
// It implements Marshaler and Unmarshaler and can
21+
// be used to delay JSON decoding or precompute a JSON encoding.
22+
type RawMessage gojay.EmbeddedJSON
23+
24+
func (m RawMessage) String() string {
25+
if m == nil {
26+
return ""
27+
}
28+
29+
return *(*string)(unsafe.Pointer(&m))
30+
}
31+
32+
// MarshalJSON implements json.Marshaler.
33+
//
34+
// The returns m as the JSON encoding of m.
35+
func (m RawMessage) MarshalJSON() ([]byte, error) {
36+
if m == nil {
37+
return []byte{110, 117, 108, 108}, nil // null
38+
}
39+
40+
return m, nil
41+
}
42+
43+
// UnmarshalJSON implements json.Unmarshaler.
44+
//
45+
// The sets *m to a copy of data.
46+
func (m *RawMessage) UnmarshalJSON(data []byte) error {
47+
if m == nil {
48+
return errors.New("jsonrpc2.RawMessage: UnmarshalJSON on nil pointer")
49+
}
50+
51+
*m = append((*m)[0:0], data...)
52+
53+
return nil
54+
}
55+
56+
var _ json.Marshaler = (*RawMessage)(nil)
57+
var _ json.Unmarshaler = (*RawMessage)(nil)
58+
1559
// UnmarshalJSONObject implements gojay's UnmarshalerJSONObject
1660
func (v *Request) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
1761
switch k {

0 commit comments

Comments
 (0)