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

YamlMember Alias usage #849

Closed
oqo0 opened this issue Sep 17, 2023 · 4 comments
Closed

YamlMember Alias usage #849

oqo0 opened this issue Sep 17, 2023 · 4 comments
Labels

Comments

@oqo0
Copy link

oqo0 commented Sep 17, 2023

I use YamlDotNet to read data from a config file like this:

ranks:
  - name: "Name"
    image-path: "assets/ranks/image1.png"
  - name: "Name"
    image-path: "assets/ranks/image2.png"

I try to deserialize it to a list of objects like this using IConfiguration:

public class Rank
{
    [YamlMember(Alias = "name")]
    public string Name { get; set; }

    [YamlMember(Alias = "image-path")]
    public string? Image { get; set; }
}
  builder.ConfigureAppConfiguration(options =>
  {
      options.AddYamlFile("config.yml", false);
  });
_ranks = _configuration.GetSection("ranks").Get<List<Rank>>();

When I read ranks from a configuration this way it doesn't work. Image property is always null. It looks like [YamlMember(Alias = "image-path")] is getting ignored when file is deserizlized.

I tried to change image-path property in my config to image like this

ranks:
  - name: "Name"
    image: "assets/ranks/image1.png"
  - name: "Name"
    image: "assets/ranks/image2.png"

And it works properly. Image property is getting deserialized to it's config value.

How can I fix this? Is there another attribute for deserialization?

I use YamlDotNet 13.3.1
net6.0

@EdwardCooke
Copy link
Collaborator

It could be the deserializerbuilder options that are set. Can you try deserializing it by itself without the configuration stuff?

something like
var yaml=….
var deserializer = new DeserializerBuilder().Build();
var o = deserializer.Deserialize<List>(yaml);

What library/code are you using to add the yaml file to the configuration object?

@oqo0
Copy link
Author

oqo0 commented Sep 18, 2023

I tried to deserializing it by itself and it didn't work without YamlMember attribute this time, but when I specified the Alias it worked.

Looks like you are right about deserializerbuilder options affecting this.

@oqo0
Copy link
Author

oqo0 commented Sep 18, 2023

I'm using generic host configuration for adding yaml file to the config object:

    builder.ConfigureAppConfiguration(options =>
    {
        options.AddYamlFile("config.yml", false);
        options.AddYamlFile("language.yml", false);
    });

This is used for a Discord.NET bot which is not supported by generic host by default, so I use Discord.Addons.Hosting for this.

@EdwardCooke
Copy link
Collaborator

Since we've narrowed it down to whatever the library you are using to add the Yaml to your configuration is doing and not an issue with YamlDotNet, I'm going to close this issue.

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