Skip to content

Commit

Permalink
added array lines option
Browse files Browse the repository at this point in the history
  • Loading branch information
tidwall committed Mar 2, 2018
1 parent 4650baa commit 75fba03
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ options:
-u Make json ugly, keypath is optional
-r Use raw values, otherwise types are auto-detected
-n Do not output color or extra formatting
-O Performance boost for value updates.
-O Performance boost for value updates
-D Delete the value at the specified key path
-l Output array values on multiple lines
-i infile Use input file instead of stdin
-o outfile Use output file instead of stdout
keypath JSON key path (like "name.last")
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e

VERSION="1.2.0"
VERSION="1.2.1"
PROTECTED_MODE="no"

export GO15VENDOREXPERIMENT=1
Expand Down
18 changes: 16 additions & 2 deletions cmd/jj/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ options:
-u Make json ugly, keypath is optional
-r Use raw values, otherwise types are auto-detected
-n Do not output color or extra formatting
-O Performance boost for value updates.
-O Performance boost for value updates
-D Delete the value at the specified key path
-l Output array values on multiple lines
-i infile Use input file instead of stdin
-o outfile Use output file instead of stdout
keypath JSON key path (like "name.last")
Expand All @@ -52,6 +53,7 @@ type args struct {
pretty bool
ugly bool
notty bool
lines bool
}

func parseArgs() args {
Expand Down Expand Up @@ -93,6 +95,8 @@ func parseArgs() args {
a.del = true
case 'n':
a.notty = true
case 'l':
a.lines = true
}
}
continue
Expand Down Expand Up @@ -138,6 +142,7 @@ func main() {
var err error
var outb []byte
var outs string
var outa bool
var outt gjson.Type
var f *os.File
if a.infile == nil {
Expand Down Expand Up @@ -195,6 +200,7 @@ func main() {
outs = res.Raw
} else {
outt = res.Type
outa = res.IsArray()
outs = res.String()
}
}
Expand All @@ -210,7 +216,15 @@ func main() {
if outb == nil {
outb = []byte(outs)
}
if a.raw || outt != gjson.String {
if a.lines && outa {
var outb2 []byte
gjson.ParseBytes(outb).ForEach(func(_, v gjson.Result) bool {
outb2 = append(outb2, pretty.Ugly([]byte(v.Raw))...)
outb2 = append(outb2, '\n')
return true
})
outb = outb2
} else if a.raw || outt != gjson.String {
if a.pretty {
outb = pretty.Pretty(outb)
} else if a.ugly {
Expand Down

0 comments on commit 75fba03

Please sign in to comment.