Coder Social home page Coder Social logo

Comments (8)

EdwardCooke avatar EdwardCooke commented on June 12, 2024

For the tilde that is a representation of null. It’s very possible it will return back an empty string or one with an empty line.

The other two you mentioned is white space that will also be treated as nothing so the result would be an empty string.

that being said I haven’t actually ran it on a computer yet so I’m not 100% certain. What are you seeing and expecting?

from yamldotnet.

mishun avatar mishun commented on June 12, 2024

Yes, des.Deserialize<string>("~") == null.

Currently:

var ser = new SerializerBuilder().Build();
Debug.Assert(ser.Serialize("~") == "~\n");
Debug.Assert(ser.Serialize("\t") == "\t\n");
Debug.Assert(ser.Serialize("\r") == ">2\n\n");

I'd expect something more like:

Debug.Assert(ser.Serialize("~") == "\"\\x7E\"\n");
Debug.Assert(ser.Serialize("\t") == "\"\\t\"\n");
Debug.Assert(ser.Serialize("\r") == "\"\\r\"\n");

perhaps? Or for tilde to be encoded just as tilde and null string --- with explicit tag?

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 12, 2024

When you instantiate the serializerbuilder cal withquotenecessarystrings.

from yamldotnet.

mishun avatar mishun commented on June 12, 2024

Thank you! That's much better:

var ser = new SerializerBuilder().WithQuotingNecessaryStrings().Build();
Console.WriteLine(ser.Serialize("\x0d"));

-->

"\r"

Unfortunately:

var ser = new SerializerBuilder().WithQuotingNecessaryStrings().Build();
Console.WriteLine(ser.Serialize("\x0d\x61"));

--->

>2-

  a

(0x61 is a code for 'a')

If you're wondering where do I get these annoying examples, I'm using QuickCheck variation for .NET.

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 12, 2024

Now that my laptop is up and running again I got to look at this.
I was able to narrow it down to differences in line endings. Windows is (0A0D) and Linux is (0A).
Using 0D is also valid, but things will get weird, as you're seeing.

Using the correct line endings things work as expected since it deserializes to the correct value.

End

The reason we output \r when that's the only character is due to the underlying emitter, when the scalar type is quoted (which quote necessary strings will set a string containing only line breaks, whitespace and other special characters) it will replace the special characters with escape codes. You can see where it does the escaping and what characters are escaped here

if (!IsPrintable(character) || IsBreak(character, out _) || character == '"' || character == '\\')

Since the second test, with the letter a on it, doesn't need to be quoted then it can be output using an empty line at the beginning as you saw.

To force it to default to double quoting where your use case will always pass, you can use .WithDefaultScalarStyle(YamlDotNet.Core.ScalarStyle.DoubleQuoted) instead of QuoteNecessaryStrings() like I suggested on the SerializerBuilder and it will pass. But, everything will default to double quotes and entries with new lines will become difficult to read.

If you want to only apply this to a specific property/field on an object, you can use the YamlMember attribute on that property/field and set the ScalarStyle to DoubleQuoted.

Here's the code I used to validate that this will work with 0A and 0D line endings.

using YamlDotNet.Serialization;

var str = new[] { "\x0a", "\x0a\x61", "\x0d", "\x0d\x61" };

Console.WriteLine("============================");
Console.WriteLine("Testing direct string");

foreach (var s in str)
{
    Test(s);
}

Console.WriteLine("============================");
Console.WriteLine("Testing class yamlmember");

foreach (var s in str)
{
    Test1(s);
}

void Test(string value)
{
    var serializer = new SerializerBuilder().WithDefaultScalarStyle(YamlDotNet.Core.ScalarStyle.DoubleQuoted).Build();
    var deserializer = new DeserializerBuilder().Build();
    var serialized = serializer.Serialize(value);
    var deserialized = deserializer.Deserialize<string>(serialized);
    Console.WriteLine("------");
    Console.WriteLine("Testing:");
    Console.Write(value);
    Console.WriteLine("---Serialized:");
    Console.WriteLine(serialized);
    Console.WriteLine("Deserialized:");
    Console.Write(deserialized);
    Console.WriteLine("---Matches:");
    Console.WriteLine(deserialized == value);
}

void Test1(string value)
{
    var tc = new TestClass {  X = value };
    var serializer = new SerializerBuilder().Build();
    var deserializer = new DeserializerBuilder().Build();
    var serialized = serializer.Serialize(tc);
    var deserialized = deserializer.Deserialize<TestClass>(serialized);
    Console.WriteLine("------");
    Console.WriteLine("Testing:");
    Console.Write(value);
    Console.WriteLine("---Serialized:");
    Console.WriteLine(serialized);
    Console.WriteLine("Deserialized:");
    Console.Write(deserialized);
    Console.WriteLine("---Matches:");
    Console.WriteLine(deserialized.X == value);
}

class TestClass
{
    [YamlMember(ScalarStyle = YamlDotNet.Core.ScalarStyle.DoubleQuoted)]
    public string X { get; set; } = string.Empty;
}

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 12, 2024

Did that answer your question?

from yamldotnet.

mishun avatar mishun commented on June 12, 2024

Sorry, got distracted.
Indeed, it seems to work with DoubleQuoted, thank you!

from yamldotnet.

EdwardCooke avatar EdwardCooke commented on June 12, 2024

Fantastic. I’m going to close this issue then

from yamldotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.