Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.
Through serialization, a developer can perform actions such as:
- sending the object to a remote application by using a web service;
- passing an object from one domain to another;
- passing an object through a firewall as a JSON or XML string;
- maintaining security or user-specific information across applications;
, see the official documentation.
This example uses as a basis a C # source code created in a previous tutorial and aims to use the serialization and deserialization process for the binary serialization part.
This is the simple source code in C# commented in order to see how this works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | // the base example from geometry001 using System; // this is used to save file using System.IO; // this is used for Serialization using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; namespace geometry002 { public class Program { public static void Main(string[] args) { Point my_point001 = new Point(0,0); Point my_point002 = new Point (0,100); Line my_line = new Line(my_point001,my_point002); my_line.PrintLineOut(); // create a name for the file const string FileName = @"test_serialization.bin"; // if the file exist extract data if (File.Exists(FileName)) { Console.WriteLine("Reading from test_serialization.bin"); Stream openFileStream = File.OpenRead(FileName); BinaryFormatter deserializer = new BinaryFormatter(); Console.WriteLine("... get data"); Point my_point003 = (Point)deserializer.Deserialize(openFileStream); Console.WriteLine("The point is deserializer"); // close the file openFileStream.Close(); } else { // create the file and fill with data Stream saveFileStream = File.Create(FileName); BinaryFormatter serializer = new BinaryFormatter(); serializer.Serialize(saveFileStream, my_point001); // close the file saveFileStream.Close(); } } } //In order to persist the values for the Point class, you must first mark the class with the Serializable attribute like this [Serializable()]. [Serializable()] public class Point { public Point () { x=y=0; } public Point (int x,int y) { this.x = x; this.y = y; } internal int x,y; } // this class is not Serializable public class Line { public Line(Point my_point001, Point my_point002) { line_point001=my_point001; line_point002=my_point002; } internal void PrintLineOut() { Console.WriteLine("Line start from (x,y): ({0},{1})",line_point001.x,line_point001.y); Console.WriteLine("Line end in (x,y): ({0},{1})",line_point002.x,line_point002.y); } internal Point line_point001,line_point002; } } |