Serialization w w w What is Serialization System

  • Slides: 17
Download presentation
Serialization w w w What is Serialization? System. Serialization Scenarios in Serialization Basic Serialization

Serialization w w w What is Serialization? System. Serialization Scenarios in Serialization Basic Serialization Custom Serialization

Serialization/Deserialization Object in memory …binary or character stream… Object in memory

Serialization/Deserialization Object in memory …binary or character stream… Object in memory

Serialization What is Serialization w Serialization is the process of converting an object, or

Serialization What is Serialization w Serialization is the process of converting an object, or a connected graph of objects, stored within computer memory, into a linear sequence of bytes w Use the sequence of bytes in several ways: n n Send it to another process Send it to the clipboard, to be browsed or used by another application Send it to another machine Send it to a file on disk

Serialization Object Graph w What is an object graph? n n An object graph

Serialization Object Graph w What is an object graph? n n An object graph is a set of objects with some set of references to each other The most obvious problem is how to represent the links between the objects in the Serialized stream 3 4 Cat Dog Cat 7 9 1 Horse Mouse 2 Duck

Serialization How Serialization Works w Because run-time metadata 'knows' about each object's layout in

Serialization How Serialization Works w Because run-time metadata 'knows' about each object's layout in memory, and its field and property definitions, you can serialize objects automatically, without having to write code to serialize each field w The serialized stream might be encoded using XML, or a compact binary representation w The format is decided by the Formatter object that you call: n n n Binary SOAP Custom

Serializaiton File. Stream Example class Serialize. Example{ public static void Main(String[] args) { Array.

Serializaiton File. Stream Example class Serialize. Example{ public static void Main(String[] args) { Array. List l = new Array. List(); for (int x=0; x< 100; x++) { l. Add (x); } // create the object graph File. Stream s = File. Create("foo. bin"); // create the filestream Binary. Formatter b = new Binary. Formatter(); // create the Binary. Formatter b. Serialize(s, l); // serialize the graph to the stream } // end main } // end class

Serializaiton Deserialize Example using System; using System. IO; System. Collections; System. Serialization. Formatters. Binary;

Serializaiton Deserialize Example using System; using System. IO; System. Collections; System. Serialization. Formatters. Binary; class De. Serialize { public static void Main(String[] args) { File. Stream s = File. Open("foo. bin"); // open the filestream Binary. Formatter b = new Binary. Formatter(); // create the formatter Array. List p = (Array. List) b. Deserialize(s); // deserialize p. To. String(); // print out the new object graph } // end Main } // end Class De. Serialize

Serialization Basic Serialization w A Type is NOT Serializable unless Type is specifically marked

Serialization Basic Serialization w A Type is NOT Serializable unless Type is specifically marked as Serializable w The Serializable Attribute [Serializable] public class My. Class {} w The Non-Serializable Attribute [Serializable] public class My. Class { [Not. Serialized] int _cash. Size; }

. NET Serialization Facilities Take an extremely simple C# class: public class Initial. Configuration

. NET Serialization Facilities Take an extremely simple C# class: public class Initial. Configuration { public enum Difficulty {hard, medium, easy}; public Initial. Configuration() { } public Difficulty starting = Difficulty. medium; }

. NET Serialization Facilities Use. NET library functions to serialize it: Initial. Configuration conf

. NET Serialization Facilities Use. NET library functions to serialize it: Initial. Configuration conf = new Initial. Configuration(); Xml. Serializer ser = new Xml. Serializer(typeof(Initial. Configuration)); Xml. Text. Writer writer = new Xml. Text. Writer( stream, System. Text. Encoding. UTF 8); ser. Serialize(writer, conf);

. NET Serialization Facilities Get XML: <? xml version="1. 0" encoding=“utf-8"? > <Initial. Configuration

. NET Serialization Facilities Get XML: <? xml version="1. 0" encoding=“utf-8"? > <Initial. Configuration xmlns: xsd="http: //www. w 3. org/2001/XMLSchema" xmlns: xsi="http: //www. w 3. org/2001/XMLSchema-instance"> <Starting>medium</starting> </Initial. Configuration>

Serialization Customize Serialization w Implementing ISerializable interface w IDeserialization. Event. Listener w Custom Formatters

Serialization Customize Serialization w Implementing ISerializable interface w IDeserialization. Event. Listener w Custom Formatters

Serialization ISerializable Interface w Customize the serialization process w If a class implements ISerializable,

Serialization ISerializable Interface w Customize the serialization process w If a class implements ISerializable, that interface will always be called in preference to default serialization. w The ISerializable interface is only contains one method: void Get. Object. Data (Serialization. Info info, Streaming. Context context); And an implied constructor that may be private <Type. Name> (Serialization. Info info, Streaming. Context)

Serialization IDeserialization. Event. Listener w If an object implements IDeserialization. Event. Listener, the serialization

Serialization IDeserialization. Event. Listener w If an object implements IDeserialization. Event. Listener, the serialization infrastructure will call that class‘ On. Deserialization method as soon as the entire graph has been deserialized and all fixups completed w Provide a reasonable opportunity for objects that need to do fix-ups based on the state of their children

Serialization Custom Formatter w Implementing IFormatter Interface: public interface IFormatter: { //Properties Serialization. Binder

Serialization Custom Formatter w Implementing IFormatter Interface: public interface IFormatter: { //Properties Serialization. Binder { get; set; } Streaming. Context { get; set; } ISurrogate. Selector { get; set; } //Methods object Deserialize(Stream serialization. Stream); void Serialize(Stream serialization. Stream, object graph); }

Conclusion w Types' metadata can be explored with Reflection w Reflection provides dynamic type

Conclusion w Types' metadata can be explored with Reflection w Reflection provides dynamic type system w The Federated Services Model is one of the core concepts for designing. NET applications in Internet w Key. NET Remoting scenarios are: n n Web Services Anywhere CLR Object Remoting w Serialization is the process of converting an object, or a connected graph of objects, stored within computer memory, into a linear sequence of bytes

Resources w http: //msdn. microsoft. com/net/ w. NET Framework SDK

Resources w http: //msdn. microsoft. com/net/ w. NET Framework SDK