C
C#14mo ago
som45oul

✅ JsonSerializer: Won't deserialize correctly

Hi there. Hoping someone can help me spot the source of my problem with my simple WinForms app here.
Basically, I can serialize an object correctly, but when I try to deserialize it, the "sub objects" don't populate correctly. No errors are thrown.
The object itself is a Dictionary<string, Dictionary<string, ScreenSettings>> Where the following class is in the same form: public class ScreenSettings { public string Name; public bool Hide; } During debug, I can see that the JSON is correctly created with Serialize, however when I Deserialize, the "ScreenSettings" are not populated. What am I doing wrong here?
Grateful in advance for the kind help.
9 Replies
Pobiega
Pobiega14mo ago
can you paste the json and the class definitions?
cap5lut
cap5lut14mo ago
STJ ignores fields by default, so u would either have to make these properties (thats usually the better way anyway), or pass correctly configured options along: https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializeroptions.includefields?view=net-7.0#system-text-json-jsonserializeroptions-includefields
MODiX
MODiX14mo ago
Thinker
REPL Result: Success
public class ScreenSettings
{
public string Name { get; set; }
public bool Hide { get; set; }
}

var json = """
{
"Foo": {
"Bar": {
"Name": "baz",
"Hide": true
}
}
}
""";

System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, ScreenSettings>>>(json)
public class ScreenSettings
{
public string Name { get; set; }
public bool Hide { get; set; }
}

var json = """
{
"Foo": {
"Bar": {
"Name": "baz",
"Hide": true
}
}
}
""";

System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, ScreenSettings>>>(json)
Result: Dictionary<string, Dictionary<string, ScreenSettings>>
{
"Foo": {
"Bar": {
"name": "baz",
"hide": true
}
}
}
{
"Foo": {
"Bar": {
"name": "baz",
"hide": true
}
}
}
Compile: 578.230ms | Execution: 59.356ms | React with ❌ to remove this embed.
som45oul
som45oulOP14mo ago
Thanks. I did add the IncludeFields option, but it didn't seem to make any difference, unfortunately. I'll try using properties to see if that makes a difference. Thanks for the quick response!
cap5lut
cap5lut14mo ago
then u did something wrong:
MODiX
MODiX14mo ago
cap5lut
REPL Result: Success
internal class ScreenSettings
{
public string Name;
public bool Hide;
}


var options = new System.Text.Json.JsonSerializerOptions()
{
IncludeFields = true
};

var json = """
{
"Foo": {
"Bar": {
"Name": "baz",
"Hide": true
}
}
}
""";

System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, ScreenSettings>>>(json, options)
internal class ScreenSettings
{
public string Name;
public bool Hide;
}


var options = new System.Text.Json.JsonSerializerOptions()
{
IncludeFields = true
};

var json = """
{
"Foo": {
"Bar": {
"Name": "baz",
"Hide": true
}
}
}
""";

System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, ScreenSettings>>>(json, options)
Result: Dictionary<string, Dictionary<string, ScreenSettings>>
{
"Foo": {
"Bar": {
"name": "baz",
"hide": true
}
}
}
{
"Foo": {
"Bar": {
"name": "baz",
"hide": true
}
}
}
Compile: 594.205ms | Execution: 72.083ms | React with ❌ to remove this embed.
som45oul
som45oulOP14mo ago
Well, that worked. I took the bot's advice and yours, and made them properties ... bingo! Thank you for the help, folks.
cap5lut
cap5lut14mo ago
but yeah, making it properties is better anyway ;p if this answers ur question, dont forget to $close this thread 🙂
MODiX
MODiX14mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server