Home
NetSerializer, A fast, mean and lean serializer for .Net
I needed a good serializer for my game [http://www.taika.org/~tomba/mygame/], and I wasn’t satisfied with anything I found. I didn’t need anything fancy like versioning, SOAP, XML, but speed of serializing and the size of the output were of top priority. Serializers in .Net framework were much too slow and produced huge output. Protobuf-net was better, but didn’t support structs. So, I had to make my own.
- Good for network serialization
- Supports classes, structs, enums
- No versioning or other extra information is serialized, only pure data
- Generates serializer and deserializer code as DynamicMethods
- No type IDs for primitive types, structs or sealed classes, so less data to be sent
- No dynamic type lookup for primitive types, structs or sealed classes, so deserialization is faster
- No extra attributes needed (like DataContract/Member)
- Not very polished, but works (for me)
Usage
Usage is simple. The types need to be marked with the standard [Serializable]. You can also use [NonSerialized] for fields you don’t want to serialize.
Initialize with known types. NetSerializer will scan through the given types, and automatically create (de)serializers for found types.
NetSerializer.Serializer.Initialize(types);
Serialize
NetSerializer.Serializer.Serialize(stream, ob);
Deserialize
(YourType)NetSerializer.Serializer.Deserialize(stream);
Limitations
There are some limitations with NetSerializer, but for me they were not an issue.
- As there’s no versioning information, the assemblies have to be the same on both serializing and deserializing ends. This also means that you shouldn’t serialize data to a file, as when the application is updated, the file is no longer readable
- Serialization and deserialization code has to be generated in one go, so all the serialized types need to be know at initialization time
Status
Works for me. I will continue to develop NetSerializer when I need something new, and I might even make it easier for others to use if asked nicely…
I have used it only on Windows, but I don’t see why it wouldn’t work with Mono also.
Performance
And some numbers, the classes below are used to create 100 ComplexTypesMessages, and the array and list in those messages contain 100 BasicTypesClasses. These are then serialized and deserialized 100 times.
class Message
{
}
class BasicTypesClass : Message
{
public byte Byte;
public short Short;
public int Int;
public long Long;
public bool Bool;
public string String;
}
class ComplexTypesMessage : Message
{
public Message[] ClassArray;
public List<Message> ClassList;
}
NetSerializer
869 ms
250700 bytes
BinaryFormatter
16128 ms
850600 bytes
ProtoBuf
3660 ms
412500 bytes

