Skip to content

Commit

Permalink
Add the ability to resolve JSON responses
Browse files Browse the repository at this point in the history
This includes two functions one that can be used to resolve an error message in JSON and another to resolve a JSON response.
  • Loading branch information
Chad Petersen authored Jul 19, 2017
1 parent 9ae6699 commit c8c41ee
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions json_resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package pastry

import (
"encoding/json"
"net/http"
)

// JSONResolver resolves JSON responses
type JSONResolver struct{}

// ResolveJSONError returns a default json error response
func (resolver *JSONResolver) ResolveJSONError(w http.ResponseWriter, code int, message string) {
resolver.ResolveJSON(w, code, map[string]string{"error": message})
}

// ResolveJSON returns a json encoded response
func (resolver *JSONResolver) ResolveJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}

0 comments on commit c8c41ee

Please sign in to comment.