views:

478

answers:

3

It appears that the DataContractSerializer isn't available in the .NET Compact Framework. I found this quite surprising, as I consider DataContractSerializer to be the Holy Grail of serialization, and one of the most widely useful classes introduced in .NET 3.

Is there a way to get the same functionality under the Compact Framework, that is, the ability to easily save and restore real-world object graphs using a textual format?

I'd rather keep away from the older, limited .NET serializers such as XmlSerializer and BinarySerializer.

I'm thinking perhaps there's a custom implementation of DataContractSerializer available, or maybe it's possible to use the code from Mono? Has anyone tried to do this?

+1  A: 

Is it not available in CF 3.5? (edit: nope, I checked - indeed it is not). I know that XmlSerialzier is there, but yes, it does have some limitations.

Does it need to be text-based? On option is protobuf-net, which works on CF2.0, CF3.5, Silverlight, Mono, .NET 2.0, etc; and includes all the things you commonly need (including callbacks etc).

By the way, the Mono code (from "Olive") is, last time I looked, not very complete...

(edit) One other option might be Json.NET; from here, this now works on CF; since this is JSON, it is text-based, and should do most of what you need.

Marc Gravell
Thanks! It has to be text based, because I found that's the only way I can refactor my code and still keep the data intact, even through namespace changes, class restructuring, etc. Most any problem is just a search-and-replace operation away to fix with text files.
Roy Peled
I'm trying out Json.NET, so far haven't been able to get it to deserialize properly. It doesn't seem to save any type information, so I can't see how it will handle, say, interfaces. Maybe there's an option somewhere, I'll keep looking...
Roy Peled
Re namespace/class etc - note that protobuf-net has *no* name information; I have users using it with obfuscated IL (i.e. members like AA1.aB) and it works fine. It won't handle interface based data, though - only concrete types. Maybe worth a look, though.
Marc Gravell
Thanks mark, protobuf-net may come in handy for other uses, but for saving and restoring state I need support for interface based data. It sounds like the same problem I'm facing with JSon.NET
Roy Peled
A: 

I don't know if it is fair to call the XmlSerializer "limited." What do you mean by that? Have you used it and found it wanting? What is the specific thing that fails for you?

In my experience, XmlSerializer actually has richer capability than the DCS. The DCS does less, which is probably a good thing. On the other hand the DCS does at least one thing the XmlSerializer does not - and that is it can serialize directly to and from private member vars.

The XmlSerializer is fast and quite capable, and you can get good utility out of it.

Cheeso
I've tried XmlSerializer and found it unusable for my needs. Main problems: 1. it can't handle interfaces or derived classes2. it forces you to create public read\write properties for any data, even if it should be private or read only.See my comment below for links about these points.
Roy Peled
http://geekswithblogs.net/SoftwareDoneRight/archive/2008/01/16/how-to-serialize-an-interface-using-the-xmlserializer.aspx) and http://www.danrigsby.com/blog/index.php/2008/03/07/xmlserializer-vs-datacontractserializer-serialization-in-wcf/
Roy Peled
A: 

Found a solution :-)

There's a great XML serializer at http://www.codeproject.com/KB/XML/GR_CustomXmlSerializer.aspx.

As is, the code doesn't work on the Compact Framework, but I've made several trivial adjustments to get it to work, with only a minor loss of functionality. I've also used OpenNetCF to fill in a few missing CF pieces.

Roy Peled