What is Serialization?
Asked Answered
H

16

226

I am getting started with Object-Oriented Programming (OOP) and would like to know: what is the meaning of serialization in OOP parlance?

Hastie answered 11/3, 2009 at 5:3 Comment(3)
Also take a look at this article which explains why not to use serialization codeproject.com/KB/dotnet/noserialise.aspxBrilliancy
That article is complete BS, hence its rating.Spendthrift
Serialization has nothing to do with OOP. You can do serialization in C or assembly or Haskell or Lisp - and in fact will have to do so in any of those languages in order to solve the same problems as they solve in OOP languages.Branch
F
223

Serialization is the process of turning an object in memory into a stream of bytes so you can do stuff like store it on disk or send it over the network.

Deserialization is the reverse process: turning a stream of bytes into an object in memory.

Featherhead answered 11/3, 2009 at 5:6 Comment(9)
Also, the verbs Marshal and Unmarshal are synonymous with Serialize and Deserialize.Padus
Isn't an object in memory already represented as bytes at the lowest level?Easter
An object in memory will be formatted by the compiler, OS and/or hardware. Change your compiler and you change your format. A serialized object will have a format defined by your code, so you can guarantee the format. This is also helpful when sending objects across a network -- the receiving machine may have a completely different architecture (and thus in-memory representation).Featherhead
So it turns it into a string?Calcification
A string can be a valid format, but it doesn't have to be a string. Strings themselves have different formats (ASCII, UTF8, UTF16, EBCDIC...) and actually demonstrate the concept fairly well. The string (an in-memory object) abc would be serialized as 0x61 0x62 0x63 (ASCII) or 0x00 0x61 0x00 0x62 0x00 0x63 (UTF16) -- with or without things like NUL terminators or encoded lengths.Featherhead
@NoName, Yes most of the time, but It doesn't have to be a string. It can be Integer or Binary number. But string happens to be an easy way of a deal with humans.Giguere
Is adding encoding format to a string an example of a valid serialization (given receiver understands encoding)?Vandalize
@SergeyBushmanov Yes.Featherhead
"Serialization is a process of encoding data to send it via network or save it to disk". At the same we all know issues exist when text files are saved (serialized?) to disk without specifying encoding, which leads to possible problems reading non-standard (non UTF-8/ non ASCII) encodings. Does it mean saved file was still improperly serialized?Vandalize
P
138

What is Serialization?

Explanation via Picture Analogy:

Rex, my dog, is serialised into data, which is transmitted further into 1s and 0s

Summary:

Serialization means transforming something (e.g. my dog Rex) into a series of 1s and 0s - which can be transported / stored etc. My friends overseas can then translate those 1s and 0s back into a perfect representation of a puppy (de-serialization).

Clarification of Analogy

Friends, this is an analogy. I don't think you can actually serialise a puppy (yet). You would serialise a data structure, or some other complex object.

(I wrote this answer so you can understand the basic concept, in a fun way, in 5 seconds, and move on - it was never meant to be an academic explanation, nor is it 100% perfect. Consider Wikipedia's entry on serialization or the other answers or feel free to write your own - you'll realise very quickly that explaining something perfectly is impossible, but caviling is super easy!)

Pentarchy answered 1/2, 2018 at 1:25 Comment(0)
G
128

Simply speaking Serialization is a process of converting an Object into stream of bytes so that it can be transferred over a network or stored in a persistent storage.

Deserialization is the exact opposite - Fetch a stream of bytes from network or persistence storage and convert it back to the Object with the same state.

The thing to understand is how those stream of bytes are interpreted or manipulated so that we get the exact same Object/ same state. There are various ways to achieve that. Some of them are -

  1. XML: Convert Object to XML, transfer it over a network or store it in a file/db. Retrieve it and convert it back to the object with same state. In Java we use JAXB(Java architecture for XML binding) library.(From java 6 it comes bundled with JDK).
  2. JSON: Same can be done by converting the Object to JSON (JavaScript Object notation). Again there is GSON library that can be used for this.
  3. Or we can use the Serialization that is provided by the OOP language itself. For example, in Java you can serialize an Object my making it implement Serializable interface and writing to Object Stream.
Gluttonous answered 30/8, 2014 at 18:36 Comment(4)
@AniketThakur A very good explanation, can you also provide a link for me to learn about JSON from scratch, as I don't know anything about itTineid
whenever we talk about serialization why we always refer to Object only. Cant we use serialization in Functional Language where we dont have any object rather than we are using files to transfer over the network.Nabal
First of all, why do we need to convert an object to stream of bytes and back ? Why isn't that an implicit operation for a user who's trying to do that ?Jewelfish
If we can use JSON or XML then why we need or saying that converting them into bytes of stream? And then we only store them into JSOn or XML instead of converting them into bytes.Centigram
C
21

