C
C#2y ago
Brent

❔ Help Serializing Dictionary

Hi everyone! I am trying to serialize a Dictionary<Manager, Privilege> so that I can save it to a file and deserialize it afterwards. I need to use a Dictionary for a school project, otherwise I would just store 'Privilege' as a field in the Manager class and use a List instead. So: The Key for the Dictionary is an instance of the 'Manager' class, the Value is an enum type. I will include screenshots of both classes in the post. I was thinking of using a JSON serializer but that doesn't seem to work. What would be the best solution to this? How can I save this Dictionary to a file? Any help would be appreciated, thanks in advance!
10 Replies
Angius
Angius2y ago
JSON keys are strings. Period. They cannot be anything else. Tbh I'd rather give the Manager class a Privilege property Much easier to handle
Brent
BrentOP2y ago
I know that's much easier, but I have to use a Dictionary in my school project
Angius
Angius2y ago
Well, then, the answer is "you can't"
Brent
BrentOP2y ago
So I'm just looking for a way to store the Dictionary data in a file, doesn't matter if it's using a JSON or XML file Because there are only 3 'Privilege' levels, I have a new idea: I can just create 3 different normal lists and save them each in a new file, one list per Privilege level. The list contains all the 'Manager' instances that have that specific privilege level. School sucks, this could be much easier but yeah
Angius
Angius2y ago
Thought you had to use a dictionary?
Brent
BrentOP2y ago
I have to use a dictionary in my program itself yes, but I can save it any way I want, it just needs to be saved somewhere some way
Angius
Angius2y ago
You could save it as a Dictionary<Privilege, List<Manager>> ig
Brent
BrentOP2y ago
Yeah that should work indeed, thanks for the help!
Omnissiah
Omnissiah2y ago
the (may i say) correct way to do this ot me would be having a manager table and a relationship with privilege using a ManagerId either way, you could serialize your Dictionary<Manager, Privilege> in xml but you have to do it by hand i guess
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server