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

How to skip the failed property automatically whitout exception when deserializing? #680

Closed
emako opened this issue Feb 18, 2022 · 2 comments

Comments

@emako
Copy link

emako commented Feb 18, 2022

Feel free to ask any question on how to use the library. Alternatively, consider asking the question on Stackoverflow, tagged with yamldotnet.


Is there any scheme to automatically skip the property of the exception when deserializing?

Such as:

config.yaml:

first: something1 
second: something2 
third: something3

Config.cs:

public class Config
{
    public string first { get; set; }
    public string second { get; set; }
}

Because third is not in the class, deserialization will fail and an exception will be thrown.

How can it skip the third property automatically and continute the deserialization whitout exception.

The purpose is to construct Config class entities as much as possible.


I know YamlDotNet.Dynamic or RepresentationModel can do it, but it's manual, not automatic.

@EdwardCooke
Copy link
Collaborator

On the DeserializerBuilder call IgnoreUnmatchedProperties. That worked when I just tested it.

var deserializer = new DeserializerBuilder().IgnoreUnmatchedProperties().Build();
var yaml = @"
first: something1 
second: something2 
third: something3
";

var config = deserializer.Deserialize<Config>(yaml);
class Config
{
    public string first { get; set; }
    public string second { get; set; }
}

@EdwardCooke
Copy link
Collaborator

Re open if you need further help.

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