context_17.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2018 Gin Core Team. 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. // +build go1.7
  5. package gin
  6. import (
  7. "time"
  8. "github.com/gin-gonic/gin/render"
  9. )
  10. // PureJSON serializes the given struct as JSON into the response body.
  11. // PureJSON, unlike JSON, does not replace special html characters with their unicode entities.
  12. func (c *Context) PureJSON(code int, obj interface{}) {
  13. c.Render(code, render.PureJSON{Data: obj})
  14. }
  15. /************************************/
  16. /***** GOLANG.ORG/X/NET/CONTEXT *****/
  17. /************************************/
  18. // Deadline returns the time when work done on behalf of this context
  19. // should be canceled. Deadline returns ok==false when no deadline is
  20. // set. Successive calls to Deadline return the same results.
  21. func (c *Context) Deadline() (time.Time, bool) {
  22. return c.Request.Context().Deadline()
  23. }
  24. // Done returns a channel that's closed when work done on behalf of this
  25. // context should be canceled. Done may return nil if this context can
  26. // never be canceled. Successive calls to Done return the same value.
  27. func (c *Context) Done() <-chan struct{} {
  28. return c.Request.Context().Done()
  29. }
  30. // Err returns a non-nil error value after Done is closed,
  31. // successive calls to Err return the same error.
  32. // If Done is not yet closed, Err returns nil.
  33. // If Done is closed, Err returns a non-nil error explaining why:
  34. // Canceled if the context was canceled
  35. // or DeadlineExceeded if the context's deadline passed.
  36. func (c *Context) Err() error {
  37. return c.Request.Context().Err()
  38. }