A library for serializing and deserializing in .net
- Primitives
- bool, string, byte, short, int, long, float, double, decimal (and unsigned variants).
- Lists + arrays
- Lists and arrays can contain any supported types.
- Note: Lists/arrays with value of
null
will be deserialized as an empty list/array.
- Objects
- Any object which has the [SerializeClass] attribute can be serialized as a field as well.
- Objects with value of
null
will be deserialized asnull
as expected.
- With attributes (automatic)
using SerializeLib.Attributes;
[SerializeClass]
public class SerializationExample {
[SerializeField(0)] public bool ExampleBool;
}
- With interface (manual)
If you want to manually serialize data which can't be serialized yet, you can read/write the stream.
Make 100% sure that you read EXACTLY as many bytes as you write. Failure to do so will fail to deserialize.
using SerializeLib.Interfaces;
internal class ManualSerializeClass : ISerializableClass<ManualSerializeClass> {
public int Number = 0; // Lets serialize this int
public void Serialize(Stream s)
{
Serializer.SerializeValue(Number, s); // Generic, so type is auto-detected here
}
public ManualSerializeClass Deserialize(Stream s)
{
Number = Serializer.DeserializeValue<int>(s); // Generic, type is specified here
return this; // "return this;" is not a hard requirement, in some cases, you might want to return something else.
}
}
using SerializeLib;
// Create an object to serialize
var exampleObject = new SerializationExample {
ExampleBool = true
}
// Serialize to a stream
var stream = new MemoryStream();
Serializer.Serialize(exampleObject, stream);
// Serialize to a byte[]
var bytes = Serializer.Serialize(exampleObject);
// Serialize and write to file
Serializer.SerializeToFile(exampleObject, "filename.bin")
using SerializeLib;
// Deserialize from a stream
var stream = new MemoryStream(); // In practice, this should be a stream with the serialized bytes
var exampleObject = Serializer.Deserialize<SerializationExample>(stream);
// Deserialize from a byte[]
var bytes = new byte[0]; // In practice, this should be a byte array with the serialized bytes
var exampleObject = Serializer.Deserialize<SerializationExample>(bytes);
// Deserialize from a file
var exampleObject = Serializer.DeserializeFromFile<SerializationExample>("filename.bin");