Serialization Format
NCache uses serialization to convert objects into a transmittable format for network communication and storage. By choosing between JSON and Binary serialization, you can optimize your cache for high-speed local processing, cross-platform portability, or memory efficiency, depending on your application's architectural requirements.
Cache data when transmitted over the network needs to be serialized. After the serialized data is transmitted or retrieved, the deserialization process reads the data and reconstructs the original objects or data structures. This whole process of serialization/deserialization involves factors like time, memory, and performance. These factors are to be kept in mind when choosing a format for serialization.
There are two types of serialization formats provided by NCache:
Important
Binary Serialization is not configurable from NCache 5.3.7 onwards. Additionally, if you are using ASP.NET 5.0 and above, it is recommended that you use JSON serialization . For more details, click here.
JSON Serialization
JSON is the text-based syntax for storing and exchanging data. It is easier for machines to parse and for users to understand. Using JSON serialization, the objects are converted into their JSON equivalent when serialized and then converted back into their custom objects at the time of deserialization.
Consider a .NET Product class containing the data of products. It contains various attributes, e.g., ProductName, ProductID, and ProductExpiry, etc.
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
This data will be serialized to the following JSON string:
{
"Name": "Apple",
"Expiry": "2008-12-28T00:00:00",
"Sizes":
[
"Small"
]
}
The serializer used is an extension of the Newtonsoft third-party serializer. It can be a preferable approach for compact and convenient serialization of data. Given below is an example of a custom Account class where the Password property is marked to be excluded from serialization:
public class Account
{
public string FullName { get; set; }
public string EmailAddress { get; set; }
[JsonIgnore]
public string Password { get; set; }
}
Now, create an Account object with values for all properties:
Account account = new Account
{
FullName = "Joe User",
EmailAddress = "joe@example.com",
Password = "VHdlZXQgJ1F1aWNrc2lsdmVyJyB0byBASmFtZXNOSw=="
};
Since the Password property is marked with [JsonIgnore], it will not be included in the serialized JSON output:
{
"FullName": "Joe User",
"EmailAddress": "joe@example.com"
}
Note
If JSON serialization does not properly handle a specific custom type, you can configure an external Newtonsoft JsonConverter for that type. For more details, refer to External Newtonsoft JsonConverter Support.
Binary Serialization
In the Binary Format, user objects, such as Product objects, are serialized on the client side before they are sent to the cache. NCache stores the item on the server in its serialized binary form. When the item is fetched, the server returns the stored binary data to the client, where it is deserialized back into the original object type.
Binary Format is useful when the application mostly stores and retrieves complete objects on the client side. Like JSON, it keeps data in a serialized form on the server for normal add and fetch operations. However, unlike JSON, Binary Format uses a binary representation, which is generally more compact than text-based JSON and is better suited when the data does not need to be human-readable or processed as JSON.
The Binary Format is beneficial when most processing occurs on the client side, especially for operations like adding, updating, fetching, and removing data from the cache. For example, if a serialized Product object is fetched from the cache to display its contents, the server returns the stored binary data and the client deserializes it locally.
Similarly, when a Product object is added to the cache using Binary Format, it is serialized before being sent over the network and is stored in the cache in the same serialized form. This makes Binary Format suitable for applications that mainly work with complete objects and do not require the cache data to be stored in a readable JSON representation.
See Also
Compact Serialization
Register Classes for Compact Serialization
Bridge For WAN Replication