Last Updated:

Serialization Objects in .net

Although the wireframe knows how to store an object marked with the Serializable  attribute, it is still necessary to determine the format in which the object will be stored and the storage medium. To determine the format in which the object will be saved, you must use an instance of an object that supports the IFormatter interface.

The framework has two such classes: System::Runtime::Serialization:Formatters:: Binary::BinaryFormatter(System:Binary::Formaters::Binary::Vtagugtig) and System::Runtime:Serialization::Formatters::Soap::Soar Formatters:Soap::Soar Formatter.

BinaryFormatter uses a binary, compact format for serialization and conversion from serial to parallel form on platforms that support the common language runtime (CLR). SoapForrnatter uses the soap industry standard, which is discussed in Chapter 11, Web Services.

Because it is XML-based, and therefore a text-based protocol, it can be used to communicate with a platform that is not based on the common language runtime (CLR). The binary format is faster when serializing and converting data from a sequential form to a parallel one.

Serialization Objects in .net

You can, of course, create your own formatting classes. This can only be done if you want to consider its own byte file object format when interacting with an external system.

The Serialization sample contains code that demonstrates how to use FileStream to save and restore both binary and SOAP formats. Of course, you can use any class that derives from Stream (Stream, Abstract Sequential File), as long as it represents some storage medium. Special steps must be taken to ensure that the Load method can change the parameter that points to HotelBroker. To do this, the parameter is declared as a reference to a pointer pointing to HotelBroker.

static void Save(// static method Save
HotelBroker *broker, String *formatter)
{
FileStream *s;
if (String::Equals(formatter, "b"))
// if (String::Equals to (formatter, "b"))
{
s = new FileStream(
"hotels.bin", FileMode::Create); // Create
BinaryFormatter *b = new BinaryFormatter;
b › Serialize(s, broker);
// Serialize (s, broker);
}
else
{
s = new FileStream(
"hotels.txt", FileMode::Create); // Create
SoapFormatter *sf = new SoapFormatter;
sf Serialize(s, broker);
// Convert to serial form (s, broker);
}
s Close();
}
static void Load(// static method Load
HotelBroker *&broker, /* pointer reference */
String *formatter) // String
{
FileStream *s;
if (String::Equals(formatter, "b"))
// if (String::Equals to (formatter, "b"))
{
s = new FileStream("hotels.bin", FileMode::Open); // Open
BinaryFormatter *b = new BinaryFormatter; broker = // broker
dynamic_cast<HotelBroker *>
(b › Deserialize(s));
}
else
{
s = new FileStream("hotels.txt", FileMode::Open);
// Open
SoapFormatter *sf = new SoapFormatter;
broker = // broker
dynamic_cast<HotelBroker *>(sf › Deserialize(s));
}
s Close();
ShowHotelList(broker › GetHotels());
}

The following is some typical output of the Serialization example: first, we add the name of the hotel and store it using the SOAP formatter.

Then we exit the program.

Enter command: cities
Atlanta
Boston
Commands: quit, cities, list, add, fetch, save
Enter command: list
City Name Rooms Rate
Atlanta Dixie 100 115
Atlanta Marriott 500 70
Boston Sheraton 250 95
Commands: quit, cities, list, add, fetch, save
Enter command: add
Hotel City: Philadelphia
Hotel Name: Franklin
Number Rooms: 100
Room Rate: 200
Commands: quit, cities, list, add, fetch, save
Enter command: save
Formatter: b(inary), s(oap)s
Commands: quit, cities, list, add, fetch, save
Enter command: cities
Atlanta
Boston
Philadelphia
Commands: quit, cities, list, add, fetch, save
Enter command: list
City Name Rooms Rate
Atlanta Dixie 100 115
Atlanta Harriot 500 70
Boston Sheraton 250 95
Philadelphia Franklin 100 200
Commands: quit, cities, list, add, fetch, save
Enter command: quit

Then we run the program again and restore the data saved at the first startup.

Enter command: cities
Atlanta
Boston
Commands: quit, cities, list, add, fetch, save
Enter command: list
City Name Rooms Rate
Atlanta Dixie 100 115
Atlanta Marriot 500 70
Boston Sheraton 250 95
Commands: quit, cities, list, add, fetch, save
Enter command: fetch Formatter: b(inary), s(oap)s
City Name Rooms Rate
Atlanta Dixie 100 115
Atlanta Marriot 500 70
Boston Sheraton 250 95
Philadelphia Franklin 100 200
Commands: quit, cities, list, add, fetch, save
Enter command: cities
Atlanta
Boston
Philadelphia