json.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // PureJSON contains the given interface object.
  37. type PureJSON struct {
  38. Data interface{}
  39. }
  40. var jsonContentType = []string{"application/json; charset=utf-8"}
  41. var jsonpContentType = []string{"application/javascript; charset=utf-8"}
  42. var jsonAsciiContentType = []string{"application/json"}
  43. // Render (JSON) writes data with custom ContentType.
  44. func (r JSON) Render(w http.ResponseWriter) (err error) {
  45. if err = WriteJSON(w, r.Data); err != nil {
  46. panic(err)
  47. }
  48. return
  49. }
  50. // WriteContentType (JSON) writes JSON ContentType.
  51. func (r JSON) WriteContentType(w http.ResponseWriter) {
  52. writeContentType(w, jsonContentType)
  53. }
  54. // WriteJSON marshals the given interface object and writes it with custom ContentType.
  55. func WriteJSON(w http.ResponseWriter, obj interface{}) error {
  56. writeContentType(w, jsonContentType)
  57. jsonBytes, err := json.Marshal(obj)
  58. if err != nil {
  59. return err
  60. }
  61. _, err = w.Write(jsonBytes)
  62. return err
  63. }
  64. // Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
  65. func (r IndentedJSON) Render(w http.ResponseWriter) error {
  66. r.WriteContentType(w)
  67. jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
  68. if err != nil {
  69. return err
  70. }
  71. _, err = w.Write(jsonBytes)
  72. return err
  73. }
  74. // WriteContentType (IndentedJSON) writes JSON ContentType.
  75. func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {
  76. writeContentType(w, jsonContentType)
  77. }
  78. // Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.
  79. func (r SecureJSON) Render(w http.ResponseWriter) error {
  80. r.WriteContentType(w)
  81. jsonBytes, err := json.Marshal(r.Data)
  82. if err != nil {
  83. return err
  84. }
  85. // if the jsonBytes is array values
  86. if bytes.HasPrefix(jsonBytes, []byte("[")) && bytes.HasSuffix(jsonBytes, []byte("]")) {
  87. _, err = w.Write([]byte(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([]byte(callback))
  112. if err != nil {
  113. return err
  114. }
  115. _, err = w.Write([]byte("("))
  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([]byte(")"))
  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 string(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. }