json.go 4.7 KB

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