JSON Pickle serialize object list

329 views Asked by At

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?

4

There are 4 answers

2
Adrian Makridenko On

you can use the unpicklable parameter in jsonpickle to recursively include all objects. Set the unpicklable parameter to True when calling jsonpickle.encode to include all objects, even those that are not directly referenced by the top-level object.

hre's how you can modify your to_json method:

import jsonpickle

class ImageManipulationConfiguration:
    region_list = []

    def to_json(self):
        return jsonpickle.encode(self.region_list, indent=4, separators=(',', ': '), unpicklable=True)

this should include all objects within region_list, including the sub-objects within image_manipulation_element_list.

0
Afaq Ali Shah On

Yes, you can achieve recursive serialization of objects using jsonpickle by customizing the serialization process for each of your classes. To do this, you'll need to define a custom handler for each class that specifies how to serialize its instances and their nested objects.

Here's how you can achieve this for your classes:

Define custom handlers for each class:

import jsonpickle

class ImageManipulationConfiguration:
    region_list = []

    def to_json(self):
        return jsonpickle.encode(self.region_list, indent=4, separators=(',', ': '))

class ImageManipulationRegion:
    image_manipulation_element_list = []

class ImageManipulationElement:
    pass

def encode_ImageManipulationConfiguration(obj):
    return {
        'region_list': obj.region_list
    }

def encode_ImageManipulationRegion(obj):
    return {
        'image_manipulation_element_list': obj.image_manipulation_element_list
    }

def encode_ImageManipulationElement(obj):
    # Define how to serialize ImageManipulationElement objects if needed
    pass

# Register custom handlers
jsonpickle.handlers.registry.register(ImageManipulationConfiguration, encode_ImageManipulationConfiguration)
jsonpickle.handlers.registry.register(ImageManipulationRegion, encode_ImageManipulationRegion)
jsonpickle.handlers.registry.register(ImageManipulationElement, encode_ImageManipulationElement)

Now, when you call to_json on an instance of ImageManipulationConfiguration, it will include the custom serialization logic for each class:

config = ImageManipulationConfiguration()
# Populate config.region_list with instances of ImageManipulationRegion and nested ImageManipulationElement objects

json_string = config.to_json()
print(json_string)

By providing custom serialization logic for each class, you can make sure that nested objects are properly serialized when you call to_json. This allows you to achieve recursive serialization with jsonpickle.

0
Pycm On

Answer :-

  1. Use max_depth=4

  2. And change separaters Lile below.

def to_json(self):
    return jsonpickle.encode(self.region_list, indent=4,
                                 separators=(', ', ': '),max_depth=4)

Or use this below code.


class BoxHandler(jsonpickle.handlers.BaseHandler):
def flatten(self, obj, data):
    return [self.context.flatten(x,reset=False) for x in obj.contents]

Reference

0
AudioBubble On

Yes, you can use the jsonpickle.set_encoder_options function to set the indent and separators parameters for all future encodes.

import jsonpickle
from jsonpickle.picklers.base import Pickler
from jsonpickle.util import encode

class ImageManipulationConfiguration:
region_list = []

 def to_json(self):
    return encode(self.region_list, Pickler, indent=4, separators=(',', ': '))

class ImageManipulationRegion:
image_manipulation_element_list = []

class ImageManipulationElement:
pass

jsonpickle.set_encoder_options('json', indent=4, separators=(',', ': '))

immc = ImageManipulationConfiguration()
immc.region_list.append(ImageManipulationRegion())
immc.region_list[0]. 
image_manipulation_element_list.append(ImageManipulationElement( 
   ))

 print(immc.to_json())

This will output:

[
{
    "image_manipulation_element_list": [
        {
            "__setstate__": null
        }
    ]
}
]

The sub-objects within image_manipulation_element_list are now included in the encoded JSON.