Browse files

Use existing RenderJsonResult

  • Loading branch information...
1 parent a7c2c2d commit f9339364908a515ca0068c68ac4db19691f880b1 @andrewmunsell andrewmunsell committed Aug 7, 2013
Showing with 13 additions and 26 deletions.
  1. +2 −2 controller.go
  2. +11 −24 results.go
View
4 controller.go
@@ -116,12 +116,12 @@ func (c *Controller) RenderTemplate(templatePath string) Result {
// Uses encoding/json.Marshal to return JSON to the client.
func (c *Controller) RenderJson(o interface{}) Result {
- return RenderJsonResult{o}
+ return RenderJsonResult{o, ""}
}
// Renders a JSONP result using encoding/json.Marshal
func (c *Controller) RenderJsonP(callback string, o interface{}) Result {
- return RenderJsonPResult{callback, o}
+ return RenderJsonResult{o, callback}
}
// Uses encoding/xml.Marshal to return XML to the client.
View
35 results.go
@@ -190,7 +190,8 @@ func (r RenderHtmlResult) Apply(req *Request, resp *Response) {
}
type RenderJsonResult struct {
- obj interface{}
+ obj interface{}
+ callback string
}
func (r RenderJsonResult) Apply(req *Request, resp *Response) {
@@ -207,34 +208,20 @@ func (r RenderJsonResult) Apply(req *Request, resp *Response) {
return
}
- resp.WriteHeader(http.StatusOK, "application/json")
- resp.Out.Write(b)
-}
+ var contentType string
-type RenderJsonPResult struct {
- callback string
- obj interface{}
-}
+ if len(r.callback) > 0 {
+ var callback string
+ callback = r.callback + "(" + string(b[:]) + ");"
-func (r RenderJsonPResult) Apply(req *Request, resp *Response) {
- var b []byte
- var err error
- if Config.BoolDefault("results.pretty", false) {
- b, err = json.MarshalIndent(r.obj, "", " ")
+ contentType = "application/javascript"
+ b = []byte(callback)
} else {
- b, err = json.Marshal(r.obj)
+ contentType = "application/json"
}
- if err != nil {
- ErrorResult{Error: err}.Apply(req, resp)
- return
- }
-
- var callback string
- callback = r.callback + "(" + string(b[:]) + ");"
-
- resp.WriteHeader(http.StatusOK, "application/javascript")
- resp.Out.Write([]byte(callback))
+ resp.WriteHeader(http.StatusOK, contentType)
+ resp.Out.Write(b)
}
type RenderXmlResult struct {

0 comments on commit f933936

Please sign in to comment.