-1

In our application which is maintained for years we use BinaryFormatter serialization of big data objects containing loads of collections and circular references. Serialization takes almost forever (~20 seconds) and takes much of CPU usage. I'd like to switch from this type of serialization to something better and light but without changing the code much as there was not much time given.

I've tried many of the solutions but some of them need:

  • class decorating or too many code changes (e.g. ProtoBuf);
  • doesn't allow circular references (e.g MsgPack);

Is there a way to smoothly switch to another and better serializer without pain and improve serialization process?

Mike G
  • 185
  • 5
  • 17

1 Answers1

-1

After doing some research and spending a day on implementing available solutions - I'll go with Json.NET.

I didn't have to change much to switch from BinaryFormatter, even the serializer attributes for objects haven't change. It runs way faster (~2 seconds with the same object size) and everything seems to work properly.

To pass the circular references and other errors I had to configure serializer:

JsonSerializer serializer = new JsonSerializer();
serializer.Converters.Add(new JavaScriptDateTimeConverter());
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
serializer.PreserveReferencesHandling = PreserveReferencesHandling.Objects;

using (StreamWriter sw = new StreamWriter(fileName))
{
    using (JsonWriter writer = new JsonTextWriter(sw))
    {
        serializer.Serialize(writer, dbObject);
    }
}
Mike G
  • 185
  • 5
  • 17