Sie sind auf Seite 1von 27

Serialization

Submitted To: Submitted By:


Mrs. Monika Sharma Shankul Bahiya 09IT044

Serialization/Deserialization
Object in memory

binary or character stream

Object in memory

What is Serialization
Serialization is the process of converting an object, or a connected graph of objects, stored within computer memory, into a linear sequence of bytes. Use the sequence of bytes in several ways:

Serialization

Send it to another process Send it to the clipboard, to be browsed or used by another application Send it to another machine Send it to a file on disk

Serialization
Object Graph
What is an object graph?

An object graph is a set of objects with some set of references to each other The most obvious problem is how to represent the links between the objects in the Serialized stream
3 Dog

Cat

Cat

Mouse

Horse

Duck

How Serialization Works


Because run-time metadata 'knows' about each object's layout in memory, and its field and property definitions, you can serialize objects automatically, without having to write code to serialize each field The serialized stream might be encoded using XML, or a compact binary representation The format is decided by the the Formatter object that you call:

Serialization

Binary SOAP Custom

Serialization in Visual basic..

FileStream Example in VB
Dim fs As FileStream = New FileStream("obj.dat", FileMode.Create) Dim bf As New BinaryFormatter() ' Serialize the class to the file stream, and flush the stream. bf.Serialize(fs, New SerClass()) fs.Close()

Serializaiton

Deserialize Example in VB
Dim fs As FileStream = New FileStream("obj.dat", FileMode.Open) Dim bf As New BinaryFormatter() ' Deserialize the contents of the file stream into an Integer array. ' Deserialize returns an object that must be coerced. Dim o As SerClass = CType(bf.Deserialize(fs), SerClass) MsgBox(o.x & Space(5) & o.y)

Serializaiton

Serialization
Formatter in VB
Binary Formatter :
The Binary Formatter object, defined in System.Runtime.Serialization.Formatters. Binary namespace, provides an efficient way to persist an object in a compact binary format. In practice, the actual bits in memory are persisted, so the serialization and deserialization processes are very fast.

Soap Formatter :
The Soap Formatter object, defined in System.Runtime.Serialization.Formatters. SOAP namespace, persists data in human-readable XML format, following the Simple Object Access Protocol (SOAP) specifications. The serialization and deserialization processes are somewhat slower than with the BinaryFormatter object. On the other hand, data can be sent easily to another application through HTTP and displayed in a humanreadable format.

Binary Serialization
The key methods that all formatter objects support are Serialize and Deserialize, whose purpose is rather evident. The Serialize method takes a Stream object as its first argument and the object to be serialized as its second argument:

Cont.
Reading back the file data and deserializing it into an object requires the Deserialize function, which takes the input Stream as its only argument and returns an Object value, which must be cast to a properly typed variable:

Soap Serialization
You can change the serialization format to SOAP by simply using another formatter object, the SoapFormatter. This namespace isn't available in the default Visual Basic console project, so you have to click Add Reference on the Project menu in Visual Studio to add the System.Runtime.Serialization.Formatters.Soap.dll library to the list of libraries that appears in the Object Browser. Note that the SoapFormatter's constructor is passed a StreamingContext object that specifies where the serialization data is stored:

To make a user-defined class Serializable we have to use <Serializable()> attribute for a class as:

Serialization in VC ++ ..

Disk Files & Archieves


How do you know whether Serialize() should read or write data? How is Serialize() connected to a disk file? With the MFC library, objects of class CFile represent disk files. A CFile object encapsulates the binary file handle that you get through the Win32 function CreateFile(). This is not the buffered FILE pointer that you'd get with a call to the C runtime fopen() function; rather, it's a handle to a binary file. The application framework uses this file handle for Win32 ReadFile(), WriteFile(), and SetFilePointer() calls.

cont..
If your application does no direct disk I/O but instead relies on the serialization process, you can avoid direct use of CFile objects. Between the Serialize() function and the CFile object is an archive object of class CArchive, as shown in Figure 1. The CArchive object buffers data for the CFile object, and it maintains an internal flag that indicates whether the archive is storing (writing to disk) or loading (reading from disk). Only one active archive is associated with a file at any one time. The application framework takes care of constructing the CFile and CArchive objects, opening the disk file for the CFile object and associating the archive object with the file. All you have to do (in your Serialize() function) is load data from or store data in the archive object. The application framework calls the document's Serialize() function during the File Open and File Save processes.

A serializable class must be derived directly or indirectly from CObject. In addition (with some exceptions), the class declaration must contain the DECLARE_SERIAL macro call, and the class implementation file must contain the IMPLEMENT_SERIAL macro call. See the Microsoft Foundation Class Reference for a description of these macros. This module's CStudent class example is modified from the class in Module 10 to include these macros.

Writing a Serialize Function

Now, your job is to write a Serialize() member function for CStudent. Because Serialize() is a virtual member function of class CObject, you must be sure that the return value and parameter types match the CObject declaration. The Serialize() function for the CStudent class is below.

Most serialization functions call the Serialize() functions of their base classes. If CStudent were derived from CPerson, for example, the first line of the Serialize() function would be: CPerson::Serialize(ar);

The Serialize() function for CObject (and for CDocument, which doesn't override it) doesn't do anything useful, so there's no need to call it. Notice that ar is a CArchive reference parameter that identifies the application's archive object. The CArchive::IsStoring member function tells us whether the archive is currently being used for storing or loading. The CArchive class has overloaded insertion operators (<<) and extraction operators (>>) for many of the C++ built-in types, as shown in the following table.

Type

Description

BYTE WORD LONG DWORD float double

8 bits, unsigned 16 bits, unsigned 32 bits, signed 32 bits, unsigned 32 bits 64 bits, IEEE standard

int short char unsigned

32 bits, signed 16 bits, signed 8 bits, unsigned 32 bits, unsigned

The insertion operators are overloaded for values; the extraction operators are overloaded for references. Sometimes you must use a cast to satisfy the compiler. Suppose you have a data member m_nType that is an enumerated type. Here's the code you would use: ar << (int) m_nType; ar >> (int&) m_nType; MFC classes that are not derived from CObject, such as CString and CRect, have their own overloaded insertion and extraction operators for CArchive.

Thank you

Das könnte Ihnen auch gefallen