D
D
DollyPapper2017-09-08 12:59:42
Python
DollyPapper, 2017-09-08 12:59:42

Why is serialization necessary?

Serialization (in programming) is the process of translating a data structure into a sequence of bits. The inverse to the serialization operation is the deserialization (structuring) operation - restoring the initial state of the data structure from the bit sequence.

Hello everyone. Explain with a simple example why you need not only serialization, but the pickle module || cPickle in python? For example, when writing to a file or passing data to a socket, we can specify the b flag in the same file so that the recording is done in binary mode. Why do we need to pre-serialize an object using the pickle module?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
Sergey Gornostaev, 2017-09-08
@DollyPapper

Suppose you have a complex hierarchy of classes, each of which has a dozen fields. Some fields refer to objects of other classes or, even worse, contain collections of objects.
When using a serializer , you only need one line to save a dictionary containing objects from this hierarchy to a file: pickle version.

A
Anton Kuzmichev, 2017-09-08
@Assargin

Serialization:
Serialization guarantees (for supported data structures) that after the serialization/deserialization process you will get the same data structures.
Without it, you would need to manually create your own protocols and methods for converting data into a form suitable for transmission somewhere.
Well, the pickle module is a built-in implementation of serialization, but at the same time - one of many. You can use e.g. json, or yaml. Anything that will translate ordinary data structures into a form suitable for transfer (string, byte string) and return (deserialize) back.

S
Saboteur, 2017-09-08
@saboteur_kiev

For example, you need to pass a data structure to a remote server.
The data structure looks something like this
struct {
int age=17;
stringname="Vasya";
date birthday="01/01/2001"
}
We are going to send it to a remote server with a regular HTTP request, in which, in principle, you can attach XML, or you can send it just in one line right in the header. To do this, we can serialize our data structure into a string, transfer it there, unpack it there and get the structure right away.
The difference is that on the other side we will not "parse" unknown data, but rather deserialize it.

A
Anton, 2017-09-08
@Eridani

As a banal example: Try to shove the array into the database without scattering its keys over your fields.
Rammed into serialization, written to the database in one line, if necessary - got it, unserialized -> use

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question