json.go 373 B

12345678910111213141516171819202122
  1. package binding
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "github.com/pkg/errors"
  6. )
  7. type jsonBinding struct{}
  8. func (jsonBinding) Name() string {
  9. return "json"
  10. }
  11. func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
  12. decoder := json.NewDecoder(req.Body)
  13. if err := decoder.Decode(obj); err != nil {
  14. return errors.WithStack(err)
  15. }
  16. return validate(obj)
  17. }