Internal Python Object Serialization using marshal

In this tutorial, you are going to learn about the Internal Python Object Serialization using the marshal module.

What is Serialization?

Serialization means converting an object into a byte stream and it is further stored like a file. To achieve Serialization in the fastest way we use the marshal module. It simply says Serialization means encoding the data.

What is the marshal module?

It consists of functions that are used to read, write and open the files in binary format. Marshaling the data that is used to convert internal form to external form which means  Serialization using the marshal module.

What is Deserialization?

Deserialization means exactly the reverse process of Serialization that is converting the byte stream into an object. It decodes the file which is already stored in the memory in encoded format. It simply says Deserialization means decoding the data.

#Serialization
import marshal
serializeddata={1:'One','name':list('Harini'),2.2:1+2j,(1,2,3):u'serialize'}
bytes = marshal.dumps(serializeddata)
print(bytes)


#Deserialization

import marshal
serializeddata={1:'One','name':list('Harini'),2.2:1+2j,(1,2,3):u'serialize'}
bytes = marshal.dumps(serializeddata)
deserializeddata=marshal.loads(bytes)
print(deserializeddata)

Below is the output of the Serialization and Deserialization:

Serialization and Deserialization Output

Program Explanation:

In this Program, we know about Internal Python Object Serialization

First of all, import the marshal module.

Initialize the data which you want to serialize.

Store some data in serialized data it is stored in marshal.dumps(). This function returns the string that would be written in a file.

Print data in encoded format.

The marshal.loads()  function converting byte stream into an object and it converts a string into value just like deserialize the data.

Print the original data which is deserialized.

This is about the explanation of Internal Python object Serialization using marshal.

Leave a Reply