SharpYaml icon indicating copy to clipboard operation
SharpYaml copied to clipboard

First line added to YAML file

Open mdmoura opened this issue 6 years ago • 5 comments

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?

mdmoura avatar Mar 31 '18 12:03 mdmoura

It serializes the type information when it is not provided explicitly when serializing. It depends how you serialized your object.

xoofx avatar Mar 31 '18 12:03 xoofx

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?

mdmoura avatar Mar 31 '18 12:03 mdmoura

Does anyone has any idea why this happens and how to solve? Thank you.

mdmoura avatar Apr 04 '18 21:04 mdmoura

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.

xoofx avatar Apr 05 '18 07:04 xoofx

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);

mdmoura avatar Apr 06 '18 18:04 mdmoura