32 lines
600 B
Go
32 lines
600 B
Go
package encoding
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
|
|
"git.nakama.town/fmartingr/gotoolkit/model"
|
|
)
|
|
|
|
var (
|
|
_ model.Encoder = &JSONEncoding{}
|
|
_ model.Decoder = &JSONEncoding{}
|
|
)
|
|
|
|
type JSONEncoding struct{}
|
|
|
|
func (e *JSONEncoding) Encode(v any) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
func (e *JSONEncoding) Decode(input []byte, output any) error {
|
|
return json.Unmarshal(input, output)
|
|
}
|
|
|
|
func (e *JSONEncoding) DecodeReader(input io.Reader, output any) error {
|
|
decoder := json.NewDecoder(input)
|
|
return decoder.Decode(output)
|
|
}
|
|
|
|
func NewJSONEncoding() *JSONEncoding {
|
|
return &JSONEncoding{}
|
|
}
|