SharpYaml
SharpYaml copied to clipboard
Serializer emits incorect data on second attempt
class Data
{
public string Text { get; set; }
}
class Program
{
static void Main(string[] args)
{
var data = new Data { Text = "text" };
var s = new Serializer();
var a1 = s.Serialize(data);
var a2 = s.Serialize(data);
Console.WriteLine(a1 == a2);
}
}
Result is False Expected is true
Could you dig the details of what it serializes?
second one returns
0o/r/n
Ok. It is because the Serializer maintains a state to support YAML aliasing. You should disable alias using a SerializerSettings.EmitAlias = false
Ok, I see. Is it possible to drop state? Should I recreate serializer for that?
As I said above, if you use a proper SerializerSettings with Serializer, by setting SerializerSettings.EmitAlias
set to false.
Just want to make it clear - If I still need aliases but only within single serialization process - I cannot use the same instanse of serializer and I have to create a new one. Correct?
yes
The state is kept inside the AnchorSerializer class, it will the top most ChainedSerializer that the Serializer will create if EmitAlias is set to true (it basically means that it is the ObjectSerializer field of the Serializer), then you can safely clear both dictionaries inside the AnchorSerializer.