What are the best practices to store and maintain serialized objects in C#? Any strategies or patterns that apply?
What I have come to believe so far is this:
- Prefer Json over XML, both for space and for speed, but xml is easier to query/mine data via LINQ to XML for bigger data sets.
- For every single property, map explicitly to a serialized name. In the future, when you need to rename a property, your serialized data won’t break. Attributes help with this.
- Store some kind of version info on the serialized object, in case you need to mass-migrate data in the future
Update: (that I found out the hard way)
- Store all datetimes in a uniform way throughout the whole application and throughout all versions. Both concerning format and time zone.
4
I’ve done quite a bit of serialization over the years. Here are some things we came up with:
-
Prefer human readable formats over binary formats. I’d generally prefer Xml to Json mainly because you can use Xslt to transform Xml when you need to upgrade the version.
-
Whatever you are serializing stuff as, the deserialization logic and data storage should allow for versioning of the serialized data. Version info should probably be outside the serialized data, makes it a bit easier to get at and choose the right deserialization strategy.
-
Your serialization should have unit tests running both ways. IE, you should have a test deserializing the data and confirming you get everything imported correctly. And you should have something serialize the data and confirm it comes out as the correct json and xml.
-
If the point of said serialization is cross-platform, test against the other platform. Probably not as much of a problem now as platforms have matured, but there were times when things did not agree on how json should be formatted.
Mentioned points are practical and good to keep in mind. I would like to mention different types of serializations that might be useful to consider depending on your application needs.
- Binary Serialization – it uses binary encoding to produce compact serialization for uses such as
storage
orsocket-based network
streams. - XML serialization – it serializes the public fields and properties of an object, or the parameters and return values of
methods, into an XML stream that conforms to a specific XML Schema
definition language (XSD) document. More info is available in System.Xml.Serialization namespace . - SOAP Serialization – it is a different flavor of XML serialization, where it can be used to serialize objects into XML streams that conform to the SOAP specification. SOAP is a protocol based on XML, designed specifically to transport procedure calls using XML. As with regular XML serialization, attributes can be used to control the literal-style SOAP messages generated by an XML Web service.
More information form official source – MSDN C# programming
Also all this need to be practices within SDLC process (meaning having environment to test, version control, etc. )
4