json_17.go 772 B

12345678910111213141516171819202122232425262728293031
  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 render
  6. import (
  7. "net/http"
  8. "github.com/gin-gonic/gin/internal/json"
  9. )
  10. // PureJSON contains the given interface object.
  11. type PureJSON struct {
  12. Data interface{}
  13. }
  14. // Render (PureJSON) writes custom ContentType and encodes the given interface object.
  15. func (r PureJSON) Render(w http.ResponseWriter) error {
  16. r.WriteContentType(w)
  17. encoder := json.NewEncoder(w)
  18. encoder.SetEscapeHTML(false)
  19. return encoder.Encode(r.Data)
  20. }
  21. // WriteContentType (PureJSON) writes custom ContentType.
  22. func (r PureJSON) WriteContentType(w http.ResponseWriter) {
  23. writeContentType(w, jsonContentType)
  24. }