.NET Serializers
UPDATED for .NET 8
As of .NET 8, .NET comes along with the following serializers:
-
BinaryFormatter: binary serializer used in .NET Remoting, type information is included, understands ISerializable; (exists in .NET Core but dropped in .NET 7)
-
SoapFormatter: SOAP (XML) serializer used in .NET Remoting, includes type information, not portable outside Windows, understands ISerializable;
-
DataContractSerializer: XML serializer used in WCF, does not include type information; (exists in .NET Core)
-
DataContractJsonSerializer: JSON serializer used in WCF, does not include type information; (exists in .NET Core)
- JavaScriptSerializer: JSON serializer included in System.Web.Extensions.dll;
-
NetDataContractSerializer: binary serializer used in WCF, includes type information and is not portable outside Windows systems;
-
XmlSerializer: XML serializer used in ASP.NET Web Services (ASMX), type information is not included, understands IXmlSerializable; (exists in .NET Core)
-
LosFormatter: uses ObjectStateFormatter internally, does not account by itself;
-
ObjectStateFormatter: binary or text (Base64 encoded) serializer used in ASP.NET for storing items in view state, control state and session, type information is included, understands ISerializable;
- JsonSerializer: JSON serializer included in System.Text.Json API, the now default serializer for JSON.
There isn't, however, a common interface for all of these serializers, and, in fact, they have quite different requirements.
The base classes or interfaces are:
- IFormatter: for BinaryFormatter, SoapFormatter, NetDataContractSerializer and ObjectStateFormatter;
- XmlObjectSerializer: for DataContractSerializer, DataContractJsonSerializer and NetDataContractSerializer;
The interfaces that can be used to control the serialization process are:
- ISerializable: for all the IFormatter serializers;
- IXmlSerializable: for XmlSerializer.
Also, there are a couple of attribute classes that can be used to control the serialization process:
- OptionalFieldAttribute (IFormatter): field may be missing from the serialized content;
- NonSerializedAttribute (IFormatter): field is not serialized;
- SoapAttributeAttribute (XmlSerializer)
- SoapElementAttribute (XmlSerializer)
- SoapEnumAttribute (XmlSerializer)
- SoapIgnoreAttribute (XmlSerializer)
- SoapIncludeAttribute (XmlSerializer)
- SoapTypeAttribute (XmlSerializer)
- SoapDocumentServiceAttribute (XmlSerializer): default format is Document
- SoapRpcServiceAttribute (XmlSerializer): default format is RPC
- SoapRpcMethodAttribute (XmlSerializer): method uses SOAP RPC format
- SoapDocumentMethodAttribute (XmlSerializer): method uses SOAP Document format
- XmlAnyAttributeAttribute (XmlSerializer)
- XmlAnyElementAttribute (XmlSerializer)
- XmlArrayAttribute (XmlSerializer)
- XmlArrayItemAttribute (XmlSerializer)
- XmlAttributeAttribute (XmlSerializer)
- XmlChoiceIdentifierAttribute (XmlSerializer)
- XmlElementAttribute (XmlSerializer)
- XmlEnumAttribute (XmlSerializer)
- XmlIgnoreAttribute (XmlSerializer)
- XmlIncludeAttribute (XmlSerializer)
- XmlRootAttribute (XmlSerializer)
- XmlTextAttribute (XmlSerializer)
- XmlTypeAttribute (XmlSerializer)
- XmlSchemaProviderAttribute (XmlSerializer): applied on a method that returns the XML schema
- CollectionDataContractAttribute (XmlObjectSerializer)
- DataContractAttribute (XmlObjectSerializer): from WCF
- DataMemberAttribute (XmlObjectSerializer): from WCF
- EnumMemberAttribute (XmlObjectSerializer): from WCF
- IgnoreDataMemberAttribute (XmlObjectSerializer): from WCF
- XmlSerializerFormatAttribute (XmlObjectSerializer): choose XmlSerializer instead of DataContractSerializer
Note that the Soap* attributes are only considered if the SoapBindingUse.Encoded is set for the Use property of the binding, in ASP.NET Web Services, otherwise, its the Xml* attributes.
It would be good to see a common interface (perhaps IFormatter, since it's the most common) for all of these serializers.