2015-05-22 20:21:23 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-05-07 13:44:52 +03:00
|
|
|
package render
|
|
|
|
|
|
|
|
import (
|
2017-07-07 20:21:30 +03:00
|
|
|
"bytes"
|
2018-07-03 12:17:08 +03:00
|
|
|
"fmt"
|
2018-04-26 06:52:19 +03:00
|
|
|
"html/template"
|
2015-05-07 13:44:52 +03:00
|
|
|
"net/http"
|
2017-07-08 11:49:09 +03:00
|
|
|
|
2020-01-17 19:32:50 +03:00
|
|
|
"github.com/gin-gonic/gin/internal/bytesconv"
|
2018-08-30 14:04:03 +03:00
|
|
|
"github.com/gin-gonic/gin/internal/json"
|
2015-05-07 13:44:52 +03:00
|
|
|
)
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// JSON contains the given interface object.
|
2017-07-06 15:59:27 +03:00
|
|
|
type JSON struct {
|
|
|
|
Data interface{}
|
|
|
|
}
|
2015-05-07 13:44:52 +03:00
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// IndentedJSON contains the given interface object.
|
2017-07-06 15:59:27 +03:00
|
|
|
type IndentedJSON struct {
|
|
|
|
Data interface{}
|
|
|
|
}
|
2015-05-07 13:44:52 +03:00
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// SecureJSON contains the given interface object and its prefix.
|
2017-07-07 20:21:30 +03:00
|
|
|
type SecureJSON struct {
|
|
|
|
Prefix string
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// JsonpJSON contains the given interface object its callback.
|
2018-04-26 06:52:19 +03:00
|
|
|
type JsonpJSON struct {
|
|
|
|
Callback string
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// AsciiJSON contains the given interface object.
|
2018-07-03 12:17:08 +03:00
|
|
|
type AsciiJSON struct {
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
2019-05-07 13:32:32 +03:00
|
|
|
// PureJSON contains the given interface object.
|
|
|
|
type PureJSON struct {
|
|
|
|
Data interface{}
|
|
|
|
}
|
|
|
|
|
2021-06-02 02:35:30 +03:00
|
|
|
var (
|
|
|
|
jsonContentType = []string{"application/json; charset=utf-8"}
|
|
|
|
jsonpContentType = []string{"application/javascript; charset=utf-8"}
|
2021-08-19 10:46:31 +03:00
|
|
|
jsonASCIIContentType = []string{"application/json"}
|
2021-06-02 02:35:30 +03:00
|
|
|
)
|
2015-05-22 05:44:29 +03:00
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// Render (JSON) writes data with custom ContentType.
|
2017-01-09 18:24:48 +03:00
|
|
|
func (r JSON) Render(w http.ResponseWriter) (err error) {
|
|
|
|
if err = WriteJSON(w, r.Data); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
2015-05-11 02:02:17 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteContentType (JSON) writes JSON ContentType.
|
2017-01-09 18:24:48 +03:00
|
|
|
func (r JSON) WriteContentType(w http.ResponseWriter) {
|
2015-06-13 05:29:10 +03:00
|
|
|
writeContentType(w, jsonContentType)
|
2017-01-09 18:24:48 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteJSON marshals the given interface object and writes it with custom ContentType.
|
2017-01-09 18:24:48 +03:00
|
|
|
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
|
|
|
writeContentType(w, jsonContentType)
|
2020-02-21 12:15:17 +03:00
|
|
|
jsonBytes, err := json.Marshal(obj)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = w.Write(jsonBytes)
|
2019-01-18 04:32:53 +03:00
|
|
|
return err
|
2015-05-07 13:44:52 +03:00
|
|
|
}
|
2015-06-04 14:08:29 +03:00
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
|
2017-01-09 18:24:48 +03:00
|
|
|
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
|
|
|
r.WriteContentType(w)
|
|
|
|
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
2016-11-16 02:51:05 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-18 04:32:53 +03:00
|
|
|
_, err = w.Write(jsonBytes)
|
|
|
|
return err
|
2015-06-04 14:08:29 +03:00
|
|
|
}
|
2017-01-09 18:24:48 +03:00
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteContentType (IndentedJSON) writes JSON ContentType.
|
2017-01-09 18:24:48 +03:00
|
|
|
func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {
|
|
|
|
writeContentType(w, jsonContentType)
|
|
|
|
}
|
2017-07-07 20:21:30 +03:00
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.
|
2017-07-07 20:21:30 +03:00
|
|
|
func (r SecureJSON) Render(w http.ResponseWriter) error {
|
|
|
|
r.WriteContentType(w)
|
|
|
|
jsonBytes, err := json.Marshal(r.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// if the jsonBytes is array values
|
2020-01-17 19:32:50 +03:00
|
|
|
if bytes.HasPrefix(jsonBytes, bytesconv.StringToBytes("[")) && bytes.HasSuffix(jsonBytes,
|
|
|
|
bytesconv.StringToBytes("]")) {
|
2021-08-22 20:32:41 +03:00
|
|
|
if _, err = w.Write(bytesconv.StringToBytes(r.Prefix)); err != nil {
|
2019-01-18 04:32:53 +03:00
|
|
|
return err
|
|
|
|
}
|
2017-07-07 20:21:30 +03:00
|
|
|
}
|
2019-01-18 04:32:53 +03:00
|
|
|
_, err = w.Write(jsonBytes)
|
|
|
|
return err
|
2017-07-07 20:21:30 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteContentType (SecureJSON) writes JSON ContentType.
|
2017-07-07 20:21:30 +03:00
|
|
|
func (r SecureJSON) WriteContentType(w http.ResponseWriter) {
|
|
|
|
writeContentType(w, jsonContentType)
|
|
|
|
}
|
2018-04-26 06:52:19 +03:00
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.
|
2018-04-26 06:52:19 +03:00
|
|
|
func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
|
|
|
|
r.WriteContentType(w)
|
|
|
|
ret, err := json.Marshal(r.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Callback == "" {
|
2019-01-18 04:32:53 +03:00
|
|
|
_, err = w.Write(ret)
|
|
|
|
return err
|
2018-04-26 06:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
callback := template.JSEscapeString(r.Callback)
|
2021-08-22 20:32:41 +03:00
|
|
|
if _, err = w.Write(bytesconv.StringToBytes(callback)); err != nil {
|
2019-01-18 04:32:53 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-08-22 20:32:41 +03:00
|
|
|
|
|
|
|
if _, err = w.Write(bytesconv.StringToBytes("(")); err != nil {
|
2019-01-18 04:32:53 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-08-22 20:32:41 +03:00
|
|
|
|
|
|
|
if _, err = w.Write(ret); err != nil {
|
2019-01-18 04:32:53 +03:00
|
|
|
return err
|
|
|
|
}
|
2021-08-22 20:32:41 +03:00
|
|
|
|
|
|
|
if _, err = w.Write(bytesconv.StringToBytes(");")); err != nil {
|
2019-01-18 04:32:53 +03:00
|
|
|
return err
|
|
|
|
}
|
2018-04-26 06:52:19 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteContentType (JsonpJSON) writes Javascript ContentType.
|
2018-04-26 06:52:19 +03:00
|
|
|
func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {
|
|
|
|
writeContentType(w, jsonpContentType)
|
|
|
|
}
|
2018-07-03 12:17:08 +03:00
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
|
2018-07-03 12:17:08 +03:00
|
|
|
func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
|
|
|
|
r.WriteContentType(w)
|
|
|
|
ret, err := json.Marshal(r.Data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var buffer bytes.Buffer
|
2020-01-17 19:32:50 +03:00
|
|
|
for _, r := range bytesconv.BytesToString(ret) {
|
2018-08-20 16:49:24 +03:00
|
|
|
cvt := string(r)
|
|
|
|
if r >= 128 {
|
2018-07-03 12:17:08 +03:00
|
|
|
cvt = fmt.Sprintf("\\u%04x", int64(r))
|
|
|
|
}
|
|
|
|
buffer.WriteString(cvt)
|
|
|
|
}
|
|
|
|
|
2019-01-18 04:32:53 +03:00
|
|
|
_, err = w.Write(buffer.Bytes())
|
|
|
|
return err
|
2018-07-03 12:17:08 +03:00
|
|
|
}
|
|
|
|
|
2018-09-15 05:23:32 +03:00
|
|
|
// WriteContentType (AsciiJSON) writes JSON ContentType.
|
2018-07-03 12:17:08 +03:00
|
|
|
func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {
|
2021-08-19 10:46:31 +03:00
|
|
|
writeContentType(w, jsonASCIIContentType)
|
2018-07-03 12:17:08 +03:00
|
|
|
}
|
2019-05-07 13:32:32 +03:00
|
|
|
|
|
|
|
// Render (PureJSON) writes custom ContentType and encodes the given interface object.
|
|
|
|
func (r PureJSON) Render(w http.ResponseWriter) error {
|
|
|
|
r.WriteContentType(w)
|
|
|
|
encoder := json.NewEncoder(w)
|
|
|
|
encoder.SetEscapeHTML(false)
|
|
|
|
return encoder.Encode(r.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteContentType (PureJSON) writes custom ContentType.
|
|
|
|
func (r PureJSON) WriteContentType(w http.ResponseWriter) {
|
|
|
|
writeContentType(w, jsonContentType)
|
|
|
|
}
|