Detailed explanation of serialization in .net

PHPz
Release: 2017-04-03 11:59:16
Original
1499 people have browsed it

Serialization in .net

Common serialization formats and methods

In .net, common serialization formats mainly include json, binary and xml, summarized in the following table.

Detailed explanation of serialization in .net

Back to top

Notes

About entity attribute annotation rules:

1, all in .net The [Serializable] mark should be added to the class of the entity used for serialization. If not, there will be no problem during json serialization, but an error will be reported when using BinaryFormatter for binary serialization.

2. If applied in wcf, [DataContract] tags should be added to the classes of all entities, and [DataMember] should be added to the fields.

3. When using newtonsoft.json, if [DataContract] is added to the entity class, some fields are added with [DataMember], and some fields are not added, but those that are not added should also be included during serialization. [DataMember] fields can be solved by adding [JsonObject(MemberSerialization.OptOut)] to the entity class, which means outputting all public fields.

4. When using newtonsoft.json, System.Web.UI.WebControls.ListItem cannot be serialized. The solution is to customize a class and mark it [Serializable].

Back to top

Summary

1, if it is json format, NewtonSoft.Json is the most versatile (can handle circular references), although it is not the fastest.

2. If it is in xml format, use the XmlSerializer that comes with .net.

3. If it is a binary format, use the BinaryFormatter that comes with .net. Although Protobuf is fast, it is troublesome to add the serial number.

The above is the detailed content of Detailed explanation of serialization in .net. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!