json.go 569 B

123456789101112131415161718192021222324252627
  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. "net/http"
  7. "github.com/json-iterator/go"
  8. )
  9. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  10. type jsonBinding struct{}
  11. func (jsonBinding) Name() string {
  12. return "json"
  13. }
  14. func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
  15. decoder := json.NewDecoder(req.Body)
  16. if err := decoder.Decode(obj); err != nil {
  17. return err
  18. }
  19. return validate(obj)
  20. }