http urls monitor.

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. "bytes"
  7. "io"
  8. "net/http"
  9. "github.com/gin-gonic/gin/json"
  10. )
  11. // EnableDecoderUseNumber is used to call the UseNumber method on the JSON
  12. // Decoder instance. UseNumber causes the Decoder to unmarshal a number into an
  13. // interface{} as a Number instead of as a float64.
  14. var EnableDecoderUseNumber = false
  15. type jsonBinding struct{}
  16. func (jsonBinding) Name() string {
  17. return "json"
  18. }
  19. func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
  20. return decodeJSON(req.Body, obj)
  21. }
  22. func (jsonBinding) BindBody(body []byte, obj interface{}) error {
  23. return decodeJSON(bytes.NewReader(body), obj)
  24. }
  25. func decodeJSON(r io.Reader, obj interface{}) error {
  26. decoder := json.NewDecoder(r)
  27. if EnableDecoderUseNumber {
  28. decoder.UseNumber()
  29. }
  30. if err := decoder.Decode(obj); err != nil {
  31. return err
  32. }
  33. return validate(obj)
  34. }