Skip to content

Commit

Permalink
Fixed empty array json bug #1880
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Nov 23, 2023
1 parent 1cf9ecc commit 26effdd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/yqlib/candidiate_node_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,13 @@ func (o *CandidateNode) MarshalJSON() ([]byte, error) {
buf.WriteByte('}')
return buf.Bytes(), nil
case SequenceNode:
log.Debugf("MarshalJSON SequenceNode")
err := enc.Encode(o.Content)
log.Debugf("MarshalJSON SequenceNode, %v, len: %v", o.Content, len(o.Content))
var err error
if len(o.Content) == 0 {
buf.WriteString("[]")
} else {
err = enc.Encode(o.Content)
}
return buf.Bytes(), err
default:
err := enc.Encode(nil)
Expand Down
21 changes: 21 additions & 0 deletions pkg/yqlib/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ const roundTripMultiLineJson = `{
`

var jsonScenarios = []formatScenario{
{
description: "array empty",
skipDoc: true,
input: "[]",
scenarioType: "roundtrip-ndjson",
expected: "[]\n",
},
{
description: "array has scalar",
skipDoc: true,
input: "[3]",
scenarioType: "roundtrip-ndjson",
expected: "[3]\n",
},
{
description: "array has object",
skipDoc: true,
input: `[{"x": 3}]`,
scenarioType: "roundtrip-ndjson",
expected: "[{\"x\":3}]\n",
},
{
description: "array null",
skipDoc: true,
Expand Down

0 comments on commit 26effdd

Please sign in to comment.