CArchive, Serializing & Schema
-
I have a situation where I need to serialize several parts of a document-derived class. Because the standard doc loading / saving mechanism is not quite what I need, I have to construct the CArchive manually. All the serialization is working fine in itself, but the one thing I can't seem to get to work now I need it is the schema business. My doc-derived class uses IMPLEMENT_SERIAL() with the current schema number correctly. I create the archive correctly and then start serializing out (when saving, this is)... but for some reason it never saves the schema in the binary file. I tried SetObjectSchema() immediately after constructing the CArchive, which certainly sets the internal schema value... but that never gets written to file. I looked at the code for CDocument where it calls our overriden Serialize() to see how it did it - from that I see it sets the archive's m_pDocument and another member. So I did this also... STILL doesn't save the schema in the file. Since CDocument::Serialize() does not do anything, calling the base class has no effect. Any clues as to what I'm missing? Otherwise I'll have to do the schema bit myself. But I'd rather use the built-in stuff as much as possible.
-
I have a situation where I need to serialize several parts of a document-derived class. Because the standard doc loading / saving mechanism is not quite what I need, I have to construct the CArchive manually. All the serialization is working fine in itself, but the one thing I can't seem to get to work now I need it is the schema business. My doc-derived class uses IMPLEMENT_SERIAL() with the current schema number correctly. I create the archive correctly and then start serializing out (when saving, this is)... but for some reason it never saves the schema in the binary file. I tried SetObjectSchema() immediately after constructing the CArchive, which certainly sets the internal schema value... but that never gets written to file. I looked at the code for CDocument where it calls our overriden Serialize() to see how it did it - from that I see it sets the archive's m_pDocument and another member. So I did this also... STILL doesn't save the schema in the file. Since CDocument::Serialize() does not do anything, calling the base class has no effect. Any clues as to what I'm missing? Otherwise I'll have to do the schema bit myself. But I'd rather use the built-in stuff as much as possible.
How are you serializing document members? Using m_foo.Serialize(ar) or ar << m_pFoo? Tomasz Sowinski -- http://www.shooltz.com
-
How are you serializing document members? Using m_foo.Serialize(ar) or ar << m_pFoo? Tomasz Sowinski -- http://www.shooltz.com
Everything is done using ar << m_pFoo (no objects other than CString are serilaized - most of what is saved is structures, and I do the members of each by hand). You sound as if you're hinting that each object (class) has its own schema... which I can understand... but should there not be an overall schema for the document / doc-derived object? It does, after all, have IMPLEMENT_SERIAL()... Basically, I'm not interested in the individual schemas of each thing I serialize - all serializing is done from one class (the doc-derived) (since there are no child objects being serialized, not even CXXXArrays). I want one overall schema that is used in all serializing operations so that I can check it on loading to avoid loading new data from old-schema files...
-
How are you serializing document members? Using m_foo.Serialize(ar) or ar << m_pFoo? Tomasz Sowinski -- http://www.shooltz.com
Everything is done using ar << m_pFoo (no objects other than CString are serilaized - most of what is saved is structures, and I do the members of each by hand). You sound as if you're hinting that each object (class) has its own schema... which I can understand... but should there not be an overall schema for the document / doc-derived object? It does, after all, have IMPLEMENT_SERIAL()... Basically, I'm not interested in the individual schemas of each thing I serialize - all serializing is done from one class (the doc-derived) (since there are no child objects being serialized, not even CXXXArrays). I want one overall schema that is used in all serializing operations so that I can check it on loading to avoid loading new data from old-schema files...
-
Everything is done using ar << m_pFoo (no objects other than CString are serilaized - most of what is saved is structures, and I do the members of each by hand). You sound as if you're hinting that each object (class) has its own schema... which I can understand... but should there not be an overall schema for the document / doc-derived object? It does, after all, have IMPLEMENT_SERIAL()... Basically, I'm not interested in the individual schemas of each thing I serialize - all serializing is done from one class (the doc-derived) (since there are no child objects being serialized, not even CXXXArrays). I want one overall schema that is used in all serializing operations so that I can check it on loading to avoid loading new data from old-schema files...
Schema number is written to file only if you use CArchive::WriteObject (operator << for CObject pointers calls this method). WriteObject uses CArchive::WriteClass for sort of 'metadata' (class name, schema version). Default serialization implemented in CDocument::OnSaveDocument uses plain Serialize. This means that you can't call GetObjectSchema to get document schema number in CYourDoc::Serialize - it's not in the file. You've mentioned that you're creating CArchive yourself - if you're replacing OnSaveDocument you can also try to replace CYourDoc::Serialize() with ar << pDoc. Tomasz Sowinski -- http://www.shooltz.com
-
Schema number is written to file only if you use CArchive::WriteObject (operator << for CObject pointers calls this method). WriteObject uses CArchive::WriteClass for sort of 'metadata' (class name, schema version). Default serialization implemented in CDocument::OnSaveDocument uses plain Serialize. This means that you can't call GetObjectSchema to get document schema number in CYourDoc::Serialize - it's not in the file. You've mentioned that you're creating CArchive yourself - if you're replacing OnSaveDocument you can also try to replace CYourDoc::Serialize() with ar << pDoc. Tomasz Sowinski -- http://www.shooltz.com
An excellent suggestion - I will try that trick tomorrow and let you know. Thanks for the help.