|
@@ -13,34 +13,41 @@ import (
|
|
|
"github.com/gin-gonic/gin/internal/json"
|
|
"github.com/gin-gonic/gin/internal/json"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
+// JSON contains the given interface object.
|
|
|
type JSON struct {
|
|
type JSON struct {
|
|
|
Data interface{}
|
|
Data interface{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// IndentedJSON contains the given interface object.
|
|
|
type IndentedJSON struct {
|
|
type IndentedJSON struct {
|
|
|
Data interface{}
|
|
Data interface{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// SecureJSON contains the given interface object and its prefix.
|
|
|
type SecureJSON struct {
|
|
type SecureJSON struct {
|
|
|
Prefix string
|
|
Prefix string
|
|
|
Data interface{}
|
|
Data interface{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// JsonpJSON contains the given interface object its callback.
|
|
|
type JsonpJSON struct {
|
|
type JsonpJSON struct {
|
|
|
Callback string
|
|
Callback string
|
|
|
Data interface{}
|
|
Data interface{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// AsciiJSON contains the given interface object.
|
|
|
type AsciiJSON struct {
|
|
type AsciiJSON struct {
|
|
|
Data interface{}
|
|
Data interface{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// SecureJSONPrefix is a string which represents SecureJSON prefix.
|
|
|
type SecureJSONPrefix string
|
|
type SecureJSONPrefix string
|
|
|
|
|
|
|
|
var jsonContentType = []string{"application/json; charset=utf-8"}
|
|
var jsonContentType = []string{"application/json; charset=utf-8"}
|
|
|
var jsonpContentType = []string{"application/javascript; charset=utf-8"}
|
|
var jsonpContentType = []string{"application/javascript; charset=utf-8"}
|
|
|
var jsonAsciiContentType = []string{"application/json"}
|
|
var jsonAsciiContentType = []string{"application/json"}
|
|
|
|
|
|
|
|
|
|
+// Render (JSON) writes data with custom ContentType.
|
|
|
func (r JSON) Render(w http.ResponseWriter) (err error) {
|
|
func (r JSON) Render(w http.ResponseWriter) (err error) {
|
|
|
if err = WriteJSON(w, r.Data); err != nil {
|
|
if err = WriteJSON(w, r.Data); err != nil {
|
|
|
panic(err)
|
|
panic(err)
|
|
@@ -48,10 +55,12 @@ func (r JSON) Render(w http.ResponseWriter) (err error) {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// WriteContentType (JSON) writes JSON ContentType.
|
|
|
func (r JSON) WriteContentType(w http.ResponseWriter) {
|
|
func (r JSON) WriteContentType(w http.ResponseWriter) {
|
|
|
writeContentType(w, jsonContentType)
|
|
writeContentType(w, jsonContentType)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// WriteJSON marshals the given interface object and writes it with custom ContentType.
|
|
|
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
|
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
|
|
writeContentType(w, jsonContentType)
|
|
writeContentType(w, jsonContentType)
|
|
|
jsonBytes, err := json.Marshal(obj)
|
|
jsonBytes, err := json.Marshal(obj)
|
|
@@ -62,6 +71,7 @@ func WriteJSON(w http.ResponseWriter, obj interface{}) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.
|
|
|
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
|
func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
|
|
r.WriteContentType(w)
|
|
r.WriteContentType(w)
|
|
|
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
|
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
|
@@ -72,10 +82,12 @@ func (r IndentedJSON) Render(w http.ResponseWriter) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// WriteContentType (IndentedJSON) writes JSON ContentType.
|
|
|
func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {
|
|
func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {
|
|
|
writeContentType(w, jsonContentType)
|
|
writeContentType(w, jsonContentType)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.
|
|
|
func (r SecureJSON) Render(w http.ResponseWriter) error {
|
|
func (r SecureJSON) Render(w http.ResponseWriter) error {
|
|
|
r.WriteContentType(w)
|
|
r.WriteContentType(w)
|
|
|
jsonBytes, err := json.Marshal(r.Data)
|
|
jsonBytes, err := json.Marshal(r.Data)
|
|
@@ -90,10 +102,12 @@ func (r SecureJSON) Render(w http.ResponseWriter) error {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// WriteContentType (SecureJSON) writes JSON ContentType.
|
|
|
func (r SecureJSON) WriteContentType(w http.ResponseWriter) {
|
|
func (r SecureJSON) WriteContentType(w http.ResponseWriter) {
|
|
|
writeContentType(w, jsonContentType)
|
|
writeContentType(w, jsonContentType)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.
|
|
|
func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
|
|
func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
|
|
|
r.WriteContentType(w)
|
|
r.WriteContentType(w)
|
|
|
ret, err := json.Marshal(r.Data)
|
|
ret, err := json.Marshal(r.Data)
|
|
@@ -115,10 +129,12 @@ func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// WriteContentType (JsonpJSON) writes Javascript ContentType.
|
|
|
func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {
|
|
func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {
|
|
|
writeContentType(w, jsonpContentType)
|
|
writeContentType(w, jsonpContentType)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.
|
|
|
func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
|
|
func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
|
|
|
r.WriteContentType(w)
|
|
r.WriteContentType(w)
|
|
|
ret, err := json.Marshal(r.Data)
|
|
ret, err := json.Marshal(r.Data)
|
|
@@ -139,6 +155,7 @@ func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {
|
|
|
return nil
|
|
return nil
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// WriteContentType (AsciiJSON) writes JSON ContentType.
|
|
|
func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {
|
|
func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {
|
|
|
writeContentType(w, jsonAsciiContentType)
|
|
writeContentType(w, jsonAsciiContentType)
|
|
|
}
|
|
}
|