SharpYaml
SharpYaml copied to clipboard
First line added to YAML file
I am serializing and object to YML and the first line on my YML file is:
!Proj.PostModel,%2520Proj,%2520Version=1.0.0.0,%2520Culture=neutral,%2520PublicKeyToken=null
Any idea where this comes from? How to avoid this being added to the YAML file?
It serializes the type information when it is not provided explicitly when serializing. It depends how you serialized your object.
I was able to determine why this is happening but don't know how to solve it. I have the following:
public class DataSeed {
protected Serializer YAMLSerializer { get; } = new Serializer();
}
public class InitDataSeed : DataSeed {
public InitDataSeed() {
Start();
}
public void Start() {
Serializer serializer = new Serializer();
Post post = new Post { Id = 2, Title = "Title" };
var result1 = serializer.Serialize(post);
var result2 = YAMLSerializer.Serialize(post);
}
}
The variable result1 doesn't get the line but result2 gets the initial line.
Any idea how to solve this but keeping the Serializer on the base class?
Does anyone has any idea why this happens and how to solve? Thank you.
The variable result1 doesn't get the line but result2 gets the initial line.
That's a different problem to your initial question but I can't reproduce this. On which .NET fwk/version are you using this?
Does anyone has any idea why this happens and how to solve?
For your initial problem, you need to pass the type to the serializer serializer.Serialize(post, typeof(Post));
to inform it that you know exactly the type and when you will deserialize from YAML, you will pass it again.
That's a different problem to your initial question but I can't reproduce this. On which .NET fwk/version are you using this?
I am using NET Core 2.0.6
For your initial problem, you need to pass the type to the serializer serializer.Serialize(post, typeof(Post));
Got it ... Didn't notice that constructor. May I leave a suggestion? How about adding the option:
serializer.Serialize<Post>(post);