1

I'm trying to implement my own serializable dictionary based on the source code from here.

  • Obviously the keys, values, and count need to be serialized.
  • MSDN says you should never serialize hash codes.
  • But I'm having trouble wrapping my head around the usage of the buckets array and Entry.next field to determine if they should be serialized and if not, then how would I go about recalculating them during deserialization?
  • It's also entirely possible that since the arrangement of entries is derived from their hash codes in the first place, the arrays can't be directly serialized anyway and I should just use a regular dictionary synchronised with separate serialized key and value arrays.

Can anyone shed some light on this?

SilentSin
  • 1,026
  • 9
  • 18
  • How is this connected to Unity3D? – Kay Dec 28 '17 at 08:29
  • 1
    I'll be using the code in Unity, which may affect the validity of the assertion that hash codes shouldn't be serialized for example. I simply don't know, so I included it as a detail. – SilentSin Dec 28 '17 at 08:38
  • `Obviously the keys, values, and count need to be serialized.` Well, I am not sure the count needs to be serialised. – mjwills Dec 28 '17 at 09:56
  • Could you use JSON serialisation to generate a string? https://stackoverflow.com/a/28245944/34092 – mjwills Dec 28 '17 at 09:57
  • @mjwills If I didn't serialize the count then I wouldn't know how many elements there actually are when deserializing. I don't need to implement a serialization system like JSON, I just need to figure out which fields to tell Unity to serialize and how to recalculate the rest during deserialization. – SilentSin Dec 28 '17 at 10:34
  • `If I didn't serialize the count then I wouldn't know how many elements there actually are when deserializing.` Why do you need to know that? Just iterate through the valus one by one and add them to your dictionary when you deserialise. You can ask the count of the target dictionary when you are done. You don't need to know it upfront. – mjwills Dec 28 '17 at 10:35
  • I'm trying to serialize the dictionary's internal arrays instead of needing to allocate extra ones for serialization and deserialization. If I were just serializing separate arrays then adding them to the dictionary on deserialization then none of my original question would even be relevant because I wouldn't need to figure out the internal workings of the Dictionary class. – SilentSin Dec 28 '17 at 10:42
  • Why _specifically_ do you want to build your own custom serializable dictionary, rather than just using the standard `Dictionary`? – mjwills Dec 28 '17 at 11:05
  • Because I was hoping it could be more efficient if I could avoid allocating extra arrays and the other internal sorting stuff dictionaries do. I've come to the conclusion that it isn't really possible so I'm just inheriting from Dictionary and going ahead with the extra arrays. – SilentSin Dec 28 '17 at 11:11

0 Answers0