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

Question - Serializing & Deserializing objects with inheritance #341

Closed
tomkerkhove opened this issue Jul 31, 2018 · 4 comments
Closed

Question - Serializing & Deserializing objects with inheritance #341

tomkerkhove opened this issue Jul 31, 2018 · 4 comments

Comments

@tomkerkhove
Copy link

tomkerkhove commented Jul 31, 2018

I'm trying to understand the serialization & deserialization of objects a bit and how this works with inheritance.

If I have a class Factory:

public class Factory
{
    public List<ICar> Cars { get; } = new List<ICar>();
}

Where ICar specifies the following:

public interface ICar
{
    string LicensePlate { get; set; }
    Make Make { get; set; }
}

Then I have different objects per car make such as Ford

public class Ford : Car, ICar
{
    public Make Make { get; set; } = Make.Ford;
    public bool HasSpareTier { get; set; }
}

Which uses Car:

public class Car
{
    public string LicensePlate { get; set; }
}

This perfectly serializes as following:

cars:
- make: Ford
  hasSpareTier: true
  licensePlate: 4391bbc9-af5c-4f50-adf4-85a522508267
- isDiesel: true
  licensePlate: 311c11ea-ca03-4525-9f9a-56fa6764b100
- hasAutomaticBreak: true
  make: Volvo
  licensePlate: 53a5c3c9-04f3-4573-9e4b-48a63befbd19

Stupid questions inbound:

  • How do I deserialize my objects so that I can cast them to Ford instead of ICar?
  • Should I use the YamlStream instead and parse it myself? This brings a lot of overhead that I would like to avoid (sample)

NuGet Version

YamlDotNet v5.0.1

Full Repro

You can find a repro of what I want to achieve here

This currently throws an exception but I'm honestly not sure why:

SerializationException: Property 'cars' not found on type TomKerkhove.YamlGenerics.Tests.Model.Factory'.

@tomkerkhove
Copy link
Author

I went for the YamlStream approach but would be good to know if this is coming/possible with the simple approach.

@Arakade
Copy link

Arakade commented Nov 14, 2022

I believe the answer is "Tagging" -- see https://github.com/aaubry/YamlDotNet/wiki/Serialization.Deserializer#withtagmappingstring-type
You register the mapping from a Ford to a tagged name with both serializer and deserializer and the former will output tags in the YAML stream to indicate the subtype and the latter will deserialize to the specific sub-type.
Have just implemented in our project to serialize and deserialize polymorphic classes implementing a single interface (declared as [YamlMember].
HTH

@BBoldenow-Alt
Copy link

@Arakade is correct - I do inheritance in this way.

@EdwardCooke
Copy link
Collaborator

Since this issue now has a really good answer I'm going to close it. Thanks @Arakade .

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

No branches or pull requests

5 participants