24

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
odiseh
  • 25,407
  • 33
  • 108
  • 151
  • 1
    Possible duplicate of [What is object serialization?](https://stackoverflow.com/questions/447898/what-is-object-serialization) – nbro Nov 27 '18 at 14:55
  • Here is short video explanation https://youtu.be/BQFYQPfERIE – VedantK May 26 '23 at 07:42

5 Answers5

38

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 / AppDomains /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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Hi, I have following questions: 1. Why do we need to serialize and object? 2. What exactly happens when serialize an object? 3. What are we gaining when serialize an object? 4. Why don't you store an object in memory instead of Serialized data? Exactly, what it is and why do we need this whole concept called Serialization? If Serialization is going to be Streamable, then what exactly the Streamable meant in computer science? Even if you provide links or anyother references, that that would be fine for me. – Vikash Sep 08 '19 at 05:59
  • 4
    @Vikash have you ever needed to *store* data? On a file system or as a raw clob/blob field in a database? Or exchange it between processes on the same machine? Perhaps even sending data over a network (local or internet) to a process on another machine? All of those things require serialization. Objects in the local process space are only reachable by that process, on that machine, for the finite lifetime of that process. – Marc Gravell Sep 08 '19 at 08:55
7

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.

jens
  • 1,763
  • 1
  • 15
  • 25
  • 1
    upvoted for etymological hints. Serialization brings an object *tree* (hierarchical) into a serial (flat) representation, without loosing semantics. – Daren Thomas Sep 01 '09 at 05:34
  • 1
    It's got nothing to do with whether or not you condense it from a 'tree' to a 'flat' representation. You can serialise to a database that is still in a tree format, and deserialise out of that. It is not relevant. – Noon Silk Sep 01 '09 at 05:35
  • 1
    I do not think that in general a db version is a serial version of an object. The data contained in an object can be split over several tables. The idea of serialization is creating a representation that can be transferred bit by bit (i.e. serially). – jens Sep 01 '09 at 06:42
5

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.

Daff
  • 43,734
  • 9
  • 106
  • 120
1

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:

  • XmlSerialization
  • BinarySerialization

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.

Noon Silk
  • 54,084
  • 6
  • 88
  • 105
  • 2
    Serialization doesn't necessarily mean writing it to disk. It just means putting it into some binary format that can be exported. A serialized object might be sent over the network rather than written to disk, for example. – Chuck Sep 01 '09 at 05:19
  • Yes, that's why at the bottom I wrote 'somewhere'. I just missed a 'generally', in the first sentence. You're right, of course. – Noon Silk Sep 01 '09 at 05:21
0

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();
    }  
}
Sheo Dayal Singh
  • 1,591
  • 19
  • 11