A serialization primer - Part 1
来源:互联网 发布:网络推广多少钱一年 编辑:程序博客网 时间:2024/05/22 03:12
This article is the first of a 3 part tutorial on serialization.
- Part 1 introduces the basics of serialization.
- Part 2 explains how to gracefully handle reading invalid data stores and support versioning.
- Part 3 describes how to serialize complex objects.
Serialization is the process of writing or reading an object to or from a persistent storage medium, such as a disk file. Serializing an object requires 3 ingredients:
- A CFile object representing the datafile
- A CArchive object that provides the serialization context
- The object being serialized
To serialize an object to the file "foo.dat", open the file with the appropriate access mode. In this example, the file is opened for exclusive read/write access.
Next, a CArchive object is hooked up to the file. The archive provides an efficient conduit to persistent storage. Instead of directly reading and writing the file, you serialize data to and from the archive. The archive needs to know if you're going to be using it to read or write data. In this example, we'll assume we're writing data.
Finally, we serialize the object by calling its serialize() method. serialize() is just a method we made up. It has nothing to with MFC's CObject::Serialize(). Also, you don't have to derive your object from CObject. Our serialize() method takes a pointer to a CArchive and returns an integer status.
We'll get to the actual serialization process in a minute. Meanwhile, let's recognize a couple of important points:
- The same method CFoo::serialize() is used to read/write the object from/to persistent storage.
- CFoo::serialize() doesn't know anything about the datafile bring accessed.
Assume CFoo represents an employee record that contains a couple of data members.
We use CArchive's streaming operators << and >> to read/write the data members from/to the archive. CArchive knows how to serialize simple data types like int, float, DWORD, and objects like CString. The archive also knows whether it's in read or write mode. You can query its mode by calling CArchive::IsStoring(). CFoo's serialization method can then be written as:
When you've finished serializing, you should clean up by closing the archive and datafile.
Well, there you have it - serialization in a (very small) nutshell. In Part 2, we'll see how to gracefully handle reading invalid data stores and support different versions of our object. In Part 3, we'll see how to serialize complex objects.
LicenseThis article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
Ravi Bhavnani
Member Ravi Bhavnani is an ardent fan of Microsoft technologies who loves building Windows apps, especially PIMs, system utilities, and things that go bump on the Internet. During his career, Ravi has developed expert systems, desktop imaging apps, enterprise marketing automation software, EDA tools, a platform to help people find, analyze and understand information, and trading software for institutional investors. He currently works for a company that provides advanced data visualization solutions for Microsoft technologies.
His interests include the .NET framework, reasoning systems, financial analysis and algorithmic trading, NLP, CHI and UI design. Ravi holds a BS in Physics and Math and an MS in Computer Science and is a Microsoft C# MVP. His claim to fame is that he crafted CodeProject's "joke" forum post icon.
Ravi's biggest fear is that one day he might actually get a life, although the chances of that happening seem extremely remote.
Occupation: Software Developer (Senior) Company: Dundas Software Location: Canada
- A serialization primer - Part 1
- A serialization primer - Part 2
- 《基于MFC的OpenGL编程》Part 1 A Primer
- 《基于MFC的OpenGL编程》Part 1 A Primer
- The serialization primer(序列化入门)
- program is a serialization procedure
- Serialization:Making a serializable Class
- Windows Debuggers: Part 1: A WinDbg Tutorial
- MFC Tutorial Part 1 - Creating a Window
- Building a Master Image - Part 1: VMware
- A Look at Commons Chain, Part 1
- LESSON 1 BEING A HACKER part II
- LESSON 1 BEING A HACKER part III
- LESSON 1 BEING A HACKER part IV
- Windows Debuggers: Part 1: A WinDbg Tutorial
- A beginner introduction to TensorFlow (Part-1)
- Learning boost 1 Serialization
- Learning boost 1 Serialization
- 标准库的异常类
- 亮剑PHP:我的未来不是梦(21)
- 和自己赛跑 (9月15日)
- A Prelude to Pointers
- Synchronization in Multithreaded Applications with MFC
- A serialization primer - Part 1
- A serialization primer - Part 2
- Think before you code, Virtual Functions in C++
- MFC under the hood
- Why I Chose C++
- VC中使用ADO操作数据库的方法
- C++中几个比较不常用的关键字
- Boost库的编译
- 一本看到技巧又能保持阳光心态励志书(来自苏鹏的推荐)