0

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 ?

  • listing by type : https://stackoverflow.com/a/74601132/18519656 – burak Dec 01 '22 at 14:31
  • we don't need to list by type. Rather we need to preserve the object structure onto JSON and then load it back to the python object. And we don't have the classes already available with us. The class are generated through the thrift file. – Jubin Mathew Dec 01 '22 at 15:36
  • The encoder and decoder which I was searching needs to encode and decode any type of objects. And at runtime we will only know the type of the object. I found this post https://www.python-engineer.com/courses/advancedpython/11-json/ which was exactly sharing the custom encoding and decoding of the objects to and from JSON format. hope this is helpful to others. – Jubin Mathew Dec 03 '22 at 04:14

0 Answers0