[code.view]

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

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2009 Doug Hellmann All rights reserved.
     #
     """
     """
     #end_pymotw_header
     
     import json
     
     class MyDecoder(json.JSONDecoder):
         
         def __init__(self):
             json.JSONDecoder.__init__(self, object_hook=self.dict_to_object)
     
         def dict_to_object(self, 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 = MyDecoder().decode(encoded_object)
     print myobj_instance
     

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

contact | logmethods.com