json.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. encoder := json.NewEncoder(w)
  58. err := encoder.Encode(&obj)
  59. return err
  60. }
  61. // Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
  62. func (r IndentedJSON) Render(w http.ResponseWriter) error {
  63. r.WriteContentType(w)
  64. jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
  65. if err != nil {
  66. return err
  67. }
  68. _, err = w.Write(jsonBytes)
  69. return err
  70. }
  71. // WriteContentType (IndentedJSON) writes JSON ContentType.
  72. func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {
  73. writeContentType(w, jsonContentType)
  74. }
  75. // Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.
  76. func (r SecureJSON) Render(w http.ResponseWriter) error {
  77. r.WriteContentType(w)
  78. jsonBytes, err := json.Marshal(r.Data)
  79. if err != nil {
  80. return err
  81. }
  82. // if the jsonBytes is array values
  83. if bytes.HasPrefix(jsonBytes, []byte("[")) && bytes.HasSuffix(jsonBytes, []byte("]")) {
  84. _, err = w.Write([]byte(r.Prefix))
  85. if err != nil {
  86. return err
  87. }
  88. }
  89. _, err = w.Write(jsonBytes)
  90. return err
  91. }
  92. // WriteContentType (SecureJSON) writes JSON ContentType.
  93. func (r SecureJSON) WriteContentType(w http.ResponseWriter) {
  94. writeContentType(w, jsonContentType)
  95. }
  96. // Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.
  97. func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
  98. r.WriteContentType(w)
  99. ret, err := json.Marshal(r.Data)
  100. if err != nil {
  101. return err
  102. }
  103. if r.Callback == "" {
  104. _, err = w.Write(ret)
  105. return err
  106. }
  107. callback := template.JSEscapeString(r.Callback)
  108. _, err = w.Write([]byte(callback))
  109. if err != nil {
  110. return err
  111. }
  112. _, err = w.Write([]byte("("))
  113. if err != nil {
  114. return err
  115. }
  116. _, err = w.Write(ret)
  117. if err != nil {
  118. return err
  119. }
  120. _, err = w.Write([]byte(");"))
  121. if err != nil {
  122. return err
  123. }
  124. return nil
  125. }
  126. // WriteContentType (JsonpJSON) writes Javascript ContentType.
  127. func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {
  128. writeContentType(w, jsonpContentType)
  129. }
  130. // Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
  131. func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
  132. r.WriteContentType(w)
  133. ret, err := json.Marshal(r.Data)
  134. if err != nil {
  135. return err
  136. }
  137. var buffer bytes.Buffer
  138. for _, r := range string(ret) {
  139. cvt := string(r)
  140. if r >= 128 {
  141. cvt = fmt.Sprintf("\\u%04x", int64(r))
  142. }
  143. buffer.WriteString(cvt)
  144. }
  145. _, err = w.Write(buffer.Bytes())
  146. return err
  147. }
  148. // WriteContentType (AsciiJSON) writes JSON ContentType.
  149. func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {
  150. writeContentType(w, jsonAsciiContentType)
  151. }
  152. // Render (PureJSON) writes custom ContentType and encodes the given interface object.
  153. func (r PureJSON) Render(w http.ResponseWriter) error {
  154. r.WriteContentType(w)
  155. encoder := json.NewEncoder(w)
  156. encoder.SetEscapeHTML(false)
  157. return encoder.Encode(r.Data)
  158. }
  159. // WriteContentType (PureJSON) writes custom ContentType.
  160. func (r PureJSON) WriteContentType(w http.ResponseWriter) {
  161. writeContentType(w, jsonContentType)
  162. }