46

I've seen the term "serialized" all over, but never explained. Please explain what that means.

sth
  • 222,467
  • 53
  • 283
  • 367
Moshe
  • 57,511
  • 78
  • 272
  • 425
  • 1
    If anyone could give a detailed example, it'd be great. When I look up this topic, I am also confused. The examples extend as far as `pickle.dump(object, file)` and `pickle.load(file)` (using Python, for instance), which honestly is totally useless in helping one to understand. – Mike Williamson Mar 12 '14 at 21:30

6 Answers6

63

Serialization usually refers to the process of converting an abstract datatype to a stream of bytes (You sometimes serialize to text, XML or CSV or other formats as well. The important thing is that it is a simple format that can be read/written without understanding the abstract objects that the data represents). When saving data to a file, or transmitting over a network, you can't just store a MyClass object, you're only able to store bytes. So you need to take all the data necessary to reconstruct your object, and turn that into a sequence of bytes that can be written to the destination device, and at some later point read back and deserialized, reconstructing your object.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • 16
    Class objects are abstract datatypes. Abstract as in "not a primitive data type, but something representing an abstract concept". A `Car` class is an abstract datatype. It represents something abstract in the program. There are no cars in it. Just data providing the abstraction of one. And that abstraction must be torn down in order to serialize it. You can't store a car to a file, but you can store the data necessary to reconstruct the `Car` class instance. – jalf Jan 31 '10 at 05:56
  • 7
    No I'm not. I'm not talking about an abstract class, which I agree is something entirely different. An abstract datatype is pretty much a more general term for a class (except it nicely covers the equivalents in classless languages). A class is an abstraction, and it is a datatype. Calling it an abstract datatype is not an idea I just came up with. :) – jalf Jan 31 '10 at 06:13
15

Serialization is the process of taking an object instance and converting it to a format in which it can be transported across a network or persisted to storage (such as a file or database). The serialized format contains the object's state information.

Deserialization is the process of using the serialized state to reconstruct the object from the serialized state to its original state.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
6

real simple explanation, serialization is the act of taking something that is in memory like an instance of a class (object) and transforming into a structure suitable for transport or storage.

A common example is XML serialization for use in web services - I have an instance of a class on the server and need to send it over the web to you, I first serialize it into xml which means to create an xml version of that data in the class, once in xml I can use a transport like HTTP to easily send it.

There are several forms of serialization like XML or JSON.

keithwarren7
  • 14,094
  • 8
  • 53
  • 74
6

There are (at least) two entirely different meanings to serialization. One is turning a data structure in memory into a stream of bits, so it can be written to disk and reconstituted later, or transmitted over a network connection and used on another machine, etc.

The other meaning relates to serial vs. parallel execution -- i.e. ensuring that only one thread of execution does something at a time. For example, if you're going to read, modify and write a variable, you need to ensure that one thread completes a read, modify, write sequence before another can start it.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • I'm glad someone mentioned the other meaning of 'serialization' - I can remember being confused the first time I came across the "spit an object out to a file" meaning since I had been using it to mean "critical sections" for a long while before that. – Michael Burr Jan 31 '10 at 07:02
  • 1
    No - "serialized" can also be used to mean "serializing *access* to data or code". "Marshalling" is the term I used for getting an object's data to/from a file (or other stream) before the term "serialization" came to be used for that in the context of Java/.NET (at least for me). – Michael Burr Jan 31 '10 at 07:08
3

What they said. The word "serial" refers to the fact that the data bytes must be put into some standardized order to be written to a serial storage device, like a file output stream or serial bus. In practice, the raw bytes seldom suffice. For example, a memory address from the program that serializes the data structure may be invalid in the program that reconstructs the object from the stored data. So a protocol is required. There have been many, many standards and implementations over the years. I remember one from the mid 80's called XDR, but it was not the first.

Jive Dadson
  • 16,680
  • 9
  • 52
  • 65
1
  • You have data in a certain format (e.g. list, map, object, etc.)
  • You want to transport that data (e.g. via an API or function call)
  • The means of transport only supports certain data types (e.g. JSON, XML, etc.)
  • Serialization: You convert your existing data to a supported data type so it can be transported.

The key is that you need to transport data and the means by which you transport only allows certain formats. Your current data format is not allowed so you must "serialize" it. Hence as Mitch answered:

Serialization is the process of taking an object instance and converting it to a format in which it can be transported.

Brady Dowling
  • 4,920
  • 3
  • 32
  • 62