What isPickle or Serialization and UnPickle or DeSerialization in python how to define.The process of converting an object from python
Object Serialization
The process of converting an object from python supported form to either file supported
form or network supported form, is called serialization (Marshalling or pickling) The
process of converting an object from either file supported form or network supported
form to a python-supported form is called deserialization (Unmarshalling or
unpickling).
- Object Serialization by using Pickle
- Object Serialization by using JSON
- Object Serialization by using YAML
Object Serialization by using Pickle:
We can perform serialization and deserialization of an object wrt file by using the pickle
module. It is Python's inbuilt module.
pickle module contains dump() function to perform Serialization(pickling).
pickle.dump(object,file)
pickle module contains load() function to perform Deserialization (unpickling).
object = pickle.load(file)
Program to perform pickling and unpickling of Employee Object:
ex:1
import pickle
class Employee:
def __init__(self,eno,ename,esal,eaddr):
self.eno = eno
self.ename=ename
self.esal=esal
self.eaddr=eaddr
def display(self):
print ('ENO: {}, ENAME :, ESAL :, EADDR:}'. format (self.eno, self.
ename,self.esal,self.eaddr))
e=Employee(100,'Durga',1000,'Hyderabad')
ex:2
with open('emp.dat','wb') as f:
pickle.dump(e,f)
print('Pickling of Employee object completed')
ex:3
with open('emp.dat','rb') as f:
obj = pickle.load(f)
print('Unpickling of Employee object complected')
print('Printing Employee Information:')
obj.display()
Object Serialization by using JSON
Importance of JSON:
JSON JavaScript Object Notation
Any programming language can understand JSON. Hence JSON is the most commonly used
message format for applications irrespective of programming language and platform. It is
very helpful for interoperability between applications.
It is a human-readable format.
It is lightweight and required less memory to store data.
Eg:
Java Application sends a request to Python application
Python application provides the required response in JSON form.
Java applications can understand JSON form and can be used based on its requirement.
What is JSON?
Python Data Types vs JavaScript Data Types
- int number
- float number
- list array
- dict object(JSON)
- str string
- True true
- false false
- None null
Comments
Post a Comment