Home > Article > Backend Development > Detailed explanation of serialization in .net
Serialization in .net
In .net, common serialization formats mainly include json, binary and xml, summarized in the following table.
Back to top
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
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!