Use custom serializer

By default, Databrain uses the Newtonsoft Json library to serialize the data to a json file. It is possible though to serialize the data with a custom serializer such as Odin Serializer.

Simply use the API methods GetSerializableData and SetSerializedData.

Here's a simple example using Odin Serializer:

using OdinSerializer;

// The data library
public DataLibrary data;

public void Save()
{
    // Get the serializable data class from the data library
    var serializableData = data.GetSerializableData();
    
    // Use Odin Serializer to serialize the data
    byte[] bytes = SerializationUtility.SerializeValue(serializableData, DataFormat.Binary);
    // Write to a file
    System.IO.File.WriteAllBytes(System.IO.Path.Combine(Application.persistentDataPath, "saveFile.data"), bytes);
}

public void Load()
{
    // Load the file as bytes
    byte[] bytes = System.IO.File.ReadAllBytes(System.IO.Path.Combine(Application.persistentDataPath, "saveFile.data"));
    
    // Deserialize the data using Odin Serializer
    var serializableData = SerializationUtility.DeserializeValue<DataLibrary.JsonSerializeableClass>(bytes, DataFormat.Binary);
    
    // Set the data back to the data library.
    // This will load the complete data and also calls the OnLoaded event.
    data.SetSerializedData(serializableData );
}

Last updated