2021-07-05 21:33:19 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultJSONSerializer implements JSON encoding using encoding/json.
|
|
|
|
type DefaultJSONSerializer struct{}
|
|
|
|
|
|
|
|
// Serialize converts an interface into a json and writes it to the response.
|
|
|
|
// You can optionally use the indent parameter to produce pretty JSONs.
|
|
|
|
func (d DefaultJSONSerializer) Serialize(c Context, i interface{}, indent string) error {
|
|
|
|
enc := json.NewEncoder(c.Response())
|
|
|
|
if indent != "" {
|
|
|
|
enc.SetIndent("", indent)
|
|
|
|
}
|
|
|
|
return enc.Encode(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize reads a JSON from a request body and converts it into an interface.
|
|
|
|
func (d DefaultJSONSerializer) Deserialize(c Context, i interface{}) error {
|
|
|
|
err := json.NewDecoder(c.Request().Body).Decode(i)
|
|
|
|
if ute, ok := err.(*json.UnmarshalTypeError); ok {
|
2021-07-15 22:34:01 +02:00
|
|
|
return NewHTTPErrorWithInternal(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Unmarshal type error: expected=%v, got=%v, field=%v, offset=%v", ute.Type, ute.Value, ute.Field, ute.Offset),
|
|
|
|
)
|
2021-07-05 21:33:19 +02:00
|
|
|
} else if se, ok := err.(*json.SyntaxError); ok {
|
2021-07-15 22:34:01 +02:00
|
|
|
return NewHTTPErrorWithInternal(http.StatusBadRequest,
|
|
|
|
err,
|
|
|
|
fmt.Sprintf("Syntax error: offset=%v, error=%v", se.Offset, se.Error()),
|
|
|
|
)
|
2021-07-05 21:33:19 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|