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 Ignoring variables during serialization? (depending on value?)

Discussion in 'Scripting' started by elFlashor, Jul 4, 2022.

  1. elFlashor

    elFlashor

    Joined:
    Jan 29, 2014
    Posts:
    2
    Hello everybody,

    I'm very new to serializing and deserializing data for saving and loading game states, and I'm current trying to understand how things work exactly.

    I have one particular question, which might be related to the format I'm saving my data (json), although I'm fairly certain it would still occur using other (binary for example), and I just want to poke you people's brains in order to help me get what I'm missing here. Or if I'm taking this issue way more seriously than it really is :)

    Let's consider the following class, that I want to serialize to save it on disk and load it later:
    Code (CSharp):
    1. public class SerializationExample
    2. {
    3.  
    4.     public Guid UID;
    5.     public List<string> content;
    6.  
    7. }
    Nothing too complex, it's just for the sake of the example.
    When I serialize this object and send it to my json, I always have all the fields added to the result, even if the variables are empty:
    Code (CSharp):
    1. {
    2.    "UID":"00000000-0000-0000-0000-000000000000",
    3.    "content":[]
    4. }
    I definitely don't need this empty UID, especially if I have thousands of them that can be empty.

    My question is: does anything exist to avoid having some variables at all in the parsed element, for example by checking against a predetermined "default" value that would then skip it entirely?

    It's not so much of a big deal, but if at some point you start saving hundreds or thousands of objects, in the long run it will eventually bloat up your save file, right? (especially if you have objects that can have many properties, and only some of them can be set while the others stay empty/default).

    Or do I have to rely on creating a much more complex hierarchy of objects to save (by inheritance for example) in order to save only the topmost required level? (something like "oh that object doesn't have a List<string> content value, then I'll just use an object of type SerializationExampleBase that only holds a UID"). Which is doomed to fail if I can have many permutations of possibly null objects and variables that I want to skip...

    Thanks for the help!
     
  2. bgulanowski

    bgulanowski

    Joined:
    Feb 9, 2018
    Posts:
    35
    How are you doing serialization? You haven't shown your code for that. But if you are using automatic serialization with the [Serializable] attribute, then, yes, the automatic serialization will do the simple thing.

    Alternately, you inherit the ISerializable interface and implement the methods, and you write conditional code to ignore fields that you don't want to serialize, and be careful to check that they exist when deserializing.

    https://docs.microsoft.com/en-ca/dotnet/api/system.runtime.serialization.iserializable

    Hope that gets you started.
     
  3. elFlashor

    elFlashor

    Joined:
    Jan 29, 2014
    Posts:
    2
    I didn't show anything for the serialization as I'm not doing anything in particular.
    Code (CSharp):
    1.  
    2. string json = JsonUtility.ToJson(data, true);
    3. File.WriteAllText(filePath, json);
    Basically that's what I'm doing, with data being a class that's supposed to hold my whole savegame. It would hold a lot of elements of type SerializationExample as I posted earlier (among other variables).
    The issue arise with whatever you're trying to serialize, as long as it has variables that can be "unused". You ask whatever formatter, be it Json, Binary, XML... to serialize your object, here for example the SerializationExample class I posted.
    If that element holds lots of variables that can be null at any moment, it will serialize the whole object, whatever the contents are. That is my problem, as I'd just like to skip some of it in order to minimize the final file size and loading process (to avoid having to set unused variables to default values and check upon loading if they are equal to these default value to ignore them...).

    But thanks for your answer :) The ISerializable interface seems to be interesting, I'll have a look! :)