Serialization in. Net
Common serialization formats and methods
In. NET, the common serialization format is mainly JSON, binary, and XML, summarized in the following table.
Back to Top
Precautions
About entity attribute labeling rules:
The [Serializable] tag should be added to the class of all entities used for serialization in 1,.net, and if not added, there is no problem with JSON serialization, but the error occurs when using BinaryFormatter for binary serialization.
2, if applied in WCF, the [DataContract] tag should also be added to the class on all entities, with [DataMember] on the field.
3, when using Newtonsoft.json, if the entity class added [DataContract], some fields added [DataMember], and some fields are not added, but also to serialize the time to include those that do not add [DataMember] field, You can add [Jsonobject (Memberserialization.optout)] on the entity class to resolve it, representing the output of all public fields.
4, when using Newtonsoft.json, System.Web.UI.WebControls.ListItem cannot be serialized, the workaround is to customize a class and tag [Serializable].
Back to Top
Summarize
1, if it is in JSON format, Newtonsoft.json is the most common (can handle circular reference), although it is not the fastest.
2, if it is in XML format, use the. NET XmlSerializer.
3, if it is a binary format, use the. NET BinaryFormatter, although the protobuf speed, but to add serial number is very troublesome.