// EncodeJSONResponse marshals the response as JSON as part of the
// payload of the AMQP Publishing object.
775469
func EncodeJSONResponse(
	ctx context.Context,
	pub *amqp.Publishing,
	response interface{},
) error {
	b, err := json.Marshal(response)
	if err != nil {
		return err
	}
	pub.Body = b
	return nil
}
16484
func (p *Package) EncodeJSON(name string, e interface{}) {
	if p.err != nil {
		return
	}

	b, err := json.Marshal(e)
	if err != nil {
		p.err = err
		return
	}
	r := bytes.NewReader(b)

	p.Copy(name, r)
}
830639
func decodeJSONResponse(resp *http.Response, out interface{}) (int, error) {
	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)

	if resp.StatusCode > 299 {
		return resp.StatusCode, errors.New(string(body))
	}

	err := json.Unmarshal(body, out)
	if err != nil {
		return resp.StatusCode, err
	}
	return resp.StatusCode, nil
}
517043
func (e *Error) MarshalJSON() (result []byte, err error) {
	ee := errEncode{
		Code: e.Code,
		Msg:  e.Msg,
		Op:   e.Op,
	}
	if e.Err != nil {
		if _, ok := e.Err.(*Error); ok {
			_, err := e.Err.(*Error).MarshalJSON()
			if err != nil {
				return result, err
			}
			ee.Err = e.Err
		} else {
			ee.Err = e.Err.Error()
		}
	}
	return json.Marshal(ee)
}
230683
func (resp *RequestVoteResponse) Encode(w io.Writer) (int, error) {
	pb := &protobuf.RequestVoteResponse{
		Term:        proto.Uint64(resp.Term),
		VoteGranted: proto.Bool(resp.VoteGranted),
	}

	p, err := proto.Marshal(pb)
	if err != nil {
		return -1, err
	}

	return w.Write(p)
}
966925
func (enc *HTTPEncoder) EncodeJSON(model Model) error {
	setContentType(enc.writer, ContentJSON)

	err := json.NewEncoder(enc.writer).Encode(model)
	if err != nil {
		http.Error(enc.writer, fmt.Sprintf("Unable to encode '%v' as JSON data: %s", model, err.Error()), http.StatusInternalServerError)
	}
	return err
}
508831
func (e encodable) MarshalJSON() ([]byte, error) {
	return Encode(e.E, e.obj)
}
1348469
func JSONErrorResponse(ctx InternalServerError, err error) error {
	jsonErr, status := ErrorToJSONAPIErrors(ctx, err)
	switch status {
	case http.StatusBadRequest:
		if ctx, ok := ctx.(BadRequest); ok {
			return errs.WithStack(ctx.BadRequest(jsonErr))
		}
	case http.StatusNotFound:
		if ctx, ok := ctx.(NotFound); ok {
			return errs.WithStack(ctx.NotFound(jsonErr))
		}
	case http.StatusUnauthorized:
		if ctx, ok := ctx.(Unauthorized); ok {
			return errs.WithStack(ctx.Unauthorized(jsonErr))
		}
	case http.StatusForbidden:
		if ctx, ok := ctx.(Forbidden); ok {
			return errs.WithStack(ctx.Forbidden(jsonErr))
		}
	case http.StatusConflict:
		if ctx, ok := ctx.(Conflict); ok {
			return errs.WithStack(ctx.Conflict(jsonErr))
		}
	}

	sentry.Sentry().CaptureError(ctx, err)
	return errs.WithStack(ctx.InternalServerError(jsonErr))
}
803271
func SendResponse(w http.ResponseWriter, resp []*Response, batch bool) error {
	w.Header().Set(contentTypeKey, contentTypeValue)
	if batch || len(resp) > 1 {
		return fastjson.NewEncoder(w).Encode(resp)
	} else if len(resp) == 1 {
		return fastjson.NewEncoder(w).Encode(resp[0])
	}
	return nil
}
1133790
func sendResponse(rw http.ResponseWriter, code int, resp interface{}) {
	enc, err := json.Marshal(resp)
	if err != nil {
		log.Errorf("Failed JSON-encoding HTTP response: %v", err)
		rw.WriteHeader(http.StatusInternalServerError)
		return
	}

	rw.Header().Set("Content-Type", "application/json")
	rw.WriteHeader(code)

	_, err = rw.Write(enc)
	if err != nil {
		log.Errorf("Failed sending HTTP response body: %v", err)
	}
}
789867
func NewJsonResponse(status int, body interface{}) (*http.Response, error) { // nolint: golint
	encoded, err := json.Marshal(body)
	if err != nil {
		return nil, err
	}
	response := NewBytesResponse(status, encoded)
	response.Header.Set("Content-Type", "application/json")
	return response, nil
}
1223432
func SendJson(w http.ResponseWriter, i interface{}) error {
	var buf bytes.Buffer
	enc := json.NewEncoder(&buf)
	if err := enc.Encode(i); err != nil {
		http.Error(w, fmt.Sprintf("unable to encode: %v", err), http.StatusInternalServerError)
		log.Printf("[SENDJSON] unable to encode json output in SendUserDetails: %v", err)
		return err
	}
	count := 0
	for count < buf.Len() {
		w, err := w.Write(buf.Bytes()[count:])
		if err != nil {
			log.Printf("[SENDJSON] failed to write: %v", err)
			return err
		}
		count += w
	}
	return nil
}
430050
func sendResponse(w http.ResponseWriter, response interface{}, status int) {
	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	marshalledResponse, err := json.Marshal(response)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(fmt.Sprintf("Failed to marshal response: %v %v", response, err)))
		return
	}
	w.WriteHeader(status)
	w.Write(marshalledResponse)
}
18613
func DecodeClientResponse(r io.Reader, reply interface{}) error {
	var c clientResponse
	if err := json.NewDecoder(r).Decode(&c); err != nil {
		return err
	}
	if c.Error != nil {
		return &Error{Data: c.Error}
	}
	if c.Result == nil {
		return fmt.Errorf("Unexpected null result")
	}
	return json.Unmarshal(*c.Result, reply)
}
1036865
func (p Protected) MarshalJSON() ([]byte, error) {
	b, err := json.Marshal(map[string]interface{}(p))
	if err != nil {
		return nil, err
	}
	return EncodeEscape(b), nil
}
1169149
func (jd *JSONDecoder) Encode(data []*FileDataEntry) ([]byte, error) {
	// Convert to json-specific structure
	var jsonFileEntries []jsonFileEntry
	for _, dataEntry := range data {
		jsonFileEntries = append(jsonFileEntries, jsonFileEntry{
			Key:   dataEntry.Key,
			Value: dataEntry.Value,
		})
	}
	// Encode to type specific structure
	jsonFile := &jsonFile{Data: jsonFileEntries}
	return json.Marshal(jsonFile)
}
914377
func (resp *QueryResponse) MarshalJSON() ([]byte, error) {
	if resp.Err != nil {
		return json.Marshal(struct {
			Err string `json:"error"`
		}{Err: resp.Err.Error()})
	}

	return json.Marshal(struct {
		Results        []interface{}    `json:"results"`
		ColumnAttrSets []*ColumnAttrSet `json:"columnAttrs,omitempty"`
	}{
		Results:        resp.Results,
		ColumnAttrSets: resp.ColumnAttrSets,
	})
}