Skip to content

Commit 497674b

Browse files
committed
Merge branch 'compact-format-flag' into master
PR #29 * compact-format-flag: Add --compact, -c bool flag to omit nonessential whitespace
2 parents 2d0be6e + d4327c5 commit 497674b

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

jp.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func main() {
2020
app.Author = ""
2121
app.Email = ""
2222
app.Flags = []cli.Flag{
23+
cli.BoolFlag{
24+
Name: "compact, c",
25+
Usage: "Produce compact JSON output that omits nonessential whitespace.",
26+
},
2327
cli.StringFlag{
2428
Name: "filename, f",
2529
Usage: "Read input JSON from a file instead of stdin.",
@@ -111,7 +115,12 @@ func runMain(c *cli.Context) int {
111115
if c.Bool("unquoted") && isString {
112116
os.Stdout.WriteString(converted)
113117
} else {
114-
toJSON, err := json.MarshalIndent(result, "", " ")
118+
var toJSON []byte
119+
if c.Bool("compact") {
120+
toJSON, err = json.Marshal(result)
121+
} else {
122+
toJSON, err = json.MarshalIndent(result, "", " ")
123+
}
115124
if err != nil {
116125
errMsg("Error marshalling result to JSON: %s\n", err)
117126
return 3

0 commit comments

Comments
 (0)