views:

26

answers:

1

Using the json module in python 2.6, I'm experiencing some unexpected behavior when passing a function to object_hook. I'm attempting to turn a json object into a class defined in my code. This class requires that instances of a different class as arguments. Something like this:

class OuterClass:
    def __init__(self, arg, *InnerClass_instances):
        self.arg = arg
        self.class2 = InnerClass_instances

class InnerClass:
    def __init__(self, name, value):
        self.name = name
        self.value = value 

It seems to me, that if to create a JSON object that contains all of the information for an instance of OuterClass, it would be necessary to include all of the information for each instance of InnerClass that would be passed to it. So, I thought I could write two functions to parse JSON object, one for each class, and have the function creating OuterClass, call the function that creates InnerClass instances. Something like this:

def create_InnerClass(dct):
    if '__InnerClass__' in dct:
        name = dct['name']
        value = dct['value']
        return InnerClass(name, value)

def create_OuterClass(dct):
    if '__OuterClass__' in dct:
        arg = dct['arg']
        InnerClass_instances = [create_InnerClass(dct2) \
                                for dct2 in dct['InnerClass_instances']]
        return OuterClass(arg, *InnerClass_intances)

Which, I assumed would work for

s =            #triple quotes omitted for syntax highlighting reasons
{
"__OuterClass__": true,
"arg": 4,
"InnerClass_instances":
    [
    {"__InnerClass__": true, "name": "Joe", "value": 12},
    {"__InnerClass__": true, "name": "Jimmy", "value":7}
    ]
} 

outerClass = json.loads(s, object_hook = creat_OuterClass)

However, calling json.loads as above returns this error:

if '__InnerClass__' in dct:
TypeError: argument of type 'NoneType' is not iterable

However, simply calling json.loads(s), and then json.dumps(outerClass) works perfectly fine. So, it seems like something in this way of approaching the object_hook parameter is causing the dictionaries that define InnerClass instances to be turned into NoneType objects. Am I missing some understanding as to how the function object_hook is used in parsing JSON objects?

Is there a way to do class hinting for multiple types of classes in a single JSON object? Nothing I've read so far has suggested that this is or is not possible.

+1  A: 

Not every time create_OuterClass is called will __OuterClass__ be a key in dct. Therefore, your create_OuterClass should handle this case as well. The default is to return dct:

def create_OuterClass(dct):
    # print('create_OuterClass: {0}'.format(dct))     
    if '__OuterClass__' in dct:
        arg = dct['arg']
        InnerClass_instances = [create_InnerClass(dct2) \
                                for dct2 in dct['InnerClass_instances']]
        return OuterClass(arg, *InnerClass_instances)
    return dct

If you include the above print statement you get

create_OuterClass: {u'__InnerClass__': True, u'name': u'Joe', u'value': 12}
create_OuterClass: {u'__InnerClass__': True, u'name': u'Jimmy', u'value': 7}
create_OuterClass: {u'__OuterClass__': True, u'InnerClass_instances': [{u'__InnerClass__': True, u'name': u'Joe', u'value': 12}, {u'__InnerClass__': True, u'name': u'Jimmy', u'value': 7}], u'arg': 4}

This shows you how json.loads is using the object_hook callback function. It calls it first with the inner dicts, and only later with the outer dict.

unutbu
I was wondering if it called it on the inner dicts first. I definitely wouldn't have come up with not including a 'return dct' line on my own though. Thanks.
Wilduck
Interestingly, while this works in the toy code I gave for the example, it isn't working in my actual code. The use of print to trace execution is helpful. At least now I'm seeing, where the list of inner class objects should be, there's just [None, None].
Wilduck
And I believe the reason is I was using a closure to pass some extra arguments to these two functions. Somehow it's messing things up...
Wilduck
And the solution was to bundle everything up in a class. Thanks again ~unutbu.
Wilduck
Ah, great! Glad you found the solution.
unutbu