Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.
share|improve this question
    
What operations will be done on those serialised data? Only read/write or something more complex? –  Euphoric Jul 24 '12 at 9:15
    
It's more of a general question but in my case I need to store small settings objects that might or might not be updated. –  Mihalis Bagos Jul 24 '12 at 9:17
1  
4. Follow the rules until it makes more sense to break the rules. –  Robert Harvey Jul 24 '12 at 23:11
    
What do you mean? What are "the rules" in this case? –  Mihalis Bagos Jul 25 '12 at 8:04
add comment

2 Answers 2

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.

share|improve this answer
add comment

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 or socket-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. )

share|improve this answer
    
Why would you want to store as binary or soap? Soap as far as I understand is overbloated so as to avoid data corruption, something that is a non-issue while storing in a known system. –  Mihalis Bagos Jul 25 '12 at 10:30
1  
it is about speed, the fastest is binary serialization. –  Yusubov Jul 25 '12 at 11:36
    
The BinaryFormatter is also the least change tolerant option. We've had endless struggles with it and now wish we'd never used it. See: stackoverflow.com/a/703361/505697 –  HappyCat Jul 25 '12 at 11:53
    
sure, everything has it's own issues, and depends where it is used. as it is mentioned, for cross-platform compatibility it is not an ideal option, this is where XML serialization takes the stage. –  Yusubov Jul 25 '12 at 12:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.