Skip to content

Commit 26effdd

Browse files
committed
Fixed empty array json bug #1880
1 parent 1cf9ecc commit 26effdd

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

pkg/yqlib/candidiate_node_json.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,13 @@ func (o *CandidateNode) MarshalJSON() ([]byte, error) {
162162
buf.WriteByte('}')
163163
return buf.Bytes(), nil
164164
case SequenceNode:
165-
log.Debugf("MarshalJSON SequenceNode")
166-
err := enc.Encode(o.Content)
165+
log.Debugf("MarshalJSON SequenceNode, %v, len: %v", o.Content, len(o.Content))
166+
var err error
167+
if len(o.Content) == 0 {
168+
buf.WriteString("[]")
169+
} else {
170+
err = enc.Encode(o.Content)
171+
}
167172
return buf.Bytes(), err
168173
default:
169174
err := enc.Encode(nil)

pkg/yqlib/json_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ const roundTripMultiLineJson = `{
8080
`
8181

8282
var jsonScenarios = []formatScenario{
83+
{
84+
description: "array empty",
85+
skipDoc: true,
86+
input: "[]",
87+
scenarioType: "roundtrip-ndjson",
88+
expected: "[]\n",
89+
},
90+
{
91+
description: "array has scalar",
92+
skipDoc: true,
93+
input: "[3]",
94+
scenarioType: "roundtrip-ndjson",
95+
expected: "[3]\n",
96+
},
97+
{
98+
description: "array has object",
99+
skipDoc: true,
100+
input: `[{"x": 3}]`,
101+
scenarioType: "roundtrip-ndjson",
102+
expected: "[{\"x\":3}]\n",
103+
},
83104
{
84105
description: "array null",
85106
skipDoc: true,

0 commit comments

Comments
 (0)