Overview
In the ever-evolving landscape of C# development, each new version introduces features that make coding more efficient and expressive. With C# 10, we have "Records," which not only simplify the process of defining and using classes, but also simplify serializing and deserializing objects. Using C# 10 Records, we can streamline the process of data interchange through serialization in this article.
Record types in C# 10
This new reference type in C# combines immutability with simplicity. As they come with built-in implementations of common methods such as Equals, GetHashCode, and ToString, records are particularly useful for modelling data. As a first step, let's define a simple record. This record has two properties: FirstName, LastName and Email. These properties can only be set when the record is initialized.
namespace ZiggyRafiq.CodeExample.Models;
public record Person
{
public string FirstName { get; init; } = string.Empty;
public string LastName { get; init; } = string.Empty;
public string Email { get; init; } = string. Empty;
}
Using C# records for serialization
A serialization is the process of converting an object into a format that can be stored, transmitted, or reconstructed easily.
Serialization of JSON
Using the System.Text.Json namespace, we can easily serialize a Person record into JSON format.
using System.Text.Json;
using ZiggyRafiq.CodeExample.Models;
Console.WriteLine("Hello, from Ziggy Rafiq!");
/**********************************************************************************************
* Code Example One
**********************************************************************************************/
// Create a Person record instance
var person = new Person { FirstName = "John", LastName = "Smith", Email ="[email protected]" };
// Serialize the record to JSON
string json = JsonSerializer.Serialize(person);
// Output the serialized JSON
Console.WriteLine(json);
The code example above uses the JsonSerializer.Serialize method to serialize the Person record into a JSON string. The resulting JSON will look like this:
{"FirstName":"John","LastName":"Smith","Email":"[email protected]"}
Deserialization of JSON
The process of deserialization, or reconstructing an object from its serialized form, is also simplified by C# 10 Records:
/**********************************************************************************************
* Code Example Two
**********************************************************************************************/
// Deserialize the JSON string into a Person record
Person? personObject = JsonSerializer.Deserialize<Person>(json);
// Output the deserialized Person
Console.WriteLine(value: $"First Name: {personObject.FirstName}, Last Name: {personObject.LastName}, Email: {personObject.Email}");
A Person record is reconstructed from the JSON string in the code example above by using the JsonSerializer.DeserializePerson> method.
First Name: John, Last Name: Smith, Email: [email protected]
Summary
Serialization and deserialization of objects are simplified and efficient with C# 10 Records. Working with records is a delight because of the built-in methods for equality, hashing, and string representation, as well as the seamless integration with System.Text.Json. You can handle serialization tasks in C# 10 Records in web applications or in databases with a clean and concise interface. Explore the power of C# 10 Records in your projects and embrace the elegance they bring to data modelling and manipulation.
Please do not forget to like this article if you have found it useful and follow me on my LinkedIn https://www.linkedin.com/in/ziggyrafiq/ also I have uploaded the source code for this article on my GitHub Repo https://github.com/ziggyrafiq/SimplifiedSerializationCSharp10