From 10e7dc856be37ae55d58b275730e382dc95b3e3d Mon Sep 17 00:00:00 2001 From: astaxie Date: Sun, 6 Jan 2013 11:25:05 +0800 Subject: [PATCH] fix a bug about json and xml --- README.md | 6 ++++-- controller.go | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5d0f7295..42b13e65 100644 --- a/README.md +++ b/README.md @@ -272,14 +272,16 @@ Helper function for serving Json, sets content type to application/json: ```go func (this *AddController) Get() { mystruct := { ... } - routes.ServeJson(w, &mystruct) + this.Data["json"] = &mystruct + this.ServeJson() } ``` Helper function for serving Xml, sets content type to application/xml: ```go func (this *AddController) Get() { mystruct := { ... } - routes.ServeXml(w, &mystruct) + this.Data["xml"]=&mystruct + this.ServeXml() } ``` diff --git a/controller.go b/controller.go index 3f797bcd..c51f155d 100644 --- a/controller.go +++ b/controller.go @@ -127,7 +127,7 @@ func (c *Controller) Redirect(url string, code int) { } func (c *Controller) ServeJson() { - content, err := json.MarshalIndent(c.Data, "", " ") + content, err := json.MarshalIndent(c.Data["json"], "", " ") if err != nil { http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) return @@ -138,7 +138,7 @@ func (c *Controller) ServeJson() { } func (c *Controller) ServeXml() { - content, err := xml.Marshal(c.Data) + content, err := xml.Marshal(c.Data["xml"]) if err != nil { http.Error(c.Ctx.ResponseWriter, err.Error(), http.StatusInternalServerError) return