CoolFace
Apppublic

llzai/axonhub

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
json.go63 linesDownload Raw Back to objects
1package objects
2
3import (
4	"encoding/json"
5	"errors"
6	"io"
7)
8
9type JSONRawMessage []byte
10
11// MarshalJSON returns m as the JSON encoding of m.
12func (m JSONRawMessage) MarshalJSON() ([]byte, error) {
13	if m == nil {
14		return []byte("null"), nil
15	}
16
17	return m, nil
18}
19
20// UnmarshalJSON sets *m to a copy of data.
21func (m *JSONRawMessage) UnmarshalJSON(data []byte) error {
22	if m == nil {
23		return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
24	}
25
26	*m = append((*m)[0:0], data...)
27
28	return nil
29}
30
31// MarshalGQL returns m as the JSON encoding of m.
32func (m JSONRawMessage) MarshalGQL(w io.Writer) {
33	if m == nil {
34		_, _ = w.Write([]byte("null"))
35		return
36	}
37
38	_, _ = w.Write(m)
39}
40
41// UnmarshalGQL sets *m to a copy of data.
42func (m *JSONRawMessage) UnmarshalGQL(v any) error {
43	if m == nil {
44		return errors.New("json.RawMessage: UnmarshalGQL on nil pointer")
45	}
46
47	switch v := v.(type) {
48	case *JSONRawMessage:
49		*m = append((*m)[0:0], *v...)
50		return nil
51	case *string:
52		*v = string(*m)
53		return nil
54	case *[]byte:
55		*v = append((*v)[0:0], *m...)
56		return nil
57	case *map[string]any:
58		return json.Unmarshal(*m, v)
59	}
60
61	return nil
62}
63