-
-
Notifications
You must be signed in to change notification settings - Fork 482
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
Polymorphic deserialization - Alias $example_word cannot precede anchor declaration #842
Comments
Anchors are supported by the library. The type discriminators are new and may be the culprit. I’ll try and take a look in a bit. Can you try doing it without the type discriminator and see if you get the same error? |
What do you mean without the type discriminator do you mean without the extension method here: .WithTypeDiscriminatingNodeDeserializer(options =>
{
options.AddKeyValueTypeDiscriminator<IExample>("type",
("Word", typeof(WordExample)),
("Number", typeof(NumberExample)));
}) because if so no I don't get the same exception: System.InvalidOperationException: Failed to create an instance of type 'Experiments.Tests.Models.IExample'. Which I'm assuming that means it is the type discriminator? |
What I was meaning was this, which results in the same exception, skipping the TypeDescriminator var yaml = @"example_word: &example_word 'AN EXAMPLE WORD'
examples:
- type: Number
value: 1337
- type: Word
value: *example_word
";
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();
var o = deserializer.Deserialize<ExampleConfig>(yaml);
Console.WriteLine(o);
public class ExampleConfig
{
public List<Example> Examples { get; set; }
}
public class Example
{
public string Type { get; set; }
public string Value { get; set; }
} When I change I'm going to look into why it needs to be assigned to something and see if I can resolve that issue. I'll keep you updated. |
Ok, I think I figured out why it needs to be a property in a class, it needs to know the type of object that it needs to deserialize to. You will need to have If you don't want that property public, you can mark it as private and use the var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.WithTypeDiscriminatingNodeDeserializer(options =>
{
options.AddKeyValueTypeDiscriminator<IExample>("type",
new Dictionary<string, Type> {
{ "Word", typeof(WordExample) },
{ "Number", typeof(NumberExample) }
});
})
.IncludeNonPublicProperties()
.Build();
var o = deserializer.Deserialize<ExampleConfig>(yaml);
Console.WriteLine(o);
public sealed class ExampleConfig
{
#pragma warning disable IDE0051 // Remove unused private members
private string? ExampleWord { get; set; }
#pragma warning restore IDE0051 // Remove unused private members
public List<IExample> Examples { get; set; } = new List<IExample>();
} I'm going to close this issue since I've given you the answer to the problem. |
Ok so here is the situation i'm attempting to deserialize a list of interfaces using:
into:
and everything works fine normally but when I do something like:
using a alias/anchor I keep getting the exception:
YamlDotNet.Core.AnchorNotFoundException : Alias $example_word cannot precede anchor declaration
am I doing something wrong or does the library not support this?
The text was updated successfully, but these errors were encountered: