Skip to main content

Posts

Showing posts with the label Serialization

Using Apache Avro for Efficient Data Serialization

Introduction In the world of software development, efficient data serialization plays a crucial role in enabling smooth communication between different components, systems, and applications. One popular technology that addresses the challenges of data serialization is Apache Avro. Avro is a powerful and versatile framework that offers a compact and efficient way to serialize data, making it an ideal choice for various scenarios. In this article, we will explore the need for efficient data serialization, the problems it helps us solve, and how to use Apache Avro with Java, using a "Product" entity as an example. The Need for Efficient Data Serialization Modern software systems are often composed of multiple components running on different platforms and communicating over various protocols. This communication involves sending data between these components, which can be in different formats and structures. Data serialization is the process of converting complex data str

Custom serialization Using Newtonsoft.Json

When using Newtonsoft. Json to serialize objects, we can avoid few properties from being included in the output for some use case and the same property be included in the serialized output in other cases. There's a feature in the Newtonsoft.Json.NET library, that lets us  determine at runtime whether or not to serialize a particular property of a class / object. During the process of creating a class, we have to include a public method named ShouldSerialize{MemberName} returning a boolean value. Json.NET will call that method during serialization to determine whether or not to serialize the corresponding property of the class. If this method returns true, the property will be serialized; otherwise, it will be ignored. The following illustration shows how this can be achieved. The following is a data definition / class that may be serialized using Newtonsoft.Json.Net public class AuthTypeClaimMapViewModel {     [JsonIgnore]     public bool? _canSerializeName { get;

C# Serialization

To use the XML Serializer in C#.NET, use as follows public static class SerializationHelper { /// /// Serializes the object and returns the serialized object's xml string /// /// The obj. /// public static string SerializeObject(Object obj) { var xmlSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType()); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); xmlSerializer.Serialize(memoryStream, obj); memoryStream.Seek(0, System.IO.SeekOrigin.Begin); return new System.IO.StreamReader(memoryStream).ReadToEnd(); } /// /// Desserializeobjs the specified object XML string. /// /// /// The object XML string. /// public static object DeSerializeObject (string objectXmlString) { var xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T)); using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { using (System.IO.StreamWriter swr = new System.IO.StreamWriter(memoryStream))