diff --git a/zio/jsonio/writer.go b/zio/jsonio/writer.go index cc0fb00b9d..364ec2502f 100644 --- a/zio/jsonio/writer.go +++ b/zio/jsonio/writer.go @@ -95,14 +95,18 @@ 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('}') } @@ -110,16 +114,19 @@ 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(']') } @@ -127,15 +134,18 @@ 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('}') } diff --git a/zio/jsonio/ztests/pretty.yaml b/zio/jsonio/ztests/pretty.yaml index c4a0c727c1..7b9b436677 100644 --- a/zio/jsonio/ztests/pretty.yaml +++ b/zio/jsonio/ztests/pretty.yaml @@ -8,6 +8,9 @@ input: | f4: "a string", f5: [1,2,3], f6: {foo: "bar"}, + f7: {}, + f8: |{}|, + f9: [], } output-flags: -f json -pretty 4 @@ -25,5 +28,8 @@ output: | ], "f6": { "foo": "bar" - } + }, + "f7": {}, + "f8": {}, + "f9": [] }