json.go
This file
Path
render/json.go
194 lines · 4.9 KiB · Go
Last changed by
feat: support custom json codec at runtime (#3391)Timbefore adoption688a429
History of this fileTake it
1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package render67import (8 "bytes"9 "fmt"10 "html/template"11 "net/http"12 "unicode"1314 "github.com/gin-gonic/gin/codec/json"15 "github.com/gin-gonic/gin/internal/bytesconv"16)1718// JSON contains the given interface object.19type JSON struct {20 Data any21}2223// IndentedJSON contains the given interface object.24type IndentedJSON struct {25 Data any26}2728// SecureJSON contains the given interface object and its prefix.29type SecureJSON struct {30 Prefix string31 Data any32}3334// JsonpJSON contains the given interface object its callback.35type JsonpJSON struct {36 Callback string37 Data any38}3940// AsciiJSON contains the given interface object.41type AsciiJSON struct {42 Data any43}4445// PureJSON contains the given interface object.46type PureJSON struct {47 Data any48}4950var (51 jsonContentType = []string{"application/json; charset=utf-8"}52 jsonpContentType = []string{"application/javascript; charset=utf-8"}53 jsonASCIIContentType = []string{"application/json"}54)5556// Render (JSON) writes data with custom ContentType.57func (r JSON) Render(w http.ResponseWriter) error {58 return WriteJSON(w, r.Data)59}6061// WriteContentType (JSON) writes JSON ContentType.62func (r JSON) WriteContentType(w http.ResponseWriter) {63 writeContentType(w, jsonContentType)64}6566// WriteJSON marshals the given interface object and writes it with custom ContentType.67func WriteJSON(w http.ResponseWriter, obj any) error {68 writeContentType(w, jsonContentType)69 jsonBytes, err := json.API.Marshal(obj)70 if err != nil {71 return err72 }73 _, err = w.Write(jsonBytes)74 return err75}7677// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.78func (r IndentedJSON) Render(w http.ResponseWriter) error {79 r.WriteContentType(w)80 jsonBytes, err := json.API.MarshalIndent(r.Data, "", " ")81 if err != nil {82 return err83 }84 _, err = w.Write(jsonBytes)85 return err86}8788// WriteContentType (IndentedJSON) writes JSON ContentType.89func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {90 writeContentType(w, jsonContentType)91}9293// Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.94func (r SecureJSON) Render(w http.ResponseWriter) error {95 r.WriteContentType(w)96 jsonBytes, err := json.API.Marshal(r.Data)97 if err != nil {98 return err99 }100 // if the jsonBytes is array values101 if bytes.HasPrefix(jsonBytes, bytesconv.StringToBytes("[")) && bytes.HasSuffix(jsonBytes,102 bytesconv.StringToBytes("]")) {103 if _, err = w.Write(bytesconv.StringToBytes(r.Prefix)); err != nil {104 return err105 }106 }107 _, err = w.Write(jsonBytes)108 return err109}110111// WriteContentType (SecureJSON) writes JSON ContentType.112func (r SecureJSON) WriteContentType(w http.ResponseWriter) {113 writeContentType(w, jsonContentType)114}115116// Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.117func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {118 r.WriteContentType(w)119 ret, err := json.API.Marshal(r.Data)120 if err != nil {121 return err122 }123124 if r.Callback == "" {125 _, err = w.Write(ret)126 return err127 }128129 callback := template.JSEscapeString(r.Callback)130 if _, err = w.Write(bytesconv.StringToBytes(callback)); err != nil {131 return err132 }133134 if _, err = w.Write(bytesconv.StringToBytes("(")); err != nil {135 return err136 }137138 if _, err = w.Write(ret); err != nil {139 return err140 }141142 if _, err = w.Write(bytesconv.StringToBytes(");")); err != nil {143 return err144 }145146 return nil147}148149// WriteContentType (JsonpJSON) writes Javascript ContentType.150func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {151 writeContentType(w, jsonpContentType)152}153154// Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.155func (r AsciiJSON) Render(w http.ResponseWriter) error {156 r.WriteContentType(w)157 ret, err := json.API.Marshal(r.Data)158 if err != nil {159 return err160 }161162 var buffer bytes.Buffer163 escapeBuf := make([]byte, 0, 6) // Preallocate 6 bytes for Unicode escape sequences164165 for _, r := range bytesconv.BytesToString(ret) {166 if r > unicode.MaxASCII {167 escapeBuf = fmt.Appendf(escapeBuf[:0], "\\u%04x", r) // Reuse escapeBuf168 buffer.Write(escapeBuf)169 } else {170 buffer.WriteByte(byte(r))171 }172 }173174 _, err = w.Write(buffer.Bytes())175 return err176}177178// WriteContentType (AsciiJSON) writes JSON ContentType.179func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {180 writeContentType(w, jsonASCIIContentType)181}182183// Render (PureJSON) writes custom ContentType and encodes the given interface object.184func (r PureJSON) Render(w http.ResponseWriter) error {185 r.WriteContentType(w)186 encoder := json.API.NewEncoder(w)187 encoder.SetEscapeHTML(false)188 return encoder.Encode(r.Data)189}190191// WriteContentType (PureJSON) writes custom ContentType.192func (r PureJSON) WriteContentType(w http.ResponseWriter) {193 writeContentType(w, jsonContentType)194}