[code.view]

[top] / python / PyMOTW / json / json_load_object_hook.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2009 Doug Hellmann All rights reserved.
     #
     """
     """
     #end_pymotw_header
     
     import json
     
     def dict_to_object(d):
         if '__class__' in d:
             class_name = d.pop('__class__')
             module_name = d.pop('__module__')
             module = __import__(module_name)
             print 'MODULE:', module
             class_ = getattr(module, class_name)
             print 'CLASS:', class_
             args = dict( (key.encode('ascii'), value) for key, value in d.items())
             print 'INSTANCE ARGS:', args
             inst = class_(**args)
         else:
             inst = d
         return inst
     
     encoded_object = '[{"s": "instance value goes here", "__module__": "json_myobj", "__class__": "MyObj"}]'
     
     myobj_instance = json.loads(encoded_object, object_hook=dict_to_object)
     print myobj_instance
     

[top] / python / PyMOTW / json / json_load_object_hook.py

contact | logmethods.com