Contents tagged with Serialization

  • .NET Serializers, part 2

    There were some things left (well, a lot, actually) from my previous article on .NET serializers. One of these things was the ability to have serializers call specific methods before or after serialization. This can be achieved in two ways:

    • Through implementation of interfaces;
    • Through the use of attributes.

    Interface System.Runtime.Serialization.IDeserializationCallback defines a method named OnDeserialization that is called after de deserialization process. You receive the deserialized object as its only argument and you can change it at your will. Only works with IFormatter-based serializers, such as BinaryFormatter and SoapFormatter, not with XmlSerializer.

    As for attributes, the following can be applied to methods of your class:

    • System.Runtime.Serialization.OnSerializingAttribute (IFormatter and XmlObjectSerializer)
    • System.Runtime.Serialization.OnSerializedAttribute (IFormatter and XmlObjectSerializer)
    • System.Runtime.Serialization.OnDeserializingAttribute (IFormatter and XmlObjectSerializer)
    • System.Runtime.Serialization.OnDeserializedAttribute (IFormatter and XmlObjectSerializer)

    Doesn't matter if the methods are public or private, but they must have the following signature:

    void

    MethodName(StreamingContext ctx)

    Through the StreamingContext parameter you can access the properties that will be serialized or deserialized and act upon them.

    Please notice that the attribute-based approach is the only one that works with WCF, which uses XmlObjectSerializer-based serializers, as well as IFormatter-based.

    Bookmark and Share

    Read more...

  • .NET Serializers

    (UPDATED) 

    As of version 3.5 SP1, .NET comes along with the following serializers:

    • System.Runtime.Serialization.Formatters.Binary.BinaryFormatter: binary serializer used in .NET Remoting, type information is included, understands System.Runtime.Serialization.ISerializable;
    • System.Runtime.Serialization.Formatters.Soap.SoapFormatter: SOAP (XML) serializer used in .NET Remoting, includes type information, understands System.Runtime.Serialization.ISerializable;
    • System.Runtime.Serialization.DataContractSerializer: XML serializer used in WCF, does not include type information;
    • System.Runtime.Serialization.Json.DataContractJsonSerializer: JSON serializer used in WCF, does not include type information;
    • System.Web.Script.Serialization.JavaScriptSerializer: JSON serializer;
    • System.Runtime.Serialization.NetDataContractSerializer: binary serializer used in WCF, includes type information;
    • System.Xml.Serialization.XmlSerializer: XML serializer used in ASP.NET Web Services (ASMX), type information is not included, understands System.Xml.Serialization.IXmlSerializable; 
    • System.Web.UI.LosFormatter: uses ObjectStateFormatter internally, does not account by itself;
    • System.Web.UI.ObjectStateFormatter: binary or text (base 64 encoded) serializer used in ASP.NET for storing items in view state, control state and session, type information is included, understands System.Runtime.Serialization.ISerializable.

    There isn't, however, a common interface for all of these serializers, and, in fact, they have quite different requirements.

    The base interfaces are:

    • System.Runtime.Serialization.IFormatter: for BinaryFormatter, SoapFormatter, NetDataContractSerializer and ObjectStateFormatter;
    • System.Runtime.Serialization.XmlObjectSerializer: for DataContractSerializer, DataContractJsonSerializer and NetDataContractSerializer;
    • XmlSerializer doesn't have any interface that specifies its behavior.

    The interfaces that can be used to control the serialization process are:

    • System.Runtime.Serialization.ISerializable: for all the IFormatter serializers;
    • System.Xml.Serialization.IXmlSerializable: for XmlSerializer.

    Also, there are a couple of attribute classes that can be used to control the serialization process:

    • System.Runtime.Serialization.OptionalFieldAttribute (IFormatter): field may be missing from the serialized content
    • System.Runtime.Serialization.NonSerializedAttribute (IFormatter): field is not serialized
    • System.Xml.Serialization.SoapAttributeAttribute (XmlSerializer)
    • System.Xml.Serialization.SoapElementAttribute (XmlSerializer)
    • System.Xml.Serialization.SoapEnumAttribute (XmlSerializer)
    • System.Xml.Serialization.SoapIgnoreAttribute (XmlSerializer)
    • System.Xml.Serialization.SoapIncludeAttribute (XmlSerializer)
    • System.Xml.Serialization.SoapTypeAttribute (XmlSerializer)
    • System.Xml.Serialization.SoapDocumentServiceAttribute (XmlSerializer): default format is Document
    • System.Xml.Serialization.SoapRpcServiceAttribute (XmlSerializer): default format is RPC
    • System.Xml.Serialization.SoapRpcMethodAttribute (XmlSerializer): method uses SOAP RPC format
    • System.Xml.Serialization.SoapDocumentMethodAttribute (XmlSerializer): method uses SOAP Document format
    • System.Xml.Serialization.XmlAnyAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlAnyElementAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlArrayAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlArrayItemAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlAttributeAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlChoiceIdentifierAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlElementAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlEnumAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlIgnoreAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlIncludeAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlRootAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlTextAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlTypeAttribute (XmlSerializer)
    • System.Xml.Serialization.XmlSchemaProviderAttribute (XmlSerializer): applied on a method that returns the XML schema
    • System.Runtime.Serialization.CollectionDataContractAttribute (XmlObjectSerializer)
    • System.Runtime.Serialization.DataContractAttribute (XmlObjectSerializer)
    • System.Runtime.Serialization.DataMemberAttribute (XmlObjectSerializer)
    • System.Runtime.Serialization.EnumMemberAttribute (XmlObjectSerializer)
    • System.Runtime.Serialization.IgnoreDataMemberAttribute (XmlObjectSerializer)
    • System.Runtime.Serialization.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.

    Bookmark and Share

    Read more...