Top Banner
.NET Serialization Understanding and effectively applying serialization in .NET applications Presented by Gregory M. Sohl
20
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Net serialization

.NET Serialization

Understanding and effectively applying serialization in .NET applications

Presented by

Gregory M. Sohl

Page 2: Net serialization

About Greg

Software Architect at StoneRiver

President of Iowa Code Camp

Co-leader and MC for CRineta

Blog: cwi-websoft.com

Twitter: @gregsohl

Page 3: Net serialization

Agenda

What is Serialization

What is Serialization Used For

How Serialization Works

Available Serializers

Examples of use

Attributes

Customizing Serialization

Versioning

Unit Testing

Page 4: Net serialization

Serialization Is …

The process of converting an object or graph of objects to a sequence of bytes. Serialized data is

Self described

Reversible - Deserialization

Page 5: Net serialization

Uses

Exchange .NET objects Two .NET Applications .NET Application Layers – loosely connected Non-.NET applications

Storage of .NET object states To file or database

Page 7: Net serialization

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

Page 8: Net serialization

Basic Examples

Let’s see a little code…

Page 9: Net serialization

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

Page 10: Net serialization

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

Page 11: Net serialization

De-Serializing

Okay, code again…

Page 12: Net serialization

We Need More Control

Attributes OnSerializing, OnSerialized OnDeserializing, OnDeserialized Use with

BinaryFormatter

WCF serializers

JSON.NET

Page 13: Net serialization

Help for XML Serializer

XmlInclude Attribute Includes definition of additional types Needed for types not directly specified

Page 14: Net serialization

Customizing SerializationUltimate Control

ISerializable Deserialization constructor GetObjectData method

IXmlSerializable ReadXml WriteXml GetSchema – Nop – just return null

Page 15: Net serialization

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

Page 16: Net serialization

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.

Page 17: Net serialization

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.

Page 18: Net serialization

Versioning

Other changes and other serializers require custom serialization Member data type changes Semantic changes

Page 19: Net serialization

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

Page 20: Net serialization

Questions

@gregsohl http://cwi-websoft.com