Home java Serialization in plain words

Serialization in plain words




Met this “serialization” many times on different resources, often associated with JSON.

Please explain in simple terms what serialization is, where and why is it used?

Answer 1, authority 100%

Serialization is the transformation of an object or tree of objects into a format so that later these objects can be restored from this format. Used, for example, to save the state of the program (that is, some of its objects) between launches. Or to transfer data between different instances of a program (or different programs), for example, over a network.

The main idea is that the serialized format is a set of bytes or a string that can be easily saved to disk or transferred to another process or, for example, over a network, as opposed to the object itself. This means that the task of saving an object / group of objects is reduced to the simple task of saving a set of bytes or a string.

JSON is one of the popular serialization formats, it is textual, lightweight, and human-readable.

Example: if you have a class

class Test
  int length;
  String name;
  public Test (int length, String name)
    this.length = length;
    this.name = name;

An object of this class in serialized form can have the form

{"length": 25, "name": "Name"}

Serialization (and deserialization) itself can be done manually, or use the appropriate libraries / frameworks.

There are also binary serialization formats.

Answer 2, authority 15%

Serialization in general is the process of storing an object as a sequence of bytes so that in the future this sequence can be used to restore the original object. In a particular case, it can be saving to a text string, although this is far from necessary.

If an object is serialized to a text string, then there can be different formats, including XML and the JSON mentioned in the question. The latter is the most popular for this purpose lately, because JSON is already a representation of an object in the same format that an object is created in JavaScript. Actually, JSON is translated as JavaScript Object Notation.

Serialization can be used to store objects on disk, to transfer them over a network, or to transfer an object to another process.

The reverse process of serialization is called deserialization. In case another process receives a JSON string, then it must deserialize the string into an object.

Answer 3, authority 11%

If on cats
You are writing a tamagotchi cat emulator. You have an object of class cat

class Cat {
  private int age;
  private int weight;
  // other cat logic

You want the same cat to continue living its life the next time you launch the application, and not recreate it. To do this, you implement serialization / deserialization of the cat – that is, maturation / loading. How – as it is convenient for you. You can make it Serializable and store it in binary form, you can save it to a text file as JSON (JavaScript Object Notation), you can store it in the database. The main thing is that you somehow save its state (in this case, the fields), and then, when you need it, load them.
Also, the serialized cat can be transmitted over the network to the server, for example. That is, you pass its state, and the server will create a new object of the Cat class and set it to this state (age and weight).

Answer 4, authority 7%

Serialization is used to store entities as a string. For example, you can take an object, serialize and write to a database.

Json – data presentation format. It can represent objects or arrays. These entities can also be serialized. If you output the serialized object to Json, then you can get the string of the serialized object.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions