Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First line added to YAML file #46

Open
mdmoura opened this issue Mar 31, 2018 · 5 comments
Open

First line added to YAML file #46

mdmoura opened this issue Mar 31, 2018 · 5 comments
Labels

Comments

@mdmoura
Copy link

mdmoura commented Mar 31, 2018

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?

@xoofx
Copy link
Owner

xoofx commented Mar 31, 2018

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

@xoofx xoofx added the question label Mar 31, 2018
@mdmoura
Copy link
Author

mdmoura commented Mar 31, 2018

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
Copy link
Author

mdmoura commented Apr 4, 2018

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

@xoofx
Copy link
Owner

xoofx commented Apr 5, 2018

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.

@mdmoura
Copy link
Author

mdmoura commented Apr 6, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants