Click here to Skip to main content
Click here to Skip to main content

Tagged as

Silverlight Binary Serialization

, 18 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
I needed a small footprint, highly performant binary, reflection based serializer for the project I am working on...
Download the C# project for the serializer from here.
 
I needed a small footprint, highly performant binary, reflection based serializer for the project I am working on. I couldn't find anything that fitted my needs on the web, so I built one and offer it here for anyone else who has similar requirements.
 
Background
 
With the introduction of shared classes between Silverlight and .NET, it can be very useful to use serialization to pass data to and from WCF services - the only in built way to do this is using the DataContractSerializer for Plain Old CLR Objects (POCO). XML serialization is very slow however and produces very large strings, plus you have to declare the KnownTypes for everything that might be included in the object graph.
 
I looked around at a number of other serializers, but none of them worked the way I wanted or in many cases worked at all!
 
So I wrote a new serializer which uses reflection to inspect the object graph being serialized and can store all of the public read/write properties and fields of the object. It stores the data in a binary format and uses tokenization to reduce the size of the outputted byte array.
You could further compress this array for transmission (I use a ZIP library in the project that this serializer comes from).
 
Features
  • Caches property information for rapid performance
  • Stores only properties which are different from a vanilla instance created using a parameterless constructor
  • Doesn't store typenames when they can be inferred from property types (but does support setting a property to a sub classed item, in which case the type is stored)
  • Tokenizes property and field names to reduce space
  • Tokenizes stored types to save space
  • Does not require definition of known types
 
You use the serializer either directly by calling the static Serialize(item) method or by using the extension method .ToBytes() on any object. To rehydrate the object, you call Deserialize passing in a previously created byte array.
There are a couple of limitations (which could be addressed easily in the code if you need to):
 
  • Only serializes 255 properties per object (though the object can have more properties than that, it uses a byte to say how many properties need to be serialize)
  • Supports up to 65535 types in the object graph
  • Supports up to 65535 unique property/field names in the graph
 
//
// Serialization
//
var bytes = AnyObject.ToBytes();
var otherWay = Serializer.Serialize(AnyObject);
        
//Deserialization
MyItem anInstanceOfMyItem = Serializer.Deserialize(bytes) as MyItem;
 
Of Interest
 
One interesting thing to note is that the code ran up to 4 times faster when I converted the enumeration of reflected PropertyInfos for a class into an Array, clearly the cost of Next on the original enumeration is significant and really proves the point about caching rather than calling every time.
 
The serializer ended up using Stacks and thread statics to handle recursion (when an item needs to store a serialized item as a body) - this would probably be better off as a utility instance, but at least it means that the class is static and can declare its own extension methods!
 
I use this to support Agent Pattern development where UI agents get sent to run in a WCF service, collect information and then return to the client.
 
The latest version and documentation are available on my blog.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

michael_john_talbot
Chief Technology Officer Alterian plc
United States United States
Mike Talbot is CTO of Alterian, a software company he helped found in 1997. Mike is a software architect and developer, currently developing Silverlight applications in C#. Alterian produces analytical database solutions for marketers as well as campaign management, email, social media monitoring and content management software packages. He started his career as a game programmer working for UbiSoft and Electronic Arts among others.
 
You can learn more about Alterian at http://www.alterian.com

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141220.1 | Last Updated 18 Sep 2010
Article Copyright 2010 by michael_john_talbot
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid