SharpSerializer
You may use sharpSerializer free of charge for any purpose.

Though sharpSerializer is no more Beta, is reliable and stable.., it's still young, it's a baby.

Don't leave the baby alone!

To grow a healthy baby, don't ask - how to donate ;-)

Instead just place a link to sharpSerializer or recommend it to your friends.

Let's grow the baby together!

Pawel Idzikowski
Author of sharpSerializer
New - sharpSerializer on CodePlex
sharpSerializer is now on GitHub!

sharpSerializer

What is sharpSerializer?

sharpSerializer is an open source XML and binary serializer for .NET.
Why sharp? Because it was developed with C#. Why Serializer? Because it should primarily replace the built in XmlSerializer. Actually sharpSerializer can serialize to Xml and to its own binary format. However after providing it with custom writers, it can serialize data to other text formats like Json or other encrypted, compressed, optimized etc. binary streams.

If you are tired of marking your data model with plenty of attributes, or passing in advance to your current serializer all the types you could ever serialize - there is a solution - a free tool for such an easy and quick object serialization.
Please refer to GitHub for the latest version of SharpSerializer.

Why is sharpSerializer better then the XmlSerializer?

sharpSerializer can serialize everything what the XmlSerializer can, but easier. Additionaly it offers some extra features.

Types to serialize
XmlSerializer
sharpSerializer
Multidimensional arrays, nested arrays, array-of-arrays cross024
check024
Polymorphic properties (where property value is inherited from the property type - is its subtype)
cross024
check024
Generic types with polymorphic arguments (i.e. MyGenericClass<AbstractArgumentClass>) cross024 check024
Generic listings (i.e. Dictionary<K,T>) cross024 check024
Polymorphic listings (Collection, Dictionary, single dimensional array, multidimensional array) cross024 check024
Listings of generic types with polymorphic arguments (i.e. an array IMyGenericInterface<MyAbstractClass>[,,]) cross024 check024

Multidimensional arrays
It does not matter, how many dimensions or nested arrays an array has, 1,2,3...8. As the element type there can be a primitive type, complex object, generic type or nested collection - sharpSerializer can serialize all of them.

Polymorphic properties
There is property of type IMyInterface in a class. But as the property value contains an instance of a class MyClass, which implements IMyInterface. This is the polymorphic property. XmlSerializer could serialize such a property, only if provided in advance with the both types (IMyInterface and MyClass) in its Serialize() method. sharpSerializer can serialize such an object without additional type definitions nor marking your model with attributes.

Generic types with polymorphic arguments
Some generic class of type MyGenericClass<AbstractDataClass> contains an instance of RealDataClass which is inherited from the AbstractDataClass. This is a polymorphic argument, which the sharpSerializer can automatic detect and serialize.

Generic dictionaries
With sharpSerializer you can serialize generic Dictionary<K,T> where K and T are of a complex type or even nested dictionary.

Polymorphic listings
There is a dictionary of type Dictionary<IKeyInterface, AbstractValueClass> where keys are inherited from IKeyInterface and values are inherited from AbstractValueClass. We say - the keys and values are polymorphic. sharpSerializer can serialize it as a normal dictionary.

Listings of generic types with polymorphic arguments
Imagine a multidimensional array IMyGenericInterface<MyAbstractClass>[,,]. Who needs such a weird type at all? Never mind! sharpSerializer can serialize it too.

Why is sharpSerializer better than the BinaryFormatter?

For Silverlight programmers the benefit is clear - there is no BinaryFormatter in Silverlight at all ;-)
A significant drawback of BinaryFormatter in .NET Full is throwing an exception during deserialization if there are unknown properties in the deserialized stream. SharpSerializer since v.2.20 ignores these properties and can be used as a data storage.

The built in the full .NET Framework BinaryFormatter can serialize only classes which are marked with SerializableAttribute, or implement ISerializable. As mentioned above - sharpSerializer needs no extra attributes for serializing.

The following small test program (VS2010) measures serialization speed and size of the output file. Serialized was an array of 10 000 elements of type RootContainer. This is almost the same class as in the HelloWorld example - It's only extended with [Serializable] to all of its subclasses ;-)

speed tests


The following table presents the serialization results (sharpSerializer v.2.6):

