go-yaml icon indicating copy to clipboard operation
go-yaml copied to clipboard

JSONToYAML unexpected behavour when marshalling multiline strings

Open scriptonist opened this issue 2 years ago • 0 comments

Thanks for this amazing library. I wanted to raise a question/concern regarding the behaviour of JSONToYAML function.

Example 1

Say I have the following JSON

var s = []byte(`
{
"body": "\n{\n  my-baz: {{$body.input.foo.baz}}\n\n}\n"
}
`)

The generated YAML from yamlbs, _ := yaml.JSONToYAML(s) will be

body: "\n{\n  my-baz: {{$body.input.foo.baz}}\n\n}\n"

Example 2

But If I remove the : character from the body key like

var s = []byte(`
{
"body": "\n{\n  my-baz {{$body.input.foo.baz}}\n\n}\n"
}
`)

The YAML output turns out to be

body: |
  
  {
    my-baz {{$body.input.foo.baz}}
  
  }

Note that now it is rendered/marshalled as a multiline string.

I couldn't figure out if this was expected behaviour. So I cross-checked this with how sigs.k8s.io/yaml behaves. I found that Example 1 seems to generate a multiline string.

body: |2

  {
    my-baz: {{$body.input.foo.baz}}

  }

Playgroung Link

Is this a bug? If it is and if you can give me some additional background around its cause, I'll be happy to submit a PR.

Thanks :)

scriptonist avatar Feb 26 '22 06:02 scriptonist