2020-04-24 06:34:08 +00:00
|
|
|
package json
|
|
|
|
|
2020-05-08 00:09:45 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2020-05-07 19:32:56 +00:00
|
|
|
type Marshaler interface {
|
|
|
|
MarshalJSON() ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Unmarshaler interface {
|
|
|
|
UnmarshalJSON([]byte) error
|
|
|
|
}
|
|
|
|
|
2020-04-24 06:34:08 +00:00
|
|
|
// Raw is a raw encoded JSON value. It implements Marshaler and Unmarshaler and
|
|
|
|
// can be used to delay JSON decoding or precompute a JSON encoding. It's taken
|
|
|
|
// from encoding/json.
|
|
|
|
type Raw []byte
|
|
|
|
|
|
|
|
// Raw returns m as the JSON encoding of m.
|
|
|
|
func (m Raw) MarshalJSON() ([]byte, error) {
|
|
|
|
if m == nil {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Raw) UnmarshalJSON(data []byte) error {
|
|
|
|
*m = append((*m)[0:0], data...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 03:01:44 +00:00
|
|
|
func (m Raw) UnmarshalTo(v interface{}) error {
|
|
|
|
// Leave as nil.
|
|
|
|
if len(m) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return Unmarshal(m, v)
|
|
|
|
}
|
|
|
|
|
2020-04-24 06:34:08 +00:00
|
|
|
func (m Raw) String() string {
|
|
|
|
return string(m)
|
|
|
|
}
|
2020-05-08 00:09:45 +00:00
|
|
|
|
|
|
|
// AlwaysString would always unmarshal into a string, from any JSON type. Quotes
|
|
|
|
// will be stripped.
|
|
|
|
type AlwaysString string
|
|
|
|
|
|
|
|
func (m *AlwaysString) UnmarshalJSON(data []byte) error {
|
|
|
|
data = bytes.Trim(data, `"`)
|
|
|
|
*m = AlwaysString(data)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m AlwaysString) Int() (int, error) {
|
|
|
|
return strconv.Atoi(string(m))
|
|
|
|
}
|