pretty

package
v0.9.11 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2023 License: MIT, MIT Imports: 4 Imported by: 0

README

Pretty

GoDoc

Pretty is a Go package that provides fast methods for formatting JSON for human readability, or to compact JSON for smaller payloads.

Getting Started

Installing

To start using Pretty, install Go and run go get:

$ go get -u github.com/tidwall/pretty

This will retrieve the library.

Pretty

Using this example:

{"name":  {"first":"Tom","last":"Anderson"},  "age":37,
"children": ["Sara","Alex","Jack"],
"fav.movie": "Deer Hunter", "friends": [
    {"first": "Janet", "last": "Murphy", "age": 44}
  ]}

The following code:

result = pretty.Pretty(example)

Will format the json to:

{
  "name": {
    "first": "Tom",
    "last": "Anderson"
  },
  "age": 37,
  "children": ["Sara", "Alex", "Jack"],
  "fav.movie": "Deer Hunter",
  "friends": [
    {
      "first": "Janet",
      "last": "Murphy",
      "age": 44
    }
  ]
}

Color

Color will colorize the json for outputing to the screen.

result = pretty.Color(json, nil)

Will add color to the result for printing to the terminal. The second param is used for a customizing the style, and passing nil will use the default pretty.TerminalStyle.

Ugly

The following code:

result = pretty.Ugly(example)

Will format the json to:

{"name":{"first":"Tom","last":"Anderson"},"age":37,"children":["Sara","Alex","Jack"],"fav.movie":"Deer Hunter","friends":[{"first":"Janet","last":"Murphy","age":44}]}```

Customized output

There's a PrettyOptions(json, opts) function which allows for customizing the output with the following options:

type Options struct {
	// Width is an max column width for single line arrays
	// 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
}

Performance

Benchmarks of Pretty alongside the builtin encoding/json Indent/Compact methods.

BenchmarkPretty-16           1000000    1034 ns/op    720 B/op     2 allocs/op
BenchmarkPrettySortKeys-16    586797    1983 ns/op   2848 B/op    14 allocs/op
BenchmarkUgly-16             4652365     254 ns/op    240 B/op     1 allocs/op
BenchmarkUglyInPlace-16      6481233     183 ns/op      0 B/op     0 allocs/op
BenchmarkJSONIndent-16        450654    2687 ns/op   1221 B/op     0 allocs/op
BenchmarkJSONCompact-16       685111    1699 ns/op    442 B/op     0 allocs/op

These benchmarks were run on a MacBook Pro 2.4 GHz 8-Core Intel Core i9.

Contact

Josh Baker @tidwall

License

Pretty source code is available under the MIT License.

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 will colorize the json. The style parma is used for customizing the colors. Passing nil to the style param will use the default TerminalStyle.

func Pretty

func Pretty(json []byte) []byte

Pretty converts the input json into a more human readable format where each element is on it's own line with clear indentation.

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 strips out comments and trailing commas and convert the input to a valid JSON per the official spec: https://tools.ietf.org/html/rfc8259

The resulting JSON will always be the same length as the input and it will include all of the same line breaks at matching offsets. This is to ensure the result can be later processed by a external parser and that that parser will report messages or errors with the correct offsets.

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 is an max column width for single line arrays
	// 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
	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