I want to serialize a list of objects using jsonpickle. Each of these objects contains a list of objects within them:
class ImageManipulationConfiguration:
region_list = []
def to_json(self):
return jsonpickle.encode(self.region_list, indent=4,
separators=(',', ': '))
here, region_list contains objects of this class:
class ImageManipulationRegion:
image_manipulation_element_list = []
Inside image_manipulation_element_list, there are objects of class inheriting this class:
class ImageManipulationElement
When I call my to_json function, only the top level items in the region_list are serialized, the sub object within the image_manipulation_element_list lists are not included. Is there a way to recursively include everything using jsonpickle?
you can use the
unpicklableparameter in jsonpickle to recursively include all objects. Set theunpicklableparameter toTruewhen callingjsonpickle.encodeto include all objects, even those that are not directly referenced by the top-level object.hre's how you can modify your
to_jsonmethod:this should include all objects within
region_list, including the sub-objects withinimage_manipulation_element_list.