func WriteJSON(w http.ResponseWriter, code int, val interface{}) error {
	// TODO consider marshalling directly to w using json.NewEncoder.
	// pro: this will not require a full buffer allocation.
	// con: if there's an error after the first write, it will be lost.
	data, err := json.Marshal(val)
	if err != nil {
		return errgo.Mask(err)
	}
	w.Header().Set("content-type", "application/json")
	if headerSetter, ok := val.(HeaderSetter); ok {
		headerSetter.SetHeader(w.Header())
	}
	w.WriteHeader(code)
	w.Write(data)
	return nil
}
// WriteJSON writes the given value to the ResponseWriter
// and sets the HTTP status to the given code.
//
// If val implements the HeaderSetter interface, the SetHeader
// method will be called to add additional headers to the
// HTTP response. It is called after the Content-Type header
// has been added, so can be used to override the content type
// if required.
WriteJSON writes the given value to the ResponseWriter
and sets the HTTP status to the given code.

If val implements the HeaderSetter interface, the SetHeader
method will be called to add additional headers to the
HTTP response. It is called after the Content-Type header
has been added, so can be used to override the content type
if required.
// WriteJSON writes the given value to the ResponseWriter // and sets the HTTP status to the given code . // // If val implements the HeaderSetter interface the SetHeader // method will be called to add additional headers to the // HTTP response . It is called after the Content - Type header // has been added so can be used to override the content type // if required .
func WriteJSON(w http.ResponseWriter, code int, val interface{}) error {
	data, err := json.Marshal(val)
	if err != nil {
		return errgo.Mask(err)
	}
	w.Header().Set("content-type", "application/json")
	if headerSetter, ok := val.(HeaderSetter); ok {
		headerSetter.SetHeader(w.Header())
	}
	w.WriteHeader(code)
	w.Write(data)
	return nil
}
func WriteJSON ( w http . ResponseWriter , code int , val interface { } ) error { data , err := json . Marshal ( val ) if err != nil { return errgo . Mask ( err ) } w . Header ( ) . Set ( " " , " " ) if headerSetter , ok := val . ( HeaderSetter ) ; ok { headerSetter . SetHeader ( w . Header ( ) ) } w . WriteHeader ( code ) w . Write ( data ) return nil }
WriteJSON writes the given value to the ResponseWriter
and sets the HTTP status to the given code.
WriteJSON writes the given value to the ResponseWriter and sets the HTTP status to the given code .
// TODO consider marshalling directly to w using json.NewEncoder. // pro: this will not require a full buffer allocation. // con: if there's an error after the first write, it will be lost.