开发者

Serializer that sees private data, has parsable output, supports generic collections, and supports custom serialization

开发者 https://www.devze.com 2023-01-16 12:07 出处:网络
I need a very specific kind of .NET serializer. My requirements: shouldn\'t skip private data output should be human-readable (or at least, easily parsable), preferably XML

I need a very specific kind of .NET serializer.

My requirements:

  1. shouldn't skip private data
  2. output should be human-readable (or at least, easily parsable), preferably XML
  3. should supp开发者_运维技巧ort generic collections (no need to be easy, but it should be possible)
  4. should support custom-implemented serialization for specific classes.

I need (2) for my elaborate version-tolerance scheme and (4) because I need to implement custom optimized serialization for a huge object (array) that I need to serialize.

XmlSerializer fails (1).

BinaryFormatter fails (2).

SoapFormatter fails (3) and seems to be deprecated.

DataContractSerializer fails (4) AFAIK.

NetDataContractSerializer seems to fail (2) because I read it's not "interoperable" (though it does use XML??)

Protobuf-net fails (2).

Could you recommend a serializer to use?

Also: How is NetDataContractSerializer not interoperable? Please give me an example where it breaks interoperability.

Note: all of those serializers support version tolerance in one way or another, but I need more advanced version tolerance features. See this question of mine if you care about why.

Edit: It turns out that both DataContractSerializer and NetDataContractSerializer support both (2) and (4) so my question is solved.


I don't think you need a custom serializer, I think you need custom serialization methods in your classes.

For example: if you're using XmlSerializer, you can implement IXmlSerializable on your data class, and implement methods by hand that can cover any combination of versions, private or public data, or anything else. Similarly ISerializable can be used to customize behaviour if you're using binary serialization.

If you need to support generic collections, you could implement a class defined something like this to solve the problem (assuing the Xml serializer for sake of example):

public class FooList : List<Foo>, IXmlSerializable

This allows you to customize the serialization of a semi-generic collection class. Or, perhaps more reusable:

public class CustomSerializableList<T> : List<T>, IXmlSerializable where T : IXmlSerializable

... which gives you a generic list whose contents are guaranteed to be IXmlSerializable, which means the implementation of IXmlSerializable for the list itself is quite straightforward.

0

精彩评论

暂无评论...
验证码 换一张
取 消