What are object serialization and deserialization?
What difference does serialization have with normal techniques like reading an object's properties and then filling a DataRow's columns with them and finally saving the DataRow in DB?
What are object serialization and deserialization?
What difference does serialization have with normal techniques like reading an object's properties and then filling a DataRow's columns with them and finally saving the DataRow in DB?
Serialization generally refers to creating a version of the data (rather than the objects) that can be used for storage (perhaps in a file), for transfer over a network, or perhaps just for transfer between processes / AppDomain
s /etc on a single machine.
Serialization typically means writing the data as a string (think: xml / json) or as raw binary (a byte[]
etc). Deserialization is the reverse process; taking the raw data (from a file, from an incoming network socket, etc) and reconstructing the object model.
The difference between using a db is that it has no intrinsic tabular layout, and no real tie to a database; the data can be any shape, and tends to map more closely to the object-oriented layout than to the rows/columns nature of tables.
Most platforms have a range of serialization tools. For example, it sounds like you're talking about .NET - so BinaryFormatter
(.NET-specific), XmlSerializer
, DataContractSerializer
, Json.NET and protobuf-net / dotnet-protobufs would all qualify.
Serialization = putting the relevant state of the object into a streamable representation. That can mean converting it to a byte stream. This does not necessarily include copying every member variable into the stream. Classic example that is used by Joshua Bloch in Effective Java is a HashSet. You would just serialize the elements in the Hashset but not the keys.
Deserialization = restoring an object from a serial representation and ensuring the invariants of the object. Deserialization can be thought of a separate constructor for the object. In the case of the HashSet mentioned above you would create a new HashSet and then insert the values from the stream into this new data structure.
Serialization means, that you persist your object into a representation, that you can store somewhere. One way to do so is just to take the pointer to where your object is stored in the memory and write every byte as it is to a file. Since that representation is very specific to your programming language (and how it represents objects in the memory), an improvement would be to convert your object into a String representation which has a certain well known structure (like XML or JSON), so that you can
a) transfer it easier
b) Store and restore it easier
c) Since everybody knows how the format is defined, any other programs can read your object, too
So putting you object into a database is just another form of serialization, too.
Deserialization means, that you can load/restore that object again from where you saved it to.
Serialisation is, generally, the process of writing the state of an object in your runtime to the disk (but it can be anywhere), and being able to read it back again.
Effectively, storing the properties of an object into a table is a form of serialisation.
In .NET, there are other forms:
You can make up your own.
But in general, if you are saving the state of your object somewhere, and then reading it back again into a 'live' object in your runtime, you are serialising it.
Serialization
Serialization is the process of converting an object or a set of objects graph into a stream.
Deserialization
Deserialization is the process of converting back the stream into an object or a set of object graph.
Here is the some custom attributes:
[OnDeserialization] -> It is used when we need to perform some action during deserialization of the stream. [OnDeserialized] -> It is a used when we need to perform some action after deserialized the stream into an object. Such as setting object’s field value properly
Below is the 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";
}
}
Calling 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();
}
}