Check this out, this will give you a good explanation:

http://en.wikipedia.org/wiki/Serialization

I think the most common use of the term serialization has to do with converting a binary object into an XML (or other string) representation so that it can be stored in a database/file or sent across a network in a web service call. Deserialization is the reverse process - converting an XML/string back into an object.

EDIT: Another term you might come across is marshalling/unmarshalling. Marshalling is basically the same concept as serializing, and unmarshalling is the same as deserializing.

Cadman answered 11/3, 2009 at 5:6 Comment(1)
Not necessarily just XML, it can be any representation, even a binary representationOs
A
17

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

...

This illustration shows the overall process of serialization

Overall process of serialization

...

Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications

From https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/

(emphasis mine)

Antidisestablishmentarianism answered 1/2, 2018 at 5:47 Comment(0)
P
8

Serialization is the process of converting unordered data (such as an object) into a series of tokens which can be used later to reconstruct the original data. The serialized form is most often a string of text, but doesn't have to be.

Puissance answered 26/2, 2010 at 11:12 Comment(0)
O
2

serialization is converting an object to storable bit sequence.

so you can save this sequence to a file, db or send over network.

later you can deserialize it to the actual object and reuse it whenever you want.

Web Services and AJAX is the most common example of serialization. The objects serialized before sending the response to the client.

Odense answered 11/3, 2009 at 5:10 Comment(0)
C
2

Serialization is the process of converting a Java,C# or any other (OOP languages) supported object to a transportable form. This way it be transported over the network or stored on a disk. For a class to be serializable, it must implement serializable interface.

Cohune answered 23/9, 2017 at 1:48 Comment(2)
not only limited to Java or C#, in most of the programming language we use serialization. E.g. in python pickle module is used for the same.Nabal
Not only limited to OOP either.Ambry
H
1

serialization is nothing but transfering the java supported object to file supported form

(OR)

converting java supported form to network supported form..the main scope of the serialization is nothing but to transfering the data from one layer to the another layer...only serialized objects we can send over the network..

Heptane answered 8/11, 2012 at 10:12 Comment(0)
K
1

When instantiating (constructing) the actual object(the thing) from a class (blueprint) there is a need to save the object (thing) by serializing it (breaking it down to its basic atomic structure) to a space in memory. (Kind of like Star Treks Transporter). You break the thing down into it stream of information that can be transported somewhere and stored. Then when you want to reconstruct the thing you just pull the atomically stored instance back into the object. Different from instaniation.

Keyek answered 24/12, 2013 at 10:35 Comment(0)
B
0

Serialization is turning data into a linear "string" of bytes.

Others have said more or less the same thing, but I stress that computer models require that data fits in the one-dimensionally addressed RAM or persistent storage.

Most things that are "data" are inherently serializable (even if you must reduce the abstract model to a linear one); not serializable are say a network connection or a complicated state-based machine like a parser.

Bateau answered 11/3, 2009 at 6:55 Comment(0)
O
0

serialization has to do with converting a binary object into an XML (or other string) representation so that it can be stored in a database/file or sent across a network in a web service call. Deserialization is the reverse process - converting an XML/string back into an object.

Olivo answered 26/2, 2010 at 10:32 Comment(0)
B
0

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed.

enter image description here

Bahuvrihi answered 9/7, 2022 at 6:59 Comment(0)
G
-1

Serialization is when object (a chunk of memory) translated in a form when object's state could be saved in file (as an example).

Just treat it as making cookies - object is a dough, cookie - is a serialized dough.

So by "serializing" you can send cookie to your friend.

Something like that :-)

Gardener answered 11/3, 2009 at 5:7 Comment(1)
...except cookies can't be turned back into dough (deserialized).Puissance
T
-1

Serialization is the process of converting an object into binary data stream so that it can be stored in a file or send across a network where it can be resurrected back to the same object.

This document should help you understand Java serialization in detail.

Thermography answered 29/7, 2016 at 2:26 Comment(0)
L
-3

simply just consider the following idea to understand it.

Serialization:

"hello world".split() returns ['hello', 'world']

De-serialization:

" ".join(['hello', 'world']) returns "hello world"
Leggy answered 17/3, 2021 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.