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.