-
Notifications
You must be signed in to change notification settings - Fork 1
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:
- JSON File Serializer with Encryption Providers Support;
- XML File Serializer;
- Binary File Serializer;
- Player Prefs Serializer with Encryption Providers Support;
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 fileThis 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;
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;
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;
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:
- PlayerPrefsKey(string) - PlayerPrefs Key Name;
- Encryptor(IDataEncryption) - Encryption Providers;
Available Methods:
- SaveObject(dataToSave) - Serialize and Save object to PlayerPrefs;
- LoadObject(inputObject) - Deserialize object from PlayerPrefs to inputObject instance;
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;
}
}
}2023 (C) Developed by TinyPlay Games. Provided under MIT License.