Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Efficient JSON beautifier and compactor for Go

License

NotificationsYou must be signed in to change notification settings

tidwall/pretty

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GoDoc

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

Getting Started

Installing

To start using Pretty, install Go and rungo 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 defaultpretty.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 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}

Performance

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/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 MITLicense.

About

Efficient JSON beautifier and compactor for Go

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp