Documentation
¶
Overview ¶
Package prettyx provides streaming JSON pretty printing with optional ANSI coloring and recursive unwrapping of JSON-looking strings.
By default, output is jq-ish (one element or key per line). Set Options.SemiCompact to enable tidwall-style formatting with soft wrapping. Options.Width controls the wrap column in SemiCompact mode; when Width <= 0, output always breaks after commas. For maximum throughput, PrettyStream and PrettyReader operate without allocations when Options.Unwrap is false.
Basic usage:
src := []byte(`{"foo":"{\"nested\":true}"}`)
opts := &prettyx.Options{Unwrap: true, Palette: "none"}
out, err := prettyx.Pretty(src, opts)
if err != nil {
log.Fatal(err)
}
fmt.Print(string(out))
Streaming:
opts := &prettyx.Options{Palette: "none"}
if err := prettyx.PrettyStream(os.Stdout, os.Stdin, opts); err != nil {
log.Fatal(err)
}
Semi-compact formatting:
opts := &prettyx.Options{SemiCompact: true, Width: 80, Palette: "none"}
if err := prettyx.PrettyStream(os.Stdout, bytes.NewReader(src), opts); err != nil {
log.Fatal(err)
}
Index ¶
- Variables
- func CompactTo(w io.Writer, r io.Reader, opts *Options) error
- func CompactToBuffer(r io.Reader, opts *Options) ([]byte, error)
- func PaletteNames() []string
- func Pretty(in []byte, opts *Options) ([]byte, error)
- func PrettyReader(r io.Reader, opts *Options) io.ReadCloser
- func PrettyStream(w io.Writer, r io.Reader, opts *Options) error
- func PrettyTo(w io.Writer, in []byte, opts *Options) error
- func PrettyToBuffer(w io.Writer, in []byte, opts *Options) ([]byte, error)
- func PrettyWithRenderer(in []byte, opts *Options, _ any) ([]byte, error)
- type ColorPalette
- type Options
Constants ¶
This section is empty.
Variables ¶
var DefaultOptions = &Options{ Width: 80, SemiCompact: false, Prefix: "", Indent: " ", Unwrap: false, ForceColor: false, Palette: "default", }
DefaultOptions holds the fallback pretty-print configuration.
var MaxNestedJSONDepth = 10
MaxNestedJSONDepth controls how deep we recursively parse JSON that appears inside string values when Unwrap is enabled. Set to 10 by default. Special case:
- If MaxNestedJSONDepth == 0, we still unwrap one level (i.e., parse the string as JSON once, but do not recurse further).
Example meanings:
0 -> unwrap once (non-recursive) 1 -> unwrap once (same as 0) 2+ -> unwrap up to that many recursive levels
Functions ¶
func CompactTo ¶ added in v0.4.0
CompactTo streams compacted JSON to the provided writer. It supports multiple JSON documents in the input stream, emitting one compacted document per line. When opts.Unwrap is true, JSON-looking strings are decoded recursively before compaction.
func CompactToBuffer ¶ added in v0.4.0
CompactToBuffer compacts JSON into memory. It preserves the one-document-per-line behavior of CompactTo.
func PaletteNames ¶ added in v0.4.0
func PaletteNames() []string
PaletteNames returns the sorted list of palette names, including "none".
func Pretty ¶
Pretty parses the input JSON, optionally unwraps nested JSON strings (recursing up to MaxNestedJSONDepth when Unwrap is enabled), formats it, and colorizes it using ANSI SGR sequences before returning the resulting bytes. Color output is automatically disabled when the destination is not a TTY unless ForceColor is set.
func PrettyReader ¶ added in v0.4.0
func PrettyReader(r io.Reader, opts *Options) io.ReadCloser
PrettyReader returns a reader that streams pretty-printed JSON from r. The returned reader must be closed to stop the internal goroutine.
func PrettyStream ¶ added in v0.4.0
PrettyStream formats one or more JSON documents from r and writes them to w. It is the streaming, zero-alloc path when opts.Unwrap is false. When opts.SemiCompact is true, output uses tidwall-style soft wrapping.
func PrettyTo ¶
PrettyTo writes a pretty-printed, colorized JSON document to the provided writer. Colors degrade automatically when the writer is not a TTY.
func PrettyToBuffer ¶ added in v0.4.0
PrettyToBuffer renders a pretty-printed JSON document into memory and returns the resulting bytes.
Types ¶
type ColorPalette ¶
type ColorPalette struct {
Key string
String string
Number string
True string
False string
Null string
Brackets string
Punctuation string
}
ColorPalette configures the ANSI styles for each JSON token class.
func NoColorPalette ¶
func NoColorPalette() ColorPalette
NoColorPalette disables all styling while keeping the formatter path shared.
type Options ¶
type Options struct {
// Width is the soft-wrap column width for SemiCompact mode. When <= 0,
// SemiCompact always breaks after commas.
Width int
// SemiCompact enables tidwall-style formatting with soft wrapping.
// When false, output is jq-ish (one element/key per line).
SemiCompact bool
// Prefix is applied to every output line. Default "".
Prefix string
// Indent defines the nested indentation. Default two spaces.
Indent string
// Unwrap enables recursive decoding of JSON strings. This mirrors the CLI's
// -u/--unwrap flag. When false, prettyx leaves any JSON-looking strings as-is.
Unwrap bool
// ForceColor emits ANSI color even when the destination is not a TTY.
ForceColor bool
// Palette selects the named colour palette. Empty chooses the default.
// Use "none" to disable colour.
Palette string
}
Options controls the pretty-printing behavior.