net serialization

20
.NET Serialization Understanding and effectively applying serialization in .NET applications Presented by Gregory M. Sohl

Upload: greg-sohl

Post on 15-Aug-2015

69 views

Category:

Documents


2 download

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

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 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

Questions

@gregsohl http://cwi-websoft.com