views:

618

answers:

3

Hi all,

In serialization (Binary Serialization) what data is being transferred? An instance of a class (object) or values assigned to that object's properties.

I want to know what happens in the serialization process basically? I mean an object is converted to it's target file or whatever in serialization. But how? What steps are followed in this process?

Can someone help?

Jay...

+1  A: 

By default, only fields of a type are serialized when using binary serialization. Properties are treated as methods, and have no effect on the serialization.

When using Xml serialization, the public properties are serialized by default.

Philippe Leybaert
Is it fields itself or the value assigned to that?
Jaywith.7
Is it field or the value assigned to that or both?
Jaywith.7
For xml serialization, public fields + properties with both a read *and* write accessor; that aren't marked [XmlIgnore]; or `IXmlSerializable` has first dibs ;-p
Marc Gravell
+2  A: 

I assume you mean BinaryFormatter; it depends ;-p

The purpose of serialization is to express a complex in-memory object as a simple sequence of bytes (or depending on the serializer - characters, etc) that can be re-hydrated at the other end to re-create the object.

Some types (primitives, strings, etc) have inbuilt direct support by the serializer - it writes these directly.

In the case of classes, the type metadata (incuding assembly name etc) is written, then all of the fields on the type are enumerated (essentially, Type.GetFields(), including private etc). For every field (not marked [NonSerialized]), the field name is written, and the value is serialized (through the same process). Eventually, everything boils down to the inbuilt primitives, some type definitions, and some name/value field pairs.

An exception here is if the type implements ISerializable - in which case the type is asked to serialize itself to the output. This is common in things like dictionary types, where the in-memory layout of the type can be expressed differently to a stream.

During deserialization the process is reversed; the type-metadata is used to create an empty object (unless it has a special serialization constructor/ISerializable); then the fields are set as they are found in the stream.

In both serialization and deserialization there are "callback" points where you can execute additional code to fix-up objects for (de)serialization.

This process is brittle; for lots of reasons, see here - but it is also version intolerant and implementation specific (you can't consume it from java etc).

protobuf-net solves a lot of these problems, by being a binary serializer that is contract-based, rather than field-based.


Marc Gravell
What about automatic properties, are their backing fields serialized?Oh and since you mentioned protobuf-net, here's the link: http://code.google.com/p/protobuf-net/
M4N
re automatic properties: yes they are; but that is *especially* brittle see the "see here" link for why. I'll edit to put the protobuf-net link in, but funnily enough I already know it quite well ;-p
Marc Gravell
Thanks. BTW: I knew that YOU knew the link, I added it for other readers :)
M4N
+3  A: 

Binary Serialization is taking snap shoot of object and serializes it. Meaning all private fields, that are not marked as NonSerializable, will be serialized with their values. All objects used in object hierarchy must be maked as Serializable. You should put [field:NonSerializable] attribute on events, so event handlers don't get serialized too: http://bytes.com/groups/net-c/250944-nonserialized-attribute-events#post1013968

Keep in mind that when deserializing object, you have to have exactly the same object in same assembly as when you serialized it (same meaning same assembly info). If not, you can use SerializationBinder class so you can reset type in which stream will be sterilized.

Andrija
Yes! This [field:NonSerializable] attribute is very good to know. The code only gives you a spurious Serialization Exception when you don't use the attribute on the events, leaving you utterly in the dark about why the exception was thrown.
Dabblernl
Pedant: [field:NonSerializable] only applies to "field-like events"; for other event implementations (explicit field, EventHandlerList, etc) you'd mark the field directly.
Marc Gravell
typo: sterlized?
jm