Introduction to serialization презентация

Introduction to Serialization
 Last update: 
 Lesya Klakovych
 August 2016 
AgendaWhat is Serialization?
 Serialization is the process of transforming an objectSerialization in .NETSerialization in .NET
 .NET Framework has classes (in the System.Runtime.Serialization andBinary serializationBinaryFormatter
 [Serializable]
 class Person {
   private int _id;
 BinaryFormatterCustom serialization. ISerializable
 With a custom serialization, you can specify exactlyISerializable.  Example of implementationCustom serialization. Using attributes
  Add attribute before a custom methodXMLSerializer
  The XmlSerializer (namespace System.Xml.Serialization) was created with the ideaXMLSerializer
 To deserialize an object:
 Construct a XmlSerializer using the typeXMLSerializer
 You can configure how the XmlSerializer serializes your type byComplex and derived types serializationComplex and derived types serializationUsing DataContract
 DataContract is used when you use WCF. 
 TheUsing DataContractUsing DataContractNetDataContractSerializerJSON Serialization 
 We can use DataContractJsonSerializer to serialize type instanceDemonstration 4.  Json Serialization. class PersonDemonstration 4.  Json Deserialization. class PersonJSON Brief Introduction
 JSON (JavaScript Object Notation) is one lightweight dataQuestions?



Слайды и текст этой презентации
Слайд 1
Описание слайда:
Introduction to Serialization Last update: Lesya Klakovych August 2016 Reviewed by Nazar Ivchenko


Слайд 2
Описание слайда:
Agenda

Слайд 3
Описание слайда:
What is Serialization? Serialization is the process of transforming an object or object graph that you have in-memory into a stream of bytes or text. Deserialization is the opposite. You take some bytes or text and transform them into an object.

Слайд 4
Описание слайда:
Serialization in .NET

Слайд 5
Описание слайда:
Serialization in .NET .NET Framework has classes (in the System.Runtime.Serialization and System.Xml.Serialization namespaces) that support: binary, XML, JSON, own custom serialization.

Слайд 6
Описание слайда:
Binary serialization

Слайд 7
Описание слайда:
BinaryFormatter [Serializable] class Person { private int _id; public string FirstName; public string LastName; public void SetId(int id) { _id = id; } }

Слайд 8
Описание слайда:
BinaryFormatter

Слайд 9
Описание слайда:
Custom serialization. ISerializable With a custom serialization, you can specify exactly which objects will be serialized, and how they will be serialized. This class must be marked with the SerializableAttribute attribute and implement the Iserializable interface . ISerializable interface: the Formatter calls the GetObjectData() at serialization time and populates the supplied SerializationInfo with all the data required to represent the object . For the custom deserialization, you should use a custom constructor.

Слайд 10
Описание слайда:
ISerializable. Example of implementation

Слайд 11
Описание слайда:
Custom serialization. Using attributes Add attribute before a custom method that manipulates the object’s data during and upon completion of serialization and deserialization. OnDeserializedAttribute, OnDeserializingAttribute, OnSerializedAttribute, and OnSerializingAttribute.

Слайд 12
Описание слайда:
XMLSerializer The XmlSerializer (namespace System.Xml.Serialization) was created with the idea of Simple Object Access Protocol (SOAP) messaging in mind. SOAP is a protocol for exchanging information with web services. It uses XML as the format for messages. XML is readable by both humans and machines, and it is independent of the environment it is used in. To serialize an object: Create the object and set its public fields and properties. Construct a XmlSerializer using the type of the object. Call the Serialize method to generate either an XML stream or a file representation of the object's public properties and fields.

Слайд 13
Описание слайда:
XMLSerializer To deserialize an object: Construct a XmlSerializer using the type of the object to deserialize. Call the Deserialize method to produce a replica of the object. After deserialization you must cast the returned object to the type of the original

Слайд 14
Описание слайда:
XMLSerializer You can configure how the XmlSerializer serializes your type by using attributes. These attributes are defined in the System.Xml.Serialization namespace : XmlIgnore - can be used to make sure that an element is not serialized XmlAttribute - you can map a member to an attribute on its parent node. XmlElement – by default XmlArray - is used when serializing collections. XmlArrayItem - is used when serializing collections.

Слайд 15
Описание слайда:
Complex and derived types serialization

Слайд 16
Описание слайда:
Complex and derived types serialization

Слайд 17
Описание слайда:
Using DataContract DataContract is used when you use WCF. The DataContractSerializer is used by WCF to serialize your objects to XML or JSON. You should use DataContractAttribute instead of SerializableAttribute. The class members are not serialized by default. You have to explicitly mark them with the DataMember attribute. As with binary serialization, you can use OnDeserializedAttribute, OnDeserializingAttribute, OnSerializedAttribute, and OnSerializingAttribute to configure the four phases of the serialization and deserialization process.

Слайд 18
Описание слайда:
Using DataContract

Слайд 19
Описание слайда:
Using DataContract

Слайд 20
Описание слайда:
NetDataContractSerializer

Слайд 21
Описание слайда:
JSON Serialization We can use DataContractJsonSerializer to serialize type instance to JSON string and deserialize JSON string to type instance DataContractJsonSerializer is under System.Runtime.Serialization.Json namespace. It is included in System.ServiceModel.Web.dll in .NET Framework 3.5 and System.Runtime.Serialization in .NET Framework 4.0. We need to add it as reference http://www.codeproject.com/Articles/272335/JSON-Serialization-and-Deserialization-in-ASP-NET#

Слайд 22
Описание слайда:
Demonstration 4. Json Serialization. class Person

Слайд 23
Описание слайда:
Demonstration 4. Json Deserialization. class Person

Слайд 24
Описание слайда:
JSON Brief Introduction JSON (JavaScript Object Notation) is one lightweight data exchange format. JSON is "name/value" assembly. Its structure is made up with {}, [], comma, colon and double quotation marks. And it includes the following data types: Object, Number, Boolean, String, Array, NULL. JSON has three styles: 1. Object: An unordered "name/value" assembly. An object begins with "{" and ends with "}". Behind each "name", there is a colon. And comma is used to separate much "name/value". For example: 2. Array: Value order set. An array begins with "[" and end with "]". And values are separated with comma. For example: 3. String: Any quantity unicode character assembly which is enclosed with quotation marks. It uses backslash to escape.

Слайд 25
Описание слайда:
Questions?


Скачать презентацию на тему Introduction to serialization можно ниже:

Похожие презентации