Note that there are some explanatory texts on larger screens.

plurals
  1. POSerialization for document storage
    primarykey
    data
    text
    <p>I write a desktop application that can open / edit / save documents.</p> <p>Those documents are described by several objects of different types that store references to each other. Of course there is a <code>Document</code> class that that serves as the root of this data structure.</p> <p>The question is how to save this document model into a file.</p> <h3>What I need:</h3> <ul> <li>Support for recursive structures.</li> <li>It must be able to open files even if they were produced from slightly different classes. My users don't want to recreate every document after every release just because I added a field somewhere.</li> <li>It must deal with classes that are not known at compile time (for plug-in support).</li> </ul> <h3>What I tired so far:</h3> <ul> <li><code>XmlSerializer</code> -> Fails the first and last criteria.</li> <li><p><code>BinarySerializer</code> -> Fails the second criteria.</p></li> <li><p><code>DataContractSerializer</code>: Similar to XmlSerializer but with support for cyclic (recursive) references. Also it was designed with (forward/backward) compatibility in mind: <a href="http://msdn.microsoft.com/en-us/library/ms733832.aspx" rel="nofollow noreferrer">Data Contract Versioning</a>. [edit]</p></li> <li><p><code>NetDataContractSerializer</code>: While the DataContractSerializer still requires to know all types in advance (i.e. it can't work very well with inheritance), NetDataContractSerializer stores type information in the output. Other than that the two seem to be equivalent. [edit]</p></li> <li><p><code>protobuf-net</code>: Didn't have time to experiment with it yet, but it seems similar in function to DataContractSerializer, but using a binary format. [edit]</p></li> </ul> <h3>Handling of unknown types [edit]</h3> <p>There seem two be two philosophies about what to do when the static and dynamic type differ (if you have a field of type object but a, lets say, Person-object in it). Basically the dynamic type must somehow get stored in the file.</p> <ul> <li><p>Use different XML tags for different dynamic types. But since the XML tag to be used for a particular class might not be equal to the class name, its only possible to go this route if the deserializer knows all possible types in advance (so that he can scan them for attributes).</p></li> <li><p>Store the CLR type (class name, assembly name &amp; version) during serialization. Use this info during deserialization to instantiate the right class. The types must not be known prior to deserialization.</p></li> </ul> <p>The second one is simpler to use, but the resulting file will be CLR dependent (and less sensitive to code modifications). Thats probably why <code>XmlSerializer</code> and <code>DataContractSerializer</code> choose the first way. <code>NetDataContractSerializer</code> is not recomended because its using the second approch (So does <code>BinarySerializer</code> by the way).</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload