mirror of
https://github.com/Bornholm/formidable.git
synced 2024-12-23 15:29:33 +01:00
41 lines
747 B
Go
41 lines
747 B
Go
package json
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"net/url"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"forge.cadoles.com/wpetit/formidable/internal/data/format"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type EncoderHandler struct{}
|
|
|
|
func (d *EncoderHandler) Match(url *url.URL) bool {
|
|
ext := filepath.Ext(path.Join(url.Host, url.Path))
|
|
|
|
return ext == ExtensionJSON ||
|
|
format.MatchURLQueryFormat(url, FormatJSON)
|
|
}
|
|
|
|
func (d *EncoderHandler) Encode(url *url.URL, data interface{}) (io.Reader, error) {
|
|
var buf bytes.Buffer
|
|
|
|
encoder := json.NewEncoder(&buf)
|
|
|
|
encoder.SetIndent("", " ")
|
|
|
|
if err := encoder.Encode(data); err != nil {
|
|
return nil, errors.WithStack(err)
|
|
}
|
|
|
|
return &buf, nil
|
|
}
|
|
|
|
func NewEncoderHandler() *EncoderHandler {
|
|
return &EncoderHandler{}
|
|
}
|