More problems with json serialization

I’m still having problems with a dictionary I set up to save data.
My data is saved in SaveData, containing lists and a dictionary of objects.
I’m saving like this

savedata = new SaveData();
savedata.dictionaryofobjects = playerdata.dictionaryofobjects;
other stuff...
SaveDictionary.Add(saveslot, savedata);

This appears to work on everything except where the dictionary (dictionaryofobjects) is concerned. If I save when that dictionary is empty, the save is fine, the save file contains everything in SaveDictionary in correct order with correct keys/saveslot, but if I save while there is stuff in the dictionaryofobjects, it doesn’t copy with that saveslot key only but in every single entry, which I can see on the saved file, so every saveslot has a copy of that dictionary in it!!! There is no loop anywhere causing this.

I suspect it has something to do with how I’m adding this to the main save dictionary like this, SaveDictionary.Add(saveslot, savedata).

I’m serializing like this:
string savePlayerData = JsonConvert.SerializeObject(SaveDictionary,Formatting.Indented);

Deserializing like this:
string loadPlayerData = File.ReadAllText(savepath);
JsonConvert.DeserializeObject<Dictionary<int, SaveData>>(loadPlayerData);

Gonna assume you’re using Newstonsoft.JSON (helps to point out what serialiser you’re using), which serialises by value and not by reference by default. Meaning multiple references to the same object will be serialised out as multiple different objects.

Here’s the docs on how to set this up: https://www.newtonsoft.com/json/help/html/preserveobjectreferences.htm

Personally I prefer the Odin Serialiser as this kind of stuff works out of the box without any extra work/hassle.

1 Like

Thanks, looks as if I’ll have to try Odin. Can’t find any info on installing though. Everyone I look it mentions odin inspector; just want the serializer. Download link on github goes to a pay version

There’s a download page on their website: Downloads

Under the ‘Open Source Serializer’ tab.

2 Likes

You can actually like to the tab like this:

https://odininspector.com/download#serializer

Note that the odin serializer is free, the odin inspector is not. Due to their advertisement the serializer is often shadowed by the paid stuff :slight_smile:

2 Likes

Yep thanks, got it set up but didn’t solve my problem. Something is amiss in my code where I’m saving the Class containing dictionaries with objects.

It seems Unity can’t handle dictionaries properly or am I wrong about this?
I’m having problems adding a class to a dictionary like this:
SaveDictionary.Add(saveslot, savedata), where savedata is the stuff from my class SaveData that I’m trying to stash in the dictionary before serialising … English spelling :slight_smile:

Everything inside SaveData transfers into the Dictionary except dictionaries inside the class. So for example, if I have this kind of thing:

[Serializable]
public class SaveData
{
public int score;
public string name;
public Dictionary<string, Dictionary<string, object>>
dictionaryofobjs = new Dictionary<string,Dictionary<string,object>>(){};
 
}

score and name will be okay. I can do this: savedata.score … savedata.name. But if I try to retrieve dictionaryofobjs from SaveDictionary (even without doing any saving to file etc.) there is a problem, either it’s missing or duplicated too many times. How do I stash class which contains nested dictionary into another dictionary and get it to work?

Unity has a more advanced serialization package if you want to give that a shot.

https://docs.unity3d.com/Packages/com.unity.serialization@3.1/manual/index.html

1 Like

have you considered creating a method that deconstructs the dictionaries into lists, then serializes the lists, then when you deserialize you would reassemble the lists into your original dictionary structure

1 Like

I wanted to avoid having to do something like that but looking like I will have to.

Is it just dictionary structure that Unity has a problem with or is it objects in the dictionary it can’t handle: Dictionary <string, object> etc.

im talking only about jsonUtility, anything that is complex enough that it cannot easily be recorded into text wont be able to be serialized

for example if you divide the dictionary into a list of keys and a list of values now you can serialize that, I see you would have to nest it further since you have dictionaries of dictionary, but you could still automate this with a bit of extra code

objects in general would also not be able to be serialized for example you would rather gather all the information and variables the object contains and serialize that, and then deserialize it and reconstruct the object with that data

dont know if the unity serializer is more straightforward where you can just lump everything in as it is and it will spit it back out the same way you left it

1 Like

I must be under stress or something. After a week going round in circles, discovered a blind spot, something that seems so obvious now that I could kick myself. I was not adding to dictionary at point of adding playerdata, I was updating. Insane that I never noticed it until this morning and I had looked at it a hundred times. smh

Anyway thanks everyone who chipped in to help.

2 Likes