Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question What is the correct way to deserialise your save file?

Discussion in 'Scripting' started by Egregorious, Jul 25, 2022.

  1. Egregorious

    Egregorious

    Joined:
    May 31, 2016
    Posts:
    4
    I'm making my save/load system based on json, and I can serialize it to a file fine enough but I'm having a tough time figuring out how I'm meant to deserialize it properly, and at a certain point I problem solved my way to a situation in which I definitely feel like I am reinventing some sort of wheel that I just don't know about, and haven't been able to find via google.

    So I have a mutable Item class with a bunch of different subclasses, each with an associated immutable BaseItem class that can initialise them, for different types of items which need different variables, like this:
    Code (CSharp):
    1. public abstract Item
    2. {
    3.     public string ID { get; }
    4.     public BaseItem BaseItem { get; }
    5. }
    6. public class FoodItem : Item
    7. {
    8.     public Flavour[] flavours;
    9. }
    10. public class DrinkItem : FoodItem
    11. {
    12.     public float Volume { get; }
    13. }
    And these are held in various inventories. So I want to save each inventory with enough information that it can reinitialise all of the mutable items they contain via the immutable base classes. Many of the items (such as FoodItem in this example) store a reference to other classes that should not be saved with the object, but that's fine because I can instead save an ID to fetch those references upon initialising the Item.

    So to do this I figured I'd make a new, more easily serializable class for each Item class that needs to save new information, and simply convert one to the other when saving/loading, so I'd end up with this for actual serialisation:

    Code (CSharp):
    1. public abstract ItemSave
    2. {
    3.     public string ID { get; }
    4.     public string BaseItemID { get; }
    5. }
    6. public class FoodItemSave : ItemSave
    7. {
    8.     public string[] flavourIDs;
    9. }
    10. public class DrinkItemSave : FoodItemSave
    11. {
    12.     public float Volume { get; }
    13. }
    But I don't think I can deserialise into these classes directly from json without first knowing which of the classes I need to deserialise into. Which I don't, I have to deserialise it into a list of the base class. Deserialising it multiple times for this purpose seems like an overly complicated process.

    So I could make a single class with a very generic field such that each item class can manually convert data to and from it e.g.:

    Code (CSharp):
    1. public class ItemSave
    2. {
    3.     public Dictionary<string, string> Data { get; }
    4. }
    5.  
    I can deserialise this easily for every item, and so long as it is given the right data during the save process I can have each item class manually convert the Data property into the info they put into it.

    However it's at this point I realised I am definitely recreating some sort of wheel here. This has to be an issue other people have encountered and solved before me, and what I've ended up at is likely a silly convoluted solution. Does anyone know what I'm missing here, what the proper way to deserialise a list of subclasses is meant to be, or what the keywords are I need to search for to find it?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
  3. Egregorious

    Egregorious

    Joined:
    May 31, 2016
    Posts:
    4
    Thanks for your reply