1package easyjson
2
3import (
4	"github.com/mailru/easyjson/jlexer"
5	"github.com/mailru/easyjson/jwriter"
6)
7
8// RawMessage is a raw piece of JSON (number, string, bool, object, array or
9// null) that is extracted without parsing and output as is during marshaling.
10type RawMessage []byte
11
12// MarshalEasyJSON does JSON marshaling using easyjson interface.
13func (v *RawMessage) MarshalEasyJSON(w *jwriter.Writer) {
14	if len(*v) == 0 {
15		w.RawString("null")
16	} else {
17		w.Raw(*v, nil)
18	}
19}
20
21// UnmarshalEasyJSON does JSON unmarshaling using easyjson interface.
22func (v *RawMessage) UnmarshalEasyJSON(l *jlexer.Lexer) {
23	*v = RawMessage(l.Raw())
24}
25
26// UnmarshalJSON implements encoding/json.Unmarshaler interface.
27func (v *RawMessage) UnmarshalJSON(data []byte) error {
28	*v = data
29	return nil
30}
31
32var nullBytes = []byte("null")
33
34// MarshalJSON implements encoding/json.Marshaler interface.
35func (v RawMessage) MarshalJSON() ([]byte, error) {
36	if len(v) == 0 {
37		return nullBytes, nil
38	}
39	return v, nil
40}
41
42// IsDefined is required for integration with omitempty easyjson logic.
43func (v *RawMessage) IsDefined() bool {
44	return len(*v) > 0
45}
46