go-yaml / yaml

YAML support for the Go language.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Newline string literals produce invalid marshaling output

Alfus opened this issue · comments

If keys or values of a map node are the string literal "\n", the encoder has odd output.

func TestYamlNewLineMaps(t *testing.T) {
	value := map[string]string{
		"\n": "\n",
	}
	data, err := yaml.Marshal(value)
	if err != nil {
		t.Fatal(err)
	}
	// Should be `"\n": "\n"`, but its garbled.
	if string(data) != "? |4+\n: |4+\n" {
		t.Fatalf("Expected garbled output, got %s", string(data))
	}
}

The output is:

? |4+
: |4+

Which doesn't appear to be valid yaml. When it should be:

"\n": "\n"