✅ 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.
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
can you paste the json and the class definitions?
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
Thinker
REPL Result: Success
Result: Dictionary<string, Dictionary<string, ScreenSettings>>
Compile: 578.230ms | Execution: 59.356ms | React with ❌ to remove this embed.
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!
then u did something wrong:
cap5lut
REPL Result: Success
Result: Dictionary<string, Dictionary<string, ScreenSettings>>
Compile: 594.205ms | Execution: 72.083ms | React with ❌ to remove this embed.
Well, that worked. I took the bot's advice and yours, and made them properties ... bingo! Thank you for the help, folks.
but yeah, making it properties is better anyway ;p
if this answers ur question, dont forget to $close this thread 🙂
Use the /close command to mark a forum thread as answered