Disabled indenting on JSON exporter as it looks so ugly on exported data.

JSONLine still supports indenting.
This commit is contained in:
Musab Gültekin 2019-07-14 03:37:52 +03:00
parent dfabcb84fd
commit df37629d4d

View File

@ -8,7 +8,7 @@ import (
"os" "os"
) )
// JSON exports response data as JSON streaming file // JSONLine exports response data as JSON streaming file
type JSONLine struct { type JSONLine struct {
FileName string FileName string
EscapeHTML bool EscapeHTML bool
@ -43,8 +43,6 @@ func (e *JSONLine) Export(exports chan interface{}) {
type JSON struct { type JSON struct {
FileName string FileName string
EscapeHTML bool EscapeHTML bool
Prefix string
Indent string
} }
// Export exports response data as JSON // Export exports response data as JSON
@ -62,7 +60,7 @@ func (e *JSON) Export(exports chan interface{}) {
// Export data as responses came // Export data as responses came
for res := range exports { for res := range exports {
data, err := jsonMarshalLine(res, e.EscapeHTML, e.Prefix, e.Indent) data, err := jsonMarshalLine(res, e.EscapeHTML)
if err != nil { if err != nil {
log.Printf("JSON encoding error on exporter: %v\n", err) log.Printf("JSON encoding error on exporter: %v\n", err)
continue continue
@ -79,12 +77,11 @@ func (e *JSON) Export(exports chan interface{}) {
file.WriteAt([]byte("\n]\n"), stat.Size()-2) file.WriteAt([]byte("\n]\n"), stat.Size()-2)
} }
// jsonMarshalLine behaves like json.Marshal but supports escapeHTML and indenting // jsonMarshalLine adds tab and comma around actual data
func jsonMarshalLine(t interface{}, escapeHTML bool, prefix string, indent string) ([]byte, error) { func jsonMarshalLine(t interface{}, escapeHTML bool) ([]byte, error) {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer) encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(escapeHTML) encoder.SetEscapeHTML(escapeHTML)
encoder.SetIndent(prefix, indent)
buffer.Write([]byte(" ")) // Tab char buffer.Write([]byte(" ")) // Tab char
err := encoder.Encode(t) // Write actual data err := encoder.Encode(t) // Write actual data