- Notifications
You must be signed in to change notification settings - Fork44
Efficient JSON beautifier and compactor for Go
License
tidwall/pretty
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Pretty is a Go package that providesfast methods for formatting JSON for human readability, or to compact JSON for smaller payloads.
To start using Pretty, install Go and rungo get:
$ go get -u github.com/tidwall/pretty
This will retrieve the library.
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 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 defaultpretty.TerminalStyle.
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}]}```There's aPrettyOptions(json, opts) function which allows for customizing the output with the following options:
typeOptionsstruct {// Width is an max column width for single line arrays// Default is 80Widthint// Prefix is a prefix for all lines// Default is an empty stringPrefixstring// Indent is the nested indentation// Default is two spacesIndentstring// SortKeys will sort the keys alphabetically// Default is falseSortKeysbool}
Benchmarks of Pretty alongside the builtinencoding/json Indent/Compact methods.
BenchmarkPretty-16 1000000 1034 ns/op 720 B/op 2 allocs/opBenchmarkPrettySortKeys-16 586797 1983 ns/op 2848 B/op 14 allocs/opBenchmarkUgly-16 4652365 254 ns/op 240 B/op 1 allocs/opBenchmarkUglyInPlace-16 6481233 183 ns/op 0 B/op 0 allocs/opBenchmarkJSONIndent-16 450654 2687 ns/op 1221 B/op 0 allocs/opBenchmarkJSONCompact-16 685111 1699 ns/op 442 B/op 0 allocs/opThese benchmarks were run on a MacBook Pro 2.4 GHz 8-Core Intel Core i9.
Josh Baker@tidwall
Pretty source code is available under the MITLicense.
About
Efficient JSON beautifier and compactor for Go
Resources
License
Uh oh!
There was an error while loading.Please reload this page.