<commit_before>package rest

import "encoding/json"

// Encoder describes an function capable of encoding a response.
type Encoder func(*Context, interface{}) error

// defaultEncoder is used as the default Encoder.
func defaultEncoder(c *Context, v interface{}) error {
	var code = c.Status()
	var response = &struct {
		Code     int         `json:"code"`
		Success  bool        `json:"success"`
		Error    string      `json:"error,omitempty"`
		Location string      `json:"location,omitempty"`
		Data     interface{} `json:"data,omitempty"`
	}{
		Code:    code,
		Success: code < 400,
	}
	switch data := v.(type) {
	case error:
		response.Error = data.Error()
	case *RedirectURL:
		response.Location = data.URL
	default:
		response.Data = data
	}
	c.SetContentType("application/json; charset=utf-8")
	enc := json.NewEncoder(c.Response)
	enc.SetIndent("", "    ")
	return enc.Encode(response)
}<commit_msg>replace default JSON encoder<commit_after>package rest

import "encoding/json"

// Encoder describes an function capable of encoding a response.
type Encoder func(*Context, interface{}) error

// defaultEncoder is used as the default Encoder.
func defaultEncoder(c *Context, v interface{}) error {
	c.SetContentType("application/json; charset=utf-8")
	enc := json.NewEncoder(c.Response)
	enc.SetIndent("", "    ")
	if err, ok := v.(error); ok {
		return enc.Encode(&struct {
			Error string `json:"error,omitempty"`
		}{
			Error: err.Error(),
		})
	}
	return enc.Encode(v)
}
{'fraction_non_alphanumeric': 0.13360323886639677, 'fraction_numerical': 0.0033738191632928477, 'mean_word_length': 3.8943894389438944, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5160054', 'n_tokens_mistral': 478, 'n_tokens_neox': 453, 'n_words': 131}