-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson.go
36 lines (30 loc) · 778 Bytes
/
json.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
33
34
35
36
package corde
import (
"encoding/json"
"errors"
)
// JsonRaw is a raw json value
type JsonRaw []byte
// MarshalJSON returns m as the JSON encoding of m.
func (m JsonRaw) MarshalJSON() ([]byte, error) {
if m == nil {
return []byte("null"), nil
}
return m, nil
}
// UnmarshalJSON sets *m to a copy of data.
func (m *JsonRaw) UnmarshalJSON(data []byte) error {
if m == nil {
return errors.New("json.RawMessage: UnmarshalJSON on nil pointer")
}
*m = append((*m)[0:0], data...)
return nil
}
// UnmarshalTo unmarshals the JSON-encoded data and stores the result in the value pointed to by v.
func (m JsonRaw) UnmarshalTo(v any) error {
return json.Unmarshal(m, v)
}
// String returns the raw json as a string
func (m JsonRaw) String() string {
return string(m)
}