json.go 811 B

123456789101112131415161718192021222324252627282930313233
  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/gin-gonic/gin/json"
  8. )
  9. // EnableDecoderUseNumber is used to call the UseNumber method on the JSON
  10. // Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
  11. // interface{} as a Number instead of as a float64.
  12. var EnableDecoderUseNumber = false
  13. type jsonBinding struct{}
  14. func (jsonBinding) Name() string {
  15. return "json"
  16. }
  17. func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
  18. decoder := json.NewDecoder(req.Body)
  19. if EnableDecoderUseNumber {
  20. decoder.UseNumber()
  21. }
  22. if err := decoder.Decode(obj); err != nil {
  23. return err
  24. }
  25. return validate(obj)
  26. }