Related Articles

Related Articles

Modules available for Serialization and Deserialization in Python
  • Last Updated : 01 Oct, 2020

Python provides three different modules which allow us to serialize and deserialize objects :  

  1. Marshal Module
  2. Pickle Module
  3. JSON Module

1. Marshal Module: It is the oldest module among these three. It is mainly used to read and write the compiled byte code of Python modules. Even we can use marshal to serialize Python objects, but it is not recommended to use. It is mainly used by the interpreter and the official documentation warns that the python maintainers may modify the format in backward-incompatible ways.

Note: It is recommended to never unmarshal data received from an untrusted or unauthenticated source.  

Example : 

Python3



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import marshal  
  
data = {'name': 'sunny','age': 34,'address': 'nasik'
  
# dumps() return byte object stored in variable 'bytes' 
bytes = marshal.dumps(data)    
print('After serilization : ', bytes) 
  
# loads() convert byte object to value
new_data = marshal.loads(bytes)    
print('After deserilization : ', new_data)

chevron_right


Output:

After serilization :  b’\xfb\xda\x04name\xda\x05sunny\xda\x03age\xe9″\x00\x00\x00\xda\x07address\xda\x05nasik0′

After deserilization :  {‘name’: ‘sunny’, ‘age’: 34, ‘address’: ‘nasik’}

2. Pickle Module: It is another way to serialize and deserialize Python objects. It serializes the Python object in a binary format, due to which it is not human-readable. It is faster and it also works with custom-defined objects. The Python pickle module is a better choice for serialization and deserialization of python objects. If you don’t need a human-readable format or if you need to serialize custom objects then it is recommended to use the pickle module.

Example: 

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importinig the module
import pickle
  
data = {'st_name': 'Sunny', 'st_id': '9607', 'st_add': 'Nasik'}
with open('data.pickle', 'wb') as f1 :         
    
    # converts object to byte stream(list, dict, etc.) 
    pickle.dump(data, f1)       
    print('Pickling Completed...')
  
with open('data.pickle', 'rb') as f2 :
    print('Unpickling the data : ')
      
    # converts byte stream(generated through pickling)back into object
    data = pickle.load(f2) 
    print(data)

chevron_right


Output:

Pickling Completed...
Unpickling the data : 
{'st_name': 'Sunny', 'st_id': '9607', 'st_add': 'Nasik'}

3. JSON Module: It is a newly created module. It allows us to work with standard JSON files. JSON is a widely used format for data exchange and it is very convenient. It is human-readable and language-independent, and it’s lighter than XML. Using the JSON module we can serialize and deserialize several standard Python types like bool, dict, int, float, list, string, tuple, none etc. The JSON module and XML are a good choice if we want to interoperability among different languages.



Example: Deserializing

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import json   
  
# JSON string 
students = '{"id":"9607", "name": "Sunny", "department":"Computer"}'
    
# convert string to Python dict 
student_dict = json.loads(students) 
print(student_dict) 
    
print(student_dict['name'])
print('Deserialization Completed.')

chevron_right


Output:

{"id": "9607", "name": "Sunny", "department":"Computer"}
Sunny
Deserialization Completed.

Example: Serializing

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import json    
  
data =
  "id": "877"
  "name": "Mayur"
  "depatment": "Comp"
     
# Serializing json  
json_object = json.dumps(data) 
print(json_object)
print('Serilization Completed.')

chevron_right


Output:

{"id": "877", "name": "Mayur", "depatment": "Comp"}
Serilization Completed.

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :