Usage

Important

Objects must be serializable in order to be serialized or deserialized. See Serializable objects for more information.

Important

The endianness for serializing and deserializing must be the same, the default is based on the system it runs on. See Endianness for more information.

Serializing an object

To a stream

var stream = new MemoryStream();
Serializer.Serialize(exampleObject, stream);

To a byte[]

byte[] bytes = Serializer.Serialize(exampleObject);

To a file

Serializer.SerializeToFile(exampleObject, "filename.bin")

Deserializing an object

From a stream

var exampleObject = Serializer.Deserialize<SerializationExample>(stream);

From a byte[]

var exampleObject = Serializer.Deserialize<SerializationExample>(bytes);

From a file

var exampleObject = Serializer.DeserializeFromFile<SerializationExample>("filename.bin");

Endianness

When deserializing, the endianness must be the same as when serializing. The default endianness is your system’s endianness.

Note

x86 systems are little endian, but many file formats, like png, use big endian. To work around this, simply set the UseBigEndian field of the Serializer object.

Serializer.UseBigEndian = true;