Serialization Format - Binary & JSON Serialization
Cache data when transmitted over the network needs to be serialized. The serialization process traverses the existing objects, extracts the data, and converts it into the serialized data. Data is then transmitted over the network and the process of deserialization reads the serialized data. This whole process of serialization/deserialization requires a lot of factors like time, memory, and performance, etc. These factors are to be kept in mind when choosing a format for serialization.
Note
Diese Funktion ist auch in verfügbar NCache Professional.
Es gibt zwei Arten von Serialisierungsformaten, die von bereitgestellt werden NCache:
- Binäre Serialisierung
- JSON-Serialisierung
Note
Es wird empfohlen, die JSON-Serialisierung zu verwenden, wenn Sie ASP.NET 5.0 und höher verwenden. Für mehr Details klicken hier.
Binäre Serialisierung
Im Binärformat werden die Benutzerobjekte (z. B. Produktobjekt) vom Client aus serialisiert und auf der Serverseite in derselben Form gespeichert. Jedes Mal, wenn ein Artikel vom Server angefordert wird, erhält der Client die Binärform des Artikels, die dann lokal in das Produktobjekt deserialisiert wird. Darüber hinaus erfolgt die Serialisierung/Deserialisierung nur auf der Clientseite und nur einmal, entweder beim Abrufen oder Hinzufügen der Daten – deserialisiert beim Abrufen und serialisiert beim Hinzufügen. Dadurch werden die Kosten für die Serialisierung/Deserialisierung eingespart, was sich insbesondere in Fällen bemerkbar macht, in denen große Datenmengen hinzugefügt oder aus dem Cache abgerufen werden.
Das Binärformat ist von Vorteil, wenn der Großteil Ihrer Verarbeitung auf der Clientseite erfolgt und die durchgeführten Vorgänge wie Hinzufügen, Aktualisieren, Abrufen und Entfernen aus dem Cache sind. Beispielsweise wird ein serialisiertes Produktobjekt aus dem Cache abgerufen, um seinen Inhalt anzuzeigen. Bei Verwendung des Binärformats muss das Element erst dann deserialisiert werden, wenn es den Client erreicht, wodurch die Verarbeitungskosten auf ein Minimum reduziert werden. Falls das Objektformat verwendet wird, serialisiert der Server das Produktobjekt und sendet es an den Client, der es dann deserialisiert. Dies erhöht den Gesamtaufwand und die Kosten für die Serialisierung und Deserialisierung.
Wenn ein Objekt im Binärformat zum Cache hinzugefügt wird, wird es entsprechend serialisiert, bevor es über das Netzwerk gesendet wird, und so wie es ist gespeichert.
JSON-Serialisierung
Note
Diese Funktion ist in verfügbar NCache Unternehmen und Professionell.
JSON is the text-based syntax for storing and exchanging data. It is fairly an easier format for the machines to parse and for the 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.
Betrachten Sie ein .NET Produkt class containing the data of products. It contains various attributes, e.g., Produktname, Produkt ID und Produktablauf usw.
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };
Diese Daten werden in die folgende JSON-Zeichenfolge serialisiert:
{
"Name": "Apple",
"Expiry": "2008-12-28T00:00:00",
"Sizes":
[
"Small"
]
}
The serializer used is an extension of the NewtonSoft 3rd party serializer. It can be a preferable approach for compact and convenient serialization of data. JSON serialization can be preferred over Binary Serialization for the following reasons:
Speichereffizient: Memory is one of the major parameters kept in mind when using large data sets. Consumption of larger memory affects other performance measures. JSON serialization affects less memory for storing the objects. The entire data set is serialized as JSON and results in reduced consumption of memory due to significantly smaller size, thus, improving network communication.
Portabilität: JSON serialization provides higher portability since it provides you with the ease of serializing the data of various technologies into the same standard. JSON serializer serializes data of all types. For example, if the user is using Java class, JSON serializer will serialize the data in the form of a string and deserialize it in the type defined by the user. This level of portability is not provided by the binary serialization.
Keine Codeänderung: Since the serialization according to the JSON format is fully automated, there is no code change required by the user unless you require code change according to your own logic.
Nicht serialisierbare Attribute: You are also provided with the choice of serializing the selective attributes and keeping the attributes of your choice non-serialized by using a method called “JsonIgnore” in your class. This means that the serializer will ignore these attributes while serializing the data and serialize the rest. This can be done only with the attributes of a custom class. Please refer to the NewtonSoft documentation for details. For example, if the user wants to keep the attribute Passwort als nicht serialisiert,
JsonIgnore
will mark it as non-serializable.
using Newtonsoft.Json;
[JsonIgnore]
public String Password()
{
get;
set;
}
Unten sehen Sie ein Beispiel für eine benutzerdefinierte Klasse mit einem nicht serialisierbaren markierten Attribut Passwort. Nach der Serialisierung enthält die serialisierte JSON-Zeichenfolge nicht den Wert des nicht serialisierten Attributs.
Account account = new Account
{
FullName = "Joe User",
EmailAddress = "joe@example.com",
Password = "VHdlZXQgJ1F1aWNrc2lsdmVyJyB0byBASmFtZXNOSw=="
};
Die obigen Daten werden in die folgende JSON-Zeichenfolge serialisiert.
{
"FullName":"Joe User",
"EmailAddress":"joe@example.com"
}
Keeping in mind the factors above, JSON serialization can result in achieving a higher level of performance. However, if you want to obtain accuracy at a higher level, use binary serialization instead of JSON serialization since Binary serialized data is deserialized more accurately.
Siehe auch
Kompakte Serialisierung
Registrieren Sie Klassen für die kompakte Serialisierung
Bridge für die WAN-Replikation
Datenlastausgleich