Python: JSON Serialization / DeSerialization
JaveScript Object Notation (JSON) has become the de-facto convention when passing data through Internet web services, primarily those using the Representational State Transfer (REST) APIs. Originally developed for JavaScript JSON is a simple, human readable format of key-value pairs that is now supported by all major languages and platforms. The following is a simple JSON containing the Name, Age and City of a person.
{"name": "John Doe",
"age": 35,
"country": "US"}
A popular way of transferring data is to serialize/de-serialize data objects using JSON functionality supported in the platform. For example, if we have the following class in Python:
class dataClass:
def __init__(self):
self.name = "John Doe"
self.age = 35
self.country = "US"
The serialized version of this class in JSON is the one we gave earlier,:
{"name": "John Doe",
"age": 35,
"country": "US"}
While JSON serialization is the process of converting a class object to its JSON form, the reverse process is called de-serialization. Using de-Serialization, a Python object's values can be assigned through a JSON string.
The module that provides JSON support in Python is json. Using the dumps and loads function of this module data is serialized and de-serialized, respectively. An in-built special attribute __dict__ which is available for any user defined class also important. The __dict__ attribute returns all member variables of a Python class as a dictionary (key-value pairs) converted to a string representation, and once you have that getting the JSON out of it is trivial (as JSON is composed of key-value pairs).
The dumps function of the json module takes a string form of a dictionary and converts it to a JSON and therefore is used in serialization. For de-serialization the __dict__ again comes to the rescue. If we assign a string representation of a dictionary to __dict__ it will assign the values to the individual elements of the class. This is pretty neat ! The loads function converts the JSON to a string form of a dictionary, and assigning the latter to the __dict__ attribute results in our object being recreated.
The following code shows json/de-serialization in action. Inline comments further elaborate on the purpose of important lines of code.
# If json module is missing type this in the python console:
# pip install json
import json
class dataClass:
def __init__(self):
# Define initial state
# NOTE: This initial state will get overwritten when we
# deserialize from JSON
self.name = "Chet"
self.age = 38
self.country = "US"
class demoJSONClass:
def __init__(self):
self._dictStr = {"One": 1, "Two": 2, "Three": 3, "Four": 4}
self._dictNumber = {1: "One", 2: "Two", 3: "Three", 4: "Four"}
def convertToJSONDict(self):
print('--------------- In convertToJSONDict()')
print('This is json of _dictStr')
print(json.dumps(self._dictStr))
print('This is json of _dictNumber')
print(json.dumps(self._dictNumber))
def serializeToJSONClass(self):
print('--------------- In serializeToJSONClass()')
dc = dataClass()
print('This is the string representation of dataClass using __dict__')
print(dc.__dict__)
print('This is a json of dataClass')
# Convert the list into json using dumps
print(json.dumps(dc.__dict__))
def deSerializeFromJSONClass(self,serializedJson):
print('--------------- In deSerializeFromJSONClass')
dc = dataClass()
print('This is the string representation of the JSON')
print(json.loads(serializedJson))
# Load the serialized string into the object list using loads
dc.__dict__ = json.loads(serializedJson)
print('This is string representation of the deSerialized dataClass using __dict__')
print(dc.__dict__)
print('These are individual values of the deSerialized dataClass')
print('Name:' + dc.name)
print('Age:' + str(dc.age))
print('Country:' + str(dc.country))
# main() function which contain the high level routines
def main():
dsc = demoJSONClass()
dsc.convertToJSONDict()
dsc.serializeToJSONClass()
# Pass the JSON to de-serialize from
dsc.deSerializeFromJSONClass('{"name": "John", "age": 61, "country": "Canada"}')
# Call the main() function
main()
The output of the above code as seen in the Python console is given below:
--------------- In convertToJSONDict()
This is json of _dictStr
{"One": 1, "Two": 2, "Three": 3, "Four": 4}
This is json of _dictNumber
{"1": "One", "2": "Two", "3": "Three", "4": "Four"}
--------------- In serializeToJSONClass()
This is the string representation of dataClass using __dict__
{'name': 'Chet', 'age': 38, 'country': 'US'}
This is a json of dataClass
{"name": "Chet", "age": 38, "country": "US"}
--------------- In deSerializeFromJSONClass
This is the string representation of the JSON
{'name': 'John', 'age': 61, 'country': 'Canada'}
This is string representation of the deSerialized dataClass using __dict__
{'name': 'John', 'age': 61, 'country': 'Canada'}
These are individual values of the deSerialized dataClass
Name:John
Age:61
Country:Canada
Process finished with exit code 0
Comments
Post a Comment