diff options
| author | MHSanaei <mc.sanaei@gmail.com> | 2023-02-09 22:18:06 +0300 |
|---|---|---|
| committer | MHSanaei <mc.sanaei@gmail.com> | 2023-02-09 22:18:06 +0300 |
| commit | b73e4173a3c1e69e02ad6b4e3b43e425e57a5be9 (patch) | |
| tree | d95d2f5e903d97082e11eb9f9023c165b1bde388 /util/json_util | |
3x-ui
Diffstat (limited to 'util/json_util')
| -rw-r--r-- | util/json_util/json.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/util/json_util/json.go b/util/json_util/json.go new file mode 100644 index 00000000..65ad789e --- /dev/null +++ b/util/json_util/json.go @@ -0,0 +1,24 @@ +package json_util + +import ( + "errors" +) + +type RawMessage []byte + +// MarshalJSON 自定义 json.RawMessage 默认行为 +func (m RawMessage) MarshalJSON() ([]byte, error) { + if len(m) == 0 { + return []byte("null"), nil + } + return m, nil +} + +// UnmarshalJSON sets *m to a copy of data. +func (m *RawMessage) UnmarshalJSON(data []byte) error { + if m == nil { + return errors.New("json.RawMessage: UnmarshalJSON on nil pointer") + } + *m = append((*m)[0:0], data...) + return nil +} |
