1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-12-11 07:54:58 +00:00
arikawa/internal/json/json.go

94 lines
1.7 KiB
Go
Raw Normal View History

2020-01-15 04:56:50 +00:00
// Package json allows for different implementations of JSON serializing, as
// well as extra optional types needed.
2020-01-02 05:39:52 +00:00
package json
import (
"encoding/json"
"io"
)
type (
OptionBool = *bool
OptionString = *string
OptionUint = *uint
OptionInt = *int
)
var (
True = getBool(true)
False = getBool(false)
ZeroUint = Uint(0)
ZeroInt = Int(0)
EmptyString = String("")
)
func Uint(u uint) OptionUint {
return &u
}
func Int(i int) OptionInt {
return &i
}
func String(s string) OptionString {
return &s
}
func getBool(Bool bool) OptionBool {
return &Bool
}
2020-01-15 04:43:34 +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
}
2020-01-15 07:34:18 +00:00
func (m *Raw) UnmarshalJSON(data []byte) error {
*m = append((*m)[0:0], data...)
return nil
}
2020-01-15 04:43:34 +00:00
func (m Raw) String() string {
return string(m)
}
//
2020-01-02 05:39:52 +00:00
type Driver interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
DecodeStream(r io.Reader, v interface{}) error
EncodeStream(w io.Writer, v interface{}) error
}
type Default struct{}
func (d Default) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}
func (d Default) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
func (d Default) DecodeStream(r io.Reader, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}
func (d Default) EncodeStream(w io.Writer, v interface{}) error {
return json.NewEncoder(w).Encode(v)
}