What are the uses of data serialization

Serialization (C #)

  • 2 minutes to read

Serialization is the process of converting an object to a stream of bytes in order to store the object or transfer it to memory, a database, or a file. The main purpose is to save the state of an object in order to be able to recreate it if necessary. The reverse process is known as deserialization.

How serialization works

This figure shows the entire serialization process:

The object is serialized in a stream that contains the data. The stream can also contain information about the object type, such as: B. Version, culture and the assembly name. From this stream, the object can be stored in a database, a file or a working memory.

Uses of serialization

With serialization, the developer can save the state of an object and recreate the object if necessary. Serialization provides both object storage and data exchange. Using serialization, a developer can do the following:

  • Send the object to a remote application using a web service
  • Passing an object from one domain to another
  • Passing an object through a firewall as a JSON or XML string
  • Manage security and user-specific information about applications

JSON serialization

The System.Text.Json namespace contains classes for JavaScript Object Notation (JSON) serialization and deserialization. JSON is an open standard that is widely used for sharing data over the internet.

JSON serialization serializes an object's public properties into a string, byte array, or stream that conforms to the RFC 8259 JSON specification. To control how JsonSerializer serializes or deserializes an instance of the class:

Binary and XML serialization

The System.Runtime.Serialization namespace contains classes for binary serialization and deserialization, and XML serialization and deserialization.

Binary serialization uses binary encoding for storage or socket-based network streams to generate a compact serialization. Binary serialization serializes all members, including read-only members, and improves performance.

With XML serialization, the public fields and properties of an object or the parameters and return values ​​of methods are serialized into an XML stream that corresponds to a specific XSD document (XML Schema Definition). XML serialization results in strongly typed classes with public properties and fields that are converted to XML. System.Xml.Serialization contains classes for serializing and deserializing in XML. You apply attributes to classes and class members to control how the XmlSerializer serializes or deserializes an instance of the class.

Enable serializability of an object

For binary or XML serialization, you will need:

Apply the SerializableAttribute attribute to a type to indicate that instances of the type can be serialized. An exception is thrown if you try to serialize but the type does not have the SerializableAttribute attribute.

Apply the NonSerializedAttribute attribute to prevent a field from being serialized. If a field of a serializable type contains a pointer, handle, or other data structure that is specific to a particular environment, and the field cannot reasonably be restored in any other environment, you should mark the type as non-serializable.

If a serialized class contains references to objects in other classes marked with SerializableAttribute, those objects are also serialized.

Simple and custom serialization

Binary and XML serialization can be done in two ways: simple and custom.

Simple serialization uses .NET to automatically serialize an object. The only requirement is that the class has the SerializableAttribute attribute. The NonSerializedAttribute can be used to prevent serialization of certain fields.

When you use simple serialization, versioning objects can be a problem. You should use custom serialization when versioning is important. Simple serialization is the easiest way to serialize, but it doesn't offer much control over the process.

With custom serialization, you can specify exactly which objects are serialized and how the serialization occurs. The class must be marked as SerializableAttribute and implemented in the ISerializable interface. If you also want to custom deserialize the object, use a custom constructor.

Designer serialization

Designer serialization is a special form of serialization that also takes into account the type of object persistence associated with development tools. Designer serialization is the process of converting an object diagram to a source file that can later be used to restore the object diagram. A source file can contain code, markup, or even SQL table information.

Related topics and examples

The System.Text.Json overview shows how to get the library.

Serializing and Deserializing JSON Data in .NET: Demonstrates how to read and write object data to and from JSON using the JsonSerializer class.

Walkthrough: Keeping an Object in Visual Studio
Demonstrates how serialization can be used to persist an object's data between instances. This allows you to save and retrieve values ​​the next time the object is instantiated.

Reading object data from an XML file (C #)
Shows how to read object data previously written to an XML file using the XmlSerializer class.

Writing object data to an XML file (C #)
Shows how to write an object from a class to an XML file using the XmlSerializer class.