How do we construct a JSON string which contains all the necessary details which are part of any python objects (primitive data types, custom classes, enums, list of classes, dictionary of different objects).
Later the same JSON string should be used to re-construct the original python object.
- We DON'T know the type of object we need to construct before hand. Depends of the json structure which is present.
- Should support complex data structure supported such as dictionaries, list etc.
- JSON should be human readable
Already Tried
- I have tried the standard JSON dumps and loads. But they don't re-construct the object back.
- Pickle library can be use for serialization and deserialization of the objects but they don't produce the results in the user readable json format.
- creating of json hooks for the json loads and dumps libraries is not a viable option as we have n number of unique classes/objects and generating of hooks for all the n objects/classes is not a feasible option.
- can use pickle and json libraries to encode and decode but they will NOT be storing the objects in JSON format.
- Tried the namedtuple but not able to get the exact functionality which is needed.
Example:
class Phone:
def __init__(self, number, type):
self.phone_numer = number
self.number_type = type
class Person:
def __init__(self, first_name, last_name, phone_numbers):
self.first_name = first_name
self.last_name = last_name
self.phone_numbers = phone_numbers
number_1 = Phone(9999999999, "Home")
number_2 = Phone(8888888888, "Work")
number_3 = Phone(7777777777, "Office")
person_1 = Person("Tom", "Timothy", [number_1, number_2, number_3])
person_2 = Person("Tim", "Jones", [])
person_list = [ person_1, person_2 ]
# convert "person_list" to JSON document and then convert it back to python object which retains its object structure.
Sample Example JSON string which might be helpful for reconstructing the same object.
{
"<class 'list'>": [
{
"<class '__main__.Person'>": {
"first_name": "Tom",
"last_name": "Timothy",
"<class 'list'>": [
{
"<class '__main__.Phone'>": {
"phone_number": 9999999999,
"number_type": "home"
}
},
{
"<class '__main__.Phone'>": {
"phone_number": 8888888888,
"number_type": "work"
}
},
{
"<class '__main__.Phone'>": {
"phone_number": 7777777777,
"number_type": "office"
}
}
]
}
},
{
"<class '__main__.Person'>": {
"first_name": "Tim",
"last_name": "Jones",
"<class 'list'>": []
}
}
]
}
Any one has encountered such problem ? or any ideas if any libraries which helps us achieve this ?