-
Notifications
You must be signed in to change notification settings - Fork 8
/
raw.go
32 lines (28 loc) · 873 Bytes
/
raw.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package protocol
import (
"bytes"
"errors"
)
// Raw type used by Centrifuge protocol as a type for fields in structs which
// value we want to stay untouched. For example custom application specific JSON
// payload data in published message. This is very similar to json.RawMessage
// type but have some extra methods to fit gogo/protobuf custom type interface.
type Raw []byte
// MarshalJSON returns *r as the JSON encoding of r.
func (r Raw) MarshalJSON() ([]byte, error) {
if r == nil {
return []byte("null"), nil
}
if !bytes.Contains(r, []byte("\n")) {
return r, nil
}
return bytes.ReplaceAll(r, []byte("\n"), []byte("")), nil
}
// UnmarshalJSON sets *r to a copy of data.
func (r *Raw) UnmarshalJSON(data []byte) error {
if r == nil {
return errors.New("unmarshal Raw: UnmarshalJSON on nil pointer")
}
*r = append((*r)[0:0], data...)
return nil
}