Skip to content

Commit

Permalink
JSON pretty: empty containers
Browse files Browse the repository at this point in the history
For the JSON writer in pretty mode ensure that empty objects and array
are printed on the same line.
  • Loading branch information
mattnibs committed May 23, 2024
1 parent e5ab915 commit cfdc9e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
34 changes: 22 additions & 12 deletions zio/jsonio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,47 +95,57 @@ func (w *Writer) writeRecord(tab int, typ *zed.TypeRecord, bytes zcode.Bytes) {
tab += w.tab
w.punc('{')
it := bytes.Iter()
for i, f := range typ.Fields {
if i != 0 {
var k int
for ; !it.Done(); k++ {
if k != 0 {
w.punc(',')
}
f := typ.Fields[k]
w.writeEntry(tab, f.Name, w.arena.New(f.Type, it.Next()))
}
w.newline()
w.indent(tab - w.tab)
if k > 0 {
w.newline()
w.indent(tab - w.tab)
}
w.punc('}')
}

func (w *Writer) writeArray(tab int, typ zed.Type, bytes zcode.Bytes) {
tab += w.tab
w.punc('[')
it := bytes.Iter()
for i := 0; !it.Done(); i++ {
if i != 0 {
var k int
for ; !it.Done(); k++ {
if k != 0 {
w.punc(',')
}
w.newline()
w.indent(tab)
w.writeAny(tab, w.arena.New(typ, it.Next()))
}
w.newline()
w.indent(tab - w.tab)
if k > 0 {
w.newline()
w.indent(tab - w.tab)
}
w.punc(']')
}

func (w *Writer) writeMap(tab int, typ *zed.TypeMap, bytes zcode.Bytes) {
tab += w.tab
w.punc('{')
it := bytes.Iter()
for i := 0; !it.Done(); i++ {
if i != 0 {
var k int
for ; !it.Done(); k++ {
if k != 0 {
w.punc(',')
}
key := mapKey(typ.KeyType, it.Next())
w.writeEntry(tab, key, w.arena.New(typ.ValType, it.Next()))
}
w.newline()
w.indent(tab - w.tab)
if k > 0 {
w.newline()
w.indent(tab - w.tab)
}
w.punc('}')
}

Expand Down
8 changes: 7 additions & 1 deletion zio/jsonio/ztests/pretty.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ input: |
f4: "a string",
f5: [1,2,3],
f6: {foo: "bar"},
f7: {},
f8: |{}|,
f9: [],
}
output-flags: -f json -pretty 4
Expand All @@ -25,5 +28,8 @@ output: |
],
"f6": {
"foo": "bar"
}
},
"f7": {},
"f8": {},
"f9": []
}

0 comments on commit cfdc9e2

Please sign in to comment.