json.go 513 B

1234567891011121314151617181920212223242526
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "encoding/json"
  7. "net/http"
  8. )
  9. type jsonBinding struct{}
  10. func (_ jsonBinding) Name() string {
  11. return "json"
  12. }
  13. func (_ jsonBinding) Bind(req *http.Request, obj interface{}) error {
  14. decoder := json.NewDecoder(req.Body)
  15. if err := decoder.Decode(obj); err == nil {
  16. return Validate(obj)
  17. } else {
  18. return err
  19. }
  20. }