2

I need to binary serialize an object that contains a collection of lots of instances of some base class. Each instance could be any of the derived types, and there are really lots of possible derived types (around one hundred). Therefore I don't really want to modify each of these types so that this object could be serialized.

I would even like to avoid adding the default public constructor to all of these types as this would damage the architecture a little bit and would also be really annoying to do (and not really DRY), not speaking of adding Serializable attribute to every public property in these types. And for the same reason writing a custom serializer/surrogate for each of these types is hardly an option.

What I have tried so far:

I have also tried serializing with Json.NET and then saving the result as a byte array (I know, I know), but I ran into an OutOfMemoryException while serializing. This likely means my object is too heavy for text serialization (it takes around 200Mb in memory, but there are lots of elements in the array, lots of properties in each of the element etc).

If it makes any better - all the objects in the collection I need to save have only public properties (I don't need to serialize fields or private properties). And there also is no particular logic in these objects' constructors - only filling the properies.

Is there any way to achieve the serialization/deserialization without modifying the serialized classes?


I don't even know if it is possible to deserialize an object that does not have parameterless constructor (the restriction required by all of the serializers I've met), but it should be as the reflection allows to create instances without calling constructors.

Community
  • 1
  • 1
bashis
  • 1,200
  • 1
  • 16
  • 35
  • Did I miss the question in there? – Ron Beyer Sep 29 '15 at 18:05
  • @RonBeyer the question is how to achieve binary serialization/deserialization without modifying the serialized classes. Sorry, I've added it now :) – bashis Sep 29 '15 at 18:10
  • I believe this is what `SerializationSurrogate` was created for. A good tutorial can be fond here http://www.techrepublic.com/article/application-development-advanced-serialization-in-net/ – Bradley Uffner Sep 29 '15 at 19:36
  • 1
    @BradleyUffner Yeah, but do I understand it correctly that usage of surrogates assumes creating one for each of the serialized types? If so, this is hardly a solution. – bashis Sep 29 '15 at 19:40
  • You can use a custom SurrogateSelector to use the same surrogate for multiple types. – Bradley Uffner Sep 30 '15 at 01:43
  • If JSON would work in theory but requres too much memory, you could try using Json.NET to [serialize to BSON](http://www.newtonsoft.com/json/help/html/SerializeToBson.htm) instead of JSON. Incidentally, Json.NET can [write directly to a stream](http://james.newtonking.com/archive/2009/02/14/writing-json-to-a-file-using-json-net). – dbc Sep 30 '15 at 05:29
  • Or, you could compress the JSON as you write it using a `DeflateStream`. See for example https://dotnetfiddle.net/Bu5eEI. JSON seems to compress well with up to a 10x size. Performance is not so great though. – dbc Sep 30 '15 at 06:10

0 Answers0