Comments (8)
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.
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.
When you instantiate the serializerbuilder cal withquotenecessarystrings.
from yamldotnet.
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.
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
YamlDotNet/YamlDotNet/Core/Emitter.cs
Line 1109 in 1a73db7
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.
Did that answer your question?
from yamldotnet.
Sorry, got distracted.
Indeed, it seems to work with DoubleQuoted
, thank you!
from yamldotnet.
Fantastic. I’m going to close this issue then
from yamldotnet.
Related Issues (20)
- Problem Deserializing a list of player HOT 5
- Serialize/Deserialize an array of different derived types HOT 3
- How to serialize the !include Directive in YamlDotNet HOT 8
- Getting Following Error for YamlDotNet.NetCore HOT 3
- IEnumerable support in StaticGenerator
- `Yaml.Text(...)` fumbles without trailing whitespace HOT 1
- `WithDuplicateChecking()` conflicts with `MergingParser` operation HOT 3
- `ExampleFromSpecificationIsHandledCorrectlyWithLateDefine()` test should fail
- Questions about Custom Serialization HOT 2
- Invalid date format used in serialized JSON HOT 2
- YamlDotNet does not write accurately equal to yaml being loaded HOT 1
- JsonCompatible serializer is not Json compatible HOT 4
- Is the IDeserializer thread safe? HOT 2
- Update benchmark file
- 15.1.0 EnsureRoundTrip no longer works without tag mapping and protected default constructors HOT 4
- yamldotnet is missing NuGet package README file
- Json Compatible Serialization does not produce JSON for nodes.
- Including (parts of) another YAML file HOT 12
- Serilizing an object with tags similar to AWS Cloudformation HOT 4
- FSharp deserialization of records with optional record fields HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yamldotnet.