Commit 0372b817 authored by astaxie's avatar astaxie

ServeJson ServeJsonp ServeXml and choice

parent a3363b06
...@@ -216,7 +216,13 @@ func (c *Controller) Abort(code string) { ...@@ -216,7 +216,13 @@ func (c *Controller) Abort(code string) {
} }
func (c *Controller) ServeJson(encoding ...bool) { func (c *Controller) ServeJson(encoding ...bool) {
content, err := json.Marshal(c.Data["json"]) var content []byte
var err error
if RunMode == "prod" {
content, err = json.Marshal(c.Data["json"])
} else {
content, err = json.MarshalIndent(c.Data["json"], "", " ")
}
if err != nil { if err != nil {
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
return return
...@@ -229,7 +235,13 @@ func (c *Controller) ServeJson(encoding ...bool) { ...@@ -229,7 +235,13 @@ func (c *Controller) ServeJson(encoding ...bool) {
} }
func (c *Controller) ServeJsonp() { func (c *Controller) ServeJsonp() {
content, err := json.Marshal(c.Data["jsonp"]) var content []byte
var err error
if RunMode == "prod" {
content, err = json.Marshal(c.Data["jsonp"])
} else {
content, err = json.MarshalIndent(c.Data["jsonp"], "", " ")
}
if err != nil { if err != nil {
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
return return
...@@ -248,7 +260,13 @@ func (c *Controller) ServeJsonp() { ...@@ -248,7 +260,13 @@ func (c *Controller) ServeJsonp() {
} }
func (c *Controller) ServeXml() { func (c *Controller) ServeXml() {
content, err := xml.Marshal(c.Data["xml"]) var content []byte
var err error
if RunMode == "prod" {
content, err = xml.Marshal(c.Data["xml"])
} else {
content, err = xml.MarshalIndent(c.Data["xml"], "", " ")
}
if err != nil { if err != nil {
http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError)
return return
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment