json.go 4.8 KB

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