Serialization of objects and deserialization?

What is object serialization and deserialization? What is the difference in Serialization with conventional methods, such as reading the properties of an object and then filling them with DataRow columns and finally storing the DataRow in the database?

thanks

+7
oop serialization
source share
5 answers

Serialization usually refers to the creation of a version of data (rather than objects) that can be used for storage (possibly in a file), for transmission over the network, or perhaps only for transfer between processes / AppDomain /, etc. by one machine.

Serialization usually means writing data as a string (think: xml / json) or as a binary file (a byte[] , etc.). Desicialization is the reverse process; taking raw data (from a file, from an incoming network socket, etc.) and restoring the object model.

The difference between using db is that it does not have an internal table layout and does not have a real database binding; data can be of any shape and tend to be more accurately mapped to an object-oriented layout than to row and column type tables.

Most platforms have a number of serialization tools. For example, it looks like you're talking about .NET - so BinaryFormatter (.NET-specific), XmlSerializer , DataContractSerializer , Json.NET and protobuf-net / dotnet-protobufs will qualify.

+14
source share

Serialization = inclusion of the corresponding state of the object in the streaming representation. This may mean converting it to a stream of bytes. This does not necessarily include copying each member variable to a stream. A classic example that Joshua Bloch uses in Effective Java is the HashSet. You just serialize the elements in a Hashset, but not with the keys.

Deserialization = restoring an object from a sequential representation and providing object invariants. Deserialization can be considered as a separate constructor of an object. In the case of the HashSet mentioned above, you will create a new HashSet, and then paste the values ​​from the stream into this new data structure.

+5
source share

Serialization means that you save your object in a view that you can store somewhere. One way to do this is simply to take a pointer to where your object is stored in memory, and write each byte as a file. Since this representation is very specific to your programming language (and how it represents objects in memory), an improvement would be to convert your object to a String view that has a certain well-known structure (like XML or JSON) so that you can

a) easier to carry

b) Keep and restore it easier

c) Since everyone knows how the format is determined, any other programs can read your object too

Therefore, placing an object in a database is another form of serialization.

Deserialization means that you can reload / restore this object from where you saved it.

+3
source share

Serialization is usually the process of writing the state of an object in your runtime to disk (but it can be anywhere) and the ability to read it again.

In fact, storing the properties of an object in a table is a form of serialization.

There are other forms in .NET:

  • XmlSerialization
  • BinarySerialization

You can create your own.

But in the general case, if you save the state of your object somewhere, and then read it again in a "living" object at runtime, you serialize it.

+1
source share

Serialization

Serialization is the process of converting an object or set of objects into a stream.

Deserialization

Deserialization is the process of converting a stream to an object or a set of graphs of objects.

Here are some user attributes:

[OnDeserialization] β†’ Used when we need to perform some actions during stream deserialization. [OnDeserialized] β†’ Used when we need to perform some actions after deserializing a stream into an object. How to set the value of the field of objects

Below is an example

  [Serializable] internal class DemoForSerializableAndDeserializable { internal string Fname = string.Empty; internal string Lname = string.Empty; internal Stream SerializeToMS(DemoForSerializableAndDeserializable objDemo) { DemoForSerializableAndDeserializable objSer = new DemoForSerializableAndDeserializable(); MemoryStream ms = new MemoryStream(); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(ms, objSer); return ms; } [OnDeserializing] private void OnDeserializing(StreamingContext context) { // Do some work while deserializing the stream } [OnDeserialized] private void OnSerialized(StreamingContext context) { Fname = "abc"; } } 

Call code

 class CallingCode { string fname = string.Empty; string Lname = string.Empty; static void Main(string[] args) { DemoForSerializableAndDeserializable demo = new DemoForSerializableAndDeserializable(); Stream ms = demo.SerializeToMS(demo); ms.Position = 0; DemoForSerializableAndDeserializable demo1 = new BinaryFormatter().Deserialize(ms) as DemoForSerializableAndDeserializable; Console.WriteLine(demo1.Fname); Console.WriteLine(demo1.Lname); Console.ReadLine(); } } 
0
source share

All Articles