❔ 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
JSON keys are strings. Period. They cannot be anything else.
Tbh I'd rather give the
Manager
class a Privilege
property
Much easier to handleI know that's much easier, but I have to use a Dictionary in my school project
Well, then, the answer is "you can't"
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
Thought you had to use a dictionary?
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
You could save it as a
Dictionary<Privilege, List<Manager>>
igYeah that should work indeed, thanks for the help!
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 guessWas 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.