Skip to content

Commit

Permalink
Use existing RenderJsonResult
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewmunsell committed Aug 7, 2013
1 parent a7c2c2d commit f933936
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 26 deletions.
4 changes: 2 additions & 2 deletions controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
35 changes: 11 additions & 24 deletions results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit f933936

Please sign in to comment.