serial ppt
TRANSCRIPT
-
8/2/2019 Serial Ppt
1/27
Serialization
Submitted To:Submitted By:
Mrs. Monika Sharma Shankul Bahiya
09IT044
-
8/2/2019 Serial Ppt
2/27
Serialization/Deserialization
Object in memory
Object in memory
binary or character stream
-
8/2/2019 Serial Ppt
3/27
SerializationWhat is Serialization
Serialization is the process of converting an
object, or a connected graph of objects, stored
within computer memory, into a linear sequenceof bytes.
Use the sequence of bytes in several ways: Send it to another process
Send it to the clipboard, to be browsed or used byanother application
Send it to another machine
Send it to a file on disk
-
8/2/2019 Serial Ppt
4/27
-
8/2/2019 Serial Ppt
5/27
SerializationObject 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
CatCat Mouse
Duck
Dog
2
1
3
4
9
7
Horse
-
8/2/2019 Serial Ppt
6/27
-
8/2/2019 Serial Ppt
7/27
-
8/2/2019 Serial Ppt
8/27
SerializaitonFileStream 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()
-
8/2/2019 Serial Ppt
9/27
-
8/2/2019 Serial Ppt
10/27
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 theserialization 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 human-
readable format.
-
8/2/2019 Serial Ppt
11/27
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:
-
8/2/2019 Serial Ppt
12/27
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:
-
8/2/2019 Serial Ppt
13/27
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 Projectmenu 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 aStreamingContext object that specifies where the
serialization data is stored:
-
8/2/2019 Serial Ppt
14/27
-
8/2/2019 Serial Ppt
15/27
-
8/2/2019 Serial Ppt
16/27
To make a user-defined class Serializable we have to use
attribute for a class as:
-
8/2/2019 Serial Ppt
17/27
Serialization in VC ++ ..
-
8/2/2019 Serial Ppt
18/27
-
8/2/2019 Serial Ppt
19/27
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. ACFile 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.
-
8/2/2019 Serial Ppt
20/27
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 andCArchive 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.
-
8/2/2019 Serial Ppt
21/27
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 theIMPLEMENT_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.
-
8/2/2019 Serial Ppt
22/27
Writing a Serialize Function
Now, your job is to write a Serialize() member function for
CStudent. Because Serialize() is a virtual member functionof 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.
-
8/2/2019 Serial Ppt
23/27
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);
-
8/2/2019 Serial Ppt
24/27
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 archiveobject. 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 () for many ofthe C++ built-in types, as shown in the following table.
-
8/2/2019 Serial Ppt
25/27
Type Description
BYTE 8 bits, unsigned
WORD 16 bits, unsigned
LONG 32 bits, signed
DWORD 32 bits, unsigned
float 32 bits
double 64 bits, IEEE standard
int 32 bits, signed
short 16 bits, signed
char 8 bits, unsigned
unsigned 32 bits, unsigned
-
8/2/2019 Serial Ppt
26/27
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 anenumerated type. Here's the code you would use:
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.
-
8/2/2019 Serial Ppt
27/27