C# types serialization library
Features:
- minimum result bytes array size (without compressing)
- program and version independence (but it's unsafe (without classes fields coincidence control))
- base for serialization methods auto builders
- opportunity to save serialization/deserialization type methods and use it
- using reflection only for type serialization methods building
Limitations
- serialization classes and structures extension isn't allowed. I.e. if
class Cat : Animal
will be passed with animal type, it will be serialized as anAnimal
- NetBinSerializer can't normal serialize/deserialize types, which use system pointers/descriptors and other resources
- you should know, which fields are in serialized class/structure
NetBinSerialization library has two serialization classes
SerializeStream - low level serialization object to serialize base types
Serializer - high level serialization static class with serialization methods caching, picking and building for difficult types
Interfaces:
ISerializable - interface with writeToStream and readToStream methods. If a class or structure implements this interface, serialization and deserialization will be complete by these methods call (deserialization works without constructor call!).
ISerializationMethodsBuilder - interface for high level serialize/deserialize custom methods builder class (implement getSerializationMethods for type serialization methods build)
As methods builder you can use SMAB library.
SerializationMethodsBase - object, that provides methods serialize(SerializeStream stream, object obj)
and object deserialize(SerializeStream stream)
. Serializer class contains cached Dictionary of <Type, ISerializationMethods>.
Stream for objects serialization/deserialization
- for serialization create SerializeStream by
new SerializeStream()
- for deserialization create SerializeStream by
new SerializeStream(byte[] bytes)
Supported main types: Int64, Int32, Int16, SByte, UInt64, UInt32, UInt16, Byte, Float, Double, String, bytes array, Serializable
- to write base type in bytes use
void Write+[var_type]([var_type])
orvoid Write([var_type])
overloaded method - to read base type in bytes use
[var_type] Read+[var_type]()
- you can write objects by
void WriteUnknown(object)
, but it's no reason to use this method. You should always know serializable/deserializable object's type
Also SerializeStream can work with other Arrays and Collections types, but it's better to use Serializer.serialize and Serializer.deserialize methods for difficult types Write and read other types methods:
void WriteArray(Array)
/Array ReadArray(Type)
with generic analogsvoid WriteCollectionObject(object)
/object ReadCollectionObject(Type)
void WriteObject(object)
/object ReadObject()
for difficultObject's (by using BinaryFormatter)
High level serialization class, with supports caching, serialize/deserialize methods save, custom methods builders It's better to use this for difficult types, which aren't SerializeStream main types (also it's better for arrays and collections)
bool IsCached(Type)
returns true if type's serialization methods are in cachebool GetCached(Type, out ISerializationMethods methods)
returns true and methods = serialization methods container if type's serialization methods are in cache else falsebool Cache(SerializationMethods.SerializeMethod, SerializationMethods.DeserializeMethod, Type)
adds serialize and deserialize methods in cache, returns false, if methods for type are already in cache.bool Cache(this ISerializationMethods, Type)
cache function for methods in ISerializationMethods shell
bool BuildAndCacheIntegrated(Type)
(builds and caches type serialization methods with integrated SimpleSerializationMethodsBuilder (only for arrays and collections)) returns true if successbool BuildAndCache(Type)
builds and caches type serialization methods with custom serializationMethodsBuilder
SerializeSafe/DeserializeSafe and generics analogs
returns true, if obejct was successfully serialized/deserializedSerialize/Deserialize
if serialization/deserialization fail occured, thrwos SerializationException Also Serializer methods has this SerializeStream arg, and because you can writestream.serialize(myObject, typeof(MyObject))
SerializationContext is optimization class, that solves some references problem (inclusive references cycles in the serialize tree) and approves null values
Example code for low and hight levels serialization/deserialization