gotoolkit/encoding/json.go
2024-11-23 19:41:21 +01:00

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{}
}