Preface
In C# programming, serialization and deserialization are very important concepts.
Serialization is the process of converting an object into a format that can be stored or transferred, while deserialization is the process of converting a stored or transferred format back to an object.
These two processes are very useful in many application scenarios, such as saving object state, transmitting objects on the network, sharing data between different applications, etc.
1. The concept and purpose of serialization
1. Concept
Serialization is the process of converting the state of an object into a format that can be stored or transferred.
In C#, serialization can be performed in a variety of ways, such as binary serialization, XML serialization, or JSON serialization.
2. Purpose
- Data storage:Save the state of the object to a file, database, or other storage medium so that the state of the object can be reloaded and restored if needed later.
- Network transmission:Send the state of an object over the network to other computers or devices so that the object can be recreated and restored to its state at the receiving end.
- Data Sharing:Share the state of the object between different applications so that the same data can be used in different environments.
2. The concept and purpose of deserialization
1. Concept
Deserialization is the process of converting stored or transferred formats back to objects.
In C#, deserialization can be performed in the same way as serialization, such as binary serialization, XML serialization, or JSON serialization, etc.
2. Purpose
- Data recovery:Read the serialized data from the storage medium and convert it back to the state of the object so that the object can continue to be used.
- Network Receive:Receive serialized data sent over the network and convert it back to the state of the object so that the object can be processed locally.
- Data Sharing:Receive shared serialized data from other applications and convert it back to the state of the object so that the same data can be used locally.
3. Serialization method in C#
1. Binary serialization
How to use: Use the BinaryFormatter class for binary serialization.
First, you need to introduce a namespace into your code.
Then, you can create a BinaryFormatter object and serialize the object into a byte stream using the Serialize method and deserialize the byte stream into an object using the Deserialize method.
using ; using ; class Program { static void Main() { // Create an object to be serialized MyClass obj = new MyClass { Name = "John", Age = 30 }; // Serialize objects into byte streams using binary serialization using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); (stream, obj); // Convert a byte stream to an array of bytes byte[] data = (); // Deserialize the byte array into an object using binary deserialization using (MemoryStream stream2 = new MemoryStream(data)) { BinaryFormatter formatter2 = new BinaryFormatter(); MyClass deserializedObj = (MyClass)(stream2); (); (); } } } } [Serializable] class MyClass { public string Name { get; set; } public int Age { get; set; } }
Features:
- Binary serialization is an efficient way of serializing objects quickly into byte streams and quickly deserialize byte streams into objects when needed.
- The byte stream generated by binary serialization is compact and takes up less memory space.
- Binary serialization is a private serialization method that can only be deserialized using the same assembly and type definitions.
2. XML Serialization
How to use: Use the XmlSerializer class for XML serialization.
First, you need to introduce a namespace into your code.
You can then create an XmlSerializer object and serialize the object into an XML document using the Serialize method and deserialize the XML document into an object using the Deserialize method.
using ; using ; class Program { static void Main() { // Create an object to be serialized MyClass obj = new MyClass { Name = "John", Age = 30 }; // Use XML serialization to serialize objects into XML documents using (StringWriter writer = new StringWriter()) { XmlSerializer serializer = new XmlSerializer(typeof(MyClass)); (writer, obj); string xml = (); // Deserialize XML documents to objects using XML deserialization using (StringReader reader = new StringReader(xml)) { XmlSerializer serializer2 = new XmlSerializer(typeof(MyClass)); MyClass deserializedObj = (MyClass)(reader); (); (); } } } } [Serializable] [XmlRoot("MyClass")] class MyClass { public string Name { get; set; } public int Age { get; set; } }
Features:
- XML serialization generates XML documents in a human-readable format that can be easily viewed and edited.
- XML serialization is a publicly available serialization method that allows data exchange between different platforms and programming languages.
- XML serialization generates XML documents usually take up more storage space than binary serialization generates byte streams.
3. JSON serialization
How to use: Use the JsonSerializer class for JSON serialization.
First, you need to introduce a namespace into your code.
Then, you can create a JsonSerializerOptions object and set serialization options such as indentation format, property name case, etc.
Finally, you can use methods to serialize the object into a JSON string and methods to deserialize the JSON string into an object.
using ; class Program { static void Main() { // Create an object to be serialized MyClass obj = new MyClass { Name = "John", Age = 30 }; // Use JSON serialization to serialize objects to JSON strings string json = (obj); // Deserialize JSON strings into objects using JSON deserialization MyClass deserializedObj = <MyClass>(json); (); (); } } class MyClass { public string Name { get; set; } public int Age { get; set; } }
Features:
- JSON serialization generates JSON strings is a lightweight data exchange format that takes up less storage space and is easy to exchange data between different platforms and programming languages.
- JSON serialization is a human-readable format that can be easily viewed and edited.
- JSON serialization can be extended using third-party libraries, for example, to provide more features and options.
4. Summary
Serialization and deserialization are very important concepts in C# programming, and can easily perform operations such as data storage, network transmission and data sharing.
In C#, binary serialization, XML serialization, and JSON serialization can be used to serialize and deserialize.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.