title: Python and JSON: using object_hook to do class hinting for multiple different classes
body: <p>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:</p>
  
  <pre><code>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 
  </code></pre>
  
  <p>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: </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Which, I assumed would work for</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>However, calling json.loads as above returns this error: </p>
  
  <pre><code>if '__InnerClass__' in dct:
  TypeError: argument of type 'NoneType' is not iterable
  </code></pre>
  
  <p>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? </p>
  
  <p>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.</p>