Skip to content

Object Serializers

TinyPlay edited this page Mar 9, 2023 · 3 revisions

About Object Serializers

The PixelSecurity Toolkit provides a wide range of classes for serializing and deserializing your objects. You might need this to save your classes to a file or other sources, and then retrieve it and convert it back to an object.

Available Object Serializers:

For Example we have an object based on class:

[System.Serializable]
public class MyObject{
   public string DataToSave = "Test";
}

MyObject myObj = new MyObject();

We can save this object using Json Serialization in the Json File:

{ "DataToSave": "Test" }

You can use our simple serializators for this:

JsonSerializer<MyObject> serializer = new JsonSerializer<MyObject>(
new JsonSerializer<A>.SerializationOptions
{
    Encoding = Encoding.UTF8,
    Path = "PathToFile"
});
serializer.LoadObject(myObj); // Load from JSON file to object myObj
serializer.SaveObject(myObj); // Save myObj object to JSON file

JSON Object Serializer

This modules provide JSON Object Serialization/Deserialization methods with Encryption Providers support.

Available Options in Constructor:

  • Path(string) - Path for save/load object from file;
  • Encoding(Encoding) - File Encoding for serialization. By default UTF8;
  • Encryptor(IDataEncryption) - Encryption Providers;

Available Methods:

  • SaveObject(dataToSave) - Serialize and Save object to file;
  • LoadObject(inputObject) - Deserialize object from file to inputObject instance;

Binary Object Serializer

This modules provide Binary Object Serialization/Deserialization methods.

Available Options in Constructor:

  • Path(string) - Path for save/load object from file;

Available Methods:

  • SaveObject(dataToSave) - Serialize and Save object to file;
  • LoadObject(inputObject) - Deserialize object from file to inputObject instance;

XML Object Serializer

This modules provide XML Object Serialization/Deserialization methods.

Available Options in Constructor:

  • Path(string) - Path for save/load object from file;
  • Encoding(Encoding) - File Encoding for serialization. By default UTF8;

Available Methods:

  • SaveObject(dataToSave) - Serialize and Save object to file;
  • LoadObject(inputObject) - Deserialize object from file to inputObject instance;

PlayerPrefs Object Serializer

This modules provide JSON Object Serialization/Deserialization to PlayerPrefs methods with Encryption Providers support.

Note! PlayerPrefs has limitation for size of the value. Please, read Unity documentation to read more

Available Options in Constructor:

Available Methods:

  • SaveObject(dataToSave) - Serialize and Save object to PlayerPrefs;
  • LoadObject(inputObject) - Deserialize object from PlayerPrefs to inputObject instance;

Write your own Object Serialzier

You also can create your own object serializer. Just create new class in "Assets/PixelSecurity/Core/Serializer/" based on ISerializer interface:

namespace PixelSecurity.Core.Serializer
{
    public class MySerializer<TObject> : ISerializer<TObject> where TObject : class
    {
        [System.Serializable]
        public class SerializationOptions : ISerializerOptions
        {
            // Serializer Options
        }
        private SerializationOptions _options;
        
        // Serializer Constructor
        public MySerializer(SerializationOptions options = null)
        {
            // Check Options
            if (options == null)
                _options = new SerializationOptions();
            else
                _options = options;

        }
        
        // Save object to file
        public void SaveObject(TObject dataToSave)
        {
            string convertedData = JsonUtility.ToJson(dataToSave);
            if (_options.Encryptor != null)
                convertedData = _options.Encryptor.EncodeString(convertedData);
            File.WriteAllText(_options.Path, convertedData, _options.Encoding);
        }

        // Load object from file
        public TObject LoadObject(TObject inputObject = null)
        {
            if (!File.Exists(_options.Path))
                return null;

            string reader = File.ReadAllText(_options.Path, _options.Encoding);
            if (_options.Encryptor != null)
                reader = _options.Encryptor.DecodeString(reader);
            inputObject = JsonUtility.FromJson<TObject>(reader);
            return inputObject;
        }
    }
}

Clone this wiki locally