views:

555

answers:

3

I need a serialization library for .net with better features than the default xml serializer. The problem is that it is not polymorphic - say you have class B : A { }, and have serialized an type B, you should be able to call A myBaseType = Deserialize(xml) on it and be returned an instance of type B in the myBaseType variable.

I have an implementation at http://refactormycode.com/codes/710-polymorphic-xml-serializer with unit tests that the serialization library should be able to deal with. This implementation will not work if an object to be serialized has a child type which is also a polymorphic serialized type, so I need something better.

thanks!

A: 

Can you use .NET's binary serializer or does it need to be XML? I had a similar list of complaints about .NET's XML handling, particularly when trying to serialize graphs as opposed to trees.

In the places that I needed XML to work properly, I ended up just rolling out my own serialization code for each class.

DavGarcia
+1  A: 

If you can handle the additional verbosity going to a serializer which embeds the type information into the resulting stream is probably for the best.

.Net provides the SoapFormatter which is overly verbose and does things that you probably won't need but when you deserialize the stream it will simply give you back the appropriate objects (no need to specify a type just cast the root object to the type you know it to be).

As a fringe benefit it will correctly handle cycles and shared references within the object graph.

ShuggyCoUk
Looks great. You're right about it being a bit verbose n that but it WORKS!
mcintyre321
A: 

protobuf-net supports most inheritance routes (even though the spec doesn't). There is one known glitch (I'm working on it ;-p) - but it covers most options.

Marc Gravell