Serializer
Serialization time [s] Filesize [MB]
Deserialization time [s]
.NET BinaryFormatter
2.1 19.3
7.9
sharpSerializer - binary (Burst)
3.1 32.4
11.0
sharpSerializer - binary (SizeOptimized) 3.8 8.5 2.6
sharpSerializer - Xml 3.7 86.0 17.6

As you can see, the fastest serialization of 2.1s offers BinaryFormatter, but its filesize of 19.3MB is twice as big as produces sharpSerializer in the binary SizeOptimized mode. Deserialization time of BinaryFormatter is over 3 times longer than the best time 2.6s of sharpSerializer. Be honest and tell me - who is the winner?

How does sharpSerializer work?

Serialization
Serialization contains of three steps:
  1. PropertyFactory converts source object to a Property class. The Property together with its nested Properties mirrors the internal object structure. Xml serialization and binary serialization use the same routine to extract properties from the serialized object and convert them into the Property.
  2. The Property is passed as an argument to IPropertySerializer.Serialize() method. When IPropertySerializer contains an instance of XmlPropertySerializer, it serializes the object into a tree oriented structure, where every element has its beginning and end, like xml. Alternatively it serializes the object into a fixed set of elements of a known length, i.e. a binary stream. In this case as IPropertySerializer operates BinaryPropertySerializer. Neither XmlPropertySerializer nor BinaryPropertySerializer knows in what format the data is stored. Storing the data as bits and bytes is responsibility of IXmlWriter or IBinaryWriter.
  3. IPropertySerializer invokes storage commands on correlated IXmlWriter or IBinaryWriter. DefaultXmlWriter stores data into Xml whereas BurstBinaryWriter or SizeOptimizedBinaryWriter uses binary format.


schema
Deserialization
Deserialization is a symmetric process. First IXmlReader or IBinaryReader reads the data from the stream. Then the data is deserialized by the IPropertyDeserializer into an instance of a Property class. The Property is passed as a parameter to the ObjectFactory.CreateObject() method which in turn converts it into a target object.

What can sharpSerializer serialize?

Out of the box sharpSerializer can serialize all properties which:
  • are public
  • are not static
  • have their set and get accessors
  • are not indexers
  • are not marked with ExcludeFromSerializationAttribute
  • are not listet in SharpSerializer.PropertyProvider.PropertiesToIgnore

Why only properties? Because it's quicker to serialize only properties and still enough to persist an object's state.

What can't sharpSerializer serialize?

In the current version of sharpSerializer there are some restrictions concerning serialization and deserialization of objects. In the future these restrictions can be neutralized, but actually they make no such pain. These are:

  • sharpSerializer can't deserialize types without public, standard constructor.
  • it cannot serialize fields.

These restrictions can be neutralized by using substitution pattern.


Following restriction concerns serialization in Silverlight and Portable Library:

  • it can't deserialize LowerBound of an array. In such case the LowerBound is set to 0 during deserialization.

LowerBound of an array is not part of Silverlight and therefore can not be handled by the sharpSerializer.

Does the world need one more serializer?

There are three reasons, why I started developing sharpSerializer:
  1. Few years ago I have been struggling with a polymorphic configuration. My purpose was - to save application configuration in a single XML file. I needed a lightweight configuration storage with support for object inheritance. The file should be easily readable and manually editable. The System.Configuration has big overhead and is too stiff.
  2. Almost every application needs repository for its data. With SharpSerializer I can freely define my data objects with no need to define database fields in advance, or reconfigure the O/R Mapper every time the model changes.
  3. Silverlight and especially WP7 (Windows Phone 7) has poor support for the local data repository with its XML DataContractSerializer. Even worse - it has no support for the binary serialization at all. SharpSerializer can tombstoning in binary and XML way. From now on it's very simple to store application data into isolated storage or serialize data and send it as binary or XML between .NET Full, Silverlight or other devices like Windows Phone, Windows RT or Xbox 360

Quo vadis sharpSerializer?

There are plenty of features which could be implemented in sharpSerializer, like:

  • custom binary writers with support for encryption, compression, checksum control
  • custom writers with support for other popular formats, like Json
  • porting of sharpSerializer to Mono and other plattforms

Everything depends on the demand and eventual popularity of sharpSerializer. The Framework is young yet, therefore each feedback is appreciated, every contributor is welcome, as I'm actually under pressure with other projects.