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

Serializing string with newlines adds extra lines #246

Closed
mwhouser opened this issue Apr 7, 2017 · 3 comments
Closed

Serializing string with newlines adds extra lines #246

mwhouser opened this issue Apr 7, 2017 · 3 comments

Comments

@mwhouser
Copy link

mwhouser commented Apr 7, 2017

YamlDotNet appears to double the end-of-line markers.

For example:

var obj = new
{
	Data = "123\n456"
};

var serializer = new YamlDotNet.Serialization.Serializer();
string yaml = serializer.Serialize(obj);
Console.WriteLine(yaml);

outputs the following:

Data: >-
  123

  456

Internally, when I inspect the yaml variable, the text includes Data: >-\r\n 123\r\n\r\n 456\r\n. So it is indeed adding an extra line.

Whereas I would expect it to be:

Data: >-
  123
  456

and internally Data: >-\r\n 123\r\n 456\r\n.

@hawkerm
Copy link

hawkerm commented May 15, 2017

I'm not sure if I'm seeing the same issue here but in reverse with deserialization as well.

I'm trying to load in some text from a document section like this:

---
Some other text is here which starts our adventure...
There are many lines to this text.

Some are farther away then others and **bold**.

However, when I deserialize this the value only contains one newline where the blank line is instead of one at the end of each line.

parser.Current.ToString()
"Scalar [anchor = , tag = , value = Some other text is here which starts our adventure... There are many lines to this text.\nSome are farther away then others and **bold**., style = Plain, isPlainImplicit = True, isQuotedImplicit = False]"

@victor9000
Copy link

Adding the following attribute to your property solves this issue.

using YamlDotNet.Core;
using YamlDotNet.Serialization;
...
[YamlMember(ScalarStyle = ScalarStyle.Literal)]
public string MyProp { get; set; }

@aaubry
Copy link
Owner

aaubry commented Sep 25, 2019

I'm sorry I was unable to answer this question in a timely fashion. As you have certainly moved on to other things, I will close this issue, but feel free to reopen it if necessary.

@aaubry aaubry closed this as completed Sep 25, 2019
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

4 participants