07 Feb 2024
Intermediate
Examples of serialization and deserialization using different formats in .NET Core:
Here are examples of serialization and deserialization using different formats in .NET Core:
Binary Serialization:
Binary serialization involves converting objects into a binary format that can be written to disk or transmitted over a network.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
// Define a class to be serialized
[Serializable]
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
class Program
{
static void Main(string[] args)
{
// Create an instance of the Person class
Person person = new Person { Name = "John", Age = 30 };
// Binary serialization
IFormatter formatter = new BinaryFormatter();
using (Stream stream = new FileStream("person.bin", FileMode.Create, FileAccess.Write))
{
formatter.Serialize(stream, person);
}
// Binary deserialization
using (Stream stream = new FileStream("person.bin", FileMode.Open, FileAccess.Read))
{
Person deserializedPerson = (Person)formatter.Deserialize(stream);
Console.WriteLine($"Deserialized Person: Name={deserializedPerson.Name}, Age={deserializedPerson.Age}");
}
}
}
- Serialization: In the example, we define a
Person
class that is marked with the[Serializable]
attribute, indicating that instances of this class can be serialized. We use theBinaryFormatter
class to serialize thePerson
object into a binary stream and write it to a file using aFileStream
. - Deserialization: To deserialize the binary data back into a
Person
object, we use the sameBinaryFormatter
class to read the binary stream from the file and reconstruct the object.
JSON Serialization (using Json.NET):
JSON serialization involves converting objects into a JSON format that is commonly used for transmitting data over the web.
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
// Create an instance of the Person class
Person person = new Person { Name = "Alice", Age = 25 };
// JSON serialization
string json = JsonConvert.SerializeObject(person);
Console.WriteLine($"Serialized JSON: {json}");
// JSON deserialization
Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json);
Console.WriteLine($"Deserialized Person: Name={deserializedPerson.Name}, Age={deserializedPerson.Age}");
}
}
- Serialization: We use the
JsonConvert.SerializeObject()
method from the Json.NET library to serialize thePerson
object into a JSON string. - Deserialization: We use the
JsonConvert.DeserializeObject()
method to deserialize the JSON string back into aPerson
object.
XML Serialization:
XML serialization involves converting objects into an XML format, which is commonly used for interoperability between different systems.
using System.Xml.Serialization;
using System.IO;
class Program
{
static void Main(string[] args)
{
// Create an instance of the Person class
Person person = new Person { Name = "Bob", Age = 35 };
// XML serialization
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (TextWriter writer = new StreamWriter("person.xml"))
{
serializer.Serialize(writer, person);
}
// XML deserialization
using (TextReader reader = new StreamReader("person.xml"))
{
Person deserializedPerson = (Person)serializer.Deserialize(reader);
Console.WriteLine($"Deserialized Person: Name={deserializedPerson.Name}, Age={deserializedPerson.Age}");
}
}
}
- Serialization: We use the
XmlSerializer
class to serialize thePerson
object into an XML format and write it to a file using aStreamWriter
. - Deserialization: We use the same
XmlSerializer
class to deserialize the XML data from the file back into aPerson
object.