pretty

package
v1.4.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2026 License: MIT, MIT Imports: 4 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = &Options{Width: 80, Prefix: "", Indent: "  ", SortKeys: false}

DefaultOptions is the default options for pretty formats.

Functions

func Color

func Color(src []byte, style *Style) []byte

Color 按 style 为 JSON 添加颜色; style 为 nil 时使用默认的 TerminalStyle.

func Pretty

func Pretty(json []byte) []byte

Pretty 将输入 JSON 格式化为更易读的文本, 通过换行和缩进展示各元素.

func PrettyOptions

func PrettyOptions(json []byte, opts *Options) []byte

PrettyOptions is like Pretty but with customized options.

func Spec

func Spec(src []byte) []byte

Spec 将注释和尾逗号替换为空白, 供普通 JSON 解析器处理扩展输入.

输入的注释和字符串语法完整时, 保留长度和换行位置, 便于外部解析器定位原始偏移. 本函数不校验 JSON 合法性, 也不修复其他语法错误或未闭合注释. JSON 规范: https://tools.ietf.org/html/rfc8259

func SpecInPlace

func SpecInPlace(src []byte) []byte

SpecInPlace is the same as Spec, but this method reuses the input json buffer to avoid allocations. Do not use the original bytes slice upon return.

func Ugly

func Ugly(json []byte) []byte

Ugly removes insignificant space characters from the input json byte slice and returns the compacted result.

func UglyInPlace

func UglyInPlace(json []byte) []byte

UglyInPlace removes insignificant space characters from the input json byte slice and returns the compacted result. This method reuses the input json buffer to avoid allocations. Do not use the original bytes slice upon return.

Types

type Options

type Options struct {
	// Width 是单行数组的最大列宽.
	// Default is 80
	Width int
	// Prefix is a prefix for all lines
	// Default is an empty string
	Prefix string
	// Indent is the nested indentation
	// Default is two spaces
	Indent string
	// SortKeys will sort the keys alphabetically
	// Default is false
	SortKeys bool
}

Options is Pretty options

type Style

type Style struct {
	Key, String, Number [2]string
	True, False, Null   [2]string
	Escape              [2]string
	Brackets            [2]string
	Append              func(dst []byte, c byte) []byte
}

Style is the color style

var TerminalStyle *Style

TerminalStyle is for terminals

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL