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

Deserializing string with newlines adds extra lines #361

Closed
superyyrrzz opened this issue Oct 26, 2018 · 3 comments
Closed

Deserializing string with newlines adds extra lines #361

superyyrrzz opened this issue Oct 26, 2018 · 3 comments
Labels

Comments

@superyyrrzz
Copy link

superyyrrzz commented Oct 26, 2018

Reproduce code:

            var obj = new
            {
                Data = @"first line
second line
    third line with indentation
"
            };

            var serializer = new Serializer();
            string yaml = serializer.Serialize(obj);
            var deserializer = new DeserializerBuilder().Build();
            var deserialized = deserializer.Deserialize<Dictionary<string, string>>(yaml);

            Console.WriteLine(deserialized["Data"]);

Expect result:

first line
second line
    third line with indentation

Actual result:

first line
second line

    third line with indentation

The intermediate yaml is:

Data: >+
  first line

  second line

      third line with indentation

It seems the default serializer chooses folded scalar style, but it will introduce extra empty line when some line contains indentation. I can workaround this by using ScalarStyle.Literal as mentioned in #246 (comment), but I this it's a bug as data is changed after serialization and deseriazation. The serializer may not use folder style as default scalar style.

@superyyrrzz
Copy link
Author

I find this issue only happens when using \r\n line endings. \n works well.

@Shmalikov
Copy link

Hi!
I've the same issue. I am making my own DSL. It is dynamically resolve yaml files, so I have to use Dictionary<object, object> and can't use attributes to avoid this issue. Is there any progress in fixing this bug?

willson556 added a commit to willson556/YamlDotNet that referenced this issue Aug 6, 2021
If "\r\n\r\n" was found in an input string, "\r\n\r\n\r\n" would be
output instead of "\r\n\r\n\r\n". Same fix was used as in aaubry#179 for
literal scalars.
willson556 added a commit to willson556/YamlDotNet that referenced this issue Aug 6, 2021
If "\r\n\r\n" was found in an input string, "\r\n\r\n\r\n" would be
output instead of "\r\n\r\n\r\n". Same fix was used as in aaubry#179 for
literal scalars.
@aaubry aaubry closed this as completed in 35013cd Aug 6, 2021
@aaubry
Copy link
Owner

aaubry commented Jul 23, 2022

A fix for this issue has been released in version 12.0.0.

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

No branches or pull requests

3 participants