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

Serialize/Deserialize an array of different derived types #876

Closed
Myonmu opened this issue Dec 4, 2023 · 3 comments
Closed

Serialize/Deserialize an array of different derived types #876

Myonmu opened this issue Dec 4, 2023 · 3 comments

Comments

@Myonmu
Copy link

Myonmu commented Dec 4, 2023

Hi,

I am fairly new to the library and I am trying to serialize and deserialize a list of types derived from a base type, consider this example:

class BaseType{
    public string name;
}
class TypeA: BaseType{
    public int a;
}
class TypeB: BaseType{
    public string b;
}

And the class I would like to serialize contains a field : public List<BaseType> list.

The desired output is like this:

list:
  - name: "A"
     a: 1
  - name: "B"
     b: "b"

The default serialization actually works and outputs result as desired, but deserialization isn't. The list is empty after default serialization. I would like to know how this could be achieved?

@EdwardCooke
Copy link
Collaborator

You probably need a type descriminator to do this.

https://github.com/aaubry/YamlDotNet/wiki/Deserialization---Type-Discriminators#determining-type-based-on-the-value-of-a-key

let me know if you need more assistance. If you do, please post the code for creating your deserializer.

@Myonmu
Copy link
Author

Myonmu commented Dec 5, 2023

You probably need a type descriminator to do this.

https://github.com/aaubry/YamlDotNet/wiki/Deserialization---Type-Discriminators#determining-type-based-on-the-value-of-a-key

let me know if you need more assistance. If you do, please post the code for creating your deserializer.

That worked! Thanks!

@EdwardCooke
Copy link
Collaborator

Great! I’ll close this issue then.

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

No branches or pull requests

2 participants