json.go 593 B

1234567891011121314151617181920212223242526272829303132
  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. "encoding/json"
  8. )
  9. var (
  10. EnableDecoderUseNumber = false
  11. )
  12. type jsonBinding struct{}
  13. func (jsonBinding) Name() string {
  14. return "json"
  15. }
  16. func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
  17. decoder := json.NewDecoder(req.Body)
  18. if EnableDecoderUseNumber {
  19. decoder.UseNumber()
  20. }
  21. if err := decoder.Decode(obj); err != nil {
  22. return err
  23. }
  24. return validate(obj)
  25. }