File size: 1,204 Bytes
9853396 | 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | package objects
import (
"encoding/json"
"errors"
"io"
)
type JSONRawMessage []byte
// MarshalJSON returns m as the JSON encoding of m.
func (m JSONRawMessage) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return m, nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *JSONRawMessage) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
*m = append((*m)[0:0], data...)
return nil
}
// MarshalGQL returns m as the JSON encoding of m.
func (m JSONRawMessage) MarshalGQL(w io.Writer) {
if m == nil {
_, _ = w.Write([]byte("null"))
return
}
_, _ = w.Write(m)
}
// UnmarshalGQL sets *m to a copy of data.
func (m *JSONRawMessage) UnmarshalGQL(v any) error {
if m == nil {
return errors.New("json.RawMessage: UnmarshalGQL on nil pointer")
}
switch v := v.(type) {
case *JSONRawMessage:
*m = append((*m)[0:0], *v...)
return nil
case *string:
*v = string(*m)
return nil
case *[]byte:
*v = append((*v)[0:0], *m...)
return nil
case *map[string]any:
return json.Unmarshal(*m, v)
}
return nil
}
|