C
C#2d ago
Sckab

error during deserialization of a yaml file

I'm building a system that add the config file in the yaml format feature in my program, the program will throw this exception:
YamlDotNet.Core.YamlException: Exception during deserialization
System.InvalidCastException: Invalid cast from 'System.String' to 'dinfo.core.Handlers.ConfigTools.YamlConfigStructure'.
YamlDotNet.Core.YamlException: Exception during deserialization
System.InvalidCastException: Invalid cast from 'System.String' to 'dinfo.core.Handlers.ConfigTools.YamlConfigStructure'.
i will link the file in the message
21 Replies
Sckab
SckabOP2d ago
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
...

var yml = @"
name: George Washington
age: 89
height_in_inches: 5.75
addresses:
home:
street: 400 Mockingbird Lane
city: Louaryland
state: Hawidaho
zip: 99970
";

var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml
.Build();

//yml contains a string containing your YAML
var p = deserializer.Deserialize<Person>(yml);
var h = p.Addresses["home"];
System.Console.WriteLine($"{p.Name} is {p.Age} years old and lives at {h.Street} in {h.City}, {h.State}.");
// Output:
// George Washington is 89 years old and lives at 400 Mockingbird Lane in Louaryland, Hawidaho.
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
...

var yml = @"
name: George Washington
age: 89
height_in_inches: 5.75
addresses:
home:
street: 400 Mockingbird Lane
city: Louaryland
state: Hawidaho
zip: 99970
";

var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance) // see height_in_inches in sample yml
.Build();

//yml contains a string containing your YAML
var p = deserializer.Deserialize<Person>(yml);
var h = p.Addresses["home"];
System.Console.WriteLine($"{p.Name} is {p.Age} years old and lives at {h.Street} in {h.City}, {h.State}.");
// Output:
// George Washington is 89 years old and lives at 400 Mockingbird Lane in Louaryland, Hawidaho.
this is what is written in the docs
Keswiik
Keswiik2d ago
Post the full stack trace of your error.
Sckab
SckabOP2d ago
it's too long
Keswiik
Keswiik2d ago
$code
MODiX
MODiX2d ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Keswiik
Keswiik2d ago
use the paste site
Sckab
SckabOP2d ago
thanks!
Keswiik
Keswiik2d ago
I'm guessing something failed with deserializing your model and that's why this is breaking post the text in your yaml config file too
Sckab
SckabOP2d ago
yessir
Sckab
SckabOP2d ago
BlazeBin - atvyaayiqsav
A tool for sharing your source code with the world!
Sckab
SckabOP2d ago
this is the error, and this is the file:
recursive = true
recursive = true
i want that the user is able to just put in the file only the configs he wants to change
Keswiik
Keswiik2d ago
also, some critique on your model:
public class YamlConfigStructure
{
[YamlMember(Alias = "recursive")]
public bool RecursiveConfig { get; set; } = false;

[YamlMember(Alias = "verbose")]
public bool VerboseConfig { get; set; } = false;

[YamlMember(Alias = "ignore_gitignore")]
public bool IgnoreGitignoreConfig { get; set; } = false;

[YamlMember(Alias = "no_tui")]
public bool NoTuiConfig { get; set; } = false;

[YamlMember(Alias = "ignored_files_or_directory")]
public IgnoreFilesOrDirectory IgnoredFilesOrDirectory { get; set; } = new();
}
public class YamlConfigStructure
{
[YamlMember(Alias = "recursive")]
public bool RecursiveConfig { get; set; } = false;

[YamlMember(Alias = "verbose")]
public bool VerboseConfig { get; set; } = false;

[YamlMember(Alias = "ignore_gitignore")]
public bool IgnoreGitignoreConfig { get; set; } = false;

[YamlMember(Alias = "no_tui")]
public bool NoTuiConfig { get; set; } = false;

[YamlMember(Alias = "ignored_files_or_directory")]
public IgnoreFilesOrDirectory IgnoredFilesOrDirectory { get; set; } = new();
}
There's no real reason to append Config to every single property name. Making your model like this (and adding the YamlMember attributes) defeats the purpose of using:
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.Build();
Sckab
SckabOP2d ago
thanks! yeah, i need to change that
Keswiik
Keswiik2d ago
Then you need to tell the deserializer that's what you want by using IgnoreUnmatchedProperties. It's failing to load because you only have 1 key in there.
Sckab
SckabOP2d ago
i modified it like this, but it still gives the same error message
Sckab
SckabOP2d ago
in the error message isn't specified the exact line where the error accours, and this doesn't help me at all
Keswiik
Keswiik2d ago
It's working fine for me locally
class Config {
public bool Recursive { get; set; } = true;
public bool Verbose { get; set; } = true;
}

class Program
{
static void Main(string[] args) {
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();

var configStr = "recursive: false";

var config = deserializer.Deserialize<Config>(configStr);
}
}
class Config {
public bool Recursive { get; set; } = true;
public bool Verbose { get; set; } = true;
}

class Program
{
static void Main(string[] args) {
var deserializer = new DeserializerBuilder()
.WithNamingConvention(UnderscoredNamingConvention.Instance)
.IgnoreUnmatchedProperties()
.Build();

var configStr = "recursive: false";

var config = deserializer.Deserialize<Config>(configStr);
}
}
Keswiik
Keswiik2d ago
No description
Sckab
SckabOP2d ago
mmmmmmmmmmmmmmmmmmmmmmmmmmmh, this is really strange, i have the code on github, do you want to see the entire codebase (this is the first big project that i make so don't judge me pls)?
Keswiik
Keswiik2d ago
no point in me digging through your entire codebase for a serialization problem for starters, i would try fixing your config model and see if that changes anything
Sckab
SckabOP2d ago
it's not really big tho, you can check the file really fast

Did you find this page helpful?