Fixed empty array json bug #1880

This commit is contained in:
Mike Farah 2023-11-23 11:53:18 +11:00
parent 1cf9ecc79d
commit 26effddb8c
2 changed files with 28 additions and 2 deletions

View File

@ -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)

View File

@ -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,