json.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 render
  5. import (
  6. "bytes"
  7. "fmt"
  8. "html/template"
  9. "net/http"
  10. "github.com/gin-gonic/gin/internal/json"
  11. )
  12. // JSON contains the given interface object.
  13. type JSON struct {
  14. Data interface{}
  15. }
  16. // IndentedJSON contains the given interface object.
  17. type IndentedJSON struct {
  18. Data interface{}
  19. }
  20. // SecureJSON contains the given interface object and its prefix.
  21. type SecureJSON struct {
  22. Prefix string
  23. Data interface{}
  24. }
  25. // JsonpJSON contains the given interface object its callback.
  26. type JsonpJSON struct {
  27. Callback string
  28. Data interface{}
  29. }
  30. // AsciiJSON contains the given interface object.
  31. type AsciiJSON struct {
  32. Data interface{}
  33. }
  34. // SecureJSONPrefix is a string which represents SecureJSON prefix.
  35. type SecureJSONPrefix string
  36. var jsonContentType = []string{"application/json; charset=utf-8"}
  37. var jsonpContentType = []string{"application/javascript; charset=utf-8"}
  38. var jsonAsciiContentType = []string{"application/json"}
  39. // Render (JSON) writes data with custom ContentType.
  40. func (r JSON) Render(w http.ResponseWriter) (err error) {
  41. if err = WriteJSON(w, r.Data); err != nil {
  42. panic(err)
  43. }
  44. return
  45. }
  46. // WriteContentType (JSON) writes JSON ContentType.
  47. func (r JSON) WriteContentType(w http.ResponseWriter) {
  48. writeContentType(w, jsonContentType)
  49. }
  50. // WriteJSON marshals the given interface object and writes it with custom ContentType.
  51. func WriteJSON(w http.ResponseWriter, obj interface{}) error {
  52. writeContentType(w, jsonContentType)
  53. jsonBytes, err := json.Marshal(obj)
  54. if err != nil {
  55. return err
  56. }
  57. w.Write(jsonBytes)
  58. return nil
  59. }
  60. // Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
  61. func (r IndentedJSON) Render(w http.ResponseWriter) error {
  62. r.WriteContentType(w)
  63. jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
  64. if err != nil {
  65. return err
  66. }
  67. w.Write(jsonBytes)
  68. return nil
  69. }
  70. // WriteContentType (IndentedJSON) writes JSON ContentType.
  71. func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {
  72. writeContentType(w, jsonContentType)
  73. }
  74. // Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.
  75. func (r SecureJSON) Render(w http.ResponseWriter) error {
  76. r.WriteContentType(w)
  77. jsonBytes, err := json.Marshal(r.Data)
  78. if err != nil {
  79. return err
  80. }
  81. // if the jsonBytes is array values
  82. if bytes.HasPrefix(jsonBytes, []byte("[")) && bytes.HasSuffix(jsonBytes, []byte("]")) {
  83. w.Write([]byte(r.Prefix))
  84. }
  85. w.Write(jsonBytes)
  86. return nil
  87. }
  88. // WriteContentType (SecureJSON) writes JSON ContentType.
  89. func (r SecureJSON) WriteContentType(w http.ResponseWriter) {
  90. writeContentType(w, jsonContentType)
  91. }
  92. // Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.
  93. func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
  94. r.WriteContentType(w)
  95. ret, err := json.Marshal(r.Data)
  96. if err != nil {
  97. return err
  98. }
  99. if r.Callback == "" {
  100. w.Write(ret)
  101. return nil
  102. }
  103. callback := template.JSEscapeString(r.Callback)
  104. w.Write([]byte(callback))
  105. w.Write([]byte("("))
  106. w.Write(ret)
  107. w.Write([]byte(")"))
  108. return nil
  109. }
  110. // WriteContentType (JsonpJSON) writes Javascript ContentType.
  111. func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {
  112. writeContentType(w, jsonpContentType)
  113. }
  114. // Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
  115. func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
  116. r.WriteContentType(w)
  117. ret, err := json.Marshal(r.Data)
  118. if err != nil {
  119. return err
  120. }
  121. var buffer bytes.Buffer
  122. for _, r := range string(ret) {
  123. cvt := string(r)
  124. if r >= 128 {
  125. cvt = fmt.Sprintf("\\u%04x", int64(r))
  126. }
  127. buffer.WriteString(cvt)
  128. }
  129. w.Write(buffer.Bytes())
  130. return nil
  131. }
  132. // WriteContentType (AsciiJSON) writes JSON ContentType.
  133. func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {
  134. writeContentType(w, jsonAsciiContentType)
  135. }