0

I'm not quite sure I'm using the right wording in my researches -- if that's the case, please let me know, I may have missed obvious answers just because of that -- but I'd like to serialize (i.e. convert to a dictionary or JSON structure) both the main (outer) and inner class arguments of a class.

Here's an example:

class Outer(object):
    
    def __init__(self, idx, array1, array2):
        self.idx = idx
        # flatten individual values:
        ## unpack first array
        self.prop_a = array1[0]
        self.prop_b = array1[1]
        self.prop_c = array1[2]
        ## unpack second array
        self.prop_d = array2[0]
        self.prop_e = array2[1]
        self.prop_f = array2[2]

        # Nest elements to fit a wanted JSON schema
        class inner1(object):
            def __init__(self, outer):
                self.prop_a = outer.prop_a
                self.prop_b = outer.prop_b
                self.prop_c = outer.prop_c

        class inner2(object):
            def __init__(self, outer):
                self.prop_d = outer.prop_d
                self.prop_e = outer.prop_e
                self.prop_f = outer.prop_f
        
        self.inner_first = inner1(self)
        self.inner_second = inner2(self)
   
    def serialize(self):
        return vars(self)

Now I can call both:

import numpy as np

obj = Outer(10, np.array([1,2,3]), np.array([4,5,6]))
obj.prop_a # returns 1, or
obj.inner_first.prop_1 # also returns 1

But when I try to serialize it, it prints:

vars(obj) # prints:

{'idx': 10,
 'prop_a': 1,
 'prop_b': 2,
 'prop_c': 3,
 'prop_d': 4,
 'prop_e': 5,
 'prop_f': 6,
 'inner_first': <__main__.Outer.__init__.<locals>.inner1 at 0x7f231a4fe3b0>,
 'inner_second': <__main__.Outer.__init__.<locals>.inner2 at 0x7f231a4febc0>}

where I want it to print:

vars(obj) # prints:

{'idx': 10,
 'prop_a': 1,
 'prop_b': 2,
 'prop_c': 3,
 'prop_d': 4,
 'prop_e': 5,
 'prop_f': 6,
 'inner_first': {'prop_a': 1, 'prop_b': 2, 'prop_c': 3},
 'inner_second': {'prop_d': 4, 'prop_e': 5, 'prop_f': 6}}

with the 'inner_first' key being the actual results of vars(obj.inner_first), and same thing for the 'inner_second' key.

Ideally I'd like to call the serialize() method to convert my object to the desired output: obj.serialize()

I feel I'm close to the results but I can simply not see where I must go to solve this task.

At the really end, I wish I could simply:

obj = Outer(10, np.array([1,2,3]), np.array([4,5,6]))
obj.serialze()

{
  'inner_first': {
    'prop_a': 1,
    'prop_b': 2,
    'prop_c': 3
  },
  'inner_second': {
    'prop_d': 4,
    'prop_e': 5,
    'prop_f': 6
  }
}

in order to basically fit a given JSON structure that I have.

Info: this thread helped me to build the inner classes.

Also note that this question only embeds two "layers" or "levels" of the final structure, but I may have more than 2.

swiss_knight
  • 5,787
  • 8
  • 50
  • 92
  • "But when I try to serialize it" ... `vars` is not serialization. Why do you think it is? – juanpa.arrivillaga May 15 '22 at 09:48
  • Indeed. But I was always using `vars()` on objects to dump them as `dict`. And as all my objects, up to now, were only having a 1-level structure, it was perfectly equivalent to a serialization to me. – swiss_knight May 15 '22 at 19:03

0 Answers0