<commit_before>package json

import (
	"net/http"

	"github.com/bborbe/log"
	"reflect"
	"encoding/json"
	error_handler "github.com/bborbe/server/handler/error"
)

var logger = log.DefaultLogger

type jsonHandler struct {
	m interface{}
}

func NewJsonHandler(m interface{}) *jsonHandler {
	h := new(jsonHandler)
	h.m = m
	return h
}

func (m *jsonHandler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
	logger.Debug("write json")
	logger.Debugf("object to convert %v", m.m)
	b, err := json.Marshal(m.m)
	if err != nil {
		logger.Debugf("Marshal json failed: %v", err)
		e := error_handler.NewErrorMessage(http.StatusInternalServerError, err.Error())
		e.ServeHTTP(responseWriter, request)
		return
	}
	logger.Debugf("json string %s", string(b))
	responseWriter.Header().Set("Content-Type", "application/json")
	responseWriter.WriteHeader(http.StatusOK)

	logger.Debugf("object type %v", reflect.TypeOf(m.m).Kind())
	if reflect.TypeOf(m.m).Kind() == reflect.Slice && string(b) == "null" {
		responseWriter.Write([]byte("[]"))
	}   else {
		responseWriter.Write(b)
	}

}<commit_msg>format<commit_after>package json

import (
	"net/http"

	"encoding/json"
	"reflect"

	"github.com/bborbe/log"
	error_handler "github.com/bborbe/server/handler/error"
)

var logger = log.DefaultLogger

type jsonHandler struct {
	m interface{}
}

func NewJsonHandler(m interface{}) *jsonHandler {
	h := new(jsonHandler)
	h.m = m
	return h
}

func (m *jsonHandler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) {
	logger.Debug("write json")
	logger.Debugf("object to convert %v", m.m)
	b, err := json.Marshal(m.m)
	if err != nil {
		logger.Debugf("Marshal json failed: %v", err)
		e := error_handler.NewErrorMessage(http.StatusInternalServerError, err.Error())
		e.ServeHTTP(responseWriter, request)
		return
	}
	logger.Debugf("json string %s", string(b))
	responseWriter.Header().Set("Content-Type", "application/json")
	responseWriter.WriteHeader(http.StatusOK)

	logger.Debugf("object type %v", reflect.TypeOf(m.m).Kind())
	if reflect.TypeOf(m.m).Kind() == reflect.Slice && string(b) == "null" {
		responseWriter.Write([]byte("[]"))
	} else {
		responseWriter.Write(b)
	}

}
{'fraction_non_alphanumeric': 0.16076294277929154, 'fraction_numerical': 0.0, 'mean_word_length': 5.01912568306011, 'pattern_counts': {'":': 0, '<': 3, '<?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': '4910860', 'n_tokens_mistral': 795, 'n_tokens_neox': 774, 'n_words': 154}