net serialization
TRANSCRIPT
.NET Serialization
Understanding and effectively applying serialization in .NET applications
Presented by
Gregory M. Sohl
About Greg
Software Architect at StoneRiver
President of Iowa Code Camp
Co-leader and MC for CRineta
Blog: cwi-websoft.com
Twitter: @gregsohl
Agenda
What is Serialization
What is Serialization Used For
How Serialization Works
Available Serializers
Examples of use
Attributes
Customizing Serialization
Versioning
Unit Testing
Serialization Is …
The process of converting an object or graph of objects to a sequence of bytes. Serialized data is
Self described
Reversible - Deserialization
Uses
Exchange .NET objects Two .NET Applications .NET Application Layers – loosely connected Non-.NET applications
Storage of .NET object states To file or database
Available SerializersSerializer Source Format
BinaryFormatter Framework Binary
XMLSerializer Framework XML
DataContractSerializer Framework XML
NetDataContractSerializer Framework XML
DataContractJsonSerializer Framework JSON
JSON.NET Open Src JSON
CompactFormatter Open Src Binary
Protobuf-Net Open Src Binary
SoapFormatter Framework XML
Many other open source tools Open Src Various
What Do They Serialize
Generally Type information Member type information Member name information or use sequence Member values
XmlSerializer, JSON.NET, DCJS Public fields and property values
BinaryFormatter, CompactFormatter, DCJS Fields
How It Works
Reflection to read properties or fields
Transformation of known types to byte or character streams
Recursion into complex types Detect / prevent loops
Varies by implementation Recording of field name Recording of data type
Recording of data value
Controlling Serialization
Serializable Apply to Types Indicates a type can be serialized
NonSerialized Apply to Properties, Fields Indicates a member should not be serialized
… and now some more code
We Need More Control
Attributes OnSerializing, OnSerialized OnDeserializing, OnDeserialized Use with
BinaryFormatter
WCF serializers
JSON.NET
Help for XML Serializer
XmlInclude Attribute Includes definition of additional types Needed for types not directly specified
Customizing SerializationUltimate Control
ISerializable Deserialization constructor GetObjectData method
IXmlSerializable ReadXml WriteXml GetSchema – Nop – just return null
Versioning
.NET 2.0 added version tolerance to BF Add new fields without breaking deserialization Additional fields in stream ignored during
deserialization Attribute fields as OptionalField
Set VersionAdded param, though still not implemented
Version Tolerance Rules
Never remove a serialized field.
Never apply the NonSerializedAttribute attribute to a field if the attribute was not applied to the field in the previous version.
Never change the name or the type of a serialized field.
When adding a new serialized field, apply the OptionalFieldAttribute attribute.
Version Tolerance Rules
When removing a NonSerializedAttribute attribute from a field (that was not serializable in a previous version), apply the OptionalFieldAttribute attribute.
For all optional fields, set meaningful defaults using the serialization callbacks unless 0 or nullas defaults are acceptable.
Versioning
Other changes and other serializers require custom serialization Member data type changes Semantic changes
Unit Testing
Now that you have all this code… Make sure various data types
serialize/deserialize Custom serialization needs testing!
Easy to mess up
Easy to break