Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force marshaler to use multi-line for string with \n #1013

Open
1755 opened this issue Jan 16, 2024 · 1 comment
Open

Force marshaler to use multi-line for string with \n #1013

1755 opened this issue Jan 16, 2024 · 1 comment

Comments

@1755
Copy link

1755 commented Jan 16, 2024

Hi!

I'm using gopkg.in/yaml.v3 version of the library and trying to serialize string with a lot of lines breaked with \n symbol.

Here is minimized code snippet which reproduces the issue(?):

type BugOrNotToBug struct {
	Description string `yaml:"description"`
}

b1 := BugOrNotToBug{
	Description: "Hello\nWorld",
}
b2 := BugOrNotToBug{
	Description: "Hello\n World",
}
b3 := BugOrNotToBug{
	Description: "Hello \nWorld",
}

c1, _ := yaml.Marshal(b1)
fmt.Printf("C1:\n%s\n", c1)

c2, _ := yaml.Marshal(b2)
fmt.Printf("C2:\n%s\n", c2)

c3, _ := yaml.Marshal(b3)
fmt.Printf("C3:\n%s\n", c3)

I've got the next results:

C1:
description: |-
    Hello
    World

C2:
description: |-
    Hello
     World

C3:
description: "Hello \nWorld"

which looks very unexpected for me. For some reason, it does not use multi-line if there is a whitespace before \n. Is it expected behavior? How can I force the library to use multi-line anyway? (I have quite large jsons which can not be modified which is serialized in a single line, but should be serialized in multi-line)

@kyouzzz
Copy link

kyouzzz commented Feb 1, 2024

got the same issue, so I split the source string by "\n", and then use strings.TrimRight(line, " ").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants