1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-17 20:32:48 +00:00
arikawa/utils/json/json.go

58 lines
1.4 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 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 DefaultDriver struct{}
2020-01-02 05:39:52 +00:00
func (d DefaultDriver) Marshal(v interface{}) ([]byte, error) {
2020-01-02 05:39:52 +00:00
return json.Marshal(v)
}
func (d DefaultDriver) Unmarshal(data []byte, v interface{}) error {
2020-01-02 05:39:52 +00:00
return json.Unmarshal(data, v)
}
func (d DefaultDriver) DecodeStream(r io.Reader, v interface{}) error {
2020-01-02 05:39:52 +00:00
return json.NewDecoder(r).Decode(v)
}
func (d DefaultDriver) EncodeStream(w io.Writer, v interface{}) error {
2020-01-02 05:39:52 +00:00
return json.NewEncoder(w).Encode(v)
}
// Default is the default JSON driver, which uses encoding/json.
var Default Driver = DefaultDriver{}
// Marshal uses the default driver.
func Marshal(v interface{}) ([]byte, error) {
return Default.Marshal(v)
}
// Unmarshal uses the default driver.
func Unmarshal(data []byte, v interface{}) error {
return Default.Unmarshal(data, v)
}
// DecodeStream uses the default driver.
func DecodeStream(r io.Reader, v interface{}) error {
return Default.DecodeStream(r, v)
}
// EncodeStream uses the default driver.
func EncodeStream(w io.Writer, v interface{}) error {
return Default.EncodeStream(w, v)
}