Fix JSON encoding removing null #985

This commit is contained in:
Mike Farah 2021-10-30 13:37:21 +11:00
parent 2b3d0552a6
commit 08fc058934
2 changed files with 20 additions and 1 deletions

View File

@ -211,7 +211,8 @@ func (o *orderedMap) UnmarshalYAML(node *yaml.Node) error {
}
return nil
case yaml.SequenceNode:
var res []orderedMap
// note that this has to be a pointer, so that nulls can be represented.
var res []*orderedMap
if err := node.Decode(&res); err != nil {
return err
}

View File

@ -48,6 +48,24 @@ banana:
test.AssertResult(t, expectedJson, actualJson)
}
func TestJsonNullInArray(t *testing.T) {
var sampleYaml = `[null]`
var actualJson = yamlToJson(sampleYaml, 0)
test.AssertResult(t, sampleYaml, actualJson)
}
func TestJsonNull(t *testing.T) {
var sampleYaml = `null`
var actualJson = yamlToJson(sampleYaml, 0)
test.AssertResult(t, sampleYaml, actualJson)
}
func TestJsonNullInObject(t *testing.T) {
var sampleYaml = `{x: null}`
var actualJson = yamlToJson(sampleYaml, 0)
test.AssertResult(t, `{"x":null}`, actualJson)
}
func TestJsonEncoderDoesNotEscapeHTMLChars(t *testing.T) {
var sampleYaml = `build: "( ./lint && ./format && ./compile ) < src.code"`
var expectedJson = `{"build":"( ./lint && ./format && ./compile ) < src.code"}`