.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 Serializers
Serializer 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
Basic Examples
Let’s see a little code…
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
De-Serializing
Okay, code again…
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 Serialization
Ultimate 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
Questions
@gregsohl http://cwi-websoft.com

Net serialization

  • 1.
    .NET Serialization Understanding andeffectively applying serialization in .NET applications Presented by Gregory M. Sohl
  • 2.
    About Greg Software Architectat StoneRiver President of Iowa Code Camp Co-leader and MC for CRineta Blog: cwi-websoft.com Twitter: @gregsohl
  • 3.
    Agenda What is Serialization Whatis Serialization Used For How Serialization Works Available Serializers Examples of use Attributes Customizing Serialization Versioning Unit Testing
  • 4.
    Serialization Is … Theprocess of converting an object or graph of objects to a sequence of bytes.  Serialized data is Self described Reversible - Deserialization
  • 5.
    Uses Exchange .NET objects Two .NET Applications  .NET Application Layers – loosely connected  Non-.NET applications Storage of .NET object states  To file or database
  • 6.
    Available Serializers Serializer SourceFormat 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
  • 7.
    What Do TheySerialize 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
  • 8.
  • 9.
    How It Works Reflectionto 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
  • 10.
    Controlling Serialization Serializable  Applyto Types  Indicates a type can be serialized NonSerialized  Apply to Properties, Fields  Indicates a member should not be serialized … and now some more code
  • 11.
  • 12.
    We Need MoreControl Attributes  OnSerializing, OnSerialized  OnDeserializing, OnDeserialized  Use with BinaryFormatter WCF serializers JSON.NET
  • 13.
    Help for XMLSerializer XmlInclude Attribute  Includes definition of additional types  Needed for types not directly specified
  • 14.
    Customizing Serialization Ultimate Control ISerializable Deserialization constructor  GetObjectData method IXmlSerializable  ReadXml  WriteXml  GetSchema – Nop – just return null
  • 15.
    Versioning .NET 2.0 addedversion 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
  • 16.
    Version Tolerance Rules Neverremove 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.
  • 17.
    Version Tolerance Rules Whenremoving 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.
  • 18.
    Versioning Other changes andother serializers require custom serialization  Member data type changes  Semantic changes
  • 19.
    Unit Testing Now thatyou have all this code…  Make sure various data types serialize/deserialize  Custom serialization needs testing! Easy to mess up Easy to break
  • 20.