sq/cli/output/jsonw/jsonw.go
2023-05-05 11:41:22 -06:00

30 lines
675 B
Go

// Package jsonw implements output writers for JSON.
package jsonw
import (
"io"
"github.com/neilotoole/sq/cli/output"
"github.com/neilotoole/sq/cli/output/jsonw/internal"
jcolorenc "github.com/neilotoole/sq/cli/output/jsonw/internal/jcolorenc"
"github.com/neilotoole/sq/libsq/core/errz"
)
// writeJSON prints a JSON representation of v to out, using specs
// from pr.
func writeJSON(out io.Writer, pr *output.Printing, v any) error {
enc := jcolorenc.NewEncoder(out)
enc.SetColors(internal.NewColors(pr))
enc.SetEscapeHTML(false)
if !pr.Compact {
enc.SetIndent("", pr.Indent)
}
err := enc.Encode(v)
if err != nil {
return errz.Err(err)
}
return nil
}